You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+103Lines changed: 103 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -849,6 +849,109 @@ const transport = new StdioServerTransport();
849
849
awaitserver.connect(transport);
850
850
```
851
851
852
+
### Eliciting User Input
853
+
854
+
MCP servers can request additional information from users through the elicitation feature. This is useful for interactive workflows where the server needs user input or confirmation:
855
+
856
+
```typescript
857
+
// Server-side: Restaurant booking tool that asks for alternatives
858
+
server.tool(
859
+
"book-restaurant",
860
+
{
861
+
restaurant: z.string(),
862
+
date: z.string(),
863
+
partySize: z.number()
864
+
},
865
+
async ({ restaurant, date, partySize }) => {
866
+
// Check availability
867
+
const available =awaitcheckAvailability(restaurant, date, partySize);
868
+
869
+
if (!available) {
870
+
// Ask user if they want to try alternative dates
871
+
const result =awaitserver.server.elicitInput({
872
+
message: `No tables available at ${restaurant} on ${date}. Would you like to check alternative dates?`,
873
+
requestedSchema: {
874
+
type: "object",
875
+
properties: {
876
+
checkAlternatives: {
877
+
type: "boolean",
878
+
title: "Check alternative dates",
879
+
description: "Would you like me to check other dates?"
880
+
},
881
+
flexibleDates: {
882
+
type: "string",
883
+
title: "Date flexibility",
884
+
description: "How flexible are your dates?",
885
+
enum: ["next_day", "same_week", "next_week"],
886
+
enumNames: ["Next day", "Same week", "Next week"]
887
+
}
888
+
},
889
+
required: ["checkAlternatives"]
890
+
}
891
+
});
892
+
893
+
if (result.action==="accept"&&result.content?.checkAlternatives) {
894
+
const alternatives =awaitfindAlternatives(
895
+
restaurant,
896
+
date,
897
+
partySize,
898
+
result.content.flexibleDatesasstring
899
+
);
900
+
return {
901
+
content: [{
902
+
type: "text",
903
+
text: `Found these alternatives: ${alternatives.join(", ")}`
904
+
}]
905
+
};
906
+
}
907
+
908
+
return {
909
+
content: [{
910
+
type: "text",
911
+
text: "No booking made. Original date not available."
912
+
}]
913
+
};
914
+
}
915
+
916
+
// Book the table
917
+
awaitmakeBooking(restaurant, date, partySize);
918
+
return {
919
+
content: [{
920
+
type: "text",
921
+
text: `Booked table for ${partySize} at ${restaurant} on ${date}`
922
+
}]
923
+
};
924
+
}
925
+
);
926
+
```
927
+
928
+
Client-side: Handle elicitation requests
929
+
930
+
```typescript
931
+
// This is a placeholder - implement based on your UI framework
0 commit comments