Skip to content

fix: preserve structured SDK MCP tool results#738

Open
anthony-maio wants to merge 2 commits intoanthropics:mainfrom
anthony-maio:wt/issue-695-structured-mcp-results
Open

fix: preserve structured SDK MCP tool results#738
anthony-maio wants to merge 2 commits intoanthropics:mainfrom
anthony-maio:wt/issue-695-structured-mcp-results

Conversation

@anthony-maio
Copy link
Copy Markdown

Summary

  • preserve structured resource and resource_link MCP content blocks in create_sdk_mcp_server() instead of flattening them into text
  • preserve those structured blocks again when the SDK MCP JSON-RPC bridge serializes tools/call results back to the CLI
  • update the SDK MCP integration tests to assert structured resources round-trip end-to-end

Problem

SDK MCP tool handlers can return structured resource blocks, but the Python SDK currently flattens them to plain text in two places:

  1. create_sdk_mcp_server() converts resource and resource_link outputs into TextContent
  2. _handle_sdk_mcp_request() converts those MCP content objects back into text again

That strips MIME type, URI, and resource structure, which prevents JSON payloads like application/json from surviving the SDK bridge.

Test Plan

  • python -m pytest tests/test_sdk_mcp_integration.py
  • python -m ruff check src/claude_agent_sdk/__init__.py src/claude_agent_sdk/_internal/query.py tests/test_sdk_mcp_integration.py
  • python -m mypy src/

Fixes #695

Copilot AI review requested due to automatic review settings March 25, 2026 23:19
Copy link
Copy Markdown

Copilot AI left a comment

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 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_link and resource blocks in create_sdk_mcp_server() by emitting MCP ResourceLink / EmbeddedResource objects instead of TextContent.
  • Preserve structured MCP content blocks when serializing tools/call results 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.

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.

Allow SDK MCP Servers to return structured data

2 participants