@@ -39,7 +39,6 @@ import {
3939 CallToolResultSchema ,
4040 CreateMessageResultSchema ,
4141 CreateMessageResultWithToolsSchema ,
42- ElicitRequestFormParamsSchema ,
4342 ElicitResultSchema ,
4443 EmptyResultSchema ,
4544 LATEST_PROTOCOL_VERSION ,
@@ -52,11 +51,12 @@ import {
5251 ProtocolErrorCode ,
5352 SdkError ,
5453 SdkErrorCode ,
55- standardSchemaToJsonSchema ,
5654 validateStandardSchema
5755} from '@modelcontextprotocol/core-internal' ;
5856import { DefaultJsonSchemaValidator } from '@modelcontextprotocol/server/_shims' ;
5957
58+ import { normalizeElicitInputFormParams } from './elicitation' ;
59+
6060export type ServerOptions = ProtocolOptions & {
6161 /**
6262 * Capabilities to advertise as being supported by this server.
@@ -86,44 +86,6 @@ export type ServerOptions = ProtocolOptions & {
8686 jsonSchemaValidator ?: jsonSchemaValidator ;
8787} ;
8888
89- function isJsonObject ( value : unknown ) : value is Record < string , unknown > {
90- return typeof value === 'object' && value !== null && ! Array . isArray ( value ) ;
91- }
92-
93- const ELICITATION_STRING_FORMATS = new Set ( [ 'email' , 'uri' , 'date' , 'date-time' ] ) ;
94-
95- function isSupportedFormatPattern ( original : Record < string , unknown > , parsed : Record < string , unknown > , key : string ) : boolean {
96- return (
97- key === 'pattern' &&
98- typeof original . pattern === 'string' &&
99- parsed . type === 'string' &&
100- typeof parsed . format === 'string' &&
101- original . format === parsed . format &&
102- ELICITATION_STRING_FORMATS . has ( parsed . format )
103- ) ;
104- }
105-
106- function findStrippedJsonSchemaPaths ( original : unknown , parsed : unknown , path = '' ) : string [ ] {
107- if ( Array . isArray ( original ) && Array . isArray ( parsed ) ) {
108- return original . flatMap ( ( item , index ) => findStrippedJsonSchemaPaths ( item , parsed [ index ] , `${ path } [${ index } ]` ) ) ;
109- }
110-
111- if ( ! isJsonObject ( original ) || ! isJsonObject ( parsed ) ) {
112- return [ ] ;
113- }
114-
115- return Object . entries ( original ) . flatMap ( ( [ key , value ] ) => {
116- const childPath = path ? `${ path } .${ key } ` : key ;
117- if ( ! Object . prototype . hasOwnProperty . call ( parsed , key ) ) {
118- if ( isSupportedFormatPattern ( original , parsed , key ) ) {
119- return [ ] ;
120- }
121- return [ childPath ] ;
122- }
123- return findStrippedJsonSchemaPaths ( value , parsed [ key ] , childPath ) ;
124- } ) ;
125- }
126-
12789/**
12890 * An MCP server on top of a pluggable transport.
12991 *
@@ -594,7 +556,7 @@ export class Server extends Protocol<ServerContext> {
594556 throw new SdkError ( SdkErrorCode . CapabilityNotSupported , 'Client does not support form elicitation.' ) ;
595557 }
596558
597- const { params : formParams , standardSchema } = this . normalizeElicitInputFormParams (
559+ const { params : formParams , standardSchema } = normalizeElicitInputFormParams (
598560 params as ElicitRequestFormParams | ElicitInputFormParams < StandardSchemaWithJSON >
599561 ) ;
600562
@@ -641,47 +603,6 @@ export class Server extends Protocol<ServerContext> {
641603 }
642604 }
643605
644- private normalizeElicitInputFormParams ( params : ElicitRequestFormParams | ElicitInputFormParams < StandardSchemaWithJSON > ) : {
645- params : ElicitRequestFormParams ;
646- standardSchema ?: StandardSchemaWithJSON ;
647- } {
648- const formParams =
649- params . mode === 'form'
650- ? ( params as ElicitRequestFormParams )
651- : { ...( params as ElicitRequestFormParams ) , mode : 'form' as const } ;
652-
653- if ( this . isElicitInputSchema ( formParams . requestedSchema ) ) {
654- const standardSchema = formParams . requestedSchema ;
655- const normalizedParams = {
656- ...formParams ,
657- requestedSchema : standardSchemaToJsonSchema ( standardSchema , 'input' )
658- } ;
659- const parsedParams = parseSchema ( ElicitRequestFormParamsSchema , normalizedParams ) ;
660- if ( ! parsedParams . success ) {
661- throw new ProtocolError (
662- ProtocolErrorCode . InvalidParams ,
663- `Elicitation requestedSchema only supports flat primitive properties (string, number, integer, boolean, and string enums): ${ parsedParams . error . message } `
664- ) ;
665- }
666- const strippedSchemaPaths = findStrippedJsonSchemaPaths ( normalizedParams . requestedSchema , parsedParams . data . requestedSchema ) ;
667- if ( strippedSchemaPaths . length > 0 ) {
668- throw new ProtocolError (
669- ProtocolErrorCode . InvalidParams ,
670- `Elicitation requestedSchema contains unsupported JSON Schema keyword(s) after Standard Schema conversion: ${ strippedSchemaPaths . join ( ', ' ) } `
671- ) ;
672- }
673- return { params : parsedParams . data , standardSchema } ;
674- }
675-
676- return { params : formParams } ;
677- }
678-
679- private isElicitInputSchema (
680- schema : ElicitRequestFormParams [ 'requestedSchema' ] | StandardSchemaWithJSON
681- ) : schema is StandardSchemaWithJSON {
682- return typeof schema === 'object' && schema !== null && '~standard' in schema ;
683- }
684-
685606 /**
686607 * Creates a reusable callback that, when invoked, will send a `notifications/elicitation/complete`
687608 * notification for the specified elicitation ID.
0 commit comments