read_project_file
Access and read local files directly from the VS Code workspace, ensuring secure and directory-restricted file retrieval for streamlined development workflows.
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)Executes the tool logic: validates the input path using validatePath helper, reads the file synchronously with fs.readFileSync, and returns the content as MCP-formatted response or 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 tool input parameters: requires a 'path' string.{ path: z.string() },
- local-mcp-server.ts:40-53 (registration)Registers the 'read_project_file' tool on the McpServer instance with name, description, input schema, and inline handler function.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 validate and resolve file paths, preventing directory traversal attacks by ensuring the path is within the WORKSPACE_ROOT.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; }