Skip to content

refactor(McpToolCallbackAdapter): 提取文本内容逻辑并增强错误处理#34

Merged
canfuu merged 2 commits intomainfrom
feature/update_mcp_support_normal_text
Mar 4, 2026
Merged

refactor(McpToolCallbackAdapter): 提取文本内容逻辑并增强错误处理#34
canfuu merged 2 commits intomainfrom
feature/update_mcp_support_normal_text

Conversation

@canfuu
Copy link
Collaborator

@canfuu canfuu commented Mar 4, 2026

将原有的 JSON 结果解析逻辑抽取为独立方法 extractTextFromMcpResult(),支持多种返回格式(JSON 数组、JSON 对象、纯文本),并对解析异常进行了更好的处理。

将原有的 JSON 结果解析逻辑抽取为独立方法 `extractTextFromMcpResult()`,支持多种返回格式(JSON 数组、JSON 对象、纯文本),并对解析异常进行了更好的处理。

Co-developed-by: Aone Copilot <noreply@alibaba-inc.com>
Copilot AI review requested due to automatic review settings March 4, 2026 03:33
@canfuu canfuu enabled auto-merge March 4, 2026 03:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 重构 McpToolCallbackAdapter 的 MCP 返回结果解析逻辑:将“从 MCP 结果中提取 text”抽取为独立方法,并扩展为支持 JSON 数组、JSON 对象及纯文本三类返回格式,同时在解析异常时进行更友好的降级处理。

Changes:

  • 提取 extractTextFromMcpResult(),统一处理 MCP 返回结果的文本提取逻辑
  • 扩展解析能力:支持 [{ "text": "..." }]{ "text": "..." } 与纯文本
  • 解析失败时以 debug 日志记录并回退到原始文本返回

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +121 to +124
JSONObject data = array.getJSONObject(0);
if (data.containsKey("text") && data.getString("text") != null
&& !data.getString("text").isEmpty()) {
return data.getString("text");
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里假设 JSONArray 的第 0 个元素一定是 JSONObject:当返回为如 ["ok"][1] 或混合数组时,array.getJSONObject(0) 会返回 null,随后 data.containsKey(...) 会触发 NullPointerException,导致 doCall 失败。建议先判断元素类型/非空(例如先取 Object first = array.get(0) 并仅在 first instanceof JSONObject 时读取 text)。

Suggested change
JSONObject data = array.getJSONObject(0);
if (data.containsKey("text") && data.getString("text") != null
&& !data.getString("text").isEmpty()) {
return data.getString("text");
Object first = array.get(0);
if (first instanceof JSONObject) {
JSONObject data = (JSONObject) first;
if (data.containsKey("text") && data.getString("text") != null
&& !data.getString("text").isEmpty()) {
return data.getString("text");
}

Copilot uses AI. Check for mistakes.
Comment on lines +128 to +130
catch (Exception e) {
logger.debug("McpToolCallbackAdapter#extractTextFromMcpResult - reason=返回结果以'['开头但非合法JSON数组,按普通文本处理, rawToolName={}", rawToolName);
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

两个 catch 分支都捕获了解析异常但日志里没有打印异常信息(未把 e 作为参数传给 logger),联调/线上排查会缺少关键上下文。建议在 debug 日志中包含异常(例如作为最后一个参数传入),或至少记录 e.getMessage()

Copilot uses AI. Check for mistakes.
}
}
catch (Exception e) {
logger.debug("McpToolCallbackAdapter#extractTextFromMcpResult - reason=返回结果以'{{'开头但非合法JSON对象,按普通文本处理, rawToolName={}", rawToolName);
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

日志文案里写的是“以'{{'开头”,但这里实际判断的是 trimmed.startsWith("{")。建议把日志中的 {{ 更正为 {,避免误导排查。

Suggested change
logger.debug("McpToolCallbackAdapter#extractTextFromMcpResult - reason=返回结果以'{{'开头但非合法JSON对象,按普通文本处理, rawToolName={}", rawToolName);
logger.debug("McpToolCallbackAdapter#extractTextFromMcpResult - reason=返回结果以'{'开头但非合法JSON对象,按普通文本处理, rawToolName={}", rawToolName);

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

@AQing-527 AQing-527 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@canfuu canfuu merged commit 5a2c789 into main Mar 4, 2026
1 check passed
@canfuu canfuu deleted the feature/update_mcp_support_normal_text branch March 4, 2026 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants