Skip to content

Commit 5effe2f

Browse files
author
CodeBuddy Attribution Bot
committed
fix(attribution): MCP 工具 400 参数错误提示不清晰,无法引导正确使用 (issue_mo8yphze_c40sfo)
1 parent e2ca73f commit 5effe2f

1 file changed

Lines changed: 57 additions & 6 deletions

File tree

mcp/src/tools/env.ts

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,59 @@ function normalizeOptionalToolString(value: unknown) {
709709
: undefined;
710710
}
711711

712+
/**
713+
* Build enhanced error message for envQuery tool errors
714+
* Provides actionable guidance based on error patterns
715+
*/
716+
function buildEnvQueryErrorMessage(error: unknown, action: string): string {
717+
const baseMessage = error instanceof Error ? error.message : String(error);
718+
719+
// Check for common error patterns and provide specific guidance
720+
const hasInvalidParameterError = /400|invalid parameter|invalid argument|parameter value/i.test(baseMessage);
721+
const hasAuthError = /|auth required|unauthorized|authentication|credential|token|secret/i.test(baseMessage);
722+
const hasNetworkError = /ECONNRESET|socket hang up|ETIMEDOUT|ENOTFOUND|timeout/i.test(baseMessage);
723+
const hasPermissionError = /permission|denied|forbidden||/i.test(baseMessage);
724+
const hasEnvNotFoundError = /env|environment|.*|not found/i.test(baseMessage);
725+
726+
const suggestions: string[] = [];
727+
728+
if (hasInvalidParameterError) {
729+
suggestions.push("参数错误:可能是认证信息无效或已过期,请尝试以下步骤:");
730+
suggestions.push("1. 先调用 auth(action=\"status\") 检查当前登录状态");
731+
suggestions.push("2. 如果未登录,调用 auth(action=\"start_auth\", authMode=\"device\") 完成登录");
732+
suggestions.push("3. 登录完成后再次调用 envQuery(action=\"list\")");
733+
}
734+
735+
if (hasAuthError) {
736+
suggestions.push("认证错误:当前未登录或认证已过期。");
737+
suggestions.push("建议先执行 auth(action=\"status\") 查看状态,然后按提示完成登录。");
738+
}
739+
740+
if (hasPermissionError) {
741+
suggestions.push("权限错误:当前账号可能没有访问该资源的权限。");
742+
suggestions.push("请确认:1) 已选择正确的环境 2) 账号有对应权限");
743+
}
744+
745+
if (hasEnvNotFoundError) {
746+
suggestions.push("环境错误:指定的环境不存在或无法访问。");
747+
suggestions.push("请使用 envQuery(action=\"list\") 查看可用的环境列表。");
748+
}
749+
750+
if (hasNetworkError) {
751+
suggestions.push("网络错误:请检查网络连接,稍后重试。");
752+
}
753+
754+
// If no specific pattern matched, provide general guidance
755+
if (suggestions.length === 0) {
756+
suggestions.push("查询环境信息时出错,建议:");
757+
suggestions.push("1. 先调用 auth(action=\"status\") 确认登录状态");
758+
suggestions.push("2. 如未登录,执行 auth(action=\"start_auth\") 完成认证");
759+
suggestions.push("3. 确认环境 ID 正确且可访问");
760+
}
761+
762+
return `[envQuery/${action}] 调用失败: ${baseMessage}\n\n解决建议:\n${suggestions.join("\n")}`;
763+
}
764+
712765
function normalizeOptionalToolBoolean(value: unknown) {
713766
return typeof value === "boolean" ? value : undefined;
714767
}
@@ -1335,15 +1388,12 @@ export function registerEnvTools(server: ExtendedMcpServer) {
13351388
return toolPayloadResult;
13361389
}
13371390
debug("降级到 listEnvs() 也失败:", fallbackError instanceof Error ? fallbackError : new Error(String(fallbackError)));
1391+
const enhancedMessage = buildEnvQueryErrorMessage(fallbackError, "list");
13381392
return {
13391393
content: [
13401394
{
13411395
type: "text",
1342-
text:
1343-
"获取环境列表时出错: " +
1344-
(fallbackError instanceof Error
1345-
? fallbackError.message
1346-
: String(fallbackError)),
1396+
text: enhancedMessage,
13471397
},
13481398
],
13491399
};
@@ -1464,11 +1514,12 @@ export function registerEnvTools(server: ExtendedMcpServer) {
14641514
if (toolPayloadResult) {
14651515
return toolPayloadResult;
14661516
}
1517+
const enhancedMessage = buildEnvQueryErrorMessage(error, action);
14671518
return {
14681519
content: [
14691520
{
14701521
type: "text",
1471-
text: `环境查询失败: ${error instanceof Error ? error.message : String(error)}`,
1522+
text: enhancedMessage,
14721523
},
14731524
],
14741525
};

0 commit comments

Comments
 (0)