get_file_contents
Extract and retrieve contents of a specific file or directory from a GitHub repository by specifying the owner, repository name, path, and branch.
Instructions
Get the contents of a file or directory from a GitHub repository
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branch | No | Branch to get contents from | |
| owner | Yes | Repository owner (username or organization) | |
| path | Yes | Path to the file or directory | |
| repo | Yes | Repository name |
Implementation Reference
- operations/files.ts:72-92 (handler)Core handler function that fetches file or directory contents from GitHub API, decodes base64 content for files, and returns parsed data.export async function getFileContents( owner: string, repo: string, path: string, branch?: string ) { let url = `https://api.github.com/repos/${owner}/${repo}/contents/${path}`; if (branch) { url += `?ref=${branch}`; } const response = await githubRequest(url); const data = GitHubContentSchema.parse(response); // If it's a file, decode the content if (!Array.isArray(data) && data.content) { data.content = Buffer.from(data.content, "base64").toString("utf8"); } return data; }
- operations/files.ts:28-33 (schema)Zod schema defining the input parameters for the get_file_contents tool: owner, repo, path, and optional branch.export const GetFileContentsSchema = z.object({ owner: z.string().describe("Repository owner (username or organization)"), repo: z.string().describe("Repository name"), path: z.string().describe("Path to the file or directory"), branch: z.string().optional().describe("Branch to get contents from"), });
- index.ts:86-88 (registration)Tool registration in the MCP server's listTools response, including name, description, and input schema reference.name: "get_file_contents", description: "Get the contents of a file or directory from a GitHub repository", inputSchema: zodToJsonSchema(files.GetFileContentsSchema),
- index.ts:357-368 (handler)MCP server dispatcher handler that parses arguments, invokes the getFileContents function, and formats the response.case "get_file_contents": { const args = files.GetFileContentsSchema.parse(request.params.arguments); const contents = await files.getFileContents( args.owner, args.repo, args.path, args.branch ); return { content: [{ type: "text", text: JSON.stringify(contents, null, 2) }], }; }