Skip to main content
Glama
ttpears

GitLab MCP Server

by ttpears

get_file_content

Retrieve file content from GitLab repositories to analyze code, review configurations, or extract data for development workflows.

Instructions

Get the content of a specific file from a GitLab repository - crucial for code analysis

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
projectPathYesFull path of the project (e.g., "group/project-name")
filePathYesPath to the file within the repository (e.g., "src/main.js")
refNoGit reference (branch, tag, or commit SHA)HEAD
userCredentialsNoYour GitLab credentials (optional - uses shared token if not provided)

Implementation Reference

  • Tool definition for get_file_content including input schema (projectPath, filePath, ref), annotations, and handler that calls client.getFileContent and returns file metadata with content
    const getFileContentTool: Tool = {
      name: 'get_file_content',
      title: 'File Content',
      description: 'Get the content of a specific file from a GitLab repository - crucial for code analysis',
      requiresAuth: false,
      requiresWrite: false,
      annotations: {
        readOnlyHint: true,
        destructiveHint: false,
        idempotentHint: true,
      },
      inputSchema: withUserAuth(z.object({
        projectPath: z.string().describe('Full path of the project (e.g., "group/project-name")'),
        filePath: z.string().describe('Path to the file within the repository (e.g., "src/main.js")'),
        ref: z.string().default('HEAD').describe('Git reference (branch, tag, or commit SHA)'),
      })),
      handler: async (input, client, userConfig) => {
        const credentials = input.userCredentials ? validateUserConfig(input.userCredentials) : userConfig;
        const result = await client.getFileContent(input.projectPath, input.filePath, input.ref, credentials);
    
        if (result.project.repository.blobs.nodes.length === 0) {
          throw new Error(`File not found: ${input.filePath} in ${input.projectPath} at ${input.ref}`);
        }
    
        const file = result.project.repository.blobs.nodes[0];
        const projectWebUrl = result.project.webUrl;
        const refParam = input.ref || 'HEAD';
        const fileWebUrl = `${projectWebUrl}/-/blob/${refParam}/${file.path}`;
        
        return {
          project: input.projectPath,
          path: file.path,
          name: file.name,
          size: file.size,
          content: file.rawBlob,
          webUrl: fileWebUrl,
          ref: refParam,
          isLFS: !!file.lfsOid
        };
      },
    };
  • Client implementation that executes GraphQL query to fetch file content from GitLab repository using blobs API, returning file metadata including rawBlob content, size, and LFS information
    async getFileContent(
      projectPath: string, 
      filePath: string, 
      ref?: string, 
      userConfig?: UserConfig
    ): Promise<any> {
      const query = gql`
        query getFileContent($projectPath: ID!, $path: String!, $ref: String) {
          project(fullPath: $projectPath) {
            webUrl
            repository {
              blobs(paths: [$path], ref: $ref) {
                nodes {
                  name
                  path
                  rawBlob
                  size
                  lfsOid
                }
              }
            }
          }
        }
      `;
      
      return this.query(query, { 
        projectPath, 
        path: filePath, 
        ref: ref || "HEAD" 
      }, userConfig);
    }
  • Input schema definition using Zod validation for get_file_content tool, specifying projectPath (required), filePath (required), and ref (defaults to 'HEAD') parameters
    inputSchema: withUserAuth(z.object({
      projectPath: z.string().describe('Full path of the project (e.g., "group/project-name")'),
      filePath: z.string().describe('Path to the file within the repository (e.g., "src/main.js")'),
      ref: z.string().default('HEAD').describe('Git reference (branch, tag, or commit SHA)'),
    })),
  • src/tools.ts:1324-1337 (registration)
    Tool registration - getFileContentTool is exported as part of searchTools array which is then included in the main tools array for MCP registration
    export const searchTools: Tool[] = [
      globalSearchTool,
      searchProjectsTool,
      searchIssuesTool,
      searchMergeRequestsTool,
      getUserIssuesTool,
      getUserMergeRequestsTool,
      searchUsersTool,
      searchGroupsTool,
      searchLabelsTool,
      browseRepositoryTool,
      getFileContentTool,
      listGroupMembersTool,
    ];
  • src/index.ts:84-96 (registration)
    MCP tool registration handler that maps all tools from the tools array (including get_file_content) to MCP protocol format, exposing them to MCP clients through ListToolsRequest
    server.setRequestHandler(ListToolsRequestSchema, async () => {
      return {
        tools: tools.map(tool => ({
          name: tool.name,
          ...(tool.title && { title: tool.title }),
          description: tool.description,
          inputSchema: toJsonSchema(tool.inputSchema),
          ...(tool.outputSchema && { outputSchema: toJsonSchema(tool.outputSchema) }),
          ...(tool.annotations && { annotations: tool.annotations }),
          ...(tool.icon && { icon: tool.icon }),
        })),
      };
    });

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/ttpears/gitlab-mcp'

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