leejunyoung5819gitgit-mcp

공개

gitgit.kr MCP 도구

85줄5.0 KB
1import { z } from "zod";
2import { GitGitApiError, repositoryPath, segment } from "../client.ts";
3import { cursor, defined, limit, owner, repo, type Registrar } from "../tooling.ts";
4
5const visibility = z.enum(["private", "public"]);
6
7export function registerAccountTools(register: Registrar) {
8 register("whoami", {
9 title: "현재 계정 확인",
10 description: "GITGIT_TOKEN 으로 인증된 계정(사용자 또는 Bot)과 연결된 GitGit 서버 주소를 확인합니다. 토큰이 없으면 익명 상태라고 알려 줍니다. 토큰에 profile:read 범위가 필요합니다.",
11 kind: "read",
12 input: {},
13 run: async (_args, { client }) => {
14 const status = await client.get<Record<string, unknown>>("/status").catch((error: unknown) => ({ error: String(error) }));
15 if (!client.authenticated) {
16 return { apiBase: client.config.apiBase, authenticated: false, readOnly: client.config.readOnly, status,
17 ...(client.config.warnings.length ? { warnings: client.config.warnings } : {}) };
18 }
19 const me = await client.get<{ user: unknown }>("/me");
20 return { apiBase: client.config.apiBase, authenticated: true, readOnly: client.config.readOnly, status, user: me.user };
21 },
22 });
23
24 register("search", {
25 title: "GitGit 전체 검색",
26 description: "저장소, 사용자, 조직을 이름과 설명으로 검색합니다. 읽을 수 있는 대상만 나옵니다. 저장소 안의 파일, Issue, PR 을 찾으려면 search_repository 를 쓰세요.",
27 kind: "read",
28 input: {
29 q: z.string().min(1).max(200).describe("검색어 (대소문자 구분 없는 부분 일치)"),
30 type: z.enum(["repositories", "users", "organizations"]).default("repositories").describe("검색 대상"),
31 limit: z.number().int().min(1).max(100).optional().describe("한 번에 받을 개수 (1~100, 기본 20)"),
32 cursor,
33 },
34 run: (args, { client }) => client.get("/search", args),
35 });
36
37 register("list_repositories", {
38 title: "저장소 목록",
39 description: "저장소 목록을 봅니다. owner 를 주면 그 사용자나 조직의 저장소(공개 + 내가 읽을 수 있는 비공개)를, 생략하면 토큰 계정이 접근할 수 있는 모든 저장소를 최근 생성 순으로 돌려줍니다. owner 없이 쓰려면 토큰이 필요합니다.",
40 kind: "read",
41 input: { owner: owner.optional(), limit, cursor },
42 run: async ({ owner: name, limit: size, cursor: next }, { client }) => {
43 const page = { limit: size, cursor: next };
44 if (!name) return client.get("/repositories", page);
45 try {
46 return await client.get(`/users/${segment(name)}`, page);
47 } catch (error) {
48 // 사용자 프로필이 없으면 조직으로 다시 찾음
49 if (!(error instanceof GitGitApiError) || error.status !== 404) throw error;
50 return client.get(`/organizations/${segment(name)}/repositories`, page);
51 }
52 },
53 });
54
55 register("get_repository", {
56 title: "저장소 정보와 README",
57 description: "저장소 메타데이터(공개 범위, 기본 브랜치, 내 역할, clone 주소)와 기본 브랜치의 최상위 파일 목록, README, 최신 커밋을 한 번에 돌려줍니다. 저장소 작업을 시작할 때 먼저 부르세요.",
58 kind: "read",
59 input: { owner, repo },
60 run: ({ owner: o, repo: r }, { client }) => client.get(repositoryPath(o, r, "/overview")),
61 });
62
63 register("list_organizations", {
64 title: "내 조직 목록",
65 description: "토큰 계정이 속한 조직과 그 안의 역할을 봅니다. 사용자 토큰과 profile:read 범위가 필요합니다.",
66 kind: "read",
67 input: { limit, cursor },
68 run: (args, { client }) => client.get("/organizations", args),
69 });
70
71 register("create_repository", {
72 title: "저장소 만들기",
73 description: "새 빈 Bare 저장소를 만듭니다. owner 를 생략하면 토큰 계정 소유로, 조직 slug 를 주면 그 조직 소유로 만듭니다. 사용자 토큰과 repositories:write 범위가 필요합니다. 응답의 sshUrl 또는 httpUrl 로 첫 Push 를 하면 됩니다.",
74 kind: "write",
75 input: {
76 name: z.string().min(1).max(100).describe("저장소 이름 (영문, 숫자, -, _, .)"),
77 owner: owner.optional().describe("조직 소유로 만들 때 조직 slug. 생략하면 내 계정"),
78 description: z.string().max(500).optional(),
79 visibility: visibility.optional().describe("기본값은 서버 정책(보통 private)"),
80 defaultBranch: z.string().min(1).max(100).optional().describe("기본 브랜치 이름 (기본 main)"),
81 issuesEnabled: z.boolean().optional(),
82 },
83 run: (args, { client }) => client.request("POST", "/repositories", { body: defined(args) }),
84 });
85}