fix: preserve structured SDK MCP tool results#738
Open
anthony-maio wants to merge 2 commits intoanthropics:mainfrom
Open
fix: preserve structured SDK MCP tool results#738anthony-maio wants to merge 2 commits intoanthropics:mainfrom
anthony-maio wants to merge 2 commits intoanthropics:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes SDK MCP tool result handling so structured MCP resource and resource_link content blocks round-trip through the Python SDK bridge without being flattened into text, enabling preservation of URI/MIME metadata and structured payloads (e.g., JSON).
Changes:
- Preserve
resource_linkandresourceblocks increate_sdk_mcp_server()by emitting MCPResourceLink/EmbeddedResourceobjects instead ofTextContent. - Preserve structured MCP content blocks when serializing
tools/callresults back through the JSON-RPC bridge (Query._handle_sdk_mcp_request()). - Update/extend integration tests to assert end-to-end structured resource round-tripping (including blob resources).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/test_sdk_mcp_integration.py | Adds/updates integration tests to assert structured resource / resource_link blocks are preserved through server + JSON-RPC bridge. |
| src/claude_agent_sdk/_internal/query.py | Updates JSON-RPC bridge serialization to emit structured MCP content via model_dump() for resources/links (and audio). |
| src/claude_agent_sdk/init.py | Updates SDK MCP server tool result conversion to construct structured ResourceLink and EmbeddedResource (text/blob) blocks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
resourceandresource_linkMCP content blocks increate_sdk_mcp_server()instead of flattening them into texttools/callresults back to the CLIProblem
SDK MCP tool handlers can return structured resource blocks, but the Python SDK currently flattens them to plain text in two places:
create_sdk_mcp_server()convertsresourceandresource_linkoutputs intoTextContent_handle_sdk_mcp_request()converts those MCP content objects back into text againThat strips MIME type, URI, and resource structure, which prevents JSON payloads like
application/jsonfrom surviving the SDK bridge.Test Plan
python -m pytest tests/test_sdk_mcp_integration.pypython -m ruff check src/claude_agent_sdk/__init__.py src/claude_agent_sdk/_internal/query.py tests/test_sdk_mcp_integration.pypython -m mypy src/Fixes #695