fix: allow content: null in ElicitResult for cancel/decline responses #967
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.
Fix ElicitResult Schema to Support null type for content in Cancel/Decline Responses
This PR fixes the ElicitResultSchema validation to properly accept
content: null
for cancel/decline responses, resolving a type validation bug that violated MCP specification flexibility requirements.Motivation and Context
The current ElicitResultSchema incorrectly rejects
content: null
for cancel/decline responses, causing validation failures in legitimate elicitation workflows. This violates the MCP specification which states that content is "typically omitted" (not forbidden) for these actions.Problem scenarios:
ElicitResultSchema.parse({ action: "cancel", content: null })
throws "Expected object, received null" errorcontent: null
as a sensible default pattern face validation failuresRoot cause:
The schema used a simple interface that didn't distinguish between accept actions (which require content) and cancel/decline actions (which should allow flexible content patterns).
Solution
Replaced the simple interface with a discriminated union that:
null
(MCP spec compliant)Tests
{ action: "cancel", content: null }
- Now passes (was failing){ action: "decline", content: null }
- Now passes (was failing){ action: "cancel" }
- Still passes (omitted content){ action: "accept", content: {...} }
- Still passes (required content)Breaking Changes
None. This is a backward-compatible fix that:
content: null
scenariosTechnical Implementation
Files Changed
src/types.ts
- Updated ElicitResultSchema with discriminated unionsrc/elicit-result.test.ts
- Added comprehensive test suite covering all scenariosfix-spec-types.cjs
- Utility script for test compatibility with external spec typespackage.json
- Enhanced spec type compatibility workflow.gitignore
- Updated to include development utility scriptTypes of Changes
Checklist
This approach provides better type inference while maintaining MCP spec compliance and supporting all reasonable developer patterns, including the common
content: null
pattern for cancelled elicitation requests.