Skip to main content
Glama

gitlab_get_file_content

Retrieve file content from GitLab projects using merge request URLs, file paths, and commit SHAs to access specific code versions.

Instructions

Fetches the content of a specific file at a given SHA in a GitLab project.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
mrUrlYesThe URL of the GitLab Merge Request (used to derive project path).
filePathYesThe path of the file to fetch.
shaYesThe SHA of the commit or branch to fetch the file from.

Implementation Reference

  • Tool schema definition: name, description, and input schema for gitlab_get_file_content
      name: 'gitlab_get_file_content',
      description:
        'Fetches the content of a specific file at a given SHA in a GitLab project.',
      inputSchema: {
        type: 'object',
        properties: {
          mrUrl: {
            type: 'string',
            description:
              'The URL of the GitLab Merge Request (used to derive project path).',
          },
          filePath: {
            type: 'string',
            description: 'The path of the file to fetch.',
          },
          sha: {
            type: 'string',
            description:
              'The SHA of the commit or branch to fetch the file from.',
          },
        },
        required: ['mrUrl', 'filePath', 'sha'],
      },
    },
  • src/index.ts:1231-1253 (registration)
    Tool registration and dispatch handler in MCP server: handles CallToolRequest for gitlab_get_file_content and delegates to GitLabService
    case 'gitlab_get_file_content': {
      if (!gitlabService) {
        throw new Error('GitLab service is not initialized.');
      }
      const { mrUrl, filePath, sha } = args as {
        mrUrl: string;
        filePath: string;
        sha: string;
      };
      const result = await gitlabService.getFileContentFromMrUrl(
        mrUrl,
        filePath,
        sha,
      );
      return {
        content: [
          {
            type: 'text',
            text: result,
          },
        ],
      };
    }
  • Primary handler function invoked by the tool dispatcher: extracts project path from MR URL and fetches file content
    async getFileContentFromMrUrl(
      mrUrl: string,
      filePath: string,
      sha: string,
    ): Promise<string> {
      const { projectPath } = this.parseMrUrl(mrUrl, this.config.url);
      return this.getFileContent(projectPath, filePath, sha);
    }
  • Core execution logic: calls GitLab API to retrieve raw file content at specified ref/SHA
    async getFileContent(
      projectPath: string,
      filePath: string,
      sha: string,
    ): Promise<string> {
      const encodedProjectPath = encodeURIComponent(projectPath);
      const encodedFilePath = encodeURIComponent(filePath);
      const content = await this.callGitLabApi<any>(
        `projects/${encodedProjectPath}/repository/files/${encodedFilePath}/raw?ref=${sha}`,
      );
      return content;
    }
  • Helper utility: parses GitLab MR URL to extract project path and MR IID, used by getFileContentFromMrUrl
    private parseMrUrl(
      mrUrl: string,
      gitlabBaseUrl: string,
    ): { projectPath: string; mrIid: number } {
      try {
        const url = new URL(mrUrl);
        const baseUrl = new URL(gitlabBaseUrl);
    
        // Ensure the URL is from the same GitLab instance
        if (url.origin !== baseUrl.origin) {
          throw new Error(
            `MR URL is not from the configured GitLab instance: ${gitlabBaseUrl}`,
          );
        }
    
        // Parse the path: /{namespace}/{project}/-/merge_requests/{iid}
        const pathMatch = url.pathname.match(/^\/(.+)\/-\/merge_requests\/(\d+)/);
        if (!pathMatch) {
          throw new Error(`Invalid GitLab MR URL format: ${mrUrl}`);
        }
    
        const projectPath = pathMatch[1];
        const mrIid = parseInt(pathMatch[2], 10);
    
        return { projectPath, mrIid };
      } catch (error) {
        throw new Error(
          `Failed to parse GitLab MR URL: ${error instanceof Error ? error.message : String(error)}`,
        );
      }
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the action ('fetches') but lacks details on permissions required, rate limits, error handling, or the format of the returned content (e.g., plain text, binary). This is a significant gap for a tool that interacts with a version control system.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, well-structured sentence that efficiently conveys the core functionality without any fluff. It's front-loaded with the key action and resource, making it easy to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (3 required parameters, no output schema, no annotations), the description is minimally adequate. It covers the basic purpose but lacks behavioral details and usage context, which are important for a file-fetching operation in GitLab. The high schema coverage helps offset some gaps.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, clearly documenting each parameter's purpose. The description adds no additional semantic context beyond what's in the schema (e.g., it doesn't explain how 'mrUrl' relates to 'filePath' or 'sha', or provide examples). This meets the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('fetches the content') and resource ('specific file at a given SHA in a GitLab project'), making the purpose immediately understandable. However, it doesn't explicitly differentiate from potential siblings like 'gitlab_get_branch_details' or 'gitlab_get_merge_request_details' that might also retrieve content, though the focus on file content is reasonably distinct.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. For example, it doesn't mention if this is for retrieving raw file content versus metadata, or how it differs from other file-related operations that might exist in GitLab. This leaves the agent to infer usage from the name and parameters alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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

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