mirror of
https://github.com/modelcontextprotocol/servers.git
synced 2026-04-20 12:55:36 +02:00
Merge pull request #2863 from modelcontextprotocol/ochafik/port-pr17-elicitation
everything: add more types to startElicitation tool
This commit is contained in:
@@ -6,7 +6,6 @@ import {
|
|||||||
CompleteRequestSchema,
|
CompleteRequestSchema,
|
||||||
CreateMessageRequest,
|
CreateMessageRequest,
|
||||||
CreateMessageResultSchema,
|
CreateMessageResultSchema,
|
||||||
ElicitRequest,
|
|
||||||
ElicitResultSchema,
|
ElicitResultSchema,
|
||||||
GetPromptRequestSchema,
|
GetPromptRequestSchema,
|
||||||
ListPromptsRequestSchema,
|
ListPromptsRequestSchema,
|
||||||
@@ -259,22 +258,6 @@ export const createServer = () => {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const requestElicitation = async (
|
|
||||||
message: string,
|
|
||||||
requestedSchema: any,
|
|
||||||
sendRequest: SendRequest
|
|
||||||
) => {
|
|
||||||
const request: ElicitRequest = {
|
|
||||||
method: 'elicitation/create',
|
|
||||||
params: {
|
|
||||||
message,
|
|
||||||
requestedSchema,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return await sendRequest(request, ElicitResultSchema);
|
|
||||||
};
|
|
||||||
|
|
||||||
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) {
|
||||||
@@ -552,7 +535,7 @@ export const createServer = () => {
|
|||||||
});
|
});
|
||||||
if (clientCapabilities!.elicitation) tools.push ({
|
if (clientCapabilities!.elicitation) tools.push ({
|
||||||
name: ToolName.ELICITATION,
|
name: ToolName.ELICITATION,
|
||||||
description: "Demonstrates the Elicitation feature by asking the user to provide information about their favorite color, number, and pets.",
|
description: "Elicitation test tool that demonstrates how to request user input with various field types (string, boolean, email, uri, date, integer, number, enum)",
|
||||||
inputSchema: zodToJsonSchema(ElicitationSchema) as ToolInput,
|
inputSchema: zodToJsonSchema(ElicitationSchema) as ToolInput,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -747,27 +730,75 @@ export const createServer = () => {
|
|||||||
if (name === ToolName.ELICITATION) {
|
if (name === ToolName.ELICITATION) {
|
||||||
ElicitationSchema.parse(args);
|
ElicitationSchema.parse(args);
|
||||||
|
|
||||||
const elicitationResult = await requestElicitation(
|
const elicitationResult = await extra.sendRequest({
|
||||||
'What are your favorite things?',
|
method: 'elicitation/create',
|
||||||
{
|
params: {
|
||||||
|
message: 'Please provide inputs for the following fields:',
|
||||||
|
requestedSchema: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
color: { type: 'string', description: 'Favorite color' },
|
name: {
|
||||||
number: {
|
title: 'Full Name',
|
||||||
|
type: 'string',
|
||||||
|
description: 'Your full, legal name',
|
||||||
|
},
|
||||||
|
check: {
|
||||||
|
title: 'Agree to terms',
|
||||||
|
type: 'boolean',
|
||||||
|
description: 'A boolean check',
|
||||||
|
},
|
||||||
|
color: {
|
||||||
|
title: 'Favorite Color',
|
||||||
|
type: 'string',
|
||||||
|
description: 'Favorite color (open text)',
|
||||||
|
default: 'blue',
|
||||||
|
},
|
||||||
|
email: {
|
||||||
|
title: 'Email Address',
|
||||||
|
type: 'string',
|
||||||
|
format: 'email',
|
||||||
|
description: 'Your email address (will be verified, and never shared with anyone else)',
|
||||||
|
},
|
||||||
|
homepage: {
|
||||||
|
type: 'string',
|
||||||
|
format: 'uri',
|
||||||
|
description: 'Homepage / personal site',
|
||||||
|
},
|
||||||
|
birthdate: {
|
||||||
|
title: 'Birthdate',
|
||||||
|
type: 'string',
|
||||||
|
format: 'date',
|
||||||
|
description: 'Your date of birth (will never be shared with anyone else)',
|
||||||
|
},
|
||||||
|
integer: {
|
||||||
|
title: 'Favorite Integer',
|
||||||
type: 'integer',
|
type: 'integer',
|
||||||
description: 'Favorite number',
|
description: 'Your favorite integer (do not give us your phone number, pin, or other sensitive info)',
|
||||||
minimum: 1,
|
minimum: 1,
|
||||||
maximum: 100,
|
maximum: 100,
|
||||||
|
default: 42,
|
||||||
},
|
},
|
||||||
pets: {
|
number: {
|
||||||
|
title: 'Favorite Number',
|
||||||
|
type: 'number',
|
||||||
|
description: 'Favorite number (there are no wrong answers)',
|
||||||
|
minimum: 0,
|
||||||
|
maximum: 1000,
|
||||||
|
default: 3.14,
|
||||||
|
},
|
||||||
|
petType: {
|
||||||
|
title: 'Pet type',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
enum: ['cats', 'dogs', 'birds', 'fish', 'reptiles'],
|
enum: ['cats', 'dogs', 'birds', 'fish', 'reptiles'],
|
||||||
description: 'Favorite pets',
|
enumNames: ['Cats', 'Dogs', 'Birds', 'Fish', 'Reptiles'],
|
||||||
|
default: 'dogs',
|
||||||
|
description: 'Your favorite pet type',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
required: ['name'],
|
||||||
},
|
},
|
||||||
extra.sendRequest
|
},
|
||||||
);
|
}, ElicitResultSchema, { timeout: 10 * 60 * 1000 /* 10 minutes */ });
|
||||||
|
|
||||||
// Handle different response actions
|
// Handle different response actions
|
||||||
const content = [];
|
const content = [];
|
||||||
@@ -775,19 +806,30 @@ export const createServer = () => {
|
|||||||
if (elicitationResult.action === 'accept' && elicitationResult.content) {
|
if (elicitationResult.action === 'accept' && elicitationResult.content) {
|
||||||
content.push({
|
content.push({
|
||||||
type: "text",
|
type: "text",
|
||||||
text: `✅ User provided their favorite things!`,
|
text: `✅ User provided the requested information!`,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Only access elicitationResult.content when action is accept
|
// Only access elicitationResult.content when action is accept
|
||||||
const { color, number, pets } = elicitationResult.content;
|
const userData = elicitationResult.content;
|
||||||
|
const lines = [];
|
||||||
|
if (userData.name) lines.push(`- Name: ${userData.name}`);
|
||||||
|
if (userData.check !== undefined) lines.push(`- Agreed to terms: ${userData.check}`);
|
||||||
|
if (userData.color) lines.push(`- Favorite Color: ${userData.color}`);
|
||||||
|
if (userData.email) lines.push(`- Email: ${userData.email}`);
|
||||||
|
if (userData.homepage) lines.push(`- Homepage: ${userData.homepage}`);
|
||||||
|
if (userData.birthdate) lines.push(`- Birthdate: ${userData.birthdate}`);
|
||||||
|
if (userData.integer !== undefined) lines.push(`- Favorite Integer: ${userData.integer}`);
|
||||||
|
if (userData.number !== undefined) lines.push(`- Favorite Number: ${userData.number}`);
|
||||||
|
if (userData.petType) lines.push(`- Pet Type: ${userData.petType}`);
|
||||||
|
|
||||||
content.push({
|
content.push({
|
||||||
type: "text",
|
type: "text",
|
||||||
text: `Their favorites are:\n- Color: ${color || 'not specified'}\n- Number: ${number || 'not specified'}\n- Pets: ${pets || 'not specified'}`,
|
text: `User inputs:\n${lines.join('\n')}`,
|
||||||
});
|
});
|
||||||
} else if (elicitationResult.action === 'decline') {
|
} else if (elicitationResult.action === 'decline') {
|
||||||
content.push({
|
content.push({
|
||||||
type: "text",
|
type: "text",
|
||||||
text: `❌ User declined to provide their favorite things.`,
|
text: `❌ User declined to provide the requested information.`,
|
||||||
});
|
});
|
||||||
} else if (elicitationResult.action === 'cancel') {
|
} else if (elicitationResult.action === 'cancel') {
|
||||||
content.push({
|
content.push({
|
||||||
|
|||||||
Reference in New Issue
Block a user