@@ -2,6 +2,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
22import { ToolAnnotations , Tool } from "@modelcontextprotocol/sdk/types.js" ;
33import { reportToolCall } from './telemetry.js' ;
44import { 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 的类型,方便其他模块使用
1213export 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