read_file
Retrieve complete file contents from the filesystem. Specify the file path to access text, code, or data stored in documents.
Instructions
Read complete contents of a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to the file to read |
Implementation Reference
- src/index.ts:237-250 (handler)The handler logic for the 'read_file' tool. It extracts the file path from the request arguments, validates the path is allowed, reads the entire file content using fs.readFile, and returns it formatted as MCP tool content with type 'text'.case 'read_file': { const { path: filePath } = request.params.arguments as { path: string }; validatePath(filePath); const content = await fs.readFile(filePath, 'utf8'); return { content: [ { type: 'text', text: content, }, ], }; }
- src/index.ts:98-107 (schema)The JSON schema defining the input parameters for the 'read_file' tool, requiring a single 'path' string property.inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file to read', }, }, required: ['path'], },
- src/index.ts:95-108 (registration)The tool registration entry in the list_tools response, including name, description, and input schema for 'read_file'.{ name: 'read_file', description: 'Read complete contents of a file', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file to read', }, }, required: ['path'], }, },
- src/index.ts:38-45 (helper)Helper function called by the read_file handler to ensure the requested file path is within the server-allowed directories, throwing an error if not.function validatePath(filePath: string): void { if (!isPathAllowed(filePath)) { throw new McpError( ErrorCode.InvalidRequest, `Access denied: ${filePath} is not within allowed directories` ); } }
- src/index.ts:28-33 (helper)Supporting helper function used by validatePath to check if a resolved path is within any of the allowed directories.function isPathAllowed(filePath: string): boolean { const resolvedPath = path.resolve(filePath); return resolvedAllowedDirectories.some(allowedDir => resolvedPath === allowedDir || resolvedPath.startsWith(allowedDir + path.sep) ); }