From 8dc1c7b164823638b555e0f941f244574d00153b Mon Sep 17 00:00:00 2001 From: CodeBuddy Attribution Bot Date: Wed, 29 Apr 2026 07:28:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(attribution):=20MCP=20=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA=E4=B8=8D=E6=B8=85=E6=99=B0?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95=E5=AE=9A=E4=BD=8D=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E9=97=AE=E9=A2=98=20(issue=5Fmo8whjxm=5Fvaf900)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mcp/src/tools/capi.test.ts | 35 +++++++++++++++++++++++++++++++++++ mcp/src/tools/capi.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/mcp/src/tools/capi.test.ts b/mcp/src/tools/capi.test.ts index 68985d8e..7036ff8b 100644 --- a/mcp/src/tools/capi.test.ts +++ b/mcp/src/tools/capi.test.ts @@ -36,4 +36,39 @@ describe("buildCapiErrorMessage", () => { expect(message).not.toContain("可能的 tcb Action"); }); + + it("provides helpful guidance for 400 invalid parameter value errors", () => { + const message = buildCapiErrorMessage( + "tcb", + "CreateEnv", + new Error("400 invalid parameter value (abc123/def456)"), + ); + + expect(message).toContain("参数值无效或不符合 API 要求"); + expect(message).toContain("逐个检查传入的参数值"); + expect(message).toContain("参数值类型是否正确"); + expect(message).toContain("searchKnowledgeBase"); + }); + + it("shows param hints for 400 errors on known tcb actions", () => { + const message = buildCapiErrorMessage( + "tcb", + "DestroyEnv", + new Error("400 invalid parameter value"), + ); + + expect(message).toContain("参数值无效或不符合 API 要求"); + expect(message).toContain("`EnvId`"); + }); + + it("provides guidance for 400 errors on non-tcb services", () => { + const message = buildCapiErrorMessage( + "scf", + "CreateFunction", + new Error("400 invalid parameter value"), + ); + + expect(message).toContain("参数值无效或不符合 API 要求"); + expect(message).toContain("逐个检查传入的参数值"); + }); }); diff --git a/mcp/src/tools/capi.ts b/mcp/src/tools/capi.ts index c1f6fca0..08e55a0a 100644 --- a/mcp/src/tools/capi.ts +++ b/mcp/src/tools/capi.ts @@ -113,6 +113,8 @@ export function buildCapiErrorMessage(service: AllowedService, action: string, e const tcbEntry = service === "tcb" ? findTcbActionEntry(action) : undefined; const hasInvalidActionError = /invalid or not found|does not exist|not recognized/i.test(baseMessage); const hasParameterError = /parameter\s+`?.+?`?\s+is not recognized|MissingParameter|missing parameter|missing required/i.test(baseMessage); + // Detect generic "400 invalid parameter" errors that don't specify which parameter + const hasInvalidParameterValueError = /400\s+invalid\s+parameter/i.test(baseMessage); if (hasInvalidActionError) { suggestions.push( @@ -148,6 +150,33 @@ export function buildCapiErrorMessage(service: AllowedService, action: string, e } } + // Handle generic "400 invalid parameter value" errors + if (hasInvalidParameterValueError) { + suggestions.push("参数值无效或不符合 API 要求。请逐个检查传入的参数值:"); + suggestions.push("1. 确认参数值类型是否正确(字符串/数字/布尔值/数组/对象)"); + suggestions.push("2. 确认字符串参数是否符合格式要求(如时间格式、ID格式等)"); + suggestions.push("3. 确认枚举值是否在允许范围内"); + suggestions.push("4. 确认必填参数是否已提供且非空"); + if (service === "tcb" && tcbEntry) { + const paramHint = [ + tcbEntry.paramKeys.length > 0 + ? `当前 Action 的参数键:${formatTcbParamKeys(tcbEntry.paramKeys)}` + : "", + tcbEntry.requiredKeys.length > 0 + ? `必填参数:${formatTcbParamKeys(tcbEntry.requiredKeys)}` + : "", + ].filter(Boolean); + if (paramHint.length > 0) { + suggestions.push(`\`${tcbEntry.action}\` ${paramHint.join(";")}。`); + } + const paramsTypeHint = formatTcbParamsTypeHint(tcbEntry.action); + if (paramsTypeHint) { + suggestions.push(paramsTypeHint); + } + } + suggestions.push(`建议使用 searchKnowledgeBase(mode="openapi", query="${action}") 查询该 API 的详细参数说明。`); + } + if (/ECONNRESET|socket hang up|ETIMEDOUT|ENOTFOUND/i.test(baseMessage)) { suggestions.push("网络请求异常,建议稍后重试,并检查本地网络/代理设置。"); }