get-file-contents
Retrieve file or directory contents from GitHub repositories using owner, repo, and path parameters to access code and project files.
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
- src/tools/files.ts:157-206 (handler)The core handler function that implements the 'get-file-contents' tool logic. It validates input using GetFileContentsSchema, fetches content using GitHub API, handles files, directories, and other types, and decodes base64 content.export async function getFileContents(args: unknown): Promise<any> { const { owner, repo, path, branch } = GetFileContentsSchema.parse(args); const github = getGitHubApi(); return tryCatchAsync(async () => { const { data } = await github.getOctokit().repos.getContent({ owner, repo, path, ref: branch, }); // Handle directory listing if (Array.isArray(data)) { return data.map((item) => ({ name: item.name, path: item.path, sha: item.sha, size: item.size, type: item.type, url: item.html_url, download_url: item.download_url, })); } // Handle file content if (data.type === 'file') { return { name: data.name, path: data.path, sha: data.sha, size: data.size, type: data.type, url: data.html_url, content: data.content ? base64ToUtf8(data.content) : null, encoding: data.encoding, }; } // Handle submodule or symlink return { name: data.name, path: data.path, sha: data.sha, size: data.size, type: data.type, url: data.html_url, }; }, 'Failed to get file contents'); }
- src/utils/validation.ts:34-37 (schema)Zod schema used for input validation inside the handler function. Extends OwnerRepoSchema with path (required) and optional branch.export const GetFileContentsSchema = OwnerRepoSchema.extend({ path: z.string().min(1, 'File path is required'), branch: z.string().optional(), });
- src/server.ts:471-497 (registration)Tool registration in the ListTools response, including name, description, and input schema definition.{ name: 'get-file-contents', description: 'Get the contents of a file or directory from a GitHub repository', inputSchema: { type: 'object', properties: { owner: { type: 'string', description: 'Repository owner (username or organization)', }, repo: { type: 'string', description: 'Repository name', }, path: { type: 'string', description: 'Path to the file or directory', }, branch: { type: 'string', description: 'Branch to get contents from', }, }, required: ['owner', 'repo', 'path'], additionalProperties: false, }, },
- src/server.ts:1198-1200 (registration)Dispatch case in the CallToolRequest handler switch statement that invokes the getFileContents function.case 'get-file-contents': result = await getFileContents(parsedArgs); break;
- src/server.ts:25-31 (helper)Import statement that brings the getFileContents handler into the server module.import { createOrUpdateFile, pushFiles, getFileContents, forkRepository, getPullRequestFiles, } from './tools/files.js';