get-file
Extract specific file content from a codebase by specifying its path and optional line range using this tool, enabling precise code analysis and workflow integration on the CodeAnalysis MCP Server.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| endLine | No | Ending line number (1-based) | |
| path | Yes | Relative path to the file | |
| startLine | No | Starting line number (1-based) |
Implementation Reference
- src/features/dev-tools/index.ts:198-267 (registration)Complete registration of the 'get-file' tool using server.tool(), including input schema definition and inline handler function that reads file contents with optional line range extraction.server.tool( "get-file", { path: z.string().describe("Relative path to the file"), startLine: z .number() .optional() .describe("Starting line number (1-based)"), endLine: z.number().optional().describe("Ending line number (1-based)"), }, async ({ path, startLine, endLine }) => { try { const filePath = resolve(process.cwd(), path); if (!existsSync(filePath)) { throw new Error(`File not found: ${path}`); } const content = readFileSync(filePath, "utf8"); const lines = content.split("\n"); let fileContent; if (startLine && endLine) { // Adjust for 0-based indexing const start = Math.max(0, startLine - 1); const end = Math.min(lines.length, endLine); fileContent = lines.slice(start, end).join("\n"); } else { fileContent = content; } const result = createSuccessResponse( { path, totalLines: lines.length, selectedLines: startLine && endLine ? { start: startLine, end: endLine } : null, content: fileContent, }, "get-file" ); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } catch (error) { return { content: [ { type: "text", text: JSON.stringify( createErrorResponse( error instanceof Error ? error.message : String(error), "get-file" ), null, 2 ), }, ], isError: true, }; } } );
- src/features/dev-tools/index.ts:208-267 (handler)The handler function executes the tool logic: resolves file path, reads content with fs.readFileSync, handles optional line slicing, formats success/error responses.async ({ path, startLine, endLine }) => { try { const filePath = resolve(process.cwd(), path); if (!existsSync(filePath)) { throw new Error(`File not found: ${path}`); } const content = readFileSync(filePath, "utf8"); const lines = content.split("\n"); let fileContent; if (startLine && endLine) { // Adjust for 0-based indexing const start = Math.max(0, startLine - 1); const end = Math.min(lines.length, endLine); fileContent = lines.slice(start, end).join("\n"); } else { fileContent = content; } const result = createSuccessResponse( { path, totalLines: lines.length, selectedLines: startLine && endLine ? { start: startLine, end: endLine } : null, content: fileContent, }, "get-file" ); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } catch (error) { return { content: [ { type: "text", text: JSON.stringify( createErrorResponse( error instanceof Error ? error.message : String(error), "get-file" ), null, 2 ), }, ], isError: true, }; } } );
- Zod input schema defining parameters for the get-file tool: path (string), optional startLine and endLine (numbers).{ path: z.string().describe("Relative path to the file"), startLine: z .number() .optional() .describe("Starting line number (1-based)"), endLine: z.number().optional().describe("Ending line number (1-based)"), },