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)}`, ); } }

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