Skip to main content
Glama

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
NameRequiredDescriptionDefault
endLineNoEnding line number (1-based)
pathYesRelative path to the file
startLineNoStarting line number (1-based)

Implementation Reference

  • 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, }; } } );
  • 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)"), },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/0xjcf/MCP_CodeAnalysis'

If you have feedback or need assistance with the MCP directory API, please join our Discord server