leejunyoung5819gitgit-mcp

공개

gitgit.kr MCP 도구

117줄6.9 KB
1import { openAsBlob } from "node:fs";
2import { stat } from "node:fs/promises";
3import { basename, extname, isAbsolute, resolve } from "node:path";
4import { z } from "zod";
5import { repositoryPath, segment } from "../client.ts";
6import { cursor, defined, limit, owner, repo, type Registrar } from "../tooling.ts";
7
8const releaseRef = z.string().min(1).max(250);
9const releaseId = z.uuid().describe("Release id (list_releases 참고)");
10const fields = {
11 target: releaseRef.optional().describe("태그가 없을 때 태그를 만들 브랜치 이름 또는 커밋 OID. 생략하면 기본 브랜치"),
12 name: z.string().max(200).optional().describe("Release 제목"),
13 body: z.string().max(20000).optional().describe("Release 노트 (Markdown)"),
14 draft: z.boolean().optional().describe("초안이면 쓰기 권한자에게만 보이고 태그를 만들지 않음"),
15 prerelease: z.boolean().optional().describe("사전 출시 표시. 최신 Release 에서 제외됨"),
16};
17const itemPath = (o: string, r: string, id: string, suffix = "") => repositoryPath(o, r, `/releases/${segment(id)}${suffix}`);
18
19// 서버가 업로드 형식으로 파일 종류를 기록하므로 흔한 확장자는 알맞은 MIME 형식을 붙임
20const mimeTypes: Record<string, string> = {
21 ".zip": "application/zip", ".gz": "application/gzip", ".tgz": "application/gzip", ".tar": "application/x-tar",
22 ".xz": "application/x-xz", ".bz2": "application/x-bzip2", ".7z": "application/x-7z-compressed",
23 ".txt": "text/plain", ".md": "text/markdown", ".json": "application/json", ".pdf": "application/pdf",
24 ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp",
25 ".exe": "application/vnd.microsoft.portable-executable", ".msi": "application/x-msi", ".dmg": "application/x-apple-diskimage",
26 ".deb": "application/vnd.debian.binary-package", ".rpm": "application/x-rpm", ".apk": "application/vnd.android.package-archive",
27 ".jar": "application/java-archive", ".wasm": "application/wasm", ".sha256": "text/plain", ".sig": "application/pgp-signature",
28 ".asc": "application/pgp-signature",
29};
30
31export function registerReleaseTools(register: Registrar) {
32 register("list_releases", {
33 title: "Release 목록",
34 description: "저장소의 Release 를 최근 순으로 봅니다. 초안은 쓰기 권한자에게만 보입니다.",
35 kind: "read",
36 input: { owner, repo, limit, cursor },
37 run: ({ owner: o, repo: r, ...page }, { client }) => client.get(repositoryPath(o, r, "/releases"), page),
38 });
39
40 register("get_release", {
41 title: "Release 상세",
42 description: "Release 하나와 Asset 목록을 봅니다. id, tag 중 하나를 주거나, 둘 다 생략하면 최신 Release(초안, 사전 출시 제외)를 돌려줍니다.",
43 kind: "read",
44 input: { owner, repo, id: releaseId.optional(), tag: releaseRef.optional().describe("태그 이름 (/ 포함 가능)") },
45 run: ({ owner: o, repo: r, id, tag }, { client }) => {
46 if (id && tag) throw new Error("id 와 tag 중 하나만 주세요.");
47 if (id) return client.get(itemPath(o, r, id));
48 if (tag) return client.get(repositoryPath(o, r, `/releases/tags/${tag.split("/").map(segment).join("/")}`));
49 return client.get(repositoryPath(o, r, "/releases/latest"));
50 },
51 });
52
53 register("create_release", {
54 title: "Release 게시",
55 description: "Git 태그 하나에 제목, 노트를 묶어 Release 를 게시하거나 초안으로 저장합니다. 태그가 없으면 target 에서 서버가 태그를 만듭니다. releases:write 범위가 필요합니다.",
56 kind: "write",
57 input: { owner, repo, tagName: releaseRef.describe("태그 이름. 예: v1.2.0"), ...fields },
58 run: ({ owner: o, repo: r, ...body }, { client }) =>
59 client.request("POST", repositoryPath(o, r, "/releases"), { body: defined(body) }),
60 });
61
62 register("update_release", {
63 title: "Release 수정",
64 description: "Release 의 태그, 제목, 노트, 초안/사전 출시 상태를 바꿉니다. 초안에서 draft=false 로 바꾸면 게시됩니다.",
65 kind: "write",
66 idempotent: true,
67 input: { owner, repo, id: releaseId, tagName: releaseRef.optional(), ...fields },
68 run: ({ owner: o, repo: r, id, ...body }, { client }) =>
69 client.request("PATCH", itemPath(o, r, id), { body: defined(body) }),
70 });
71
72 register("delete_release", {
73 title: "Release 삭제",
74 description: "Release 와 그 Asset 을 삭제합니다. Git 태그는 남습니다. 되돌릴 수 없습니다.",
75 kind: "destructive",
76 idempotent: true,
77 input: { owner, repo, id: releaseId },
78 run: async ({ owner: o, repo: r, id }, { client }) => {
79 await client.request("DELETE", itemPath(o, r, id));
80 return { deleted: true, id };
81 },
82 });
83
84 register("upload_release_asset", {
85 title: "Release Asset 올리기",
86 description: "이 MCP 서버가 실행 중인 컴퓨터의 파일을 Release 의 배포 파일(Asset)로 올립니다. filePath 는 절대 경로를 권장합니다. 같은 이름의 Asset 이 있으면 거부됩니다.",
87 kind: "write",
88 input: {
89 owner, repo, id: releaseId,
90 filePath: z.string().min(1).describe("올릴 로컬 파일 경로"),
91 filename: z.string().min(1).max(255).optional().describe("GitGit 에 저장할 파일 이름. 생략하면 로컬 파일 이름"),
92 contentType: z.string().min(1).max(200).optional().describe("MIME 형식. 생략하면 확장자로 추정"),
93 },
94 run: async ({ owner: o, repo: r, id, filePath, filename, contentType }, { client }) => {
95 const path = isAbsolute(filePath) ? filePath : resolve(filePath);
96 const info = await stat(path);
97 if (!info.isFile()) throw new Error(`일반 파일이 아닙니다: ${path}`);
98 const name = filename ?? basename(path);
99 const type = contentType ?? mimeTypes[extname(name).toLowerCase()] ?? "application/octet-stream";
100 const form = new FormData();
101 form.append("file", await openAsBlob(path, { type }), name);
102 return client.request("POST", itemPath(o, r, id, "/assets"), { form, timeoutMs: client.config.uploadTimeoutMs });
103 },
104 });
105
106 register("delete_release_asset", {
107 title: "Release Asset 삭제",
108 description: "Release 의 배포 파일 하나를 삭제합니다. 되돌릴 수 없습니다.",
109 kind: "destructive",
110 idempotent: true,
111 input: { owner, repo, id: releaseId, assetId: z.uuid().describe("Asset id (get_release 참고)") },
112 run: async ({ owner: o, repo: r, id, assetId }, { client }) => {
113 await client.request("DELETE", itemPath(o, r, id, `/assets/${segment(assetId)}`));
114 return { deleted: true, id, assetId };
115 },
116 });
117}