Skip to content

Conversation

@oskar-korczak
Copy link

@oskar-korczak oskar-korczak commented Jan 13, 2026

Summary

Add support for document content (e.g., PDFs) in MCP tool results.

Problem

Currently, when a tool returns a document block in its result content, it is silently dropped because the SDK only handles text and image content types. This causes Claude to hallucinate content instead of reading the actual document.

Solution

Use MCP's EmbeddedResource with BlobResourceContents to transport document data through the MCP layer:

1. __init__.py - Convert document → EmbeddedResource:

elif item.get("type") == "document":
    source = item.get("source", {})
    content.append(
        EmbeddedResource(
            type="resource",
            resource=BlobResourceContents(
                uri=f"document://{source.get('type', 'base64')}",
                mimeType=source.get("media_type", "application/pdf"),
                blob=source.get("data", ""),
            ),
        )
    )

2. query.py - Convert EmbeddedResource → Anthropic document format:

elif hasattr(item, "resource") and getattr(item, "type", None) == "resource":
    resource = item.resource
    uri = getattr(resource, "uri", "")
    if uri.startswith("document://") or mime_type == "application/pdf":
        content.append({
            "type": "document",
            "source": {
                "type": source_type,
                "media_type": mime_type,
                "data": getattr(resource, "blob", ""),
            },
        })

This enables native PDF support in tool results as documented in the Anthropic API:
https://platform.claude.com/docs/en/build-with-claude/pdf-support

Real-world use case

We encountered this issue in production where a tool fetches PDF attachments from an email system. The tool returned document blocks, but they were silently dropped, causing Claude to hallucinate the PDF content. Our workaround was to render PDFs as images, which works but is suboptimal.

Test Plan

  • Add test for document content support (test_document_content_support)
  • Verify backward compatibility - all existing tests pass (7/7)
  • Test document blocks are correctly converted to EmbeddedResource
  • Test EmbeddedResource is correctly converted to Anthropic document format

🤖 Generated with Claude Code

Add support for document content (e.g., PDFs) in MCP tool results by:

1. In __init__.py: Convert document blocks to EmbeddedResource with
   BlobResourceContents, preserving the document data through MCP's
   existing content type system.

2. In query.py: Recognize EmbeddedResource with document:// URI or
   application/pdf mimeType and convert back to Anthropic document
   format for the API.

This enables tools to return PDF documents that Claude can read natively,
using the Anthropic API's PDF support:
https://platform.claude.com/docs/en/build-with-claude/pdf-support

Includes test for document content support.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@oskar-korczak oskar-korczak force-pushed the feat/add-document-block-support branch from 6afa113 to 2b1c6ee Compare January 13, 2026 08:47
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