Repository navigation
fix(rust): keep the key order of maps in the v2 and unstable types - #2894
Merged
Merged
Conversation
The Rust types stored JSON objects in `BTreeMap` and `HashMap`, so parsing and writing a message sorted or shuffled its keys. An elicitation form lost the field order the Agent chose, and a proxy reordered the payload of an unknown variant. Use `IndexMap` for these maps in the v2 types and in the v1 types behind unstable features: - `ElicitationSchema.properties` and `ElicitationAcceptAction.content` - the `fields` of every unknown-variant payload type - `SetProviderRequest.headers` Discriminators are removed with `shift_remove`, which keeps the order of the remaining keys. `apply_meta_update` now removes a `_meta` key the same way. It used to move the last stored key into the removed slot. The stable v1 types keep their map types, since changing them is a breaking change. Each has a TODO for the next major release. The generated schema is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Rust types stored JSON objects in
BTreeMapandHashMap, so parsing and writing a message sorted or shuffled its keys:requestedSchema.propertiessent aszeta,alphacame back asalpha,zeta.providers/setwrote itsheadersin a different order on each run.The crate already enables
serde_json'spreserve_orderfor raw JSON values and_meta. This does the same for the typed maps.The spec does not define a field order for elicitation forms, and neither does MCP. This adds no rule and no ordering field. It only stops the Rust types from discarding the order.
Changes
These maps are now
IndexMap, in the v2 types and in the v1 types behind unstable features:ElicitationSchema.propertiesandElicitationAcceptAction.contentfieldsof every unknown-variant payload type (OtherContentBlock,OtherSessionUpdate, and the rest)SetProviderRequest.headersAlso:
typeand similar keys while parsing. They now useshift_remove, so the last key no longer moves into the gap.apply_meta_updatekeeps order. Removing a_metakey through a patch used to move the last stored key into its place._metaitself stays aserde_json::Map, which already keeps its order.properties, acceptcontent, an unknown content block, and a_metapatch.indexmap2 is a new direct dependency. It was already in the tree throughserde_jsonandserde_with, soCargo.lockgains two edges and no package.IndexMapnow appears in public signatures of the v2 and unstable types.Not changed
TODO(next major): six inv1/elicitation.rsandAuthMethodTerminal.env.Downstream
Code that builds a
BTreeMaporHashMapfor one of the changed fields needs to build anIndexMapinstead. This only affects code that uses the v2 types or the unstable v1 features.Checks
cargo fmtpass.unstablealone,unstable_protocol_v2alone, and each affected unstable feature alone.