Skip to main content
Glama

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

Implementation Reference

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

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