@cnbcool/mcp-server
Version:
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
124 lines (123 loc) • 7.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = registerPullTools;
const zod_1 = require("zod");
const toolNames_js_1 = require("../constants/toolNames.js");
const toolDescriptions_js_1 = require("../constants/toolDescriptions.js");
const pull_js_1 = require("../api/pull.js");
const formatToolResult_js_1 = require("../helpers/formatToolResult.js");
function registerPullTools(server, client) {
server.tool(toolNames_js_1.ToolNames.LIST_PULLS, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.LIST_PULLS], {
repo: zod_1.z.string().describe('仓库路径,格式为 {group}/{repo}'),
state: zod_1.z
.preprocess((val) => (val === null ? undefined : val), zod_1.z.enum(['open', 'closed', 'all']).optional())
.describe('Pull Request状态'),
sort: zod_1.z
.preprocess((val) => (val === null ? undefined : val), zod_1.z.enum(['created', 'updated']).optional())
.describe('排序字段'),
direction: zod_1.z
.preprocess((val) => (val === null ? undefined : val), zod_1.z.enum(['asc', 'desc']).optional())
.describe('排序方向'),
page: zod_1.z.number().default(1).describe('页码'),
per_page: zod_1.z.number().default(30).describe('每页数量')
}, async ({ repo, ...params }) => {
try {
const pulls = await (0, pull_js_1.listPulls)(client, repo, params);
return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(pulls, null, 2), toolNames_js_1.ToolNames.LIST_PULLS);
}
catch (error) {
return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.LIST_PULLS);
}
});
server.tool(toolNames_js_1.ToolNames.GET_PULL, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.GET_PULL], {
repo: zod_1.z.string().describe('仓库路径,格式为 {group}/{repo}'),
number: zod_1.z.number().describe('Pull Request编号')
}, async ({ repo, number }) => {
try {
const pull = await (0, pull_js_1.getPull)(client, repo, number);
return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(pull, null, 2), toolNames_js_1.ToolNames.GET_PULL);
}
catch (error) {
return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.GET_PULL);
}
});
server.tool(toolNames_js_1.ToolNames.CREATE_PULL, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.CREATE_PULL], {
repo: zod_1.z.string().describe('目标仓库路径,格式为 {group}/{repo}'),
base: zod_1.z.string().describe('目标仓库目标分支'),
head_repo: zod_1.z.string().optional().describe('来源仓库路径,格式为 {group}/{repo},不填则为目标仓库'),
head: zod_1.z.string().describe('来源仓库分支'),
title: zod_1.z.string().describe('标题'),
body: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('描述')
}, async ({ repo, ...params }) => {
try {
const pull = await (0, pull_js_1.createPull)(client, repo, params);
return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(pull, null, 2), toolNames_js_1.ToolNames.CREATE_PULL);
}
catch (error) {
return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.CREATE_PULL);
}
});
server.tool(toolNames_js_1.ToolNames.UPDATE_PULL, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.UPDATE_PULL], {
repo: zod_1.z.string().describe('仓库路径,格式为 {group}/{repo}'),
number: zod_1.z.number().describe('Pull Request编号'),
title: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('标题'),
body: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('描述'),
state: zod_1.z
.preprocess((val) => (val === null ? undefined : val), zod_1.z.enum(['open', 'closed']).optional())
.describe('状态')
}, async ({ repo, number, ...params }) => {
try {
const pull = await (0, pull_js_1.updatePull)(client, repo, number, params);
return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(pull, null, 2), toolNames_js_1.ToolNames.UPDATE_PULL);
}
catch (error) {
return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.UPDATE_PULL);
}
});
server.tool(toolNames_js_1.ToolNames.MERGE_PULL, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.MERGE_PULL], {
repo: zod_1.z.string().describe('仓库路径,格式为 {group}/{repo}'),
number: zod_1.z.number().describe('Pull Request编号'),
merge_style: zod_1.z
.preprocess((val) => (val === null ? undefined : val), zod_1.z.enum(['merge', 'squash', 'rebase']).optional())
.describe('合并方式'),
commit_title: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string()).describe('合并提交标题'),
commit_message: zod_1.z
.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional())
.describe('合并提交信息')
}, async ({ repo, number, ...params }) => {
try {
const result = await (0, pull_js_1.mergePull)(client, repo, number, params);
return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(result, null, 2), toolNames_js_1.ToolNames.MERGE_PULL);
}
catch (error) {
return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.MERGE_PULL);
}
});
server.tool(toolNames_js_1.ToolNames.LIST_PULL_COMMENTS, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.LIST_PULL_COMMENTS], {
repo: zod_1.z.string().describe('仓库路径,格式为 {group}/{repo}'),
number: zod_1.z.number().describe('Pull Request编号'),
page: zod_1.z.number().default(1).describe('页码'),
per_page: zod_1.z.number().default(30).describe('每页数量')
}, async ({ repo, number, ...params }) => {
try {
const comments = await (0, pull_js_1.listPullComments)(client, repo, number, params);
return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(comments, null, 2), toolNames_js_1.ToolNames.LIST_PULL_COMMENTS);
}
catch (error) {
return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.LIST_PULL_COMMENTS);
}
});
server.tool(toolNames_js_1.ToolNames.CREATE_PULL_COMMENT, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.CREATE_PULL_COMMENT], {
repo: zod_1.z.string().describe('仓库路径,格式为 {group}/{repo}'),
number: zod_1.z.number().describe('Pull Request编号'),
body: zod_1.z.string().describe('评论内容')
}, async ({ repo, number, body }) => {
try {
await (0, pull_js_1.createPullComment)(client, repo, number, { body });
return (0, formatToolResult_js_1.formatTextToolResult)('Comment created', toolNames_js_1.ToolNames.CREATE_PULL_COMMENT);
}
catch (error) {
return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.CREATE_PULL_COMMENT);
}
});
}