-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(server): accept Standard Schemas for elicitation #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
9
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
79dc1cd
feat(server): accept standard schemas for elicitation
mattzcarey c230f1a
fix: validate standard schema elicitation
mattzcarey a3817bb
fix: preserve elicitation field titles in examples
mattzcarey 1580662
fix(server): report elicitation schema parse errors
mattzcarey bb7a377
fix(server): reject unsupported elicitation schema keywords
mattzcarey 0ad602d
fix(server): allow elicitation string formats from standard schemas
mattzcarey 1308739
fix(server): isolate elicitation schema normalization
mattzcarey a5ba576
fix(server): allow zod datetime format patterns
mattzcarey 918c601
fix(server): update elicitation changeset packages
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,8 @@ | ||
| --- | ||
| '@modelcontextprotocol/core-internal': minor | ||
| '@modelcontextprotocol/server': minor | ||
| '@modelcontextprotocol/client': minor | ||
| --- | ||
|
|
||
| Allow form elicitation requests to accept Standard Schema values such as Zod objects for `requestedSchema`. The server converts these schemas to MCP's restricted elicitation JSON Schema before sending and parses accepted content with the original schema before returning typed | ||
| results. Zod string formats that map to MCP's supported `email`, `uri`, `date`, or `date-time` formats are accepted; arbitrary regex patterns remain rejected because form elicitation does not carry JSON Schema `pattern`. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| // AUTO-GENERATED — do not edit. Run `pnpm run generate:versions` to regenerate. | ||
| export const V2_PACKAGE_VERSIONS: Record<string, string> = { | ||
| '@modelcontextprotocol/client': '^2.0.0-alpha.2', | ||
| '@modelcontextprotocol/server': '^2.0.0-alpha.2', | ||
| '@modelcontextprotocol/node': '^2.0.0-alpha.2', | ||
| '@modelcontextprotocol/express': '^2.0.0-alpha.2', | ||
| '@modelcontextprotocol/server-legacy': '^2.0.0-alpha.2', | ||
| '@modelcontextprotocol/core': '^2.0.0-alpha.0' | ||
| '@modelcontextprotocol/client': '^2.0.0-alpha.3', | ||
| '@modelcontextprotocol/server': '^2.0.0-alpha.3', | ||
| '@modelcontextprotocol/node': '^2.0.0-alpha.3', | ||
| '@modelcontextprotocol/express': '^2.0.0-alpha.3', | ||
| '@modelcontextprotocol/server-legacy': '^2.0.0-alpha.3', | ||
| '@modelcontextprotocol/core': '^2.0.0-alpha.1' | ||
| }; |
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,142 @@ | ||
| import type { ElicitInputFormParams, ElicitRequestFormParams, StandardSchemaWithJSON } from '@modelcontextprotocol/core-internal'; | ||
| import { | ||
| ElicitRequestFormParamsSchema, | ||
| parseSchema, | ||
| ProtocolError, | ||
| ProtocolErrorCode, | ||
| standardSchemaToJsonSchema | ||
| } from '@modelcontextprotocol/core-internal'; | ||
|
|
||
| export type NormalizedElicitInputFormParams = { | ||
| params: ElicitRequestFormParams; | ||
| standardSchema?: StandardSchemaWithJSON; | ||
| }; | ||
|
|
||
| function isJsonObject(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value); | ||
| } | ||
|
|
||
| const ZOD_ISO_DATE_PATTERN = String.raw`(?:(?:\d\d[2468][048]|\d\d[13579][26]|\d\d0[48]|[02468][048]00|[13579][26]00)-02-29|\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\d|30)|(?:02)-(?:0[1-9]|1\d|2[0-8])))`; | ||
| const ZOD_ISO_TIME_PREFIX = String.raw`(?:[01]\d|2[0-3]):[0-5]\d`; | ||
| const ZOD_ISO_OFFSET_PATTERN = String.raw`([+-](?:[01]\d|2[0-3]):[0-5]\d)`; | ||
|
|
||
| const ZOD_REDUNDANT_FORMAT_PATTERNS: ReadonlyMap<string, ReadonlySet<string>> = new Map([ | ||
| ['email', new Set([String.raw`^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+\-\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$`])], | ||
| [ | ||
| 'date', | ||
| new Set([ | ||
| String.raw`^(?:(?:\d\d[2468][048]|\d\d[13579][26]|\d\d0[48]|[02468][048]00|[13579][26]00)-02-29|\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\d|30)|(?:02)-(?:0[1-9]|1\d|2[0-8])))$` | ||
| ]) | ||
| ] | ||
| ]); | ||
|
|
||
| const ZOD_DATETIME_ZONE_SUFFIXES = [ | ||
| String.raw`(?:Z)`, | ||
| String.raw`(?:Z|)`, | ||
| String.raw`(?:Z|${ZOD_ISO_OFFSET_PATTERN})`, | ||
| String.raw`(?:Z||${ZOD_ISO_OFFSET_PATTERN})` | ||
| ] as const; | ||
|
|
||
| function escapeRegExpLiteral(value: string): string { | ||
| return value.replaceAll(/[.*+?^${}()|[\]\\]/g, match => `\\${match}`); | ||
| } | ||
|
|
||
| const ZOD_PRECISION_TIME_PATTERN = new RegExp(String.raw`^${escapeRegExpLiteral(String.raw`${ZOD_ISO_TIME_PREFIX}:[0-5]\d\.\d{`)}\d+\}$`); | ||
|
|
||
| function isZodIsoDatetimePattern(pattern: string): boolean { | ||
| const prefix = `^${ZOD_ISO_DATE_PATTERN}T(?:`; | ||
| if (!pattern.startsWith(prefix) || !pattern.endsWith(')$')) { | ||
| return false; | ||
| } | ||
|
|
||
| const innerPattern = pattern.slice(prefix.length, -2); | ||
| const zoneSuffix = ZOD_DATETIME_ZONE_SUFFIXES.find(suffix => innerPattern.endsWith(suffix)); | ||
| if (!zoneSuffix) { | ||
| return false; | ||
| } | ||
|
|
||
| const timePattern = innerPattern.slice(0, -zoneSuffix.length); | ||
| return ( | ||
| timePattern === String.raw`${ZOD_ISO_TIME_PREFIX}` || | ||
| timePattern === String.raw`${ZOD_ISO_TIME_PREFIX}:[0-5]\d` || | ||
| timePattern === String.raw`${ZOD_ISO_TIME_PREFIX}(?::[0-5]\d(?:\.\d+)?)?` || | ||
| ZOD_PRECISION_TIME_PATTERN.test(timePattern) | ||
| ); | ||
| } | ||
|
|
||
| function isRedundantFormatPattern(original: Record<string, unknown>, parsed: Record<string, unknown>, key: string): boolean { | ||
| if ( | ||
| key !== 'pattern' || | ||
| typeof original.pattern !== 'string' || | ||
| parsed.type !== 'string' || | ||
| typeof parsed.format !== 'string' || | ||
| original.format !== parsed.format | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| if (parsed.format === 'date-time') { | ||
| return isZodIsoDatetimePattern(original.pattern); | ||
| } | ||
|
|
||
| return ZOD_REDUNDANT_FORMAT_PATTERNS.get(parsed.format)?.has(original.pattern) === true; | ||
| } | ||
|
mattzcarey marked this conversation as resolved.
|
||
|
|
||
| function findStrippedJsonSchemaPaths(original: unknown, parsed: unknown, path = ''): string[] { | ||
| if (Array.isArray(original) && Array.isArray(parsed)) { | ||
| return original.flatMap((item, index) => findStrippedJsonSchemaPaths(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)) { | ||
| if (isRedundantFormatPattern(original, parsed, key)) { | ||
| return []; | ||
| } | ||
| return [childPath]; | ||
| } | ||
| return findStrippedJsonSchemaPaths(value, parsed[key], childPath); | ||
| }); | ||
| } | ||
|
|
||
| function isElicitInputSchema( | ||
| schema: ElicitRequestFormParams['requestedSchema'] | StandardSchemaWithJSON | ||
| ): schema is StandardSchemaWithJSON { | ||
| return typeof schema === 'object' && schema !== null && '~standard' in schema; | ||
| } | ||
|
|
||
| export function normalizeElicitInputFormParams( | ||
| params: ElicitRequestFormParams | ElicitInputFormParams<StandardSchemaWithJSON> | ||
| ): NormalizedElicitInputFormParams { | ||
| const formParams = | ||
| params.mode === 'form' ? (params as ElicitRequestFormParams) : { ...(params as ElicitRequestFormParams), mode: 'form' as const }; | ||
|
|
||
| if (isElicitInputSchema(formParams.requestedSchema)) { | ||
| const standardSchema = formParams.requestedSchema; | ||
| const normalizedParams = { | ||
| ...formParams, | ||
| requestedSchema: standardSchemaToJsonSchema(standardSchema, 'input') | ||
| }; | ||
| const parsedParams = parseSchema(ElicitRequestFormParamsSchema, normalizedParams); | ||
| if (!parsedParams.success) { | ||
| throw new ProtocolError( | ||
| ProtocolErrorCode.InvalidParams, | ||
| `Elicitation requestedSchema only supports flat primitive properties (string, number, integer, boolean, and string enums): ${parsedParams.error.message}` | ||
| ); | ||
| } | ||
| const strippedSchemaPaths = findStrippedJsonSchemaPaths(normalizedParams.requestedSchema, parsedParams.data.requestedSchema); | ||
| if (strippedSchemaPaths.length > 0) { | ||
| throw new ProtocolError( | ||
| ProtocolErrorCode.InvalidParams, | ||
| `Elicitation requestedSchema contains unsupported JSON Schema keyword(s) after Standard Schema conversion: ${strippedSchemaPaths.join(', ')}` | ||
| ); | ||
| } | ||
| return { params: parsedParams.data, standardSchema }; | ||
| } | ||
|
|
||
| return { params: formParams }; | ||
| } | ||
Oops, something went wrong.
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.