Skip to main content
Glama

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
NameRequiredDescriptionDefault
pathYesThe file path to fetch
repoUrlYesThe URL of the Github repo

Implementation Reference

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

Other Tools

Related Tools

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/Ryan0204/github-repo-mcp'

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