getRepoFile
Fetch and retrieve specific files directly from GitHub repositories using a URL and file path, enabling quick access to repository content for analysis or integration.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | The file path to fetch | |
| repoUrl | Yes | The URL of the Github repo |
Implementation Reference
- index.ts:128-186 (handler)The handler function that fetches the specified file from the GitHub repository, decodes base64 content, checks if it's binary, and returns the content or an error message.async ({ repoUrl, path }, extra) => { try { const { owner, repo } = parseGitHubUrl(repoUrl); const { data } = await octokit.rest.repos.getContent({ owner, repo, path, }); // For files, data won't be an array if (Array.isArray(data) || data.type !== 'file') { throw new Error("Requested path is not a file"); } // Get content and decode if base64 let content = ""; if (data.encoding === 'base64' && data.content) { content = Buffer.from(data.content, 'base64').toString('utf-8'); } else if (data.content) { content = data.content; } // Check for binary files const fileExtension = path.split('.').pop() || "txt"; const isBinary = /^(jpg|jpeg|png|gif|bmp|ico|webp|mp3|mp4|wav|ogg|pdf|zip|tar|gz|rar|exe|dll|so|bin)$/i.test(fileExtension); if (isBinary) { return { content: [ { type: "text", text: `File ${path} appears to be a binary file and cannot be displayed as text.`, }, ], }; } return { content: [ { type: "text", text: `File content for ${path} in ${owner}/${repo}:\n\n${content}`, }, ], }; } catch (error) { console.error("Error fetching file:", error); return { content: [ { type: "text", text: `Error fetching file: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } }
- index.ts:124-127 (schema)Input schema using Zod for validating repoUrl (must be a valid URL) and path parameters.{ repoUrl: z.string().url().describe("The URL of the Github repo"), path: z.string().describe("The file path to fetch"), },
- index.ts:122-187 (registration)Registers the 'getRepoFile' tool with the MCP server, including inline schema and handler.server.tool( "getRepoFile", { repoUrl: z.string().url().describe("The URL of the Github repo"), path: z.string().describe("The file path to fetch"), }, async ({ repoUrl, path }, extra) => { try { const { owner, repo } = parseGitHubUrl(repoUrl); const { data } = await octokit.rest.repos.getContent({ owner, repo, path, }); // For files, data won't be an array if (Array.isArray(data) || data.type !== 'file') { throw new Error("Requested path is not a file"); } // Get content and decode if base64 let content = ""; if (data.encoding === 'base64' && data.content) { content = Buffer.from(data.content, 'base64').toString('utf-8'); } else if (data.content) { content = data.content; } // Check for binary files const fileExtension = path.split('.').pop() || "txt"; const isBinary = /^(jpg|jpeg|png|gif|bmp|ico|webp|mp3|mp4|wav|ogg|pdf|zip|tar|gz|rar|exe|dll|so|bin)$/i.test(fileExtension); if (isBinary) { return { content: [ { type: "text", text: `File ${path} appears to be a binary file and cannot be displayed as text.`, }, ], }; } return { content: [ { type: "text", text: `File content for ${path} in ${owner}/${repo}:\n\n${content}`, }, ], }; } catch (error) { console.error("Error fetching file:", error); return { content: [ { type: "text", text: `Error fetching file: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } );
- index.ts:12-20 (helper)Helper function to parse GitHub repository URL into owner and repo name, used by the handler.function parseGitHubUrl(url: string) { const regex = /github\.com\/([^\/]+)\/([^\/]+)/; const match = url.match(regex); if (!match) { throw new Error("Invalid GitHub repository URL"); } const [, owner, repo] = match; return { owner, repo: repo.replace(".git", "") }; }