git_read_important_files
Read contents of specified files from any Git repository to quickly access important documentation, configuration, or source code without cloning the entire project.
Instructions
Read the contents of specified files in a given git repository.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_url | Yes | The URL of the Git repository | |
| file_paths | Yes | List of file paths to read (relative to repository root) |
Implementation Reference
- src/index.js:180-217 (handler)The main handler function that clones the Git repository to a temporary directory, reads the contents of the specified file paths relative to the repo root, handles missing files and read errors, and returns a JSON object with file contents or error messages as text content.async handleGitReadImportantFiles({ repo_url, file_paths }) { try { const repoPath = await cloneRepo(repo_url); const results = {}; for (const filePath of file_paths) { const fullPath = path.join(repoPath, filePath); try { if (await fs.pathExists(fullPath)) { results[filePath] = await fs.readFile(fullPath, 'utf8'); } else { results[filePath] = 'Error: File not found'; } } catch (error) { results[filePath] = `Error reading file: ${error.message}`; } } return { content: [ { type: 'text', text: JSON.stringify(results, null, 2), }, ], }; } catch (error) { return { content: [ { type: 'text', text: JSON.stringify({ error: `Failed to process repository: ${error.message}` }, null, 2), }, ], isError: true, }; } }
- src/index.js:121-135 (schema)Input schema for the tool, specifying repo_url as a string and file_paths as an array of strings, both required.inputSchema: { type: 'object', properties: { repo_url: { type: 'string', description: 'The URL of the Git repository', }, file_paths: { type: 'array', items: { type: 'string' }, description: 'List of file paths to read (relative to repository root)', }, }, required: ['repo_url', 'file_paths'], },
- src/index.js:118-136 (registration)Tool registration in the ListToolsRequestHandler response, including name, description, and input schema.{ name: 'git_read_important_files', description: 'Read the contents of specified files in a given git repository.', inputSchema: { type: 'object', properties: { repo_url: { type: 'string', description: 'The URL of the Git repository', }, file_paths: { type: 'array', items: { type: 'string' }, description: 'List of file paths to read (relative to repository root)', }, }, required: ['repo_url', 'file_paths'], }, },
- src/index.js:17-49 (helper)Helper function to clone the Git repository to a deterministic temporary directory based on repo URL hash, reusing if valid, cleaning up on errors.async function cloneRepo(repoUrl) { // Create deterministic directory name based on repo URL const repoHash = crypto.createHash('sha256') .update(repoUrl) .digest('hex') .slice(0, 12); const tempDir = path.join(os.tmpdir(), `github_tools_${repoHash}`); // Check if directory exists and is a valid git repo if (await fs.pathExists(tempDir)) { try { const git = simpleGit(tempDir); const remotes = await git.getRemotes(true); if (remotes.length > 0 && remotes[0].refs.fetch === repoUrl) { return tempDir; } } catch (error) { // If there's any error with existing repo, clean it up await fs.remove(tempDir); } } // Create directory and clone repository await fs.ensureDir(tempDir); try { await simpleGit().clone(repoUrl, tempDir); return tempDir; } catch (error) { // Clean up on error await fs.remove(tempDir); throw new Error(`Failed to clone repository: ${error.message}`); } }