get-file
Retrieve specific file content or code segments from a codebase for analysis. Specify file path and optional line ranges to extract relevant portions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Relative path to the file | |
| startLine | No | Starting line number (1-based) | |
| endLine | No | Ending line number (1-based) |
Implementation Reference
- src/features/dev-tools/index.ts:198-267 (registration)Registers the 'get-file' MCP tool with the server, specifying input schema and inline handler function.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-266 (handler)Executes the 'get-file' tool: resolves file path, reads content with fs.readFileSync, extracts line range if specified, formats success/error JSON responses using helpers.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, }; } }
- Input validation schema using Zod: path (string, required), startLine/endLine (optional numbers for line range).{ 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)"), },