@cnbcool/mcp-server 0.2.0-beta.0 → 0.3.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/client.js +42 -0
- package/dist/api/issue.js +27 -0
- package/dist/api/repository.js +28 -0
- package/dist/api/workspace.js +20 -0
- package/dist/index.js +11 -16
- package/dist/tools/index.js +12 -5
- package/dist/tools/issueTools.js +71 -46
- package/dist/tools/repoTools.js +63 -42
- package/dist/tools/workspaceTools.js +87 -0
- package/package.json +1 -1
- package/dist/CnbApiClient.js +0 -76
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export default class CnbApiClient {
|
|
2
|
+
static instance = null;
|
|
3
|
+
_baseUrl;
|
|
4
|
+
_token;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this._baseUrl = options.baseUrl;
|
|
7
|
+
this._token = options.token;
|
|
8
|
+
}
|
|
9
|
+
get baseUrl() {
|
|
10
|
+
return this._baseUrl;
|
|
11
|
+
}
|
|
12
|
+
static initialize(options) {
|
|
13
|
+
if (!CnbApiClient.instance) {
|
|
14
|
+
CnbApiClient.instance = new CnbApiClient(options);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
static getInstance() {
|
|
18
|
+
if (!CnbApiClient.instance) {
|
|
19
|
+
throw new Error('CnbApiClient not initialized. Call CnbApiClient.initialize(baseUrl, token) first.');
|
|
20
|
+
}
|
|
21
|
+
return CnbApiClient.instance;
|
|
22
|
+
}
|
|
23
|
+
async request(method, path, body, config) {
|
|
24
|
+
const url = `${this._baseUrl}${path}`;
|
|
25
|
+
const headers = {
|
|
26
|
+
Authorization: `Bearer ${this._token}`,
|
|
27
|
+
Accept: 'application/vnd.cnb.api+json',
|
|
28
|
+
...(config?.header || {})
|
|
29
|
+
};
|
|
30
|
+
const options = {
|
|
31
|
+
method,
|
|
32
|
+
headers,
|
|
33
|
+
body: body ? JSON.stringify(body) : undefined
|
|
34
|
+
};
|
|
35
|
+
const response = await fetch(url, options);
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
const errorText = await response.text();
|
|
38
|
+
throw new Error(`API request failed: ${response.status} ${errorText}`);
|
|
39
|
+
}
|
|
40
|
+
return response.json();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import CnbApiClient from './client.js';
|
|
2
|
+
export async function listIssues(repo, params) {
|
|
3
|
+
const cnbInst = CnbApiClient.getInstance();
|
|
4
|
+
const url = new URL(`/${repo}/-/issues`, cnbInst.baseUrl);
|
|
5
|
+
if (params) {
|
|
6
|
+
for (const [key, value] of Object.entries(params)) {
|
|
7
|
+
if (value === undefined)
|
|
8
|
+
continue;
|
|
9
|
+
url.searchParams.set(key, value.toString());
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return cnbInst.request('GET', `${url.pathname}${url.search}`);
|
|
13
|
+
}
|
|
14
|
+
export async function getIssue(repo, issueId) {
|
|
15
|
+
return CnbApiClient.getInstance().request('GET', `/${repo}/-/issues/${issueId}`);
|
|
16
|
+
}
|
|
17
|
+
export async function createIssue(repo, params) {
|
|
18
|
+
const newParams = Object.entries(params).reduce((acc, [key, value]) => {
|
|
19
|
+
if (value === undefined)
|
|
20
|
+
return acc;
|
|
21
|
+
Object.assign(acc, { [key]: value });
|
|
22
|
+
return acc;
|
|
23
|
+
}, {});
|
|
24
|
+
return CnbApiClient.getInstance().request('POST', `/${repo}/-/issues`, newParams, {
|
|
25
|
+
header: { 'Content-Type': 'application/json' }
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import CnbApiClient from './client.js';
|
|
2
|
+
export async function listRepositories(params) {
|
|
3
|
+
const cnbInst = CnbApiClient.getInstance();
|
|
4
|
+
const url = new URL('/user/repos', cnbInst.baseUrl);
|
|
5
|
+
if (params) {
|
|
6
|
+
for (const [key, value] of Object.entries(params)) {
|
|
7
|
+
if (value === undefined)
|
|
8
|
+
continue;
|
|
9
|
+
url.searchParams.set(key, value.toString());
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return cnbInst.request('GET', `${url.pathname}${url.search}`);
|
|
13
|
+
}
|
|
14
|
+
export async function listGroupRepositories(group, params) {
|
|
15
|
+
const cnbInst = CnbApiClient.getInstance();
|
|
16
|
+
const url = new URL(`/${group}/-/repos`, cnbInst.baseUrl);
|
|
17
|
+
if (params) {
|
|
18
|
+
for (const [key, value] of Object.entries(params)) {
|
|
19
|
+
if (value === undefined)
|
|
20
|
+
continue;
|
|
21
|
+
url.searchParams.set(key, value.toString());
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return cnbInst.request('GET', `${url.pathname}${url.search}`);
|
|
25
|
+
}
|
|
26
|
+
export async function getRepository(repo) {
|
|
27
|
+
return CnbApiClient.getInstance().request('GET', `/${repo}`);
|
|
28
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import CnbApiClient from './client.js';
|
|
2
|
+
export async function listWorkspace(params) {
|
|
3
|
+
const cnbInst = CnbApiClient.getInstance();
|
|
4
|
+
const url = new URL('/workspace/list', cnbInst.baseUrl);
|
|
5
|
+
if (params) {
|
|
6
|
+
for (const [key, value] of Object.entries(params)) {
|
|
7
|
+
if (value === undefined)
|
|
8
|
+
continue;
|
|
9
|
+
url.searchParams.set(key, value.toString());
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return cnbInst.request('GET', `${url.pathname}${url.search}`);
|
|
13
|
+
}
|
|
14
|
+
export async function deleteWorkspace(params) {
|
|
15
|
+
const cnbInst = CnbApiClient.getInstance();
|
|
16
|
+
const url = new URL('/workspace/delete', cnbInst.baseUrl);
|
|
17
|
+
return cnbInst.request('POST', `${url.pathname}`, params, {
|
|
18
|
+
header: { 'Content-Type': 'application/json' }
|
|
19
|
+
});
|
|
20
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { McpServer } from
|
|
3
|
-
import { StdioServerTransport } from
|
|
4
|
-
import dotenv from
|
|
5
|
-
import {
|
|
6
|
-
import { registerTools } from "./tools/index.js";
|
|
2
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
4
|
+
import dotenv from 'dotenv';
|
|
5
|
+
import { registerTools } from './tools/index.js';
|
|
7
6
|
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html#import-attributes
|
|
8
|
-
import packageJSON from
|
|
7
|
+
import packageJSON from '../package.json' with { type: 'json' };
|
|
9
8
|
dotenv.config();
|
|
10
|
-
const cnbApiClient = new CnbApiClient({
|
|
11
|
-
baseUrl: process.env.API_BASE_URL || "https://api.cnb.cool",
|
|
12
|
-
token: process.env.API_TOKEN || ""
|
|
13
|
-
});
|
|
14
9
|
const server = new McpServer({
|
|
15
|
-
name:
|
|
10
|
+
name: 'cnb-mcp-server',
|
|
16
11
|
version: packageJSON.version
|
|
17
12
|
});
|
|
18
|
-
registerTools(server
|
|
13
|
+
registerTools(server);
|
|
19
14
|
async function main() {
|
|
20
|
-
console.error(
|
|
15
|
+
console.error('server starting...');
|
|
21
16
|
const transport = new StdioServerTransport();
|
|
22
17
|
await server.connect(transport);
|
|
23
|
-
console.error(
|
|
18
|
+
console.error('server connected');
|
|
24
19
|
}
|
|
25
|
-
main().catch(error => {
|
|
26
|
-
console.error(
|
|
20
|
+
main().catch((error) => {
|
|
21
|
+
console.error('Fatal error:', error);
|
|
27
22
|
process.exit(1);
|
|
28
23
|
});
|
package/dist/tools/index.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import CnbApiClient from '../api/client.js';
|
|
2
|
+
import registerRepoTools from './repoTools.js';
|
|
3
|
+
import registerIssueTools from './issueTools.js';
|
|
4
|
+
import registerWorkspaceTools from './workspaceTools.js';
|
|
5
|
+
export function registerTools(server) {
|
|
6
|
+
CnbApiClient.initialize({
|
|
7
|
+
baseUrl: process.env.API_BASE_URL || 'https://api.cnb.cool',
|
|
8
|
+
token: process.env.API_TOKEN || ''
|
|
9
|
+
});
|
|
10
|
+
registerRepoTools(server);
|
|
11
|
+
registerIssueTools(server);
|
|
12
|
+
registerWorkspaceTools(server);
|
|
6
13
|
}
|
package/dist/tools/issueTools.js
CHANGED
|
@@ -1,71 +1,92 @@
|
|
|
1
|
-
import { z } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { createIssue, getIssue, listIssues } from '../api/issue.js';
|
|
3
|
+
export default function registerIssueTools(server) {
|
|
4
|
+
server.tool('list-issues', '查询仓库的 Issues', {
|
|
5
|
+
repoId: z.string().describe('仓库 ID'),
|
|
6
|
+
page: z.number().optional().describe('第几页,从1开始'),
|
|
7
|
+
page_size: z.number().optional().describe('每页多少条数据'),
|
|
8
|
+
state: z.enum(['open', 'closed']).optional().describe('Issue 状态'),
|
|
9
|
+
keyword: z.string().optional().describe('Issue 关键字'),
|
|
10
|
+
priority: z.string().optional().describe('Issue 优先级'),
|
|
11
|
+
labels: z.string().optional().describe('Issue 标签'),
|
|
12
|
+
authors: z.string().optional().describe('Issue 作者的名字'),
|
|
13
|
+
assignees: z.string().optional().describe('Issue 处理人'),
|
|
14
|
+
updated_time_begin: z.string().optional().describe('Issue 更新时间的范围,开始时间点'),
|
|
15
|
+
updated_time_end: z.string().optional().describe('Issue 更新时间的范围,结束时间点'),
|
|
16
|
+
order_by: z.string().optional().describe('Issue 排序顺序')
|
|
16
17
|
}, async ({ repoId, page, page_size, state, keyword, priority, labels, authors, assignees, updated_time_begin, updated_time_end, order_by }) => {
|
|
17
18
|
try {
|
|
18
|
-
const issues = await
|
|
19
|
+
const issues = await listIssues(repoId, {
|
|
20
|
+
page,
|
|
21
|
+
page_size,
|
|
22
|
+
state,
|
|
23
|
+
keyword,
|
|
24
|
+
priority,
|
|
25
|
+
labels,
|
|
26
|
+
authors,
|
|
27
|
+
assignees,
|
|
28
|
+
updated_time_begin,
|
|
29
|
+
updated_time_end,
|
|
30
|
+
order_by
|
|
31
|
+
});
|
|
19
32
|
return {
|
|
20
|
-
content: [
|
|
21
|
-
|
|
33
|
+
content: [
|
|
34
|
+
{
|
|
35
|
+
type: 'text',
|
|
22
36
|
text: JSON.stringify(issues, null, 2)
|
|
23
|
-
}
|
|
37
|
+
}
|
|
38
|
+
]
|
|
24
39
|
};
|
|
25
40
|
}
|
|
26
41
|
catch (error) {
|
|
27
42
|
return {
|
|
28
|
-
content: [
|
|
29
|
-
|
|
43
|
+
content: [
|
|
44
|
+
{
|
|
45
|
+
type: 'text',
|
|
30
46
|
text: `Error listing issues: ${error instanceof Error ? error.message : String(error)}`
|
|
31
|
-
}
|
|
47
|
+
}
|
|
48
|
+
],
|
|
32
49
|
isError: true
|
|
33
50
|
};
|
|
34
51
|
}
|
|
35
52
|
});
|
|
36
|
-
server.tool(
|
|
37
|
-
repoId: z.string().describe(
|
|
38
|
-
issueId: z.number().describe(
|
|
53
|
+
server.tool('get-issue', '获取指定 Issue 信息', {
|
|
54
|
+
repoId: z.string().describe('仓库 ID'),
|
|
55
|
+
issueId: z.number().describe('Issue ID')
|
|
39
56
|
}, async ({ repoId, issueId }) => {
|
|
40
57
|
try {
|
|
41
|
-
const issues = await
|
|
58
|
+
const issues = await getIssue(repoId, issueId);
|
|
42
59
|
return {
|
|
43
|
-
content: [
|
|
44
|
-
|
|
60
|
+
content: [
|
|
61
|
+
{
|
|
62
|
+
type: 'text',
|
|
45
63
|
text: JSON.stringify(issues, null, 2)
|
|
46
|
-
}
|
|
64
|
+
}
|
|
65
|
+
]
|
|
47
66
|
};
|
|
48
67
|
}
|
|
49
68
|
catch (error) {
|
|
50
69
|
return {
|
|
51
|
-
content: [
|
|
52
|
-
|
|
70
|
+
content: [
|
|
71
|
+
{
|
|
72
|
+
type: 'text',
|
|
53
73
|
text: `Error listing issues: ${error instanceof Error ? error.message : String(error)}`
|
|
54
|
-
}
|
|
74
|
+
}
|
|
75
|
+
],
|
|
55
76
|
isError: true
|
|
56
77
|
};
|
|
57
78
|
}
|
|
58
79
|
});
|
|
59
|
-
server.tool(
|
|
60
|
-
repoId: z.string().describe(
|
|
61
|
-
title: z.string().describe(
|
|
62
|
-
body: z.string().optional().describe(
|
|
63
|
-
assignees: z.array(z.string()).optional().describe(
|
|
64
|
-
labels: z.array(z.string()).optional().describe(
|
|
65
|
-
priority: z.string().optional().describe(
|
|
80
|
+
server.tool('create-issue', '创建一个 Issue', {
|
|
81
|
+
repoId: z.string().describe('仓库 ID'),
|
|
82
|
+
title: z.string().describe('Issue 标题'),
|
|
83
|
+
body: z.string().optional().describe('Issue 描述'),
|
|
84
|
+
assignees: z.array(z.string()).optional().describe('一个或多个 Issue 处理人的用户名'),
|
|
85
|
+
labels: z.array(z.string()).optional().describe('一个或多个 Issue 标签'),
|
|
86
|
+
priority: z.string().optional().describe('Issue 优先级')
|
|
66
87
|
}, async ({ repoId, title, body, assignees, labels, priority }) => {
|
|
67
88
|
try {
|
|
68
|
-
const issue = await
|
|
89
|
+
const issue = await createIssue(repoId, {
|
|
69
90
|
title,
|
|
70
91
|
body,
|
|
71
92
|
assignees,
|
|
@@ -73,18 +94,22 @@ export default function registerIssueTools(server, cnbApi) {
|
|
|
73
94
|
priority
|
|
74
95
|
});
|
|
75
96
|
return {
|
|
76
|
-
content: [
|
|
77
|
-
|
|
97
|
+
content: [
|
|
98
|
+
{
|
|
99
|
+
type: 'text',
|
|
78
100
|
text: `Issue created successfully:\n${JSON.stringify(issue, null, 2)}`
|
|
79
|
-
}
|
|
101
|
+
}
|
|
102
|
+
]
|
|
80
103
|
};
|
|
81
104
|
}
|
|
82
105
|
catch (error) {
|
|
83
106
|
return {
|
|
84
|
-
content: [
|
|
85
|
-
|
|
107
|
+
content: [
|
|
108
|
+
{
|
|
109
|
+
type: 'text',
|
|
86
110
|
text: `Error creating issue: ${error instanceof Error ? error.message : String(error)}`
|
|
87
|
-
}
|
|
111
|
+
}
|
|
112
|
+
],
|
|
88
113
|
isError: true
|
|
89
114
|
};
|
|
90
115
|
}
|
package/dist/tools/repoTools.js
CHANGED
|
@@ -1,80 +1,101 @@
|
|
|
1
|
-
import { z } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { getRepository, listGroupRepositories, listRepositories } from '../api/repository.js';
|
|
3
|
+
export default function registerRepoTools(server) {
|
|
4
|
+
server.tool('list-repositories', '获取当前用户拥有指定权限及其以上权限的仓库', {
|
|
5
|
+
page: z.number().default(1).describe('第几页,从1开始'),
|
|
6
|
+
page_size: z.number().default(20).describe('每页多少条数据'),
|
|
7
|
+
search: z.string().optional().describe('仓库关键字'),
|
|
8
|
+
filter_type: z.enum(['private', 'public', 'encrypted']).optional().describe('仓库类型'),
|
|
9
|
+
role: z.enum(['Reporter', 'Developer', 'Master', 'Owner']).optional().describe('最小仓库权限'),
|
|
10
|
+
order_by: z.enum(['created_at', 'last_updated_at', 'stars']).optional().describe('排序类型'),
|
|
11
|
+
desc: z.boolean().optional().describe('排序顺序')
|
|
11
12
|
}, async ({ page, page_size, search, filter_type, role, order_by, desc }) => {
|
|
12
13
|
try {
|
|
13
|
-
const repos = await
|
|
14
|
+
const repos = await listRepositories({ page, page_size, search, filter_type, role, order_by, desc });
|
|
14
15
|
return {
|
|
15
|
-
content: [
|
|
16
|
-
|
|
16
|
+
content: [
|
|
17
|
+
{
|
|
18
|
+
type: 'text',
|
|
17
19
|
text: JSON.stringify(repos, null, 2)
|
|
18
|
-
}
|
|
20
|
+
}
|
|
21
|
+
]
|
|
19
22
|
};
|
|
20
23
|
}
|
|
21
24
|
catch (error) {
|
|
22
25
|
return {
|
|
23
|
-
content: [
|
|
24
|
-
|
|
26
|
+
content: [
|
|
27
|
+
{
|
|
28
|
+
type: 'text',
|
|
25
29
|
text: `Error listing repositories: ${error instanceof Error ? error.message : String(error)}`
|
|
26
|
-
}
|
|
30
|
+
}
|
|
31
|
+
],
|
|
27
32
|
isError: true
|
|
28
33
|
};
|
|
29
34
|
}
|
|
30
35
|
});
|
|
31
|
-
server.tool(
|
|
32
|
-
group: z.string().describe(
|
|
33
|
-
page: z.number().default(1).describe(
|
|
34
|
-
page_size: z.number().default(20).describe(
|
|
35
|
-
search: z.string().optional().describe(
|
|
36
|
-
filter_type: z.enum([
|
|
37
|
-
descendant: z.enum([
|
|
38
|
-
order_by: z.enum([
|
|
39
|
-
desc: z.boolean().optional().describe(
|
|
36
|
+
server.tool('list-group-repositories', '获取分组里当前用户有权限的仓库', {
|
|
37
|
+
group: z.string().describe('组织名称'),
|
|
38
|
+
page: z.number().default(1).describe('第几页,从1开始'),
|
|
39
|
+
page_size: z.number().default(20).describe('每页多少条数据'),
|
|
40
|
+
search: z.string().optional().describe('仓库关键字'),
|
|
41
|
+
filter_type: z.enum(['private', 'public', 'encrypted']).optional().describe('仓库类型'),
|
|
42
|
+
descendant: z.enum(['all', 'sub', 'grand']).optional().describe('查全部、直接属于当前组织的仓库、子组织的仓库'),
|
|
43
|
+
order_by: z.enum(['created_at', 'last_updated_at', 'stars', 'slug_path']).optional().describe('排序类型'),
|
|
44
|
+
desc: z.boolean().optional().describe('排序顺序')
|
|
40
45
|
}, async ({ group, page, page_size, search, filter_type, descendant, order_by, desc }) => {
|
|
41
46
|
try {
|
|
42
|
-
const repos = await
|
|
47
|
+
const repos = await listGroupRepositories(group, {
|
|
48
|
+
page,
|
|
49
|
+
page_size,
|
|
50
|
+
search,
|
|
51
|
+
filter_type,
|
|
52
|
+
descendant,
|
|
53
|
+
order_by,
|
|
54
|
+
desc
|
|
55
|
+
});
|
|
43
56
|
return {
|
|
44
|
-
content: [
|
|
45
|
-
|
|
57
|
+
content: [
|
|
58
|
+
{
|
|
59
|
+
type: 'text',
|
|
46
60
|
text: JSON.stringify(repos, null, 2)
|
|
47
|
-
}
|
|
61
|
+
}
|
|
62
|
+
]
|
|
48
63
|
};
|
|
49
64
|
}
|
|
50
65
|
catch (error) {
|
|
51
66
|
return {
|
|
52
|
-
content: [
|
|
53
|
-
|
|
67
|
+
content: [
|
|
68
|
+
{
|
|
69
|
+
type: 'text',
|
|
54
70
|
text: `Error listing repositories: ${error instanceof Error ? error.message : String(error)}`
|
|
55
|
-
}
|
|
71
|
+
}
|
|
72
|
+
],
|
|
56
73
|
isError: true
|
|
57
74
|
};
|
|
58
75
|
}
|
|
59
76
|
});
|
|
60
|
-
server.tool(
|
|
61
|
-
repoId: z.string().describe(
|
|
77
|
+
server.tool('get-repository', '获取指定仓库信息', {
|
|
78
|
+
repoId: z.string().describe('仓库 ID')
|
|
62
79
|
}, async ({ repoId }) => {
|
|
63
80
|
try {
|
|
64
|
-
const repo = await
|
|
81
|
+
const repo = await getRepository(repoId);
|
|
65
82
|
return {
|
|
66
|
-
content: [
|
|
67
|
-
|
|
83
|
+
content: [
|
|
84
|
+
{
|
|
85
|
+
type: 'text',
|
|
68
86
|
text: JSON.stringify(repo, null, 2)
|
|
69
|
-
}
|
|
87
|
+
}
|
|
88
|
+
]
|
|
70
89
|
};
|
|
71
90
|
}
|
|
72
91
|
catch (error) {
|
|
73
92
|
return {
|
|
74
|
-
content: [
|
|
75
|
-
|
|
93
|
+
content: [
|
|
94
|
+
{
|
|
95
|
+
type: 'text',
|
|
76
96
|
text: `Error getting repository: ${error instanceof Error ? error.message : String(error)}`
|
|
77
|
-
}
|
|
97
|
+
}
|
|
98
|
+
],
|
|
78
99
|
isError: true
|
|
79
100
|
};
|
|
80
101
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { deleteWorkspace, listWorkspace } from '../api/workspace.js';
|
|
3
|
+
export default function registerWorkspaceTools(server) {
|
|
4
|
+
server.tool('list-workspace', '获取我的云原生开发环境列表', {
|
|
5
|
+
branch: z.string().optional().describe('分支名,例如:main'),
|
|
6
|
+
start: z
|
|
7
|
+
.string()
|
|
8
|
+
.optional()
|
|
9
|
+
.describe('查询结束时间,格式:YYYY-MM-DD HH:mm:ssZZ,例如:2024-12-01 00:00:00+0800'),
|
|
10
|
+
end: z.string().optional().describe('查询开始时间,格式:YYYY-MM-DD HH:mm:ssZZ,例如:2024-12-01 00:00:00+0800'),
|
|
11
|
+
page: z.number().optional().describe('分页页码,从 1 开始,默认为 1'),
|
|
12
|
+
pageSize: z.number().optional().describe('每页条数,默认为 20,最高 100'),
|
|
13
|
+
slug: z.string().optional().describe('仓库路径,例如:groupname/reponame'),
|
|
14
|
+
status: z
|
|
15
|
+
.enum(['running', 'closed'])
|
|
16
|
+
.optional()
|
|
17
|
+
.describe('开发环境状态,running: 开发环境已启动,closed:开发环境已关闭,默认为所有状态')
|
|
18
|
+
}, async ({ branch, page, pageSize, start, end, slug, status }) => {
|
|
19
|
+
console.log('list-workspace', {
|
|
20
|
+
branch,
|
|
21
|
+
page,
|
|
22
|
+
pageSize,
|
|
23
|
+
start,
|
|
24
|
+
end,
|
|
25
|
+
slug,
|
|
26
|
+
status
|
|
27
|
+
});
|
|
28
|
+
try {
|
|
29
|
+
const workspaces = await listWorkspace({
|
|
30
|
+
branch,
|
|
31
|
+
page,
|
|
32
|
+
pageSize,
|
|
33
|
+
start,
|
|
34
|
+
end,
|
|
35
|
+
slug,
|
|
36
|
+
status
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
content: [
|
|
40
|
+
{
|
|
41
|
+
type: 'text',
|
|
42
|
+
text: JSON.stringify(workspaces, null, 2)
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
return {
|
|
49
|
+
content: [
|
|
50
|
+
{
|
|
51
|
+
type: 'text',
|
|
52
|
+
text: `Error listing workspace: ${error instanceof Error ? error.message : String(error)}`
|
|
53
|
+
}
|
|
54
|
+
],
|
|
55
|
+
isError: true
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
server.tool('delete-workspace', '删除我的云原生开发环境', {
|
|
60
|
+
pipelineId: z.string().describe('开发环境 ID')
|
|
61
|
+
}, async ({ pipelineId }) => {
|
|
62
|
+
try {
|
|
63
|
+
const result = await deleteWorkspace({
|
|
64
|
+
pipelineId
|
|
65
|
+
});
|
|
66
|
+
return {
|
|
67
|
+
content: [
|
|
68
|
+
{
|
|
69
|
+
type: 'text',
|
|
70
|
+
text: JSON.stringify(result, null, 2)
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
return {
|
|
77
|
+
content: [
|
|
78
|
+
{
|
|
79
|
+
type: 'text',
|
|
80
|
+
text: `error delete workspace: ${error instanceof Error ? error.message : String(error)}`
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
isError: true
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cnbcool/mcp-server",
|
|
3
3
|
"description": "CNB MCP Server. A comprehensive MCP server that provides seamless integration to the CNB's API(https://cnb.cool), offering a wide range of tools for repository management, pipelines operations and collaboration features",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0-beta.1",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"cnb-mcp-server": "dist/index.js"
|
package/dist/CnbApiClient.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
export class CnbApiClient {
|
|
2
|
-
baseUrl;
|
|
3
|
-
token;
|
|
4
|
-
constructor(options) {
|
|
5
|
-
this.baseUrl = options.baseUrl;
|
|
6
|
-
this.token = options.token;
|
|
7
|
-
}
|
|
8
|
-
async request(method, path, body, config) {
|
|
9
|
-
const url = `${this.baseUrl}${path}`;
|
|
10
|
-
const headers = {
|
|
11
|
-
'Authorization': `Bearer ${this.token}`,
|
|
12
|
-
'Accept': 'application/vnd.cnb.api+json',
|
|
13
|
-
...(config?.header || {})
|
|
14
|
-
};
|
|
15
|
-
const options = {
|
|
16
|
-
method,
|
|
17
|
-
headers,
|
|
18
|
-
body: body ? JSON.stringify(body) : undefined
|
|
19
|
-
};
|
|
20
|
-
const response = await fetch(url, options);
|
|
21
|
-
if (!response.ok) {
|
|
22
|
-
const errorText = await response.text();
|
|
23
|
-
throw new Error(`API request failed: ${response.status} ${errorText}`);
|
|
24
|
-
}
|
|
25
|
-
return response.json();
|
|
26
|
-
}
|
|
27
|
-
async listRepositories(params) {
|
|
28
|
-
const url = new URL('/user/repos', this.baseUrl);
|
|
29
|
-
if (params) {
|
|
30
|
-
for (const [key, value] of Object.entries(params)) {
|
|
31
|
-
if (value === undefined)
|
|
32
|
-
continue;
|
|
33
|
-
url.searchParams.set(key, value.toString());
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return this.request('GET', `${url.pathname}${url.search}`);
|
|
37
|
-
}
|
|
38
|
-
async listGroupRepositories(group, params) {
|
|
39
|
-
const url = new URL(`/${group}/-/repos`, this.baseUrl);
|
|
40
|
-
if (params) {
|
|
41
|
-
for (const [key, value] of Object.entries(params)) {
|
|
42
|
-
if (value === undefined)
|
|
43
|
-
continue;
|
|
44
|
-
url.searchParams.set(key, value.toString());
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return this.request('GET', `${url.pathname}${url.search}`);
|
|
48
|
-
}
|
|
49
|
-
async getRepository(repo) {
|
|
50
|
-
return this.request('GET', `/${repo}`);
|
|
51
|
-
}
|
|
52
|
-
async listIssues(repo, params) {
|
|
53
|
-
const url = new URL(`/${repo}/-/issues`, this.baseUrl);
|
|
54
|
-
if (params) {
|
|
55
|
-
for (const [key, value] of Object.entries(params)) {
|
|
56
|
-
if (value === undefined)
|
|
57
|
-
continue;
|
|
58
|
-
url.searchParams.set(key, value.toString());
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return this.request('GET', `${url.pathname}${url.search}`);
|
|
62
|
-
}
|
|
63
|
-
async getIssue(repo, issueId) {
|
|
64
|
-
return this.request('GET', `/${repo}/-/issues/${issueId}`);
|
|
65
|
-
}
|
|
66
|
-
async createIssue(repo, params) {
|
|
67
|
-
const newParams = Object.entries(params).reduce((acc, [key, value]) => {
|
|
68
|
-
if (value === undefined)
|
|
69
|
-
return acc;
|
|
70
|
-
// @ts-ignore
|
|
71
|
-
acc[key] = value;
|
|
72
|
-
return acc;
|
|
73
|
-
}, {});
|
|
74
|
-
return this.request('POST', `/${repo}/-/issues`, newParams, { header: { 'Content-Type': 'application/json' } });
|
|
75
|
-
}
|
|
76
|
-
}
|