get-file-content
Retrieve specific file content from GitHub repositories by providing owner, repository name, and file path to access code, documentation, or configuration files.
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 | |
| repo | Yes | GitHub repository name | |
| path | Yes | Path to the file in the repository |
Implementation Reference
- src/index.ts:163-197 (handler)Handler function that fetches the content of a specific file from a GitHub repository using the getFileContent helper and returns it formatted as text content block or an error message.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 defined using Zod for the parameters: owner, repo, and path.{ 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 using server.tool method, including name, description, input schema, and handler.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-58 (helper)Helper function that retrieves the raw content of a file from a GitHub repository using the Octokit API, decoding base64 content to UTF-8 string.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; } }