Purpose
hledit-mcp is an MCP server that provides hledit's hash-anchored file editing capabilities to standard MCP clients, such as Claude Code, Claude Desktop, and Cursor. It follows the same approach as pi-hledit (Pi native integration), but is implemented through MCP rather than Pi's extension API, so the same editing primitives can be used in MCP-compatible clients. Its official MCP Registry identifier is io.github.dabito/hledit-mcp.
It is suitable for local workspace editing scenarios: you want explicit stale-context failures rather than blind text replacement.
Main capabilities
- Hash-anchored editing: when reading a file, hledit read annotates each line with a stable anchor, for example 6#M9A.
- Stale write rejection: write commands reference anchors; before changing the file, hledit recomputes that line's hash. If the file has changed since it was read, the anchor is rejected, no write occurs, and the agent receives a stale error and remapping hint instead of silently corrupting the wrong line.
- A single tool, three operations, fully consistent with pi-hledit's contract: read (get anchors), edit (single change), batch (multiple changes).
- Successful edits and batch calls return a concise summary, including Lines: +N -M when the installed hledit CLI provides line delta metadata (hledit >= 1.2.4). Older versions still work, but omit the line delta summary.
- Context grep: when grep is set, use a smaller default window (context:2); pass context:0 to get match-only output.
- Optional diff: set HLEDIT_MCP_DIFF=1 to append a limited fenced diff block to successful edits; turn it off to get the most token-efficient MCP response.
- Bounded reads by default: when offset / limit are omitted, the server uses offset=1 and limit=2000.
Usage
Prerequisites: Go 1.21+ for installing the hledit CLI (or a prebuilt binary in PATH), and an MCP-compatible client.
First install the hledit CLI, then configure the MCP client to run hledit-mcp. For Claude Code, use claude mcp add --help to confirm the current CLI syntax; recent versions use the corresponding command. Claude Desktop and other MCP clients can use the same stdio server shape. Set HLEDIT_CWD to the workspace that the MCP client should allow hledit to edit.
Workflow: read to get an anchor → edit (single change) or batch (multiple changes). If an edit returns stale, re-read to get a new anchor and then retry—the line containing the anchor has moved or changed since the read.
The preferred batch form uses structured edits so that the MCP/RPC layer handles escaping. Contextual grep examples and match-only grep examples are also provided. During the transition period, legacy JSON string edits are still supported.
When to use
- When you need stale-write rejection at the MCP tool boundary, rather than relying on the host's native text replacement behavior.
- For local workspace edits by coding agents using MCP-compatible clients such as Claude Code, Claude Desktop, and Cursor.
Limitations and notes
- MCP has no equivalent of Pi's renderCall / renderResult terminal rendering, so this package does not have that layer either; that layer is a Pi-specific interface and is not part of the portable tool contract.
- The published bin / main points to dist/index.js, built with esbuild (build.mjs) and run through plain node, in order to maintain the >=18 Node requirement. Running index.ts directly with node (without tsx) only works on Node >=22.6, due to its built-in TypeScript type stripping; older LTS versions do not support it at all.
- The unit test core.test.ts covers the same contract as the pi-hledit test suite, minus the Pi-specific rendering assertions. e2e.test.ts drives the built dist/index.js through a real MCP stdio handshake via @modelcontextprotocol/sdk's Client / StdioClientTransport.
- The hashed line editing idea comes from Can Bölük / @can1357's coding agent harness work, especially the related article and oh-my-pi; for more prior art, see hledit's credits.
- Related packages: hledit (a standalone CLI for the two integration wrappers), pi-hledit (a Pi-native integration with the same tool contract).
Demo
See docs/demo/transcript.md, which contains a deterministic MCP stdio transcript showing stale-write rejection on the actual hledit-mcp tool surface. The transcript is generated by docs/demo/hledit-mcp-demo.mjs, which drives dist/index.js through @modelcontextprotocol/sdk's stdio client and does not call hledit directly.