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
| Name | Required | Description | Default |
|---|---|---|---|
| mrUrl | Yes | The URL of the GitLab Merge Request (used to derive project path). | |
| filePath | Yes | The path of the file to fetch. | |
| sha | Yes | The SHA of the commit or branch to fetch the file from. |
Implementation Reference
- src/index.ts:85-108 (schema)Tool schema definition: name, description, and input schema for gitlab_get_file_contentname: '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 GitLabServicecase '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, }, ], }; }
- src/gitlab.service.ts:272-279 (handler)Primary handler function invoked by the tool dispatcher: extracts project path from MR URL and fetches file contentasync getFileContentFromMrUrl( mrUrl: string, filePath: string, sha: string, ): Promise<string> { const { projectPath } = this.parseMrUrl(mrUrl, this.config.url); return this.getFileContent(projectPath, filePath, sha); }
- src/gitlab.service.ts:258-269 (handler)Core execution logic: calls GitLab API to retrieve raw file content at specified ref/SHAasync 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; }
- src/gitlab.service.ts:120-150 (helper)Helper utility: parses GitLab MR URL to extract project path and MR IID, used by getFileContentFromMrUrlprivate 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)}`, ); } }