Skip to main content
Glama

get_file_contents

Retrieve file or directory contents from a GitHub repository by specifying the owner, repo, and path. Supports base64 encoding and branch selection for precise content access.

Instructions

Get the contents of a file or directory from a GitHub repository

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
branchNoBranch to get contents from
encodedNoWhether to return the content encoded in base64 (default = true)
ownerYesRepository owner (username or organization)
pathYesPath to the file or directory
repoYesRepository name

Implementation Reference

  • Core handler function implementing the logic to retrieve file or directory contents from a GitHub repository via the Contents API, with optional base64 decoding.
    export async function getFileContents( { github_pat, owner, repo, path, encoded = false, branch }: z.infer< typeof _GetFileContentsSchema >, ) { let url = `https://api.github.com/repos/${owner}/${repo}/contents/${path}`; if (branch) { url += `?ref=${branch}`; } const response = await githubRequest(github_pat, url); const data = GitHubContentSchema.parse(response); // If it's a file, decode the content? if (!Array.isArray(data) && data.content && !encoded) { data.content = Buffer.from(data.content, "base64").toString("utf8"); data.encoding = "utf8"; } return data; }
  • Zod schemas for input validation: public GetFileContentsSchema and internal _GetFileContentsSchema including GitHub PAT.
    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"), encoded: z.boolean().optional().describe("Whether to return the content encoded in base64 (default = true)"), branch: z.string().optional().describe("Branch to get contents from"), }); export const _GetFileContentsSchema = GetFileContentsSchema.extend({ github_pat: z.string().describe("GitHub Personal Access Token"), });
  • src/index.ts:93-97 (registration)
    Registration of the 'get_file_contents' tool in the MCP server's listTools handler, providing name, description, and input schema.
    { name: "get_file_contents", description: "Get the contents of a file or directory from a GitHub repository", inputSchema: zodToJsonSchema(files.GetFileContentsSchema), },
  • MCP server dispatch handler for 'get_file_contents' tool calls: parses arguments, invokes files.getFileContents, and formats the response as formatted text.
    case "get_file_contents": { const args = files._GetFileContentsSchema.parse(params.arguments); const contents = await files.getFileContents(args); let text = ''; if (Array.isArray(contents)) { // this means it's a directory text = `Directory Contents:\n${contents.map(c => `- ${c.path} (${c.type}, ${c.type === 'file' ? c.size.toString() + ' bytes' : ''})`).join('\n')}` } else { // this means it's a singular file text = `File Name: ${contents.name} File Path: ${contents.path} File SHA: ${contents.sha} File Size: ${contents.size} File URL: ${contents.url} File HTML URL: ${contents.html_url} File Download URL: ${contents.download_url} File Type: ${contents.type} File Encoding: ${contents.encoding} File Content: \`\`\` ${contents.content} \`\`\` ` } return { content: [{ type: "text", text }], }; }

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/MissionSquad/mcp-github'

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