read_file
Read and retrieve the contents of a file using a specified path and optional encoding with Edit-MCP. Ideal for accessing file data directly within your workflow.
Instructions
Read the contents of a file
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| encoding | No | Encoding to use when reading the file (default: utf8) | |
| path | Yes | Path to the file to read |
Input Schema (JSON Schema)
{
"properties": {
"encoding": {
"description": "Encoding to use when reading the file (default: utf8)",
"type": "string"
},
"path": {
"description": "Path to the file to read",
"type": "string"
}
},
"required": [
"path"
],
"type": "object"
}
Implementation Reference
- src/core/mcp-server.ts:397-424 (handler)Generic handler for all 'tools/call' requests, including 'read_file'. Validates tool existence and currently returns a placeholder response. This is the entry point for executing the tool logic./** * Handles the tools/call request */ private async handleToolsCall(params: { name: string, arguments?: any }): Promise<CallToolResult> { const { name, arguments: args } = params; if (!name) { throw new Error('Tool name is required'); } const tool = this.tools.get(name); if (!tool) { throw new Error(`Tool not found: ${name}`); } // In a real implementation, we would execute the tool here // For now, we'll just return a placeholder return { content: [ { type: 'text', text: `Executed tool ${name} with arguments: ${JSON.stringify(args)}` } as TextContent ] }; }
- src/index.ts:138-158 (schema)Defines the input schema and annotations for the 'read_file' tool and registers it with the MCP server.name: 'read_file', description: 'Read the contents of a file', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file to read' }, encoding: { type: 'string', description: 'Encoding to use when reading the file (default: utf8)' } }, required: ['path'] }, annotations: { readOnlyHint: true, openWorldHint: false } });
- Implements the core file reading functionality using promisified fs.readFile, matching the tool's parameters (path and encoding)./** * Reads a file and returns its content */ public async readFile(filePath: string, encoding: BufferEncoding = 'utf8'): Promise<string> { try { return await readFile(filePath, { encoding }); } catch (error: any) { throw new Error(`Failed to read file ${filePath}: ${error.message}`); } }