get-content
Retrieve file or directory content from GitHub Enterprise repositories by specifying owner, repo, and path parameters.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| owner | Yes | User or organization name | |
| repo | Yes | Repository name | |
| path | Yes | File or directory path | |
| ref | No | Branch or commit hash (default: default branch) |
Implementation Reference
- server/index.js:294-364 (handler)The primary handler for the 'get-content' MCP tool. Validates inputs using Zod, fetches content from GitHub via RepositoryAPI, formats the response with formatContent, and returns a structured text response.server.tool("get-content", { owner: z.string().describe("저장소 소유자 (사용자 또는 조직)"), repo: z.string().describe("저장소 이름"), path: z.string().describe("파일 또는 디렉토리 경로"), ref: z.string().optional().describe("브랜치 또는 커밋 해시 (기본값: 기본 브랜치)") }, async ({ owner, repo, path, ref }) => { try { // 매개변수 검증 if (!owner || typeof owner !== 'string' || owner.trim() === '') { return { content: [ { type: "text", text: "오류: 저장소 소유자(owner)는 필수 항목입니다." } ], isError: true }; } if (!repo || typeof repo !== 'string' || repo.trim() === '') { return { content: [ { type: "text", text: "오류: 저장소 이름(repo)은 필수 항목입니다." } ], isError: true }; } if (!path || typeof path !== 'string') { return { content: [ { type: "text", text: "오류: 파일 또는 디렉토리 경로(path)는 필수 항목입니다." } ], isError: true }; } const content = await context.repository.getContent(owner, repo, path, ref); // 내용 형식화 const formattedContent = formatContent(content); // 디렉토리인지 파일인지에 따라 다른 응답 메시지 const isDirectory = Array.isArray(content); const responseText = isDirectory ? `디렉토리 '${path}'의 내용 (${formattedContent.length}개 항목):` : `파일 '${path}'의 내용:`; return { content: [ { type: "text", text: `저장소 '${owner}/${repo}'의 ${responseText}\n\n${JSON.stringify(formattedContent, null, 2)}` } ] }; } catch (error) { console.error('파일/디렉토리 내용 조회 오류:', error); return { content: [ { type: "text", text: `파일/디렉토리 내용 조회 중 오류가 발생했습니다: ${error.message}` } ], isError: true }; } });
- server/index.js:295-298 (schema)Zod input schema for the 'get-content' tool parameters.owner: z.string().describe("저장소 소유자 (사용자 또는 조직)"), repo: z.string().describe("저장소 이름"), path: z.string().describe("파일 또는 디렉토리 경로"), ref: z.string().optional().describe("브랜치 또는 커밋 해시 (기본값: 기본 브랜치)")
- server/index.js:54-83 (helper)Helper function to format GitHub contents API response, handling both files (decoding base64) and directories.function formatContent(content) { // 디렉토리인 경우 (배열) if (Array.isArray(content)) { return content.map(item => ({ name: item.name, path: item.path, type: item.type, size: item.size, url: item.html_url })); } // 파일인 경우 (단일 객체) const result = { name: content.name, path: content.path, type: content.type, size: content.size, url: content.html_url }; // 파일 내용이 있으면 디코딩 if (content.content && content.encoding === 'base64') { try { result.content = Buffer.from(content.content, 'base64').toString('utf-8'); } catch (e) { result.content = '파일 내용을 디코딩할 수 없습니다.'; } } return result; }
- api/repos/repository.js:86-90 (helper)RepositoryAPI method that performs the actual GitHub API call to retrieve file or directory contents.async getContent(owner, repo, path, ref) { return this.client.get(`repos/${owner}/${repo}/contents/${path}`, { params: { ref } }); }