get_file_contents
Retrieve file or directory contents from a GitLab project by specifying the file path, project ID, and reference. Simplify access to project data within the GitLab MCP server.
Instructions
Get the contents of a file or directory from a GitLab project
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | No | ||
| project_id | No | ||
| ref | No |
Input Schema (JSON Schema)
{
"properties": {
"file_path": {
"type": "string"
},
"project_id": {
"type": "string"
},
"ref": {
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/index.ts:358-362 (handler)MCP tool handler for 'get_file_contents' that parses arguments using the schema and delegates to GitLabApi.getFileContentscase "get_file_contents": { const args = GetFileContentsSchema.parse(request.params.arguments); const contents = await gitlabApi.getFileContents(args.project_id, args.file_path, args.ref); return { content: [{ type: "text", text: JSON.stringify(contents, null, 2) }] }; }
- src/gitlab-api.ts:185-213 (handler)Core implementation of getFileContents that fetches file from GitLab API, decodes base64 content, and returns structured dataasync getFileContents( projectId: string, filePath: string, ref: string ): Promise<GitLabContent> { const encodedPath = encodeURIComponent(filePath); const url = `${this.apiUrl}/projects/${encodeURIComponent(projectId)}/repository/files/${encodedPath}?ref=${encodeURIComponent(ref)}`; const response = await fetch(url, { headers: { "Authorization": `Bearer ${this.token}` } }); if (!response.ok) { throw new McpError( ErrorCode.InternalError, `GitLab API error: ${response.statusText}` ); } const data = GitLabContentSchema.parse(await response.json()); if (!Array.isArray(data) && data.content) { data.content = Buffer.from(data.content, 'base64').toString('utf8'); } return data; }
- src/schemas.ts:409-413 (schema)Zod schema defining input parameters for get_file_contents tool: project_id, file_path, refexport const GetFileContentsSchema = z.object({ project_id: z.string(), file_path: z.string(), ref: z.string() });
- src/index.ts:127-131 (registration)Tool registration in ALL_TOOLS array, including name, description, input schema, and read-only flag used by listTools handlername: "get_file_contents", description: "Get the contents of a file or directory from a GitLab project", inputSchema: createJsonSchema(GetFileContentsSchema), readOnly: true },