-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathindex.ts
More file actions
284 lines (247 loc) · 11 KB
/
index.ts
File metadata and controls
284 lines (247 loc) · 11 KB
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
276
277
278
279
280
281
282
283
284
#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import * as branches from './operations/codeup/branches.js';
import * as files from './operations/codeup/files.js';
import * as repositories from './operations/codeup/repositories.js';
import * as changeRequests from './operations/codeup/changeRequests.js';
import * as changeRequestComments from './operations/codeup/changeRequestComments.js';
import * as organization from './operations/organization/organization.js';
import * as members from './operations/organization/members.js';
import * as project from './operations/projex/project.js';
import * as workitem from './operations/projex/workitem.js';
import * as sprint from './operations/projex/sprint.js';
import * as compare from './operations/codeup/compare.js'
import * as pipeline from './operations/flow/pipeline.js'
import * as pipelineJob from './operations/flow/pipelineJob.js'
import * as serviceConnection from './operations/flow/serviceConnection.js'
import * as packageRepositories from './operations/packages/repositories.js'
import * as artifacts from './operations/packages/artifacts.js'
import {
isYunxiaoError,
YunxiaoError,
YunxiaoValidationError
} from "./common/errors.js";
import { VERSION } from "./common/version.js";
import {config} from "dotenv";
import * as types from "./common/types.js";
import { getAllTools, getEnabledTools } from "./tool-registry/index.js";
import { handleToolRequest, handleEnabledToolRequest } from "./tool-handlers/index.js";
import { Toolset } from "./common/toolsets.js";
const server = new Server(
{
name: "alibabacloud-devops-mcp-server",
version: VERSION,
},
{
capabilities: {
tools: {},
},
}
);
function formatYunxiaoError(error: YunxiaoError): string {
let message = `Yunxiao API Error: ${error.message}`;
// 添加请求上下文信息
if (error.method || error.url) {
message += `\n Request: ${error.method || 'GET'} ${error.url || 'unknown'}`;
}
if (error.requestHeaders) {
message += `\n Request Headers: ${JSON.stringify(error.requestHeaders, null, 2)}`;
}
if (error.requestBody) {
message += `\n Request Body: ${JSON.stringify(error.requestBody, null, 2)}`;
}
if (error instanceof YunxiaoValidationError) {
message = `Parameter validation failed: ${error.message}`;
if (error.response) {
message += `\n Response: ${JSON.stringify(error.response, null, 2)}`;
}
// 添加常见参数错误的提示
if (error.message.includes('name')) {
message += `\n Suggestion: Please check whether the pipeline name meets the requirements.`;
}
if (error.message.includes('content') || error.message.includes('yaml')) {
message += `\n Suggestion: Please check whether the generated YAML format is correct.`;
}
} else {
// 处理通用的Yunxiao错误
message = `Yunxiao API error (${error.status}): ${error.message}`;
if (error.response) {
const response = error.response as any;
if (response.errorCode) {
message += `\n errorCode: ${response.errorCode}`;
}
if (response.errorMessage && response.errorMessage !== error.message) {
message += `\n errorMessage: ${response.errorMessage}`;
}
if (response.data && typeof response.data === 'object') {
message += `\n data: ${JSON.stringify(response.data, null, 2)}`;
}
// 如果响应体中有更多详细信息,也一并显示
if (Object.keys(response).length > 0) {
message += `\n Full Response: ${JSON.stringify(response, null, 2)}`;
}
}
// 根据状态码提供通用建议
switch (error.status) {
case 400:
message += `\n Suggestion: Please check whether the request parameters are correct, especially whether all required parameters have been provided.`;
break;
case 500:
message += `\n Suggestion: Internal server error. Please try again later or contact technical support.`;
break;
case 502:
case 503:
case 504:
message += `\n Suggestion: The service is temporarily unavailable. Please try again later.`;
break;
}
}
return message;
}
server.setRequestHandler(ListToolsRequestSchema, async () => {
let tools: any[];
if (enabledToolsets.length > 0) {
// 获取基础工具(总是加载)
const baseTools = getEnabledTools([Toolset.BASE]);
// 获取启用的工具集工具
const enabledTools = getEnabledTools(enabledToolsets);
// 合并基础工具和启用的工具集工具
tools = [...baseTools, ...enabledTools];
} else {
// 如果没有指定启用的工具集,则获取所有工具(已包含基础工具)
tools = getAllTools();
}
return {
tools,
};
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
if (!request.params.arguments) {
throw new Error("Arguments are required");
}
// Delegate to our modular tool handler with toolset support
const result = enabledToolsets.length > 0
? await handleEnabledToolRequest(request, enabledToolsets)
: await handleToolRequest(request);
return result;
} catch (error) {
if (error instanceof z.ZodError) {
throw new Error(`Invalid input: ${JSON.stringify(error.errors)}`);
}
if (isYunxiaoError(error)) {
throw new Error(formatYunxiaoError(error));
}
throw error;
}
});
config({ quiet: true });
// 解析启用的工具集
const parseEnabledToolsets = (input: string | undefined): Toolset[] => {
if (!input) return [];
return input.split(',').map(toolset => {
const trimmed = toolset.trim() as Toolset;
// 验证工具集名称是否有效
if (!Object.values(Toolset).includes(trimmed)) {
throw new Error(`Unknown toolset: ${trimmed}`);
}
return trimmed;
});
};
// 获取启用的工具集(从命令行参数或环境变量)
const enabledToolsets = parseEnabledToolsets(
process.argv.find(arg => arg.startsWith('--toolsets='))?.split('=')[1] ||
process.env.DEVOPS_TOOLSETS
);
// Check if we should run in SSE mode
const useSSE = process.argv.includes('--sse') || process.env.MCP_TRANSPORT === 'sse';
async function runServer() {
if (useSSE) {
// Import express only when needed for SSE mode
const { default: express } = await import('express');
const app: any = express();
const port = process.env.PORT || 3000;
// Store sessions with their tokens
const sessions: Record<string, { transport: SSEServerTransport; server: Server; yunxiao_access_token?: string }> = {};
// SSE endpoint - handles initial connection
app.get('/sse', async (req: any, res: any) => {
// In SSE mode, we can use console.log for debugging since it doesn't interfere with the protocol
console.log(`New SSE connection from ${req.ip}`);
// Get token from query parameters or headers
const yunxiao_access_token = req.query.yunxiao_access_token || req.headers['x-yunxiao-token'] || process.env.YUNXIAO_ACCESS_TOKEN;
// Create transport with endpoint for POST messages
const sseTransport = new SSEServerTransport('/messages', res);
const sessionId = sseTransport.sessionId;
if (sessionId) {
sessions[sessionId] = { transport: sseTransport, server, yunxiao_access_token };
}
try {
await server.connect(sseTransport);
// In SSE mode, console.error is acceptable for status messages
console.info(`Yunxiao MCP Server connected via SSE with session ${sessionId}`);
if (yunxiao_access_token) {
console.error(`Session ${sessionId} using custom token`);
} else {
console.error(`Session ${sessionId} using default token from environment`);
}
} catch (error) {
console.error("Failed to start SSE server:", error);
res.status(500).send("Server error");
}
});
// POST endpoint - handles incoming messages
app.use(express.json({ limit: '10mb' })); // Add JSON body parser
app.post('/messages', async (req: any, res: any) => {
const sessionId = req.query.sessionId as string;
const session = sessions[sessionId];
if (!session) {
res.status(404).send("Session not found");
return;
}
try {
// Set the session token before handling the message
const utils = await import('./common/utils.js');
utils.setCurrentSessionToken(session.yunxiao_access_token);
await session.transport.handlePostMessage(req, res, req.body);
} catch (error) {
console.error("Error handling POST message:", error);
res.status(500).send("Server error");
}
});
// Start server
const serverInstance: any = app.listen(port, () => {
console.log(`Yunxiao MCP Server running in SSE mode on port ${port}`);
console.log(`Connect via SSE at http://localhost:${port}/sse`);
console.log(`Send messages to http://localhost:${port}/messages?sessionId=<session-id>`);
});
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('Shutting down SSE server...');
serverInstance.close(() => {
console.log('Server closed.');
process.exit(0);
});
});
} else {
// Stdio mode (default)
// In stdio mode, we must avoid console.log/console.error as they interfere with the JSON-RPC protocol
const transport = new StdioServerTransport();
await server.connect(transport);
// Don't output anything to stdout/stderr in stdio mode - only JSON-RPC messages should go through the transport
}
}
runServer().catch((error) => {
// Only output error to stderr in SSE mode, not in stdio mode
if (useSSE) {
console.error("Fatal error in main():", error);
}
process.exit(1);
});