Skip to content

Commit 4e11631

Browse files
claude[bot]binggg
andauthored
feat: 为 MCP 工具错误添加 GitHub Issue 快速创建链接
- 新增 generateGitHubIssueLink 函数,生成包含详细错误信息的 GitHub Issue 创建链接 - 修改 createWrappedHandler 函数,在错误发生时自动在错误消息中包含 GitHub Issue 链接 - 链接包含工具名称、错误信息、环境信息、工具参数等详细信息,方便用户快速创建 Issue - 使用现有的 sanitizeArgs 函数保护敏感参数信息 解决 #35 Co-authored-by: Booker Zhao <binggg@users.noreply.github.com>
1 parent d0bc951 commit 4e11631

1 file changed

Lines changed: 72 additions & 2 deletions

File tree

mcp/src/utils/tool-wrapper.ts

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
22
import { ToolAnnotations, Tool } from "@modelcontextprotocol/sdk/types.js";
33
import { reportToolCall } from './telemetry.js';
44
import { debug } from './logger.js';
5+
import os from 'os';
56

67
/**
78
* 工具包装器,为 MCP 工具添加数据上报功能
@@ -11,6 +12,58 @@ import { debug } from './logger.js';
1112
// 重新导出 MCP SDK 的类型,方便其他模块使用
1213
export type { ToolAnnotations, Tool } from "@modelcontextprotocol/sdk/types.js";
1314

15+
/**
16+
* 生成 GitHub Issue 创建链接
17+
* @param toolName 工具名称
18+
* @param errorMessage 错误消息
19+
* @param args 工具参数
20+
* @returns GitHub Issue 创建链接
21+
*/
22+
function generateGitHubIssueLink(toolName: string, errorMessage: string, args: any): string {
23+
const baseUrl = 'https://github.com/TencentCloudBase/CloudBase-AI-ToolKit/issues/new';
24+
25+
// 构建标题
26+
const title = `MCP工具错误: ${toolName}`;
27+
28+
// 构建问题描述
29+
const body = `## 错误描述
30+
工具 \`${toolName}\` 执行时发生错误
31+
32+
## 错误信息
33+
\`\`\`
34+
${errorMessage}
35+
\`\`\`
36+
37+
## 环境信息
38+
- 操作系统: ${os.type()} ${os.release()}
39+
- Node.js版本: ${process.version}
40+
- 系统架构: ${os.arch()}
41+
- 时间: ${new Date().toISOString()}
42+
43+
## 工具参数
44+
\`\`\`json
45+
${JSON.stringify(sanitizeArgs(args), null, 2)}
46+
\`\`\`
47+
48+
## 复现步骤
49+
1. 使用工具: ${toolName}
50+
2. 传入参数: [请根据上述参数信息填写]
51+
3. 出现错误
52+
53+
## 期望行为
54+
[请描述您期望的正确行为]
55+
56+
## 其他信息
57+
[如有其他相关信息,请在此补充]
58+
`;
59+
60+
// URL 编码
61+
const encodedTitle = encodeURIComponent(title);
62+
const encodedBody = encodeURIComponent(body);
63+
64+
return `${baseUrl}?title=${encodedTitle}&body=${encodedBody}`;
65+
}
66+
1467
/**
1568
* 创建包装后的处理函数,添加数据上报功能
1669
*/
@@ -39,8 +92,25 @@ function createWrappedHandler(name: string, handler: any) {
3992
duration: Date.now() - startTime
4093
});
4194

42-
// 重新抛出错误,保持原有行为
43-
throw error;
95+
// 生成 GitHub Issue 创建链接
96+
const issueLink = generateGitHubIssueLink(name, errorMessage, args);
97+
98+
// 创建增强的错误消息,包含 GitHub Issue 链接
99+
const enhancedErrorMessage = `${errorMessage}\n\n🔗 遇到问题?请点击以下链接快速创建 GitHub Issue:\n${issueLink}`;
100+
101+
// 创建新的错误对象,保持原有的错误类型但更新消息
102+
const enhancedError = error instanceof Error
103+
? new Error(enhancedErrorMessage)
104+
: new Error(enhancedErrorMessage);
105+
106+
// 保持原有的错误属性
107+
if (error instanceof Error) {
108+
enhancedError.stack = error.stack;
109+
enhancedError.name = error.name;
110+
}
111+
112+
// 重新抛出增强的错误
113+
throw enhancedError;
44114
} finally {
45115
// 上报工具调用数据
46116
const duration = Date.now() - startTime;

0 commit comments

Comments
 (0)