* Creates an elicitation request for the given parameters. * For backwards compatibility, `mode` may be omitted for form requests and will default to `'form'`. * @param params The parameters for the elicitation request. * @param options Optional request options. * @returns The re
(params: ElicitRequestFormParams | ElicitRequestURLParams, options?: RequestOptions)
| 555 | * @returns The result of the elicitation request. |
| 556 | */ |
| 557 | async elicitInput(params: ElicitRequestFormParams | ElicitRequestURLParams, options?: RequestOptions): Promise<ElicitResult> { |
| 558 | const mode = (params.mode ?? 'form') as 'form' | 'url'; |
| 559 | |
| 560 | switch (mode) { |
| 561 | case 'url': { |
| 562 | if (!this._clientCapabilities?.elicitation?.url) { |
| 563 | throw new Error('Client does not support url elicitation.'); |
| 564 | } |
| 565 | |
| 566 | const urlParams = params as ElicitRequestURLParams; |
| 567 | return this.request({ method: 'elicitation/create', params: urlParams }, ElicitResultSchema, options); |
| 568 | } |
| 569 | case 'form': { |
| 570 | if (!this._clientCapabilities?.elicitation?.form) { |
| 571 | throw new Error('Client does not support form elicitation.'); |
| 572 | } |
| 573 | |
| 574 | const formParams: ElicitRequestFormParams = |
| 575 | params.mode === 'form' ? (params as ElicitRequestFormParams) : { ...(params as ElicitRequestFormParams), mode: 'form' }; |
| 576 | |
| 577 | const result = await this.request({ method: 'elicitation/create', params: formParams }, ElicitResultSchema, options); |
| 578 | |
| 579 | if (result.action === 'accept' && result.content && formParams.requestedSchema) { |
| 580 | try { |
| 581 | const validator = this._jsonSchemaValidator.getValidator(formParams.requestedSchema as JsonSchemaType); |
| 582 | const validationResult = validator(result.content); |
| 583 | |
| 584 | if (!validationResult.valid) { |
| 585 | throw new McpError( |
| 586 | ErrorCode.InvalidParams, |
| 587 | `Elicitation response content does not match requested schema: ${validationResult.errorMessage}` |
| 588 | ); |
| 589 | } |
| 590 | } catch (error) { |
| 591 | if (error instanceof McpError) { |
| 592 | throw error; |
| 593 | } |
| 594 | throw new McpError( |
| 595 | ErrorCode.InternalError, |
| 596 | `Error validating elicitation response: ${error instanceof Error ? error.message : String(error)}` |
| 597 | ); |
| 598 | } |
| 599 | } |
| 600 | return result; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Creates a reusable callback that, when invoked, will send a `notifications/elicitation/complete` |