-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
275 lines (249 loc) · 8.71 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env node
import { MCPServer } from "./common/server.js";
import { VERSION } from "./common/version.js";
// 导入操作模块
import * as branchOperations from "./operations/branches.js";
import * as fileOperations from "./operations/files.js";
import * as issueOperations from "./operations/issues.js";
import * as pullOperations from "./operations/pulls.js";
import * as repoOperations from "./operations/repos.js";
import * as userOperations from "./operations/users.js";
import { z } from 'zod';
// 创建 Gitee MCP 服务器
export function createGiteeMCPServer() {
const server = new MCPServer({
name: "gitee",
version: VERSION,
});
// 注册仓库操作工具
server.registerTool({
name: "create_repository",
description: "创建 Gitee 仓库",
schema: repoOperations.CreateRepositorySchema,
handler: async (params: any) => {
try {
// 确保 private 参数是布尔值
if (params.private !== undefined) {
if (typeof params.private === 'string') {
// 将字符串转换为布尔值
params.private = params.private.toLowerCase() === 'true';
}
}
// 处理其他可能的布尔值字段
['has_issues', 'has_wiki', 'auto_init'].forEach(field => {
if (params[field] !== undefined && typeof params[field] === 'string') {
params[field] = params[field].toLowerCase() === 'true';
}
});
return await repoOperations.createRepository(params);
} catch (error) {
console.error('创建仓库失败:', error);
throw error;
}
},
});
server.registerTool({
name: "fork_repository",
description: "Fork Gitee 仓库",
schema: repoOperations.ForkRepositorySchema,
handler: async (params: any) => {
const { owner, repo, organization } = params;
return await repoOperations.forkRepository(owner, repo, organization);
},
});
// 注册分支操作工具
server.registerTool({
name: "create_branch",
description: "在 Gitee 仓库中创建一个新分支",
schema: branchOperations.CreateBranchSchema,
handler: async (params: any) => {
const { owner, repo, branch_name, refs } = params;
return await branchOperations.createBranchFromRef(owner, repo, branch_name, refs);
},
});
server.registerTool({
name: "list_branches",
description: "列出 Gitee 仓库中的分支",
schema: branchOperations.ListBranchesSchema,
handler: async (params: any) => {
const { owner, repo, sort, direction, page, per_page } = params;
return await branchOperations.listBranches(
owner,
repo,
sort,
direction,
page,
per_page
);
},
});
server.registerTool({
name: "get_branch",
description: "获取 Gitee 仓库中的特定分支信息",
schema: branchOperations.GetBranchSchema,
handler: async (params: any) => {
const { owner, repo, branch } = params;
return await branchOperations.getBranch(owner, repo, branch);
},
});
// 注册文件操作工具
server.registerTool({
name: "get_file_contents",
description: "获取 Gitee 仓库中文件或目录的内容",
schema: fileOperations.GetFileContentsSchema,
handler: async (params: any) => {
const { owner, repo, path, branch } = params;
return await fileOperations.getFileContents(owner, repo, path, branch);
},
});
server.registerTool({
name: "create_or_update_file",
description: "在 Gitee 仓库中创建或更新文件",
schema: fileOperations.CreateOrUpdateFileSchema,
handler: async (params: any) => {
const { owner, repo, path, content, message, branch, sha } = params;
return await fileOperations.createOrUpdateFile(
owner,
repo,
path,
content,
message,
branch,
sha
);
},
});
server.registerTool({
name: "push_files",
description: "向 Gitee 仓库提交多个文件",
schema: fileOperations.PushFilesSchema,
handler: async (params: any) => {
const { owner, repo, branch, message, files } = params;
return await fileOperations.pushFiles(owner, repo, branch, files, message);
},
});
// 注册 Issue 操作工具
server.registerTool({
name: "create_issue",
description: "在 Gitee 仓库中创建 Issue",
schema: issueOperations.CreateIssueSchema,
handler: async (params: any) => {
const { owner, repo, ...options } = params;
return await issueOperations.createIssue(owner, repo, options);
},
});
server.registerTool({
name: "list_issues",
description: "列出 Gitee 仓库中的 Issues",
schema: issueOperations.ListIssuesOptionsSchema,
handler: async (params: any) => {
const { owner, repo, ...options } = params;
return await issueOperations.listIssues(owner, repo, options);
},
});
server.registerTool({
name: "get_issue",
description: "获取 Gitee 仓库中的特定 Issue",
schema: issueOperations.GetIssueSchema,
handler: async (params: any) => {
const { owner, repo, issue_number } = params;
return await issueOperations.getIssue(owner, repo, issue_number);
},
});
server.registerTool({
name: "update_issue",
description: "更新 Gitee 仓库中的 Issue",
schema: issueOperations.UpdateIssueOptionsSchema,
handler: async (params: any) => {
const { owner, repo, issue_number, ...options } = params;
return await issueOperations.updateIssue(owner, repo, issue_number, options);
},
});
server.registerTool({
name: "add_issue_comment",
description: "向 Gitee 仓库中的 Issue 添加评论",
schema: issueOperations.IssueCommentSchema,
handler: async (params: any) => {
const { owner, repo, issue_number, body } = params;
return await issueOperations.addIssueComment(owner, repo, issue_number, body);
},
});
// 注册 Pull Request 操作工具
server.registerTool({
name: "create_pull_request",
description: "在 Gitee 仓库中创建 Pull Request",
schema: pullOperations.CreatePullRequestSchema,
handler: async (params: any) => {
const { owner, repo, ...rest } = params;
// 确保 owner 和 repo 参数存在
if (!owner || !repo) {
throw new Error("owner 和 repo 参数是必需的");
}
return await pullOperations.createPullRequest({ owner, repo, ...rest });
},
});
server.registerTool({
name: "list_pull_requests",
description: "列出 Gitee 仓库中的 Pull Requests",
schema: pullOperations.ListPullRequestsSchema,
handler: async (params: any) => {
const { owner, repo, ...options } = params;
return await pullOperations.listPullRequests(owner, repo, options);
},
});
server.registerTool({
name: "get_pull_request",
description: "获取 Gitee 仓库中的特定 Pull Request",
schema: pullOperations.GetPullRequestSchema,
handler: async (params: any) => {
const { owner, repo, pull_number } = params;
return await pullOperations.getPullRequest(owner, repo, pull_number);
},
});
server.registerTool({
name: "update_pull_request",
description: "更新 Gitee 仓库中的 Pull Request",
schema: pullOperations.UpdatePullRequestSchema,
handler: async (params: any) => {
const { owner, repo, pull_number, ...options } = params;
// 确保必需参数存在
if (!owner || !repo || pull_number === undefined) {
throw new Error("owner, repo 和 pull_number 参数是必需的");
}
return await pullOperations.updatePullRequest(owner, repo, pull_number, options);
},
});
server.registerTool({
name: "merge_pull_request",
description: "合并 Gitee 仓库中的 Pull Request",
schema: pullOperations.MergePullRequestSchema,
handler: async (params: any) => {
const { owner, repo, pull_number, ...options } = params;
// 确保必需参数存在
if (!owner || !repo || pull_number === undefined) {
throw new Error("owner, repo 和 pull_number 参数是必需的");
}
return await pullOperations.mergePullRequest(owner, repo, pull_number, options);
},
});
// 注册用户操作工具
server.registerTool({
name: "get_user",
description: "获取 Gitee 用户信息",
schema: userOperations.GetUserSchema,
handler: async (params: any) => {
const { username } = params;
return await userOperations.getUser(username);
},
});
server.registerTool({
name: "get_current_user",
description: "获取当前认证的 Gitee 用户信息",
schema: z.object({}),
handler: async () => {
return await userOperations.getCurrentUser();
},
});
return server;
}
export default createGiteeMCPServer;