Skip to content

fix: prefer sending MCP structuredContent as the function call response, if available - #2594

Merged
bolinfest merged 1 commit into
mainfrom
pr2594
Aug 22, 2025
Merged

bolinfest merged 1 commit into
mainfrom
pr2594

Conversation

@bolinfest

@bolinfest bolinfest commented Aug 22, 2025 •

Copy link
Copy Markdown
Collaborator

Prior to this change, when we got a CallToolResult from an MCP server, we JSON-serialized its content field as the content to 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 the structuredContent on the floor.

Though reading https://modelcontextprotocol.io/specification/2025-06-18/schema#tool, it appears that if outputSchema is specified, then structuredContent should be set, which seems to be a "higher-fidelity" response to the function call. This PR updates our handling of CallToolResult to prefer using the JSON-serialization of structuredContent, if present, using content as a fallback.

Also, it appears that the sense of success was inverted prior to this PR!

@bolinfest
bolinfest force-pushed the pr2594 branch 2 times, most recently from 2786e36 to 14d5157 Compare August 22, 2025 20:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 structuredContent over content when available and valid
  • Fixes inverted success logic where is_error was incorrectly used as success indicator

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread codex-rs/core/src/codex.rs Outdated
// If we could not serialize either content or structured_content to
// JSON, flag this as an error.
is_success = false;
"".to_owned()

Copilot AI Aug 22, 2025

Copy link

Choose a reason for hiding this comment

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

[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)).

Suggested change
"".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)

Copilot uses AI. Check for mistakes.
Comment thread codex-rs/core/src/codex.rs Outdated
// If we could not serialize either content or structured_content to
// JSON, flag this as an error.
is_success = false;
"".to_owned()

Copilot AI Aug 22, 2025

Copy link

Choose a reason for hiding this comment

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

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".

Suggested change
"".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)
}
}

Copilot uses AI. Check for mistakes.
@dylan-hurd-oai

Copy link
Copy Markdown
Contributor

thanks for adding the tests!

@bolinfest

Copy link
Copy Markdown
Collaborator Author

thanks for adding the tests!

You can thank Codex :P

@bolinfest
bolinfest merged commit 8ba8089 into main Aug 22, 2025
@bolinfest
bolinfest deleted the pr2594 branch August 22, 2025 21:10
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants