UNPKG

@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

242 lines (241 loc) 14.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = registerIssueTools; const zod_1 = require("zod"); const toolNames_js_1 = require("../constants/toolNames.js"); const toolDescriptions_js_1 = require("../constants/toolDescriptions.js"); const issue_js_1 = require("../api/issue.js"); const formatToolResult_js_1 = require("../helpers/formatToolResult.js"); function registerIssueTools(server, client) { server.tool(toolNames_js_1.ToolNames.LIST_ISSUES, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.LIST_ISSUES], { repo: zod_1.z.string().describe('仓库路径'), page: zod_1.z.number().default(1).describe('第几页,从1开始'), page_size: zod_1.z.number().default(10).describe('每页多少条数据,默认是30'), state: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.enum(['open', 'closed']).optional()) .describe('Issue 状态'), keyword: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('Issue 关键字'), priority: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('Issue 优先级,example: p0,p1,p2,p3'), labels: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('Issue 标签'), authors: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('Issue 作者的名字, example: 张三,李四'), assignees: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('Issue 处理人,example: 张三,李四,-; - means assign to nobody'), updated_time_begin: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('Issue 更新时间的范围,开始时间点,example: 2022-01-31'), updated_time_end: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('Issue 更新时间的范围,结束时间点,example: 2022-01-31'), order_by: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('Issue 排序顺序.example: created_at, -updated_at, reference_count。‘-’ prefix means descending order') }, async ({ repo, page, page_size, state, keyword, priority, labels, authors, assignees, updated_time_begin, updated_time_end, order_by }) => { try { const issues = await (0, issue_js_1.listIssues)(client, repo, { page, page_size, state, keyword, priority, labels, authors, assignees, updated_time_begin, updated_time_end, order_by }); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(issues, null, 2), toolNames_js_1.ToolNames.LIST_ISSUES); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.LIST_ISSUES); } }); server.tool(toolNames_js_1.ToolNames.GET_ISSUE, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.GET_ISSUE], { repo: zod_1.z.string().describe('仓库路径'), issueId: zod_1.z.number().describe('Issue ID') }, async ({ repo, issueId }) => { try { const issues = await (0, issue_js_1.getIssue)(client, repo, issueId); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(issues, null, 2), toolNames_js_1.ToolNames.GET_ISSUE); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.GET_ISSUE); } }); server.tool(toolNames_js_1.ToolNames.CREATE_ISSUE, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.CREATE_ISSUE], { repo: zod_1.z.string().describe('仓库路径'), title: zod_1.z.string().describe('Issue 标题'), body: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('Issue 描述'), assignees: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.array(zod_1.z.string()).optional()) .describe('一个或多个 Issue 处理人的用户名'), labels: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.array(zod_1.z.string()).optional()) .describe('一个或多个 Issue 标签'), priority: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('Issue 优先级'), end_date: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('Issue 截止时间,格式为 YYYY-MM-DD'), start_date: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('Issue 起始时间,格式为 YYYY-MM-DD') }, async ({ repo, title, body, assignees, labels, priority, end_date, start_date }) => { try { const issue = await (0, issue_js_1.createIssue)(client, repo, { title, body, assignees, labels, priority, end_date, start_date }); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(issue, null, 2), toolNames_js_1.ToolNames.CREATE_ISSUE); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.CREATE_ISSUE); } }); server.tool(toolNames_js_1.ToolNames.UPDATE_ISSUE, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.UPDATE_ISSUE], { repo: zod_1.z.string().describe('仓库路径'), issueId: zod_1.z.number().describe('Issue ID'), title: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('Issue 标题'), body: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('Issue 描述'), priority: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('Issue 优先级'), end_date: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('Issue 截止时间,格式为 YYYY-MM-DD'), start_date: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('Issue 起始时间,格式为 YYYY-MM-DD'), state: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('Issue 状态'), state_reason: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.enum(['completed', 'not_planned', 'reopened']).optional()) .describe('Issue 状态原因') }, async ({ repo, issueId, title, body, priority, end_date, start_date, state, state_reason }) => { try { const issue = await (0, issue_js_1.updateIssue)(client, repo, issueId, { title, body, priority, end_date, start_date, state, state_reason }); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(issue, null, 2), toolNames_js_1.ToolNames.UPDATE_ISSUE); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.UPDATE_ISSUE); } }); server.tool(toolNames_js_1.ToolNames.LIST_ISSUE_COMMENTS, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.LIST_ISSUE_COMMENTS], { repo: zod_1.z.string().describe('仓库路径'), issueId: zod_1.z.number().describe('Issue ID'), page: zod_1.z.number().default(1).describe('第几页,从1开始'), page_size: zod_1.z.number().default(30).describe('每页多少条数据,默认是30') }, async ({ repo, issueId, page, page_size }) => { try { const comments = await (0, issue_js_1.listIssueComments)(client, repo, issueId, { page, page_size }); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(comments, null, 2), toolNames_js_1.ToolNames.LIST_ISSUE_COMMENTS); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.LIST_ISSUE_COMMENTS); } }); server.tool(toolNames_js_1.ToolNames.CREATE_ISSUE_COMMENT, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.CREATE_ISSUE_COMMENT], { repo: zod_1.z.string().describe('仓库路径'), issueId: zod_1.z.number().describe('Issue ID'), body: zod_1.z.string().describe('评论内容') }, async ({ repo, issueId, body }) => { try { const comment = await (0, issue_js_1.createIssueComment)(client, repo, issueId, { body }); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(comment, null, 2), toolNames_js_1.ToolNames.CREATE_ISSUE_COMMENT); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.CREATE_ISSUE_COMMENT); } }); server.tool(toolNames_js_1.ToolNames.UPDATE_ISSUE_COMMENT, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.UPDATE_ISSUE_COMMENT], { repo: zod_1.z.string().describe('仓库路径'), issueId: zod_1.z.number().describe('Issue ID'), commentId: zod_1.z.string().describe('评论 ID'), body: zod_1.z.string().describe('评论内容') }, async ({ repo, issueId, commentId, body }) => { try { const comment = await (0, issue_js_1.updateIssueComment)(client, repo, issueId, commentId, { body }); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(comment, null, 2), toolNames_js_1.ToolNames.UPDATE_ISSUE_COMMENT); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.UPDATE_ISSUE_COMMENT); } }); server.tool(toolNames_js_1.ToolNames.LIST_ISSUE_LABELS, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.LIST_ISSUE_LABELS], { repo: zod_1.z.string().describe('仓库路径'), issueId: zod_1.z.number().describe('Issue ID') }, async ({ repo, issueId }) => { try { const labels = await (0, issue_js_1.listIssueLabels)(client, repo, issueId); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(labels, null, 2), toolNames_js_1.ToolNames.LIST_ISSUE_LABELS); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.LIST_ISSUE_LABELS); } }); server.tool(toolNames_js_1.ToolNames.ADD_ISSUE_LABELS, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.ADD_ISSUE_LABELS], { repo: zod_1.z.string().describe('仓库路径'), issueId: zod_1.z.number().describe('Issue ID'), labels: zod_1.z.array(zod_1.z.string()).describe('要添加的标签列表,每个标签需要从仓库标签列表中选择') }, async ({ repo, issueId, labels }) => { try { const result = await (0, issue_js_1.addIssueLabels)(client, repo, issueId, labels); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(result, null, 2), toolNames_js_1.ToolNames.ADD_ISSUE_LABELS); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.ADD_ISSUE_LABELS); } }); server.tool(toolNames_js_1.ToolNames.SET_ISSUE_LABELS, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.SET_ISSUE_LABELS], { repo: zod_1.z.string().describe('仓库路径'), issueId: zod_1.z.number().describe('Issue ID'), labels: zod_1.z.array(zod_1.z.string()).describe('新的标签列表(将替换所有现有标签),每个标签需要从仓库标签列表中选择') }, async ({ repo, issueId, labels }) => { try { const result = await (0, issue_js_1.setIssueLabels)(client, repo, issueId, labels); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(result, null, 2), toolNames_js_1.ToolNames.SET_ISSUE_LABELS); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.SET_ISSUE_LABELS); } }); server.tool(toolNames_js_1.ToolNames.CLEAR_ISSUE_LABELS, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.CLEAR_ISSUE_LABELS], { repo: zod_1.z.string().describe('仓库路径'), issueId: zod_1.z.number().describe('Issue ID') }, async ({ repo, issueId }) => { try { await (0, issue_js_1.deleteIssueLabels)(client, repo, issueId); return (0, formatToolResult_js_1.formatTextToolResult)('All labels deleted', toolNames_js_1.ToolNames.CLEAR_ISSUE_LABELS); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.CLEAR_ISSUE_LABELS); } }); server.tool(toolNames_js_1.ToolNames.REMOVE_ISSUE_LABEL, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.REMOVE_ISSUE_LABEL], { repo: zod_1.z.string().describe('仓库路径'), issueId: zod_1.z.number().describe('Issue ID'), labelName: zod_1.z.string().describe('要删除的标签名称') }, async ({ repo, issueId, labelName }) => { try { await (0, issue_js_1.deleteIssueLabel)(client, repo, issueId, labelName); return (0, formatToolResult_js_1.formatTextToolResult)(`${labelName} deleted`, toolNames_js_1.ToolNames.REMOVE_ISSUE_LABEL); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.REMOVE_ISSUE_LABEL); } }); }