startElicitation
Collect user preferences dynamically by prompting for favorite color, number, and pets using the MCP Elicitations Demo Server’s elicitation feature.
Instructions
Demonstrates the Elicitation feature by asking the user to provide information about their favorite color, number, and pets.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/tools/tool-elicitation.ts:12-66 (handler)The core handler function for the startElicitation tool. It triggers an elicitation request for user preferences (color, number, pets), handles the response based on user action (accept/decline/cancel), formats content accordingly, and includes raw result for debugging.handler: async (args: any, request: any, server: Server) => { ElicitationSchema.parse(args); const elicitationResult = await requestElicitation( 'What are your favorite things?', { type: 'object', properties: { color: { type: 'string', description: 'Favorite color' }, number: { type: 'integer', description: 'Favorite number', minimum: 1, maximum: 100 }, pets: { type: 'string', enum: ['cats', 'dogs', 'birds', 'fish', 'reptiles'], description: 'Favorite pets' }, } }, server ); // Handle different response actions const content: any[] = []; if (elicitationResult.action === 'accept' && elicitationResult.content) { content.push({ type: "text" as const, text: `✅ User provided their favorite things!`, }); // Only access elicitationResult.content when action is accept const { color, number, pets } = elicitationResult.content; content.push({ type: "text" as const, text: `Their favorites are:\n- Color: ${color || 'not specified'}\n- Number: ${number || 'not specified'}\n- Pets: ${pets || 'not specified'}`, }); } else if (elicitationResult.action === 'decline') { content.push({ type: "text" as const, text: `❌ User declined to provide their favorite things.`, }); } else if (elicitationResult.action === 'cancel') { content.push({ type: "text" as const, text: `⚠️ User cancelled the elicitation dialog.`, }); } // Include raw result for debugging content.push({ type: "text" as const, text: `\nRaw result: ${JSON.stringify(elicitationResult, null, 2)}`, }); return { content }; },
- src/tools/tool-elicitation.ts:6-11 (schema)Schema definition using Zod (empty object since no input arguments required) and its conversion to JSON schema for the tool's input validation.const ElicitationSchema = z.object({}); export const elicitationTool = { name: "startElicitation", description: "Demonstrates the Elicitation feature by asking the user to provide information about their favorite color, number, and pets.", inputSchema: zodToJsonSchema(ElicitationSchema),
- src/tools/index.ts:35-35 (registration)The elicitationTool is registered by inclusion in the allTools array, which is used to provide tool metadata via getTools() and dispatch handlers via getToolHandler().elicitationTool,
- src/lib/elicitation.ts:5-19 (helper)Supporting utility function that sends an 'elicitation/create' request to the server to initiate user elicitation, used within the tool handler.export const requestElicitation = async ( message: string, requestedSchema: any, server: Server ) => { const request = { method: 'elicitation/create', params: { message, requestedSchema } }; return await server.request(request, z.any()); };