Skip to main content
Glama

get_file_contents

Retrieve file or directory contents from a GitHub repository by specifying owner, repo, and path parameters.

Instructions

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

Input Schema

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

Implementation Reference

  • Core handler function that fetches file or directory contents from GitHub repository using the Contents API, optionally decoding base64 content to UTF-8.
    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;
    }
  • MCP server dispatch handler for 'get_file_contents' tool: validates input, invokes the core getFileContents function, and formats a user-friendly text response.
          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 }],
            };
          }
  • Zod input schemas for the get_file_contents tool: public schema (GetFileContentsSchema) used in registration and internal schema (_GetFileContentsSchema) with GitHub PAT used in handler.
    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:94-97 (registration)
    Registration of the 'get_file_contents' tool in the MCP server's listTools response, specifying 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),
    },

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