Skip to content

fix(rust): keep the key order of maps in the v2 and unstable types - #2894

Merged
benbrandt merged 1 commit into
mainfrom
fix/preserve-map-key-order
Nov 19, 2026
Merged

benbrandt merged 1 commit into
mainfrom
fix/preserve-map-key-order

Conversation

@benbrandt

Copy link
Copy Markdown
Member

Summary

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. requestedSchema.properties sent as zeta, alpha came back as alpha, zeta.
  • A proxy re-sorted the payload of a variant it did not understand.
  • providers/set wrote its headers in a different order on each run.

The crate already enables serde_json's preserve_order for 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.properties and ElicitationAcceptAction.content
  • the fields of every unknown-variant payload type (OtherContentBlock, OtherSessionUpdate, and the rest)
  • SetProviderRequest.headers

Also:

  • Removing a discriminator keeps order. The payload types strip type and similar keys while parsing. They now use shift_remove, so the last key no longer moves into the gap.
  • apply_meta_update keeps order. Removing a _meta key through a patch used to move the last stored key into its place. _meta itself stays a serde_json::Map, which already keeps its order.
  • Tests pin the order for properties, accept content, an unknown content block, and a _meta patch.

indexmap 2 is a new direct dependency. It was already in the tree through serde_json and serde_with, so Cargo.lock gains two edges and no package. IndexMap now appears in public signatures of the v2 and unstable types.

Not changed

  • Stable v1 types. Changing their map type is a breaking change. Seven fields carry a TODO(next major): six in v1/elicitation.rs and AuthMethodTerminal.env.
  • The generated schema. It is byte-identical.
  • The spec pages.

Downstream

Code that builds a BTreeMap or HashMap for one of the changed fields needs to build an IndexMap instead. This only affects code that uses the v2 types or the unstable v1 features.

Checks

  • Clippy, the tests, rustdoc and cargo fmt pass.
  • The crate builds with each feature set I tried, including no default features, unstable alone, unstable_protocol_v2 alone, and each affected unstable feature alone.
  • Regenerating the schema gives no diff.

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.
@benbrandt
benbrandt requested a review from a team as a code owner November 19, 2026 20:38
@benbrandt
benbrandt enabled auto-merge (squash) November 19, 2026 20:39
@benbrandt
benbrandt merged commit 5a0e4a9 into main Nov 19, 2026
13 checks passed
@benbrandt
benbrandt deleted the fix/preserve-map-key-order branch November 19, 2026 20:42
@acp-release-bot acp-release-bot Bot mentioned this pull request Nov 19, 2026
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