Elicitation support

This commit is contained in:
Den Delimarsky 2025-06-23 15:54:45 -07:00
commit 72714ed0f1

View file

@ -86,6 +86,14 @@ const GetResourceReferenceSchema = z.object({
.describe("ID of the resource to reference (1-100)"), .describe("ID of the resource to reference (1-100)"),
}); });
const ElicitationSchema = z.object({
message: z.string().describe("Message to use for elicitation"),
includeSchema: z
.boolean()
.default(true)
.describe("Whether to include the favorite things schema"),
});
enum ToolName { enum ToolName {
ECHO = "echo", ECHO = "echo",
ADD = "add", ADD = "add",
@ -95,6 +103,7 @@ enum ToolName {
GET_TINY_IMAGE = "getTinyImage", GET_TINY_IMAGE = "getTinyImage",
ANNOTATED_MESSAGE = "annotatedMessage", ANNOTATED_MESSAGE = "annotatedMessage",
GET_RESOURCE_REFERENCE = "getResourceReference", GET_RESOURCE_REFERENCE = "getResourceReference",
ELICITATION_DEMO = "elicitationDemo",
} }
enum PromptName { enum PromptName {
@ -109,13 +118,13 @@ export const createServer = () => {
name: "example-servers/everything", name: "example-servers/everything",
version: "1.0.0", version: "1.0.0",
}, },
{ { capabilities: {
capabilities: {
prompts: {}, prompts: {},
resources: { subscribe: true }, resources: { subscribe: true },
tools: {}, tools: {},
logging: {}, logging: {},
completions: {}, completions: {},
elicitation: {},
}, },
instructions instructions
} }
@ -206,6 +215,22 @@ export const createServer = () => {
return await server.request(request, CreateMessageResultSchema); return await server.request(request, CreateMessageResultSchema);
}; };
// Helper method to make elicitation requests
const requestElicitation = async (
message: string,
requestedSchema: any
) => {
const request = {
method: 'elicitation/create',
params: {
message,
requestedSchema
}
};
return await server.request(request, z.any());
};
const ALL_RESOURCES: Resource[] = Array.from({ length: 100 }, (_, i) => { const ALL_RESOURCES: Resource[] = Array.from({ length: 100 }, (_, i) => {
const uri = `test://static/resource/${i + 1}`; const uri = `test://static/resource/${i + 1}`;
if (i % 2 === 0) { if (i % 2 === 0) {
@ -459,6 +484,11 @@ export const createServer = () => {
"Returns a resource reference that can be used by MCP clients", "Returns a resource reference that can be used by MCP clients",
inputSchema: zodToJsonSchema(GetResourceReferenceSchema) as ToolInput, inputSchema: zodToJsonSchema(GetResourceReferenceSchema) as ToolInput,
}, },
{
name: ToolName.ELICITATION_DEMO,
description: "Demonstrates the elicitation feature",
inputSchema: zodToJsonSchema(ElicitationSchema) as ToolInput,
},
]; ];
return { tools }; return { tools };
@ -648,6 +678,40 @@ export const createServer = () => {
return { content }; return { content };
} }
if (name === ToolName.ELICITATION_DEMO) {
const { message, includeSchema } = ElicitationSchema.parse(args);
// Make the elicitation request like in your example
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 },
things: {
type: 'string',
enum: ['cats', 'dogs', 'birds', 'brown paper packages tied up with string'],
description: 'Favorite things'
},
}
}
);
return {
content: [
{
type: "text",
text: `Elicitation demo completed! Message: ${message}`,
},
{
type: "text",
text: `Elicitation result: ${JSON.stringify(elicitationResult, null, 2)}`,
},
],
};
}
throw new Error(`Unknown tool: ${name}`); throw new Error(`Unknown tool: ${name}`);
}); });