Skip to content

Commit 5a48c0b

Browse files
authored
Merge pull request #2215 from dend/elicitation-support
Example showing how to use elicitation
2 parents f13adbd + 4ed232a commit 5a48c0b

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/everything/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ This MCP server attempts to exercise all the features of the MCP protocol. It is
7272
- Embedded resource with `type: "resource"`
7373
- Text instruction for using the resource URI
7474

75+
9. `startElicitation`
76+
- Initiates an elicitation (interaction) within the MCP client.
77+
- Inputs:
78+
- `color` (string): Favorite color
79+
- `number` (number, 1-100): Favorite number
80+
- `pets` (enum): Favorite pet
81+
- Returns: Confirmation of the elicitation demo with selection summary.
82+
7583
### Resources
7684

7785
The server provides 100 test resources in two formats:

src/everything/everything.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ const GetResourceReferenceSchema = z.object({
8686
.describe("ID of the resource to reference (1-100)"),
8787
});
8888

89+
const ElicitationSchema = z.object({});
90+
8991
const GetResourceLinksSchema = z.object({
9092
count: z
9193
.number()
@@ -104,6 +106,7 @@ enum ToolName {
104106
GET_TINY_IMAGE = "getTinyImage",
105107
ANNOTATED_MESSAGE = "annotatedMessage",
106108
GET_RESOURCE_REFERENCE = "getResourceReference",
109+
ELICITATION = "startElicitation",
107110
GET_RESOURCE_LINKS = "getResourceLinks",
108111
}
109112

@@ -126,6 +129,7 @@ export const createServer = () => {
126129
tools: {},
127130
logging: {},
128131
completions: {},
132+
elicitation: {},
129133
},
130134
instructions
131135
}
@@ -216,6 +220,21 @@ export const createServer = () => {
216220
return await server.request(request, CreateMessageResultSchema);
217221
};
218222

223+
const requestElicitation = async (
224+
message: string,
225+
requestedSchema: any
226+
) => {
227+
const request = {
228+
method: 'elicitation/create',
229+
params: {
230+
message,
231+
requestedSchema
232+
}
233+
};
234+
235+
return await server.request(request, z.any());
236+
};
237+
219238
const ALL_RESOURCES: Resource[] = Array.from({ length: 100 }, (_, i) => {
220239
const uri = `test://static/resource/${i + 1}`;
221240
if (i % 2 === 0) {
@@ -469,6 +488,11 @@ export const createServer = () => {
469488
"Returns a resource reference that can be used by MCP clients",
470489
inputSchema: zodToJsonSchema(GetResourceReferenceSchema) as ToolInput,
471490
},
491+
{
492+
name: ToolName.ELICITATION,
493+
description: "Demonstrates the Elicitation feature by asking the user to provide information about their favorite color, number, and pets.",
494+
inputSchema: zodToJsonSchema(ElicitationSchema) as ToolInput,
495+
},
472496
{
473497
name: ToolName.GET_RESOURCE_LINKS,
474498
description:
@@ -664,6 +688,61 @@ export const createServer = () => {
664688
return { content };
665689
}
666690

691+
if (name === ToolName.ELICITATION) {
692+
ElicitationSchema.parse(args);
693+
694+
const elicitationResult = await requestElicitation(
695+
'What are your favorite things?',
696+
{
697+
type: 'object',
698+
properties: {
699+
color: { type: 'string', description: 'Favorite color' },
700+
number: { type: 'integer', description: 'Favorite number', minimum: 1, maximum: 100 },
701+
pets: {
702+
type: 'string',
703+
enum: ['cats', 'dogs', 'birds', 'fish', 'reptiles'],
704+
description: 'Favorite pets'
705+
},
706+
}
707+
}
708+
);
709+
710+
// Handle different response actions
711+
const content = [];
712+
713+
if (elicitationResult.action === 'accept' && elicitationResult.content) {
714+
content.push({
715+
type: "text",
716+
text: `✅ User provided their favorite things!`,
717+
});
718+
719+
// Only access elicitationResult.content when action is accept
720+
const { color, number, pets } = elicitationResult.content;
721+
content.push({
722+
type: "text",
723+
text: `Their favorites are:\n- Color: ${color || 'not specified'}\n- Number: ${number || 'not specified'}\n- Pets: ${pets || 'not specified'}`,
724+
});
725+
} else if (elicitationResult.action === 'decline') {
726+
content.push({
727+
type: "text",
728+
text: `❌ User declined to provide their favorite things.`,
729+
});
730+
} else if (elicitationResult.action === 'cancel') {
731+
content.push({
732+
type: "text",
733+
text: `⚠️ User cancelled the elicitation dialog.`,
734+
});
735+
}
736+
737+
// Include raw result for debugging
738+
content.push({
739+
type: "text",
740+
text: `\nRaw result: ${JSON.stringify(elicitationResult, null, 2)}`,
741+
});
742+
743+
return { content };
744+
}
745+
667746
if (name === ToolName.GET_RESOURCE_LINKS) {
668747
const { count } = GetResourceLinksSchema.parse(args);
669748
const content = [];

0 commit comments

Comments
 (0)