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

159 lines (158 loc) 10.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = registerBuildTools; const zod_1 = require("zod"); const toolNames_js_1 = require("../constants/toolNames.js"); const toolDescriptions_js_1 = require("../constants/toolDescriptions.js"); const formatToolResult_js_1 = require("../helpers/formatToolResult.js"); const index_js_1 = require("../helpers/request/index.js"); const build_logs_delete_js_1 = require("../api/build/build-logs-delete.js"); const build_runner_download_log_js_1 = require("../api/build/build-runner-download-log.js"); const get_build_logs_js_1 = require("../api/build/get-build-logs.js"); const get_build_stage_js_1 = require("../api/build/get-build-stage.js"); const get_build_status_js_1 = require("../api/build/get-build-status.js"); const start_build_js_1 = require("../api/build/start-build.js"); const stop_build_js_1 = require("../api/build/stop-build.js"); function registerBuildTools(server, token) { const headers = (0, index_js_1.createHeaders)(token); server.tool(toolNames_js_1.ToolNames.BUILD_LOGS_DELETE, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.BUILD_LOGS_DELETE], { repo: zod_1.z.string().describe('仓库路径'), sn: zod_1.z.string().describe('构建 ID') }, async ({ repo, sn }) => { try { const { error, result } = await (0, build_logs_delete_js_1.buildLogsDelete)({ repo, sn }, { headers }); if (error) throw new Error(`ERROR ${error.code}: ${error.message}`); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(result, null, 2), toolNames_js_1.ToolNames.BUILD_LOGS_DELETE); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.BUILD_LOGS_DELETE); } }); server.tool(toolNames_js_1.ToolNames.BUILD_RUNNER_DOWNLOAD_LOG, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.BUILD_RUNNER_DOWNLOAD_LOG], { repo: zod_1.z.string().describe('仓库路径'), pipelineId: zod_1.z.string().describe('流水线 ID') }, async ({ repo, pipelineId }) => { try { const { error, result } = await (0, build_runner_download_log_js_1.buildRunnerDownloadLog)({ repo, pipelineId }, { headers }); if (error) throw new Error(`ERROR ${error.code}: ${error.message}`); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(result, null, 2), toolNames_js_1.ToolNames.BUILD_RUNNER_DOWNLOAD_LOG); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.BUILD_RUNNER_DOWNLOAD_LOG); } }); server.tool(toolNames_js_1.ToolNames.GET_BUILD_LOGS, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.GET_BUILD_LOGS], { repo: zod_1.z.string().describe('仓库路径'), createTime: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('构建开始时间,格式为 YYYY-MM-DD'), endTime: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()) .describe('构建结束时间,格式为 YYYY-MM-DD'), event: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('触发事件'), page: zod_1.z.number().default(1).describe('页码'), pagesize: zod_1.z.number().max(100).default(30).describe('每页数据的条数'), sha: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('Commit ID'), sn: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('构建 ID'), sourceRef: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('源分支名'), status: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('构建状态'), targetRef: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('目标分支名'), userId: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('用户 ID'), userName: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('用户名') }, async (params) => { try { console.error(params, headers); const { error, result } = await (0, get_build_logs_js_1.getBuildLogs)(params, { headers }); if (error) throw new Error(`ERROR ${error.code}: ${error.message}`); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(result, null, 2), toolNames_js_1.ToolNames.GET_BUILD_LOGS); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.GET_BUILD_LOGS); } }); server.tool(toolNames_js_1.ToolNames.GET_BUILD_STAGE, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.GET_BUILD_STAGE], { repo: zod_1.z.string().describe('仓库路径'), sn: zod_1.z.string().describe('构建序列号或 ID'), pipelineId: zod_1.z .string() .describe('流水线 ID,一个构建可以包含多条流水线。格式 {sn}-001, {sn}-002 等等,001 为起始,加一递增'), stageId: zod_1.z .string() .describe('流水线阶段 ID,一条流水线可以包含多个阶段。格式 prepare, beforeEnd, end, stage-0, stage-1 等等,0 为起始,加一递增') }, async (params) => { const { sn, pipelineId, stageId } = params; // 流水线 ID 格式检查 const pipelineIdRegexp = new RegExp(`^${sn}-\\d{3}$`); if (!pipelineIdRegexp.test(pipelineId)) { return (0, formatToolResult_js_1.formatToolError)('流水线 ID 格式错误。正确格式:<构建 ID>-<三位序号>,001 为起始,加一递增', toolNames_js_1.ToolNames.GET_BUILD_STAGE); } // 流水线阶段 ID 格式检查 const fixedStageId = ['prepare', 'beforeEnd', 'end']; const stageIdRegexp = /^stage-\d$/; if (!fixedStageId.includes(stageId) && !stageIdRegexp.test(stageId)) { return (0, formatToolResult_js_1.formatToolError)('流水线阶段 ID 格式错误。正确格式:prepare、beforeEnd、end、stage-<序号>,0 为起始,加一递增', toolNames_js_1.ToolNames.GET_BUILD_STAGE); } try { const { error, result } = await (0, get_build_stage_js_1.getBuildStage)(params, { headers }); if (error) throw new Error(`ERROR ${error.code}: ${error.message}`); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(result, null, 2), toolNames_js_1.ToolNames.GET_BUILD_STAGE); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.GET_BUILD_STAGE); } }); server.tool(toolNames_js_1.ToolNames.GET_BUILD_STATUS, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.GET_BUILD_STATUS], { repo: zod_1.z.string().describe('仓库路径'), sn: zod_1.z.string().describe('构建 ID') }, async ({ repo, sn }) => { try { const { error, result } = await (0, get_build_status_js_1.getBuildStatus)({ repo, sn }, { headers }); if (error) throw new Error(`ERROR ${error.code}: ${error.message}`); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(result, null, 2), toolNames_js_1.ToolNames.GET_BUILD_STATUS); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.GET_BUILD_STATUS); } }); server.tool(toolNames_js_1.ToolNames.START_BUILD, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.START_BUILD], { repo: zod_1.z.string().describe('仓库路径'), branch: zod_1.z.string().describe('分支名'), event: zod_1.z.string().describe('触发事件'), sync: zod_1.z.string().default('false').describe('同步模式,等待请求完成'), config: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('配置'), env: zod_1.z .preprocess((val) => (val === null ? undefined : val), zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional()) .describe('环境变量'), sha: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('Commit ID'), tag: zod_1.z.preprocess((val) => (val === null ? undefined : val), zod_1.z.string().optional()).describe('git tag') }, async ({ repo, ...body }) => { try { const { error, result } = await (0, start_build_js_1.startBuild)(repo, body, { headers }); if (error) throw new Error(`ERROR ${error.code}: ${error.message}`); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(result, null, 2), toolNames_js_1.ToolNames.START_BUILD); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.START_BUILD); } }); server.tool(toolNames_js_1.ToolNames.STOP_BUILD, toolDescriptions_js_1.toolDescriptions[toolNames_js_1.ToolNames.STOP_BUILD], { repo: zod_1.z.string().describe('仓库路径'), sn: zod_1.z.string().describe('构建 ID') }, async ({ repo, sn }) => { try { const { error, result } = await (0, stop_build_js_1.stopBuild)({ repo, sn }, { headers }); if (error) throw new Error(`ERROR ${error.code}: ${error.message}`); return (0, formatToolResult_js_1.formatTextToolResult)(JSON.stringify(result, null, 2), toolNames_js_1.ToolNames.STOP_BUILD); } catch (error) { return (0, formatToolResult_js_1.formatToolError)(error, toolNames_js_1.ToolNames.STOP_BUILD); } }); }