Skip to main content
Glama

get-file-content

Retrieve the content of a specific file from a GitHub repository by providing the owner, repository name, and file path. Use this tool to access and utilize file data within AI interactions or workflows.

Instructions

Get content of a specific file from a GitHub repository

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
ownerYesGitHub repository owner/organization name
pathYesPath to the file in the repository
repoYesGitHub repository name

Implementation Reference

  • The MCP tool handler for 'get-file-content'. Fetches file content via helper, formats and returns as text content block, handles errors.
    async ({ owner, repo, path }) => { try { const content = await getFileContent(owner, repo, path); if (!content) { return { content: [ { type: "text", text: `Could not retrieve content for ${path} in ${owner}/${repo}`, }, ], }; } return { content: [ { type: "text", text: `File: ${path}\n\n\`\`\`\n${content}\n\`\`\``, }, ], }; } catch (error) { console.error(`Error fetching file content for ${path}:`, error); return { content: [ { type: "text", text: `Error fetching file content: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } },
  • Input schema using Zod for the 'get-file-content' tool parameters.
    { owner: z.string().describe("GitHub repository owner/organization name"), repo: z.string().describe("GitHub repository name"), path: z.string().describe("Path to the file in the repository"), },
  • src/index.ts:155-198 (registration)
    Registration of the 'get-file-content' tool on the MCP server.
    server.tool( "get-file-content", "Get content of a specific file from a GitHub repository", { owner: z.string().describe("GitHub repository owner/organization name"), repo: z.string().describe("GitHub repository name"), path: z.string().describe("Path to the file in the repository"), }, async ({ owner, repo, path }) => { try { const content = await getFileContent(owner, repo, path); if (!content) { return { content: [ { type: "text", text: `Could not retrieve content for ${path} in ${owner}/${repo}`, }, ], }; } return { content: [ { type: "text", text: `File: ${path}\n\n\`\`\`\n${content}\n\`\`\``, }, ], }; } catch (error) { console.error(`Error fetching file content for ${path}:`, error); return { content: [ { type: "text", text: `Error fetching file content: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }, );
  • Helper function implementing the core logic: fetches file via GitHub API (Octokit), decodes base64 content.
    async function getFileContent(owner: string, repo: string, path: string): Promise<string | null> { try { const response = await octokit.repos.getContent({ owner, repo, path, }); if ('content' in response.data && 'encoding' in response.data) { if (response.data.encoding === 'base64') { return Buffer.from(response.data.content, 'base64').toString('utf-8'); } } return null; } catch (error) { console.error(`Error getting file content for ${owner}/${repo}/${path}:`, error); return null; }

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/shanksxz/gh-mcp-server'

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