Skip to main content
Glama

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

TableJSON Schema
NameRequiredDescriptionDefault
file_pathNo
project_idNo
refNo

Implementation Reference

  • MCP tool handler for 'get_file_contents' that parses arguments using the schema and delegates to GitLabApi.getFileContents
    case "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) }] };
    }
  • Core implementation of getFileContents that fetches file from GitLab API, decodes base64 content, and returns structured data
    async 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;
    }
  • Zod schema defining input parameters for get_file_contents tool: project_id, file_path, ref
    export 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 handler
      name: "get_file_contents",
      description: "Get the contents of a file or directory from a GitLab project",
      inputSchema: createJsonSchema(GetFileContentsSchema),
      readOnly: true
    },

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/yoda-digital/mcp-gitlab-server'

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