Skip to main content
Glama
shanksxz

GitHub Repository MCP Server

get-file-content

Retrieve specific file content from GitHub repositories by providing owner, repository name, and file path to access code, documentation, or configuration files.

Instructions

Get content of a specific file from a GitHub repository

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
ownerYesGitHub repository owner/organization name
repoYesGitHub repository name
pathYesPath to the file in the repository

Implementation Reference

  • Handler function that fetches the content of a specific file from a GitHub repository using the getFileContent helper and returns it formatted as text content block or an error message.
    async ({ owner, repo, path }) => {
      try {
        const content = await getFileContent(owner, repo, path);
    
        if (!content) {
          return {
            content: [
              {
                type: "text",
                text: `Could not retrieve content for ${path} in ${owner}/${repo}`,
              },
            ],
          };
        }
    
        return {
          content: [
            {
              type: "text",
              text: `File: ${path}\n\n\`\`\`\n${content}\n\`\`\``,
            },
          ],
        };
      } catch (error) {
        console.error(`Error fetching file content for ${path}:`, error);
        return {
          content: [
            {
              type: "text",
              text: `Error fetching file content: ${error instanceof Error ? error.message : String(error)}`,
            },
          ],
        };
      }
    },
  • Input schema defined using Zod for the parameters: owner, repo, and path.
    {
      owner: z.string().describe("GitHub repository owner/organization name"),
      repo: z.string().describe("GitHub repository name"),
      path: z.string().describe("Path to the file in the repository"),
    },
  • src/index.ts:155-198 (registration)
    Registration of the 'get-file-content' tool on the MCP server using server.tool method, including name, description, input schema, and handler.
    server.tool(
      "get-file-content",
      "Get content of a specific file from a GitHub repository",
      {
        owner: z.string().describe("GitHub repository owner/organization name"),
        repo: z.string().describe("GitHub repository name"),
        path: z.string().describe("Path to the file in the repository"),
      },
      async ({ owner, repo, path }) => {
        try {
          const content = await getFileContent(owner, repo, path);
    
          if (!content) {
            return {
              content: [
                {
                  type: "text",
                  text: `Could not retrieve content for ${path} in ${owner}/${repo}`,
                },
              ],
            };
          }
    
          return {
            content: [
              {
                type: "text",
                text: `File: ${path}\n\n\`\`\`\n${content}\n\`\`\``,
              },
            ],
          };
        } catch (error) {
          console.error(`Error fetching file content for ${path}:`, error);
          return {
            content: [
              {
                type: "text",
                text: `Error fetching file content: ${error instanceof Error ? error.message : String(error)}`,
              },
            ],
          };
        }
      },
    );
  • Helper function that retrieves the raw content of a file from a GitHub repository using the Octokit API, decoding base64 content to UTF-8 string.
    async function getFileContent(owner: string, repo: string, path: string): Promise<string | null> {
      try {
        const response = await octokit.repos.getContent({
          owner,
          repo,
          path,
        });
    
        if ('content' in response.data && 'encoding' in response.data) {
          if (response.data.encoding === 'base64') {
            return Buffer.from(response.data.content, 'base64').toString('utf-8');
          }
        }
    
        return null;
      } catch (error) {
        console.error(`Error getting file content for ${owner}/${repo}/${path}:`, error);
        return null;
      }
    }

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/shanksxz/gh-mcp-server'

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