Skip to content

fix(mcp): preserve empty structured content in tool output#3575

Open
mshsheikh wants to merge 1 commit into
openai:mainfrom
mshsheikh:patch-44
Open

fix(mcp): preserve empty structured content in tool output#3575
mshsheikh wants to merge 1 commit into
openai:mainfrom
mshsheikh:patch-44

Conversation

@mshsheikh
Copy link
Copy Markdown
Contributor

When server.use_structured_content is enabled, invoke_mcp_tool() should treat structured content as available whenever it is not None, even if it is an empty list or object. The current truthiness check drops valid empty JSON results and falls back to the content-based path instead. That conflicts with the code comment and documented behavior that structured content should be used exclusively when it is requested and available.

This change updates the condition from a truthiness check to an explicit is not None check so that empty structured results such as [] and {} are still serialized and returned correctly. This aligns the runtime behavior with the intended MCP output handling and addresses the empty-tool-output bug reported in #1035.

Testing:

  • Add a regression test for structuredContent=[] with use_structured_content=True
  • Add a regression test for structuredContent={} with use_structured_content=True
  • Verify the method returns JSON text for both cases rather than falling back to result.content

When `server.use_structured_content` is enabled, `invoke_mcp_tool()` should treat structured content as available whenever it is not `None`, even if it is an empty list or object. The current truthiness check drops valid empty JSON results and falls back to the content-based path instead. That conflicts with the code comment and documented behavior that structured content should be used exclusively when it is requested and available. 

This change updates the condition from a truthiness check to an explicit `is not None` check so that empty structured results such as `[]` and `{}` are still serialized and returned correctly. This aligns the runtime behavior with the intended MCP output handling and addresses the empty-tool-output bug reported in openai#1035. 

Testing:
- Add a regression test for `structuredContent=[]` with `use_structured_content=True`
- Add a regression test for `structuredContent={}` with `use_structured_content=True`
- Verify the method returns JSON text for both cases rather than falling back to `result.content`
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6c382b8eb1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/mcp/util.py
# If structured content is requested and available, use it exclusively
tool_output: ToolOutput
if server.use_structured_content and result.structuredContent:
if server.use_structured_content and result.structuredContent is not None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Update tests for empty structured content

When this branch now serializes empty structured content such as {} instead of falling back to result.content, the committed tests still assert the old fallback behavior (for example tests/mcp/test_mcp_util.py::test_structured_content_handling scenario 4 and test_empty_structured_content_fallback). That leaves the MCP test suite failing for the exact case this change intends to fix, so the expected outputs/regression tests need to be updated in the same change.

Useful? React with 👍 / 👎.

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.

1 participant