-
Notifications
You must be signed in to change notification settings - Fork 88
refactor(McpToolCallbackAdapter): 提取文本内容逻辑并增强错误处理 #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -84,22 +84,70 @@ protected String doCall(Map<String, Object> args, @Nullable ToolContext toolCont | |||||
| result = delegate.call(argsJson); | ||||||
| } | ||||||
|
|
||||||
| if (result.startsWith("[")) { | ||||||
| JSONArray array = JSONArray.parseArray(result); | ||||||
| if (!array.isEmpty()) { | ||||||
| JSONObject data = array.getJSONObject(0); | ||||||
| if (data.containsKey("text") && data.getString("text") != null && !data.getString("text").isEmpty()) { | ||||||
| result = data.getString("text"); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| result = extractTextFromMcpResult(result); | ||||||
|
|
||||||
| logger.debug("McpToolCallbackAdapter#doCall - reason=原始ToolCallback调用完成, rawToolName={}, resultLength={}", | ||||||
| rawToolName, result != null ? result.length() : 0); | ||||||
|
|
||||||
| return result; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * 从 MCP 返回结果中提取文本内容。 | ||||||
| * | ||||||
| * <p>MCP 工具返回结果可能是以下格式之一: | ||||||
| * <ul> | ||||||
| * <li>JSON 数组,如 {@code [{"text":"..."}]}</li> | ||||||
| * <li>JSON 对象,如 {@code {"text":"..."}}</li> | ||||||
| * <li>普通文本字符串</li> | ||||||
| * </ul> | ||||||
| * 本方法会尝试按 JSON 解析,解析失败则直接返回原始文本。 | ||||||
| * | ||||||
| * @param result MCP 工具返回的原始字符串 | ||||||
| * @return 提取后的文本内容 | ||||||
| */ | ||||||
| private String extractTextFromMcpResult(String result) { | ||||||
| if (result == null || result.isEmpty()) { | ||||||
| return result; | ||||||
| } | ||||||
|
|
||||||
| String trimmed = result.trim(); | ||||||
|
|
||||||
| // 尝试按 JSON 数组解析 | ||||||
| if (trimmed.startsWith("[")) { | ||||||
| try { | ||||||
| JSONArray array = JSONArray.parseArray(trimmed); | ||||||
| if (!array.isEmpty()) { | ||||||
| JSONObject data = array.getJSONObject(0); | ||||||
| if (data.containsKey("text") && data.getString("text") != null | ||||||
| && !data.getString("text").isEmpty()) { | ||||||
| return data.getString("text"); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| catch (Exception e) { | ||||||
| logger.debug("McpToolCallbackAdapter#extractTextFromMcpResult - reason=返回结果以'['开头但非合法JSON数组,按普通文本处理, rawToolName={}", rawToolName); | ||||||
| } | ||||||
|
Comment on lines
+128
to
+130
|
||||||
| } | ||||||
|
|
||||||
| // 尝试按 JSON 对象解析 | ||||||
| if (trimmed.startsWith("{")) { | ||||||
| try { | ||||||
| JSONObject data = JSONObject.parseObject(trimmed); | ||||||
| if (data.containsKey("text") && data.getString("text") != null | ||||||
| && !data.getString("text").isEmpty()) { | ||||||
| return data.getString("text"); | ||||||
| } | ||||||
| } | ||||||
| catch (Exception e) { | ||||||
| logger.debug("McpToolCallbackAdapter#extractTextFromMcpResult - reason=返回结果以'{{'开头但非合法JSON对象,按普通文本处理, rawToolName={}", rawToolName); | ||||||
|
||||||
| logger.debug("McpToolCallbackAdapter#extractTextFromMcpResult - reason=返回结果以'{{'开头但非合法JSON对象,按普通文本处理, rawToolName={}", rawToolName); | |
| logger.debug("McpToolCallbackAdapter#extractTextFromMcpResult - reason=返回结果以'{'开头但非合法JSON对象,按普通文本处理, rawToolName={}", rawToolName); |
There was a problem hiding this comment.
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)。