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
| Name | Required | Description | Default |
|---|---|---|---|
| owner | Yes | GitHub repository owner/organization name | |
| path | Yes | Path to the file in the repository | |
| repo | Yes | GitHub repository name |
Implementation Reference
- src/index.ts:163-197 (handler)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)}`, }, ], }; } },
- src/index.ts:158-162 (schema)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)}`, }, ], }; } }, );
- src/index.ts:39-57 (helper)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; }