Skip to main content
Glama
abhishekbhakat

mcp-server-code-assist

modify_file

Modify file content by replacing specified text strings to update code or configuration files programmatically.

Instructions

Modifies parts of a file using string replacements

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes
replacementsYes

Implementation Reference

  • Core handler function that validates the path, reads the file, applies string replacements, writes the modified content, and generates a unified diff.
    async def modify_file(self, path: str, replacements: dict[str, str]) -> str:
        path = await self.validate_path(path)
        content = await self.read_file(path)
        original = content
    
        for old, new in replacements.items():
            content = content.replace(old, new)
    
        await self.write_file(path, content)
        return self.generate_diff(original, content)
  • Pydantic BaseModel defining the input schema for the modify_file tool: path (str or Path) and replacements (dict of str to str). Used for validation and JSON schema generation.
    class FileModify(BaseModel):
        path: str | Path
        replacements: dict[str, str]
  • Registration of the 'modify_file' tool in the MCP server's list_tools() method, specifying name, description, and input schema.
    Tool(
        name=CodeAssistTools.MODIFY_FILE,
        description="Modifies parts of a file using string replacements",
        inputSchema=FileModify.model_json_schema(),
    ),
  • Dispatch handler in the MCP server's call_tool() method that parses arguments into FileModify model and invokes the file_tools.modify_file implementation.
    case CodeAssistTools.MODIFY_FILE:
        model = FileModify(path=arguments["path"], replacements=arguments["replacements"])
        result = await file_tools.modify_file(model.path, model.replacements)
        return [TextContent(type="text", text=result)]
  • Enum constant defining the tool name 'modify_file' in CodeAssistTools.
    MODIFY_FILE = "modify_file"

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/abhishekbhakat/mcp_server_code_assist'

If you have feedback or need assistance with the MCP directory API, please join our Discord server