patch_apply
Apply unified diff patches to files with support for markdown-fenced diffs and new file creation through /dev/null.
Instructions
Apply a unified diff patch to a file. Accepts markdown-fenced diffs. Use --- /dev/null to create new files. Returns error in read-only mode.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| patch | Yes | Unified diff to apply (may be wrapped in a markdown code fence) | |
| path | Yes | Absolute path to the file to patch |
Implementation Reference
- The `chisel_patch_apply` function is the direct handler that calls the chisel backend's 'patch_apply' tool via `_chisel_call`. However, the actual business logic is delegated to the remote chisel instance.
async def chisel_patch_apply(path: str, patch: str) -> str: return await _chisel_call("patch_apply", path=path, patch=patch) - examples/python-mcp-server/server.py:101-110 (registration)The `apply_patch` FastMCP tool (registered via @mcp.tool()) exposes 'patch_apply' functionality to MCP clients. It calls `chisel_patch_apply` internally.
@mcp.tool() async def apply_patch(path: str, patch: str) -> str: """ Apply a unified diff to a file via chisel (atomic write, hunk-safe). Args: path: Absolute path to the target file patch: Unified diff string (markdown code fence optional) """ return await chisel_patch_apply(path, patch) - The `_chisel_call` helper function opens a session to the chisel MCP server, calls the named tool with provided kwargs, and returns the text result.
async def _chisel_call(tool: str, **kwargs) -> str: """Open a session to chisel, call one tool, return the text result.""" async with streamablehttp_client(CHISEL_URL, headers=_HEADERS) as (read, write, _): async with ClientSession(read, write) as session: await session.initialize() result = await session.call_tool(tool, kwargs) return result.content[0].text