Skip to content

Fix data-URL strings decoding as binary values - #295

Open
EhsanAzish80 wants to merge 6 commits into
modelcontextprotocol:mainfrom
EhsanAzish80:fix/preserve-data-url-json-strings
Open

EhsanAzish80 wants to merge 6 commits into
modelcontextprotocol:mainfrom
EhsanAzish80:fix/preserve-data-url-json-strings

Conversation

@EhsanAzish80

@EhsanAzish80 EhsanAzish80 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #277. JSON strings that look like data URLs now stay strings, so tool text is preserved when it passes through Value. This is an independently tested alternative to #278.

Behavior change: Value.data still encodes as a data-URL string, but decoding that JSON returns .string. dataValue is therefore nil on the decoded value. Callers that need the bytes can use Data.parseDataURL(_:) on the string.

The branch also pins the Swift 6.1 CI job to Ubuntu 24.04 and makes the existing client tests wait for the initialize request instead of a fixed 10 ms sleep. These CI fixes are shared with #307.

Tests: 34 focused client and Value tests passed locally. All six CI jobs passed on 7c1f982, including Ubuntu tests, both macOS test jobs, conformance, documentation, and the Linux static build. AI helped write the tests; I ran them.

Copy link
Copy Markdown

This fixes the accidental data-URL coercion, but it also makes the public Value.data case non-round-trippable through its own Codable conformance.

Value.data still encodes as a plain data-URL string:

case let .data(mimeType, value):
    try container.encode(value.dataURLEncoded(mimeType: mimeType))

while this PR now decodes every JSON string as .string. So:

let original = Value.data(mimeType: "application/octet-stream", Data([0, 1, 2]))
let decoded = try JSONDecoder().decode(Value.self, from: JSONEncoder().encode(original))

changes type from .data to .string. The new test explicitly codifies that asymmetry.

If generic JSON strings must never be auto-promoted to binary (which makes sense), could Value.data use an explicit tagged representation for encoding/decoding, or otherwise be removed/documented as non-Codable-lossless? As it stands, persistence or forwarding of a Value.data through JSON silently loses the enum case.

AI-assisted review; checked the current PR head and Value Codable paths before posting.

@EhsanAzish80

Copy link
Copy Markdown
Contributor Author

I kept JSON strings as strings and documented that Value.data is not lossless through generic JSON decoding. The new test shows how to recover the bytes with Data.parseDataURL. All 554 Swift tests passed locally. AI helped write the test; I ran it. Could a maintainer review this choice and approve CI?

@varun06

varun06 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Hi @EhsanAzish80, thanks for this PR! I've reviewed it and it looks good to merge.

Could you please rebase it onto the latest main? CI was broken on macOS runners for every PR (a toolchain/SDK mismatch, fixed in #306), so rebasing will pick up the fix and give us a green run. I'll review and merge once it's updated.

If you're no longer able to work on this, no problem: just let us know. If we don't hear back in about two weeks, we may carry the change forward in a new PR, with credit to you.

@EhsanAzish80
EhsanAzish80 force-pushed the fix/preserve-data-url-json-strings branch from a925398 to 7f454f4 Compare October 24, 2026 18:12
@EhsanAzish80
EhsanAzish80 requested a review from a team as a code owner October 24, 2026 18:12

@akhilcb akhilcb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verified locally: the new tests fail on main and pass here, and the full suite passes. This matches the MCP spec: binary data only travels in explicitly typed base64 fields (ImageContent.data, BlobResourceContents.blob), so free-form JSON strings should come back exactly as sent.

Optional: since Value decoding no longer produces .data, it might be worth noting this as a behavior change in the PR description (as #123 did), and mentioning Data.parseDataURL(_:) in the .data doc comment for callers who relied on dataValue.

@EhsanAzish80

Copy link
Copy Markdown
Contributor Author

The requested .data doc comment is pushed

Copy link
Copy Markdown
Contributor Author

Thanks @akhilcb. I added Data.parseDataURL(_:) to the .data doc comment and made the behavior change explicit in the PR description, including what happens to dataValue. The updated branch passed all six CI jobs.

@varun06

varun06 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Hi @EhsanAzish80, thank you for this PR, and for the care you put into the data-URL fix!

We have just opened the Swift SDK 1.0.0 release (#372, branch rel/1.0.0), a large rework of the SDK. It conflicts with this PR and covers part of it: text sent between Client and Server now arrives as written, because their message decoders keep data: URL strings as .string (valuePreservesStrings, and the WireGoldenTests test "The client reads a data: URL text block as sent").

What 1.0.0 does not cover yet: a plain JSONDecoder().decode(Value.self) still decodes a data: URL as .data. The typed decode before a handler does the same for Value fields such as tool arguments, which the 1.0.0 design keeps on purpose (design/design.md). If you think the plain decoder should change too, a PR or issue against rel/1.0.0 would be a good place to discuss it.

If you are still interested, we would welcome a PR for that remaining piece, rebased onto rel/1.0.0 (or main once 1.0.0 merges), and this PR could then be closed.

Thank you again for contributing. We would love to see more from you on the new stack!

@EhsanAzish80

Copy link
Copy Markdown
Contributor Author

@varun06 Thanks Varun. I agree the client/server text path is covered. Plain Value decoding and typed Value fields still behave differently from #295, as you noted. I’ll review the 1.0 design for that remaining case before deciding on a focused follow-up and closing this branch.

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.

Value decoding changes data-URL-looking JSON strings into Value.data

4 participants