github_get_code
Extract and combine code from GitHub repositories into a single file or directory structure. Use filters like file extensions, directories, or branches to streamline retrieval. Supports tree views and specific file fetching for precise code access.
Instructions
Retrieves code from a GitHub repository URL and combines it into a single file. The URL must start with "https://".
Query Parameters:
dir: Filter files by directory paths (comma-separated) Example: ?dir=src/components,tests/unit
ext: Filter files by extensions (comma-separated) Example: ?ext=ts,tsx,js
mode: Display mode Example: ?mode=tree (Shows directory structure and README files only)
branch: Specify the branch to fetch from (optional) Example: ?branch=feature/new-feature
file: Specify a single file to retrieve (optional) Example: ?file=src/components/Button.tsx
Examples:
For GitHub tree URLs with branch: https://github.com/kazuph/pera1/tree/feature/great-branch This URL will be automatically parsed to extract the branch information.
For specific directory in a branch: url: https://github.com/modelcontextprotocol/servers dir: src/fetch branch: develop
For a single file: url: https://github.com/username/repository file: src/components/Button.tsx
For directory structure with README files only: url: https://github.com/username/repository mode: tree
The tool will correctly parse the repository structure and fetch the files from the specified branch.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branch | No | ||
| dir | No | ||
| ext | No | ||
| file | No | ||
| mode | No | ||
| url | Yes |
Implementation Reference
- src/index.ts:86-124 (handler)The main handler for the 'github_get_code' tool within the CallToolRequest switch statement. It validates input arguments using GithubUrlSchema, constructs a worker URL using buildWorkerUrl, fetches the content, and returns it as text or an error response.case 'github_get_code': { const parsed = GithubUrlSchema.safeParse(args); if (!parsed.success) { throw new Error( `Invalid arguments for github_get_code: ${parsed.error}` ); } try { const response = await fetch(buildWorkerUrl(parsed.data)); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const text = await response.text(); return { content: [ { type: 'text', text, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: 'text', text: `API Error: ${errorMessage}`, }, ], isError: true, }; } }
- src/githubTool.ts:5-12 (schema)Zod schema defining the input structure for the 'github_get_code' tool, including required URL and optional filters like dir, ext, mode, branch, and file.export const GithubUrlSchema = z.object({ url: z.string().url(), dir: z.string().optional(), ext: z.string().optional(), mode: z.enum(['tree']).optional(), branch: z.string().optional(), file: z.string().optional(), });
- src/index.ts:36-73 (registration)Tool registration object returned by ListToolsRequest handler, specifying name, detailed description, and input schema for 'github_get_code'.name: 'github_get_code', description: ` Retrieves code from a GitHub repository URL and combines it into a single file. The URL must start with "https://". Query Parameters: - dir: Filter files by directory paths (comma-separated) Example: ?dir=src/components,tests/unit - ext: Filter files by extensions (comma-separated) Example: ?ext=ts,tsx,js - mode: Display mode Example: ?mode=tree (Shows directory structure and README files only) - branch: Specify the branch to fetch from (optional) Example: ?branch=feature/new-feature - file: Specify a single file to retrieve (optional) Example: ?file=src/components/Button.tsx Examples: 1. For GitHub tree URLs with branch: https://github.com/kazuph/pera1/tree/feature/great-branch This URL will be automatically parsed to extract the branch information. 2. For specific directory in a branch: url: https://github.com/modelcontextprotocol/servers dir: src/fetch branch: develop 3. For a single file: url: https://github.com/username/repository file: src/components/Button.tsx 4. For directory structure with README files only: url: https://github.com/username/repository mode: tree The tool will correctly parse the repository structure and fetch the files from the specified branch. `, inputSchema: zodToJsonSchema(GithubUrlSchema) as ToolInput, },
- src/githubTool.ts:16-26 (helper)Utility function to build the complete worker URL by combining the base worker URL with the GitHub URL and appending optional query parameters.export const buildWorkerUrl = (params: GithubWorkerRequest): string => { const url = new URL(`${WORKER_BASE_URL}${params.url}`); if (params.dir) url.searchParams.set('dir', params.dir); if (params.ext) url.searchParams.set('ext', params.ext); if (params.mode) url.searchParams.set('mode', params.mode); if (params.branch) url.searchParams.set('branch', params.branch); if (params.file) url.searchParams.set('file', params.file); return url.toString(); };
- src/githubTool.ts:3-4 (helper)Constant defining the base URL of the Cloudflare Worker that performs the actual GitHub code fetching.export const WORKER_BASE_URL = 'https://pera1.kazu-san.workers.dev/';