-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(server): accept Standard Schemas in inputRequired.elicit #2369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mattzcarey
wants to merge
14
commits into
main
Choose a base branch
from
feat/elicitation-standard-schema
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+236
−17
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8b473a6
feat(server): accept standard schemas for elicitation
mattzcarey 4bdca96
fix: validate standard schema elicitation
mattzcarey e1f83f6
fix: preserve elicitation field titles in examples
mattzcarey 7400137
fix(server): report elicitation schema parse errors
mattzcarey 14eb6ab
fix(server): reject unsupported elicitation schema keywords
mattzcarey 4fd56f5
fix(server): allow elicitation string formats from standard schemas
mattzcarey 0370324
fix(server): isolate elicitation schema normalization
mattzcarey 738f65c
fix(server): allow zod datetime format patterns
mattzcarey 3687e95
fix(server): update elicitation changeset packages
mattzcarey 8c75281
fix(server): type standard schema elicitation leg
mattzcarey 7b9fe01
fix(server): normalize input-required elicitation schemas
mattzcarey 709ca59
fix(server): tolerate elicitation annotation metadata
mattzcarey 481c112
starting again lol
mattzcarey 43d2b31
fix(docs): avoid unresolved inputRequired link
mattzcarey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@modelcontextprotocol/core-internal': minor | ||
| '@modelcontextprotocol/server': minor | ||
| --- | ||
|
|
||
| Allow `inputRequired.elicit()` to accept a Standard Schema such as a Zod object for `requestedSchema`. The builder converts it to MCP's restricted form-elicitation JSON Schema, while the same schema can validate and type the response through `acceptedContent()` on handler re-entry. | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { ProtocolErrorCode } from '../types/enums'; | ||
| import { ProtocolError } from '../types/errors'; | ||
| import { ElicitRequestFormParamsSchema } from '../types/schemas'; | ||
| import type { ElicitRequestFormParams } from '../types/types'; | ||
| import { parseSchema } from '../util/schema'; | ||
| import type { StandardSchemaWithJSON } from '../util/standardSchema'; | ||
| import { isStandardSchemaWithJSON, standardSchemaToJsonSchema } from '../util/standardSchema'; | ||
|
|
||
| /** Input accepted by `inputRequired.elicit()`. */ | ||
| export type ElicitInputParams = Omit<ElicitRequestFormParams, 'mode' | 'requestedSchema'> & { | ||
| mode?: 'form'; | ||
| requestedSchema: ElicitRequestFormParams['requestedSchema'] | StandardSchemaWithJSON; | ||
| }; | ||
|
|
||
| function isJsonObject(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value); | ||
| } | ||
|
|
||
| function convertStandardElicitationSchema(schema: StandardSchemaWithJSON): Record<string, unknown> { | ||
| try { | ||
| return standardSchemaToJsonSchema(schema, 'input'); | ||
| } catch (error) { | ||
| const detail = error instanceof Error ? error.message : String(error); | ||
| throw new ProtocolError( | ||
| ProtocolErrorCode.InvalidParams, | ||
| `Elicitation requestedSchema must describe an object with flat primitive properties: ${detail}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const ANNOTATION_ONLY_JSON_SCHEMA_KEYWORDS = new Set(['$comment', 'deprecated', 'examples', 'readOnly', 'writeOnly']); | ||
|
|
||
| function isAnnotationOnlyJsonSchemaKeyword(key: string): boolean { | ||
| return ANNOTATION_ONLY_JSON_SCHEMA_KEYWORDS.has(key) || key.startsWith('x-'); | ||
| } | ||
|
|
||
| /** | ||
| * Finds converted keywords that MCP's restricted elicitation schema removed. | ||
| * Annotation-only metadata may be dropped; validation constraints may not be | ||
| * weakened silently. | ||
| */ | ||
| function findStrippedConstraintPaths(original: unknown, parsed: unknown, path = ''): string[] { | ||
| if (Array.isArray(original) && Array.isArray(parsed)) { | ||
| return original.flatMap((item, index) => findStrippedConstraintPaths(item, parsed[index], `${path}[${index}]`)); | ||
| } | ||
|
|
||
| if (!isJsonObject(original) || !isJsonObject(parsed)) { | ||
| return []; | ||
| } | ||
|
|
||
| return Object.entries(original).flatMap(([key, value]) => { | ||
| const childPath = path ? `${path}.${key}` : key; | ||
| if (!Object.prototype.hasOwnProperty.call(parsed, key)) { | ||
| return isAnnotationOnlyJsonSchemaKeyword(key) ? [] : [childPath]; | ||
| } | ||
| return findStrippedConstraintPaths(value, parsed[key], childPath); | ||
| }); | ||
| } | ||
|
|
||
| /** Converts an authoring-friendly elicitation input into its wire-ready form. */ | ||
| export function normalizeElicitInputParams(input: ElicitInputParams): ElicitRequestFormParams { | ||
| if (!isStandardSchemaWithJSON(input.requestedSchema)) { | ||
| return { ...input, mode: 'form', requestedSchema: input.requestedSchema }; | ||
| } | ||
|
|
||
| const convertedSchema = convertStandardElicitationSchema(input.requestedSchema); | ||
| const normalized = { ...input, mode: 'form' as const, requestedSchema: convertedSchema }; | ||
| const parsed = parseSchema(ElicitRequestFormParamsSchema, normalized); | ||
| if (!parsed.success) { | ||
| throw new ProtocolError( | ||
| ProtocolErrorCode.InvalidParams, | ||
| `Elicitation requestedSchema only supports flat primitive properties (string, number, integer, boolean, and string enums): ${parsed.error.message}` | ||
| ); | ||
| } | ||
|
|
||
| const strippedConstraints = findStrippedConstraintPaths(convertedSchema, parsed.data.requestedSchema); | ||
| if (strippedConstraints.length > 0) { | ||
| throw new ProtocolError( | ||
| ProtocolErrorCode.InvalidParams, | ||
| `Elicitation requestedSchema contains unsupported JSON Schema constraint(s) after Standard Schema conversion: ${strippedConstraints.join(', ')}` | ||
| ); | ||
| } | ||
|
|
||
| return parsed.data; | ||
| } |
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.