Skip to content

Commit 9c5a4d4

Browse files
ochafikclaude
andcommitted
Add elicitInputs tool to MCP server
Adds a new elicitInputs tool that demonstrates how to use MCP's elicitation feature to request user input. The tool showcases various input field types including strings, booleans, emails, dates, numbers, and enums. This allows clients to interactively collect structured user input during tool execution. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 22c7f74 commit 9c5a4d4

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

mcp-server/src/services/mcp.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
CompleteRequestSchema,
55
CreateMessageRequest,
66
CreateMessageResultSchema,
7+
ElicitResultSchema,
78
GetPromptRequestSchema,
89
ListPromptsRequestSchema,
910
ListResourcesRequestSchema,
@@ -79,6 +80,8 @@ const GetResourceReferenceSchema = z.object({
7980
.describe("ID of the resource to reference (1-100)"),
8081
});
8182

83+
const ElicitInputsSchema = z.object({});
84+
8285
enum ToolName {
8386
ECHO = "echo",
8487
ADD = "add",
@@ -87,6 +90,7 @@ enum ToolName {
8790
GET_TINY_IMAGE = "getTinyImage",
8891
ANNOTATED_MESSAGE = "annotatedMessage",
8992
GET_RESOURCE_REFERENCE = "getResourceReference",
93+
ELICIT_INPUTS = "elicitInputs",
9094
}
9195

9296
enum PromptName {
@@ -446,6 +450,12 @@ export const createMcpServer = (): McpServerWrapper => {
446450
"Returns a resource reference that can be used by MCP clients",
447451
inputSchema: zodToJsonSchema(GetResourceReferenceSchema) as ToolInput,
448452
},
453+
{
454+
name: ToolName.ELICIT_INPUTS,
455+
description:
456+
"Elicitation test tool that demonstrates how to request user input with various field types",
457+
inputSchema: zodToJsonSchema(ElicitInputsSchema) as ToolInput,
458+
},
449459
];
450460

451461
return { tools };
@@ -624,6 +634,96 @@ export const createMcpServer = (): McpServerWrapper => {
624634
return { content };
625635
}
626636

637+
if (name === ToolName.ELICIT_INPUTS) {
638+
ElicitInputsSchema.parse(args);
639+
640+
// Call elicitInput on the server to request user input
641+
const result = await server.request(
642+
{
643+
method: "sampling/elicitInput",
644+
params: {
645+
message: "Please provide inputs for the following fields:",
646+
requestedSchema: {
647+
type: "object",
648+
properties: {
649+
name: {
650+
title: "Full Name",
651+
type: "string",
652+
description: "Your full, legal name",
653+
},
654+
check: {
655+
title: "Agree to terms",
656+
type: "boolean",
657+
description: "A boolean check",
658+
},
659+
color: {
660+
title: "Favorite Color",
661+
type: "string",
662+
description: "Favorite color (open text)",
663+
default: "blue",
664+
},
665+
email: {
666+
title: "Email Address",
667+
type: "string",
668+
format: "email",
669+
description:
670+
"Your email address (will be verified, and never shared with anyone else)",
671+
},
672+
homepage: {
673+
type: "string",
674+
format: "uri",
675+
description: "Homepage / personal site",
676+
},
677+
birthdate: {
678+
title: "Birthdate",
679+
type: "string",
680+
format: "date",
681+
description:
682+
"Your date of birth (will never be shared with anyone else)",
683+
},
684+
integer: {
685+
title: "Favorite Integer",
686+
type: "integer",
687+
description:
688+
"Your favorite integer (do not give us your phone number, pin, or other sensitive info)",
689+
minimum: 1,
690+
maximum: 100,
691+
default: 42,
692+
},
693+
number: {
694+
title: "Favorite Number",
695+
type: "number",
696+
description: "Favorite number (there are no wrong answers)",
697+
minimum: 0,
698+
maximum: 1000,
699+
default: 3.14,
700+
},
701+
petType: {
702+
title: "Pet type",
703+
type: "string",
704+
enum: ["cats", "dogs", "birds", "fish", "reptiles"],
705+
enumNames: ["Cats", "Dogs", "Birds", "Fish", "Reptiles"],
706+
default: "dogs",
707+
description: "Your favorite pet type",
708+
},
709+
},
710+
required: ["name"],
711+
},
712+
},
713+
},
714+
ElicitResultSchema
715+
);
716+
717+
return {
718+
content: [
719+
{
720+
type: "text",
721+
text: `Elicitation result: ${JSON.stringify(result, null, 2)}`,
722+
},
723+
],
724+
};
725+
}
726+
627727
throw new Error(`Unknown tool: ${name}`);
628728
});
629729

0 commit comments

Comments
 (0)