Skip to content

MCP Server Base64 encode/decode mismatch for Resources #342

Open
@evalstate

Description

@evalstate

Describe the bug

In src/mcp/server/lowlevel/server.py, binary data is encoded using base64.urlsafe_b64encode(), which replaces + with - and / with _ to make it URL-safe.
However, when the client tries to validate this data with BlobResourceContents, the validator expects standard base64 encoding (with + and /).
This causes validation failures with "Invalid base64" errors when processing larger binary files that contain these special characters.

Before Fix:

# Create server
server = FastMCP("Base64 Test")

# Add resource directly
binary_resource = FileResource(
    uri=AnyUrl("resource://test/simple.bin"),
    path=Path("sample.pdf").absolute(),
    name="Test PDF",
    mime_type="application/pdf",
)

server.add_resource(binary_resource)

if __name__ == "__main__":
    server.run()

Image

Use FastMCP to return a binary resource (e.g. a PDF file), and inspect the contents.

After Fix:

Image

Expected behavior
That binary content is returned as correctly base64 content rather than an error message.

Additional context
PR incoming with automated test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions