leejunyoung5819gitgit-mcp

공개

gitgit.kr MCP 도구

71줄3.3 KB
1import { readFileSync } from "node:fs";
2
3export const version = "0.1.0";
4
5export type Config = {
6 /** API 서버 주소. 끝에 /api/v1 을 붙인 형태 (예: https://api.gitgit.kr/api/v1) */
7 apiBase: string;
8 token: string | undefined;
9 readOnly: boolean;
10 timeoutMs: number;
11 uploadTimeoutMs: number;
12 maxOutputChars: number;
13 /** 시작은 했지만 사용자가 알아야 할 설정 문제 */
14 warnings: string[];
15};
16
17const tokenPattern = /^gg[pb]_[A-Za-z0-9_-]{43}$/;
18
19function flag(value: string | undefined) {
20 return value !== undefined && ["1", "true", "yes", "on"].includes(value.trim().toLowerCase());
21}
22
23function positive(name: string, value: string | undefined, fallback: number) {
24 if (value === undefined || value.trim() === "") return fallback;
25 const parsed = Number(value);
26 if (!Number.isInteger(parsed) || parsed <= 0) throw new Error(`${name} 은(는) 양의 정수여야 합니다: ${value}`);
27 return parsed;
28}
29
30// GITGIT_API_URL 에는 https://api.gitgit.kr, http://localhost:8080 처럼 서버 주소만 적어도 되고 /api/v1 까지 적어도 됨
31export function apiBaseFrom(value: string) {
32 let url: URL;
33 try {
34 url = new URL(value.trim());
35 } catch {
36 throw new Error(`GITGIT_API_URL 이 올바른 주소가 아닙니다: ${value}`);
37 }
38 if (url.protocol !== "https:" && url.protocol !== "http:") throw new Error("GITGIT_API_URL 은 http:// 또는 https:// 로 시작해야 합니다.");
39 if (url.search || url.hash) throw new Error("GITGIT_API_URL 에 ? 나 # 를 넣지 마세요.");
40 const path = url.pathname.replace(/\/+$/, "");
41 url.pathname = path.endsWith("/api/v1") ? path : path + "/api/v1";
42 return url.toString().replace(/\/+$/, "");
43}
44
45export function loadConfig(env: NodeJS.ProcessEnv = process.env): Config {
46 const warnings: string[] = [];
47 let token = env.GITGIT_TOKEN?.trim() || undefined;
48 const tokenFile = env.GITGIT_TOKEN_FILE?.trim();
49 if (!token && tokenFile) {
50 try {
51 token = readFileSync(tokenFile, "utf8").trim() || undefined;
52 if (!token) warnings.push(`GITGIT_TOKEN_FILE(${tokenFile})이 비어 있어 익명으로 실행합니다.`);
53 } catch (error) {
54 // 토큰 파일을 아직 만들지 않았어도 공개 저장소 읽기는 되도록 익명으로 시작함
55 if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
56 warnings.push(`GITGIT_TOKEN_FILE(${tokenFile})이 없어 익명으로 실행합니다. 토큰을 이 파일에 저장한 뒤 MCP 서버를 다시 시작하세요.`);
57 }
58 }
59 if (token && !tokenPattern.test(token)) {
60 throw new Error("GITGIT_TOKEN 형식이 올바르지 않습니다. 계정 설정이나 Bot 설정에서 발급한 ggp_ 또는 ggb_ 토큰을 넣으세요.");
61 }
62 return {
63 apiBase: apiBaseFrom(env.GITGIT_API_URL?.trim() || "https://api.gitgit.kr"),
64 token,
65 readOnly: flag(env.GITGIT_READ_ONLY),
66 timeoutMs: positive("GITGIT_TIMEOUT_MS", env.GITGIT_TIMEOUT_MS, 30_000),
67 uploadTimeoutMs: positive("GITGIT_UPLOAD_TIMEOUT_MS", env.GITGIT_UPLOAD_TIMEOUT_MS, 10 * 60_000),
68 maxOutputChars: positive("GITGIT_MAX_OUTPUT_CHARS", env.GITGIT_MAX_OUTPUT_CHARS, 100_000),
69 warnings,
70 };
71}