read_project_file
Read files from the VS Code workspace directory to access project content for AI-assisted development tasks.
Instructions
Read a local file from the VS Code workspace (restricted to workspace directory)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- local-mcp-server.ts:44-52 (handler)The async handler function for the 'read_project_file' tool. It validates the input path using validatePath helper, reads the file content using fs.readFileSync, and returns the content or an error message.async ({ path }) => { try { const safePath = validatePath(path); const content = fs.readFileSync(safePath, 'utf8'); return { content: [{ type: 'text', text: content }] }; } catch (err: any) { return { content: [{ type: 'text', text: `File read error: ${err.message}` }] }; } }
- local-mcp-server.ts:43-43 (schema)Zod schema for the tool input, defining a required 'path' parameter as a string.{ path: z.string() },
- local-mcp-server.ts:40-53 (registration)Registration of the 'read_project_file' tool on the MCP server, including name, description, schema, and handler.server.tool( 'read_project_file', 'Read a local file from the VS Code workspace (restricted to workspace directory)', { path: z.string() }, async ({ path }) => { try { const safePath = validatePath(path); const content = fs.readFileSync(safePath, 'utf8'); return { content: [{ type: 'text', text: content }] }; } catch (err: any) { return { content: [{ type: 'text', text: `File read error: ${err.message}` }] }; } } );
- local-mcp-server.ts:15-21 (helper)Helper function to securely resolve and validate file paths, ensuring they are within the workspace root to prevent path traversal attacks. Used by the read_project_file handler.function validatePath(filePath: string): string { const resolvedPath = path.resolve(WORKSPACE_ROOT, filePath); if (!resolvedPath.startsWith(WORKSPACE_ROOT)) { throw new Error('Path traversal detected - access denied'); } return resolvedPath; }