Skip to content

fix: serialize ElicitRequest mode discriminator only once - #1045

Open
aditya-786 wants to merge 2 commits into
modelcontextprotocol:mainfrom
aditya-786:fix/elicit-request-duplicate-mode
Open

aditya-786 wants to merge 2 commits into
modelcontextprotocol:mainfrom
aditya-786:fix/elicit-request-duplicate-mode

Conversation

@aditya-786

Copy link
Copy Markdown

Problem

ElicitRequest declares its subtype discriminator with @JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "mode"), so Jackson already writes the mode property when serializing an ElicitFormRequest or ElicitUrlRequest. Both subtypes also annotate their overridden mode() accessor with @JsonProperty("mode"), so mode is written a second time, producing a duplicate JSON key:

{"mode":"form","message":"msg","requestedSchema":{"type":"object"},"mode":"form"}
{"mode":"url","message":"msg","url":"https://example.com","elicitationId":"eid","mode":"url"}

Duplicate object keys violate RFC 8259 and can be rejected by strict client-side parsers.

This is the same defect already fixed on the parallel Content hierarchy, where Content.type() is marked @JsonIgnore and ContentJsonTests guards it by counting raw type occurrences. The elicitation request types were missed.

Fix

Mark the overridden mode() accessors on ElicitFormRequest and ElicitUrlRequest @JsonIgnore, mirroring Content.type(). The @JsonTypeInfo machinery still writes mode exactly once, and deserialization is unaffected since it uses the discriminator plus the @JsonCreator fromJson factories.

Tests

ElicitRequestJsonTests mirrors ContentJsonTests: it asserts each subtype serializes mode exactly once (counting raw occurrences, since the existing assertThatJson tree comparison silently collapses duplicate keys) and that both round-trip back to the correct subtype.

mvn -pl mcp-test -am test -Dtest=ElicitRequestJsonTests,ContentJsonTests,McpSchemaTests

All green (195 schema + 5 content + 4 new). Before the change the two count assertions fail (mode appears twice); after, they pass.

aditya-786 and others added 2 commits June 28, 2026 16:15
ElicitRequest declares its subtype discriminator with
@JsonTypeInfo(..., property = "mode"), so Jackson already writes the mode
property during serialization. ElicitFormRequest and ElicitUrlRequest also
annotated their mode() accessor with @JsonProperty("mode"), so mode was written
twice, producing a duplicate JSON key.

Mark the overridden mode() accessors @JsonIgnore, mirroring the existing fix on
Content.type(). Deserialization is unaffected (it uses the discriminator and the
@jsoncreator factories). Adds ElicitRequestJsonTests, mirroring ContentJsonTests.
@Mintio

Mintio commented Nov 22, 2026

Copy link
Copy Markdown

Thanks for picking this up! I tested it against a real server and found a side effect on the wire, details in #1345.

Setup: cherry-picked onto v2.2.0, Jackson 3.3.0, Streamable HTTP servlet transport, MCP Inspector 2.13.0, server-initiated form elicitation.

The exception is gone and elicitation works, but mode is no longer sent at all. Raw SSE data: lines from the same elicitation/create call:

2.2.0 + Jackson 3.1.8:

{"jsonrpc":"2.0","method":"elicitation/create","id":"…","params":{"message":"Delete task #2 …","requestedSchema":{…},"mode":"form"}}

This PR + Jackson 3.3.0:

{"jsonrpc":"2.0","method":"elicitation/create","id":"…","params":{"message":"Delete task #2 …","requestedSchema":{…}}}

ElicitRequestJsonTests serializes the request as the root value, where the inherited @JsonTypeInfo writes mode. On the transport path the request is the value of JSONRPCRequest.params (declared as Object), so no type id is written, and @JsonIgnore removes the property too. Form mode still works by default, but URL mode would lose mode.

#1345 has a test that catches this and an alternative (As.EXISTING_PROPERTY + keeping @JsonProperty("mode")) that passes your tests and sends mode exactly once.

@papidelec5

Copy link
Copy Markdown

mvn -pl mcp-test -am test -Dtest=ElicitRequestJsonTests,ContentJsonTests,McpSchemaTests

This branch has not been deployed

No deployments
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.

3 participants