Skip to main content
Glama
piyushgIITian

GitHub Enterprise MCP Server

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
NameRequiredDescriptionDefault
branchNoBranch to get contents from
ownerYesRepository owner (username or organization)
pathYesPath to the file or directory
repoYesRepository name

Implementation Reference

  • 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');
    }
  • 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,
      },
    },
  • Dispatch case in the CallToolRequest handler switch statement that invokes the getFileContents function.
    case 'get-file-contents':
      result = await getFileContents(parsedArgs);
      break;
  • Import statement that brings the getFileContents handler into the server module.
    import {
      createOrUpdateFile,
      pushFiles,
      getFileContents,
      forkRepository,
      getPullRequestFiles,
    } from './tools/files.js';

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

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