@@ -137,7 +137,10 @@ export enum ErrorCode {
137137 InvalidRequest = - 32600 ,
138138 MethodNotFound = - 32601 ,
139139 InvalidParams = - 32602 ,
140- InternalError = - 32603
140+ InternalError = - 32603 ,
141+
142+ // MCP-specific error codes
143+ UrlElicitationRequired = - 32042
141144}
142145
143146/**
@@ -271,6 +274,28 @@ export const ImplementationSchema = BaseMetadataSchema.extend({
271274 websiteUrl : z . string ( ) . optional ( )
272275} ) . merge ( IconsSchema ) ;
273276
277+ const ElicitationCapabilitySchema = z
278+ . preprocess (
279+ value => {
280+ if ( value && typeof value === 'object' && ! Array . isArray ( value ) ) {
281+ const hasForm = Object . prototype . hasOwnProperty . call ( value , 'form' ) ;
282+ const hasUrl = Object . prototype . hasOwnProperty . call ( value , 'url' ) ;
283+ if ( ! hasForm && ! hasUrl ) {
284+ return { ...( value as Record < string , unknown > ) , form : { } } ;
285+ }
286+ }
287+ return value ;
288+ } ,
289+ z
290+ . object ( {
291+ applyDefaults : z . boolean ( ) . optional ( ) ,
292+ form : z . object ( { } ) . passthrough ( ) . optional ( ) ,
293+ url : z . object ( { } ) . passthrough ( ) . optional ( )
294+ } )
295+ . passthrough ( )
296+ )
297+ . optional ( ) ;
298+
274299/**
275300 * Capabilities a client may support. Known capabilities are defined here, in this schema, but this is not a closed set: any client can define its own, additional capabilities.
276301 */
@@ -286,17 +311,7 @@ export const ClientCapabilitiesSchema = z.object({
286311 /**
287312 * Present if the client supports eliciting user input.
288313 */
289- elicitation : z . intersection (
290- z
291- . object ( {
292- /**
293- * Whether the client should apply defaults to the user input.
294- */
295- applyDefaults : z . boolean ( ) . optional ( )
296- } )
297- . optional ( ) ,
298- z . record ( z . string ( ) , z . unknown ( ) ) . optional ( )
299- ) ,
314+ elicitation : ElicitationCapabilitySchema ,
300315 /**
301316 * Present if the client supports listing roots.
302317 */
@@ -1337,9 +1352,25 @@ export const PrimitiveSchemaDefinitionSchema = z.union([EnumSchemaSchema, Boolea
13371352 */
13381353export const ElicitRequestParamsSchema = BaseRequestParamsSchema . extend ( {
13391354 /**
1340- * The message to present to the user.
1355+ * The mode of elicitation.
1356+ * - "form": In-band structured data collection with optional schema validation
1357+ * - "url": Out-of-band interaction via URL navigation
1358+ */
1359+ mode : z . enum ( [ 'form' , 'url' ] ) ,
1360+ /**
1361+ * The progress token as specified in the Progress capability, required in this request.
1362+ */
1363+ message : z . string ( )
1364+ } ) ;
1365+
1366+ /**
1367+ * Parameters for form-based elicitation.
1368+ */
1369+ export const ElicitRequestFormParamsSchema = ElicitRequestParamsSchema . extend ( {
1370+ /**
1371+ * The elicitation mode.
13411372 */
1342- message : z . string ( ) ,
1373+ mode : z . literal ( 'form' ) ,
13431374 /**
13441375 * A restricted subset of JSON Schema.
13451376 * Only top-level properties are allowed, without nesting.
@@ -1351,13 +1382,55 @@ export const ElicitRequestParamsSchema = BaseRequestParamsSchema.extend({
13511382 } )
13521383} ) ;
13531384
1385+ /**
1386+ * Parameters for URL-based elicitation.
1387+ */
1388+ export const ElicitRequestURLParamsSchema = ElicitRequestParamsSchema . extend ( {
1389+ /**
1390+ * The elicitation mode.
1391+ */
1392+ mode : z . literal ( 'url' ) ,
1393+ /**
1394+ * The ID of the elicitation, which must be unique within the context of the server.
1395+ * The client MUST treat this ID as an opaque value.
1396+ */
1397+ elicitationId : z . string ( ) ,
1398+ /**
1399+ * The URL that the user should navigate to.
1400+ */
1401+ url : z . string ( ) . url ( )
1402+ } ) ;
1403+
13541404/**
13551405 * A request from the server to elicit user input via the client.
1356- * The client should present the message and form fields to the user.
1406+ * The client should present the message and form fields to the user (form mode)
1407+ * or navigate to a URL (URL mode).
13571408 */
13581409export const ElicitRequestSchema = RequestSchema . extend ( {
13591410 method : z . literal ( 'elicitation/create' ) ,
1360- params : ElicitRequestParamsSchema
1411+ params : z . union ( [ ElicitRequestFormParamsSchema , ElicitRequestURLParamsSchema ] )
1412+ } ) ;
1413+
1414+ /**
1415+ * Parameters for a `notifications/elicitation/complete` notification.
1416+ *
1417+ * @category notifications/elicitation/complete
1418+ */
1419+ export const ElicitationCompleteNotificationParamsSchema = NotificationsParamsSchema . extend ( {
1420+ /**
1421+ * The ID of the elicitation that completed.
1422+ */
1423+ elicitationId : z . string ( )
1424+ } ) ;
1425+
1426+ /**
1427+ * A notification from the server to the client, informing it of a completion of an out-of-band elicitation request.
1428+ *
1429+ * @category notifications/elicitation/complete
1430+ */
1431+ export const ElicitationCompleteNotificationSchema = NotificationSchema . extend ( {
1432+ method : z . literal ( 'notifications/elicitation/complete' ) ,
1433+ params : ElicitationCompleteNotificationParamsSchema
13611434} ) ;
13621435
13631436/**
@@ -1553,7 +1626,8 @@ export const ServerNotificationSchema = z.union([
15531626 ResourceUpdatedNotificationSchema ,
15541627 ResourceListChangedNotificationSchema ,
15551628 ToolListChangedNotificationSchema ,
1556- PromptListChangedNotificationSchema
1629+ PromptListChangedNotificationSchema ,
1630+ ElicitationCompleteNotificationSchema
15571631] ) ;
15581632
15591633export const ServerResultSchema = z . union ( [
@@ -1578,6 +1652,38 @@ export class McpError extends Error {
15781652 super ( `MCP error ${ code } : ${ message } ` ) ;
15791653 this . name = 'McpError' ;
15801654 }
1655+
1656+ /**
1657+ * Factory method to create the appropriate error type based on the error code and data
1658+ */
1659+ static fromError ( code : number , message : string , data ?: unknown ) : McpError {
1660+ // Check for specific error types
1661+ if ( code === ErrorCode . UrlElicitationRequired && data ) {
1662+ const errorData = data as { elicitations ?: unknown [ ] } ;
1663+ if ( errorData . elicitations ) {
1664+ return new UrlElicitationRequiredError ( errorData . elicitations as ElicitRequestURLParams [ ] , message ) ;
1665+ }
1666+ }
1667+
1668+ // Default to generic McpError
1669+ return new McpError ( code , message , data ) ;
1670+ }
1671+ }
1672+
1673+ /**
1674+ * Specialized error type when a tool requires a URL mode elicitation.
1675+ * This makes it nicer for the client to handle since there is specific data to work with instead of just a code to check against.
1676+ */
1677+ export class UrlElicitationRequiredError extends McpError {
1678+ constructor ( elicitations : ElicitRequestURLParams [ ] , message : string = `URL elicitation${ elicitations . length > 1 ? 's' : '' } required` ) {
1679+ super ( ErrorCode . UrlElicitationRequired , message , {
1680+ elicitations : elicitations
1681+ } ) ;
1682+ }
1683+
1684+ get elicitations ( ) : ElicitRequestURLParams [ ] {
1685+ return ( this . data as { elicitations : ElicitRequestURLParams [ ] } ) ?. elicitations ?? [ ] ;
1686+ }
15811687}
15821688
15831689type Primitive = string | number | boolean | bigint | null | undefined ;
@@ -1755,8 +1861,12 @@ export type SingleSelectEnumSchema = Infer<typeof SingleSelectEnumSchemaSchema>;
17551861export type MultiSelectEnumSchema = Infer < typeof MultiSelectEnumSchemaSchema > ;
17561862
17571863export type PrimitiveSchemaDefinition = Infer < typeof PrimitiveSchemaDefinitionSchema > ;
1758- export type ElicitRequestParams = Infer < typeof ElicitRequestParamsSchema > ;
1864+ //export type ElicitRequestParams = Infer<typeof ElicitRequestParamsSchema>; // TODO: remove this
1865+ export type ElicitRequestFormParams = Infer < typeof ElicitRequestFormParamsSchema > ;
1866+ export type ElicitRequestURLParams = Infer < typeof ElicitRequestURLParamsSchema > ;
17591867export type ElicitRequest = Infer < typeof ElicitRequestSchema > ;
1868+ export type ElicitationCompleteNotificationParams = Infer < typeof ElicitationCompleteNotificationParamsSchema > ;
1869+ export type ElicitationCompleteNotification = Infer < typeof ElicitationCompleteNotificationSchema > ;
17601870export type ElicitResult = Infer < typeof ElicitResultSchema > ;
17611871
17621872/* Autocomplete */
0 commit comments