Repository navigation
Conversation
2786e36 to
14d5157
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR fixes the handling of MCP (Model Context Protocol) tool call results by properly utilizing structured content and correcting the success flag logic. The changes ensure that when an MCP server returns a CallToolResult, the system prefers the higher-fidelity structuredContent over the basic content field, and fixes an inverted success logic bug.
- Extracts tool result conversion logic into a dedicated function for better maintainability
- Implements preference for
structuredContentovercontentwhen available and valid - Fixes inverted success logic where
is_errorwas incorrectly used as success indicator
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| // If we could not serialize either content or structured_content to | ||
| // JSON, flag this as an error. | ||
| is_success = false; | ||
| "".to_owned() |
There was a problem hiding this comment.
[nitpick] The variable is_success is declared as mutable but only assigned once. Consider removing the mut keyword or using a more explicit boolean expression like !matches!(is_error, Some(true)).
| "".to_owned() | |
| let (content, is_success) = if let Some(structured_content) = structured_content | |
| && structured_content != &serde_json::Value::Null | |
| && let Ok(serialized_structured_content) = serde_json::to_string(&structured_content) | |
| { | |
| (serialized_structured_content, is_error != &Some(true)) | |
| } else if let Ok(serialized_content) = serde_json::to_string(&content) { | |
| (serialized_content, is_error != &Some(true)) | |
| } else { | |
| // If we could not serialize either content or structured_content to | |
| // JSON, flag this as an error. | |
| ("".to_owned(), false) |
| // If we could not serialize either content or structured_content to | ||
| // JSON, flag this as an error. | ||
| is_success = false; | ||
| "".to_owned() |
There was a problem hiding this comment.
Returning an empty string when serialization fails may not provide helpful debugging information. Consider including error details in the content or using a more descriptive error message like "Failed to serialize tool result".
| "".to_owned() | |
| { | |
| match serde_json::to_string(&structured_content) { | |
| Ok(serialized_structured_content) => serialized_structured_content, | |
| Err(e) => { | |
| is_success = false; | |
| format!("Failed to serialize structured_content: {}", e) | |
| } | |
| } | |
| } else { | |
| match serde_json::to_string(&content) { | |
| Ok(serialized_content) => serialized_content, | |
| Err(e) => { | |
| is_success = false; | |
| format!("Failed to serialize content: {}", e) | |
| } | |
| } |
|
thanks for adding the tests! |
You can thank Codex :P |
Prior to this change, when we got a
CallToolResultfrom an MCP server, we JSON-serialized itscontentfield as thecontentto send back to the model as part of the function call output that we send back to the model. This meant that we were dropping thestructuredContenton the floor.Though reading https://modelcontextprotocol.io/specification/2025-06-18/schema#tool, it appears that if
outputSchemais specified, thenstructuredContentshould be set, which seems to be a "higher-fidelity" response to the function call. This PR updates our handling ofCallToolResultto prefer using the JSON-serialization ofstructuredContent, if present, usingcontentas a fallback.Also, it appears that the sense of
successwas inverted prior to this PR!