Value decoding changes data-URL-looking JSON strings into Value.data
Les mainteneurs répondent en général sous 1 jour
@bitbemol y travaille déjà.
Depuis le 29/8/2026.
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 76/100
Piste de recherche
Commencez par la branche des chaînes de Value.init(from:) et examinez les appels existants à Data.isDataURL et Data.parseDataURL. Exécutez d’abord les tests de régression et de contrôle ciblés sur le texte typé, puis la suite complète de tests du SDK ; le travail est terminé lorsque les chaînes JSON génériques restent Value.string et que l’encodage explicite des données ainsi que la sérialisation typée des images et de l’audio restent inchangés.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Describe the bug
This is my first contribution to this repository, so I may be missing some intended design context. I have tried to verify the behavior carefully and would appreciate correction if this conversion is intentional.
Value.init(from:) currently interprets a JSON string as Value.data whenever the string looks like a data URL and can be parsed successfully.
For example, this valid JSON string:
"data:text/plain,Hello%20World"
is decoded as binary data rather than:
Value.string("data:text/plain,Hello%20World")
Re-encoding the decoded value changes the string to:
data:text/plain;base64,SGVsbG8gV29ybGQ=
This changes both the Value case and the original string’s spelling.
The same behavior can affect explicitly typed MCP text content. A CallTool.Result containing:
.text(
text: "data:text/plain,Hello%20World",
annotations: nil,
_meta: nil
)
is rewritten when it passes through the SDK’s generic Value conversion. The recovered content is still tagged as text, but its text payload has changed.
To Reproduce
import Foundation
import MCP
let json = Data(#""data:text/plain,Hello%20World""#.utf8)
let decoded = try JSONDecoder().decode(Value.self, from: json)
print(decoded.stringValue as Any)
let reencoded = try JSONEncoder().encode(decoded)
let recoveredString = try JSONDecoder().decode(String.self, from: reencoded)
print(recoveredString)
Current behavior:
nil
data:text/plain;base64,SGVsbG8gV29ybGQ=
The behavior comes from the string branch of Value.init(from:), which calls Data.isDataURL and Data.parseDataURL before deciding between .data and .string.
Strings that do not form parseable data URLs remain .string. For example, Hello%20World by itself is unaffected. The conversion is triggered by complete data-URL-looking strings such as:
data:text/plain,Hello%20World
data:text/plain,Hello World
data:text/plain;base64,SGVsbG8=
data:,
Expected behavior
I believe generic JSON decoding should follow the JSON wire type:
- Every JSON string decoded through
ValueremainsValue.string. - Data-URL interpretation remains explicitly available through
Data.parseDataURL. - Explicitly constructed
Value.datavalues continue to encode using the existing data-URL representation. - Typed image and audio content remains unchanged because those MCP content types have explicit
dataandmimeTypefields.
A generic JSON string does not carry enough information to distinguish:
Value.string("data:text/plain;base64,SGVsbG8=")
from an encoded:
Value.data(mimeType: "text/plain", Data("Hello".utf8))
because both have the same JSON representation.
Logs
A focused typed-text regression currently fails with:
text → "data:text/plain;base64,SGVsbG8gV29ybGQ="
original → "data:text/plain,Hello%20World"
With the original decoder implementation, 6 of 8 focused regression and control tests fail. The two unaffected controls verify explicit Value.data encoding and typed image/audio serialization.
Changing the generic string branch to assign .string(value) makes all 8 focused tests pass. The complete SDK test plan also passes: 559 tests, 0 failures.
Additional context
I encountered this through a downstream MCP server that needs to preserve literal text exactly:
https://github.com/bitbemol/second-brain-mcp
The downstream project currently vendors the Swift SDK solely to avoid this transformation.
I have a minimal patch and regression tests ready. The explicit data-URL parsing helpers are preserved.
There is a compatibility consideration: callers that rely on generic Value decoding to recognize data URLs would instead need to call Data.parseDataURL explicitly. If the current implicit decoding is intentional API behavior, I would appreciate guidance on how ordinary JSON strings with the same spelling should be represented without being transformed.
Tested against Swift SDK main at a0ae212 and release 0.12.1.
AI assistance disclosure: An LLM initially identified this behavior while I was diagnosing the downstream issue. Codex assisted with repository analysis, regression-test development, and drafting this report. I reviewed the affected code, reproduced the behavior with the original implementation in Xcode, and verified the proposed behavior with the focused and complete test suites.
- Langage dominant
- Swift
- Étoiles
- 1.6k
- Forks
- 278
- Merge moyen
- 11 h 15 min
- PR mergées (30 j)
- 46
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de modelcontextprotocol/swift-sdk
-
-32042 without elicitations drops JSON-RPC error dataPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 67/100
modelcontextprotocol/swift-sdk#381 ·
Les mainteneurs répondent en général sous 1 jour
-
StdioTransport traps ("Negative value is not representable") when stdio is a socket, e.g. spawned by Node clientsPeut-être pris @branben l’a pris il y a 9 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 73/100
modelcontextprotocol/swift-sdk#327 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
enhancement
Difficulté 1/5 Moins d'une heure Accessibilité débutants 78/100
modelcontextprotocol/swift-sdk#274 ·
Les mainteneurs répondent en général sous 1 jour
-
`HTTPClientTransport` fails to build on Windows: `#if !os(Linux)` guards no longer match the `EventSource` platform conditionPeut-être à nouveau libre Une pull request pour cette issue a été fermée sans être fusionnée. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
modelcontextprotocol/swift-sdk#261 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
modelcontextprotocol/swift-sdk#376 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de modelcontextprotocol/swift-sdk
Issues similaires
-
needs-triage
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
manaflow-ai/cmux#59692 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 71/100
jbcoe/cc-protocol#520 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
Les mainteneurs répondent en général sous 2 jours
-
App 仍在下载和解析用不上的会话列表Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
vibe-cafe/vibe-usage-app#125 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 76/100
touyou/CheckItOut#39 ·
Les mainteneurs répondent en général sous 1 jour