Model Context Protocol (MCP) Elicitation

Learn how to handle elicitation requests from MCP servers with the AI TOOLKIT

3 min readnodemcpelicitationView source

Elicitation is a mechanism where MCP servers can request additional information from the client during tool execution. This example demonstrates how to handle elicitation requests, such as collecting user registration information.

Run it locally

$ npm install ai

Example: User Registration

This example shows how to set up an MCP client to handle elicitation requests from a server that needs to collect user input.

import { createMCPClient, ElicitationRequestSchema } from '@ai-toolkit/mcp';

import { generateText } from 'ai-toolkit';

// Create the MCP client with elicitation capability enabled

const mcpClient = await createMCPClient({

transport: {

type: 'sse',

url: 'http://localhost:8083/sse',

},

capabilities: {

elicitation: {},

},

});

// Register a handler for elicitation requests

mcpClient.onElicitationRequest(ElicitationRequestSchema, async request => {

console.log('Server is requesting:', request.params.message);

console.log('Expected schema:', request.params.requestedSchema);

// Collect user input according to the schema

// This is where you would implement your own logic to prompt the user

const userData = await promptUserForInput(request.params.requestedSchema);

// Return the result with one of three actions:

// - 'accept': User provided the requested information

// - 'decline': User chose not to provide the information

// - 'cancel': User cancelled the operation entirely

return {

action: 'accept',

content: userData,

};

});

try {

const tools = await mcpClient.tools();

const { text } = await generateText({

model: 'openai/gpt-4o-mini',

tools,

prompt: 'Register a new user account',

});

console.log('Response:', text);

} finally {

await mcpClient.close();

}

// Example implementation of promptUserForInput

async function promptUserForInput(

schema: unknown,

): Promise<Record<string, unknown>> {

// Implement your own logic to collect input based on the schema

// This could be:

// - A CLI prompt using readline

// - A web form

// - A GUI dialog

// - Any other input mechanism

// For this example, we'll return mock data

return {

username: 'johndoe',

email: 'john@example.com',

password: 'securepassword123',

newsletter: true,

};

}

ts
  1. 1import { createMCPClient, ElicitationRequestSchema } from '@ai-toolkit/mcp';
  2. 2import { generateText } from 'ai-toolkit';
  3. 3// Create the MCP client with elicitation capability enabled
  4. 4const mcpClient = await createMCPClient({
  5. 5 transport: {
  6. 6 type: 'sse',
  7. 7 url: 'http://localhost:8083/sse',
  8. 8 },
  9. 9 capabilities: {
  10. 10 elicitation: {},
  11. 11 },
  12. 12});
  13. 13// Register a handler for elicitation requests
  14. 14mcpClient.onElicitationRequest(ElicitationRequestSchema, async request => {
  15. 15 console.log('Server is requesting:', request.params.message);
  16. 16 console.log('Expected schema:', request.params.requestedSchema);
  17. 17 // Collect user input according to the schema
  18. 18 // This is where you would implement your own logic to prompt the user
  19. 19 const userData = await promptUserForInput(request.params.requestedSchema);
  20. 20 // Return the result with one of three actions:
  21. 21 // - 'accept': User provided the requested information
  22. 22 // - 'decline': User chose not to provide the information
  23. 23 // - 'cancel': User cancelled the operation entirely
  24. 24 return {
  25. 25 action: 'accept',
  26. 26 content: userData,
  27. 27 };
  28. 28});
  29. 29try {
  30. 30 const tools = await mcpClient.tools();
  31. 31 const { text } = await generateText({
  32. 32 model: 'openai/gpt-4o-mini',
  33. 33 tools,
  34. 34 prompt: 'Register a new user account',
  35. 35 });
  36. 36 console.log('Response:', text);
  37. 37} finally {
  38. 38 await mcpClient.close();
  39. 39}
  40. 40// Example implementation of promptUserForInput
  41. 41async function promptUserForInput(
  42. 42 schema: unknown,
  43. 43): Promise<Record<string, unknown>> {
  44. 44 // Implement your own logic to collect input based on the schema
  45. 45 // This could be:
  46. 46 // - A CLI prompt using readline
  47. 47 // - A web form
  48. 48 // - A GUI dialog
  49. 49 // - Any other input mechanism
  50. 50 // For this example, we'll return mock data
  51. 51 return {
  52. 52 username: 'johndoe',
  53. 53 email: 'john@example.com',
  54. 54 password: 'securepassword123',
  55. 55 newsletter: true,
  56. 56 };
  57. 57}

Elicitation Response Actions

Your handler must return an object with an action field:

- `'accept'`: User provided the requested information. Must include content with the data.

- `'decline'`: User chose not to provide the information.

- `'cancel'`: User cancelled the operation entirely.

Important Notes

<Note type="warning">

It is up to the client application to handle elicitation requests properly.

The MCP client simply surfaces these requests from the server to your

application code.

</Note>

The elicitation handler should:

1. Parse the request.params.requestedSchema to understand what data the server needs

2. Implement appropriate user input collection (CLI, web form, etc.)

3. Validate the input matches the requested schema

4. Return the appropriate action and content