@modelcontextprotocol/server-everything 0.6.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +140 -76
- package/dist/docs/architecture.md +45 -0
- package/dist/docs/extension.md +25 -0
- package/dist/docs/features.md +99 -0
- package/dist/docs/how-it-works.md +60 -0
- package/dist/docs/instructions.md +44 -0
- package/dist/docs/startup.md +71 -0
- package/dist/docs/structure.md +246 -0
- package/dist/index.js +73 -16
- package/dist/prompts/args.js +34 -0
- package/dist/prompts/completions.js +52 -0
- package/dist/prompts/index.js +15 -0
- package/dist/prompts/resource.js +61 -0
- package/dist/prompts/simple.js +23 -0
- package/dist/resources/files.js +91 -0
- package/dist/resources/index.js +38 -0
- package/dist/resources/minted.js +119 -0
- package/dist/resources/session.js +62 -0
- package/dist/resources/subscriptions.js +66 -0
- package/dist/resources/templates.js +167 -0
- package/dist/server/context.js +47 -0
- package/dist/server/index.js +153 -0
- package/dist/server/logging.js +43 -0
- package/dist/server/roots.js +49 -0
- package/dist/server/tasks.js +295 -0
- package/dist/tools/echo.js +35 -0
- package/dist/tools/get-annotated-message.js +89 -0
- package/dist/tools/get-env.js +34 -0
- package/dist/tools/get-header-params.js +95 -0
- package/dist/tools/get-resource-links.js +68 -0
- package/dist/tools/get-resource-reference.js +81 -0
- package/dist/tools/get-roots-list.js +76 -0
- package/dist/tools/get-structured-content.js +78 -0
- package/dist/tools/get-sum.js +46 -0
- package/dist/tools/get-tiny-image.js +47 -0
- package/dist/tools/gzip-file-as-resource.js +213 -0
- package/dist/tools/index.js +110 -0
- package/dist/tools/simulate-research-query.js +216 -0
- package/dist/tools/toggle-simulated-logging.js +60 -0
- package/dist/tools/toggle-subscriber-updates.js +60 -0
- package/dist/tools/trigger-elicitation-request-async.js +122 -0
- package/dist/tools/trigger-elicitation-request.js +223 -0
- package/dist/tools/trigger-list-changed.js +57 -0
- package/dist/tools/trigger-log-messages.js +60 -0
- package/dist/tools/trigger-long-running-operation.js +65 -0
- package/dist/tools/trigger-multi-step-elicitation.js +125 -0
- package/dist/tools/trigger-resource-update.js +52 -0
- package/dist/tools/trigger-sampling-request-async.js +110 -0
- package/dist/tools/trigger-sampling-request.js +83 -0
- package/dist/tools/trigger-url-elicitation.js +206 -0
- package/dist/transports/in-memory-event-store.js +44 -0
- package/dist/transports/listen.js +36 -0
- package/dist/transports/sse.js +112 -0
- package/dist/transports/stdio.js +68 -0
- package/dist/transports/streamableHttp.js +326 -0
- package/dist/version.js +35 -0
- package/package.json +39 -13
- package/dist/everything.js +0 -361
- package/dist/sse.js +0 -24
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// The `simulate-research-query` tool: a simulated deep-research job that runs
|
|
2
|
+
// as a task on the Tasks extension (`io.modelcontextprotocol/tasks`), on
|
|
3
|
+
// protocol version 2026-07-28 (#4852).
|
|
4
|
+
//
|
|
5
|
+
// The call returns a task handle at once. The task works through four
|
|
6
|
+
// stages, one a second, and the client follows it with `tasks/get`. For an
|
|
7
|
+
// ambiguous topic it stops at the third stage with status `input_required`,
|
|
8
|
+
// carrying an elicitation that asks which interpretation the user means; the
|
|
9
|
+
// client answers it with `tasks/update` and the task carries on. The final
|
|
10
|
+
// report arrives inside the `completed` status. The task's progress is worked
|
|
11
|
+
// out from the clock when it is read (server/tasks.ts), so nothing runs in
|
|
12
|
+
// the background.
|
|
13
|
+
//
|
|
14
|
+
// SDK v2 removed the experimental 2025-11-25 tasks layer this tool was first
|
|
15
|
+
// built on (SEP-1686), so it is registered for 2026-07-28 clients only.
|
|
16
|
+
import { acceptedContent, inputRequired, inputResponse, } from "@modelcontextprotocol/server";
|
|
17
|
+
import { z } from "zod";
|
|
18
|
+
import { clientSupportsFormElicitation, createTask, } from "../server/tasks.js";
|
|
19
|
+
// Tool input schema
|
|
20
|
+
const SimulateResearchQuerySchema = z.object({
|
|
21
|
+
topic: z
|
|
22
|
+
.string()
|
|
23
|
+
.max(1000)
|
|
24
|
+
.describe("The research topic to investigate (at most 1,000 characters; the task keeps it until it expires)"),
|
|
25
|
+
ambiguous: z
|
|
26
|
+
.boolean()
|
|
27
|
+
.default(false)
|
|
28
|
+
.describe("Simulate an ambiguous query that requires clarification (triggers input_required status)"),
|
|
29
|
+
});
|
|
30
|
+
/** The stages a research task works through, in order. */
|
|
31
|
+
export const STAGES = [
|
|
32
|
+
"Gathering sources",
|
|
33
|
+
"Analyzing content",
|
|
34
|
+
"Synthesizing findings",
|
|
35
|
+
"Generating report",
|
|
36
|
+
];
|
|
37
|
+
/** How long each stage takes, in milliseconds. */
|
|
38
|
+
export const STAGE_DURATION = 1000;
|
|
39
|
+
/** The stage an ambiguous query stops before, to ask for a clarification. */
|
|
40
|
+
const CLARIFY_AT = 2;
|
|
41
|
+
// Tool configuration
|
|
42
|
+
const name = "simulate-research-query";
|
|
43
|
+
const config = {
|
|
44
|
+
title: "Simulate Research Query",
|
|
45
|
+
description: "Simulates a deep research operation that gathers, analyzes, and synthesizes information. " +
|
|
46
|
+
"Runs as a task on the io.modelcontextprotocol/tasks extension (protocol version 2026-07-28): " +
|
|
47
|
+
"the call returns a task handle, and the client polls tasks/get as the task moves through four stages. " +
|
|
48
|
+
"If 'ambiguous' is true and the client supports elicitation, the task pauses with status " +
|
|
49
|
+
"input_required until the client answers its clarification request with tasks/update.",
|
|
50
|
+
inputSchema: SimulateResearchQuerySchema,
|
|
51
|
+
annotations: {
|
|
52
|
+
// Each call creates a task in the process-wide task store.
|
|
53
|
+
readOnlyHint: false,
|
|
54
|
+
destructiveHint: false,
|
|
55
|
+
idempotentHint: false,
|
|
56
|
+
openWorldHint: false,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Returns contextual interpretation options based on the topic.
|
|
61
|
+
*/
|
|
62
|
+
function getInterpretationsForTopic(topic) {
|
|
63
|
+
const lowerTopic = topic.toLowerCase();
|
|
64
|
+
if (lowerTopic.includes("python")) {
|
|
65
|
+
return [
|
|
66
|
+
{ const: "programming", title: "Python programming language" },
|
|
67
|
+
{ const: "snake", title: "Python snake species" },
|
|
68
|
+
{ const: "comedy", title: "Monty Python comedy group" },
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
return [
|
|
72
|
+
{ const: "technical", title: "Technical/scientific perspective" },
|
|
73
|
+
{ const: "historical", title: "Historical perspective" },
|
|
74
|
+
{ const: "current", title: "Current events/news perspective" },
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* What the user's answer to the clarification request means. An accepted
|
|
79
|
+
* answer counts only if it is one of the interpretations offered for `topic`.
|
|
80
|
+
*/
|
|
81
|
+
function clarificationFrom(responses, topic) {
|
|
82
|
+
const offered = getInterpretationsForTopic(topic).map((o) => o.const);
|
|
83
|
+
const ClarificationSchema = z.object({
|
|
84
|
+
interpretation: z.string().refine((value) => offered.includes(value)),
|
|
85
|
+
});
|
|
86
|
+
const view = inputResponse(responses, "clarification");
|
|
87
|
+
const accepted = acceptedContent(responses, "clarification", ClarificationSchema);
|
|
88
|
+
if (accepted)
|
|
89
|
+
return accepted.interpretation;
|
|
90
|
+
if (view.kind === "elicit" && view.action === "decline") {
|
|
91
|
+
return "User declined - using default interpretation";
|
|
92
|
+
}
|
|
93
|
+
if (view.kind === "elicit" && view.action === "cancel") {
|
|
94
|
+
return "User cancelled - using default interpretation";
|
|
95
|
+
}
|
|
96
|
+
return "No usable answer - using default interpretation";
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Generates the final research report with educational content about tasks.
|
|
100
|
+
*/
|
|
101
|
+
function generateResearchReport(topic, clarification) {
|
|
102
|
+
const subject = clarification ? `${topic} (${clarification})` : topic;
|
|
103
|
+
const report = `# Research Report: ${subject}
|
|
104
|
+
|
|
105
|
+
## Research Parameters
|
|
106
|
+
- **Topic**: ${topic}
|
|
107
|
+
${clarification ? `- **Clarification**: ${clarification}` : ""}
|
|
108
|
+
|
|
109
|
+
## Synthesis
|
|
110
|
+
This research query was processed through ${STAGES.length} stages:
|
|
111
|
+
${STAGES.map((s, i) => `- Stage ${i + 1}: ${s} ✓`).join("\n")}
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## About This Demo (SEP-2663: the Tasks extension)
|
|
116
|
+
|
|
117
|
+
This tool demonstrates MCP's task-based execution pattern for long-running operations:
|
|
118
|
+
|
|
119
|
+
**Task Lifecycle Demonstrated:**
|
|
120
|
+
1. \`tools/call\` → the server returns a task handle (\`resultType: "task"\`), not the final result
|
|
121
|
+
2. Client polls \`tasks/get\` → the server returns the current status and \`statusMessage\`
|
|
122
|
+
3. Status progressed: \`working\` → ${clarification ? `\`input_required\` → \`working\` → ` : ""}\`completed\`
|
|
123
|
+
4. The \`completed\` status carries this final result
|
|
124
|
+
${clarification
|
|
125
|
+
? `
|
|
126
|
+
**Input Flow:**
|
|
127
|
+
The query was ambiguous, so the task paused with status \`input_required\`,
|
|
128
|
+
carrying an elicitation request. The client answered it with \`tasks/update\`
|
|
129
|
+
("${clarification}"), and the task resumed and completed.
|
|
130
|
+
`
|
|
131
|
+
: ""}
|
|
132
|
+
**Key Concepts:**
|
|
133
|
+
- Tasks enable "call now, fetch later" patterns
|
|
134
|
+
- \`statusMessage\` provides human-readable progress updates
|
|
135
|
+
- Tasks have a TTL (\`ttlMs\`) for automatic cleanup
|
|
136
|
+
- \`pollIntervalMs\` suggests how often to check status
|
|
137
|
+
- A task asks for input in-band, through its \`input_required\` status
|
|
138
|
+
|
|
139
|
+
*This is a simulated research report from the Everything MCP Server.*
|
|
140
|
+
`;
|
|
141
|
+
return { content: [{ type: "text", text: report }] };
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Registers the 'simulate-research-query' tool, for 2026-07-28 clients.
|
|
145
|
+
*
|
|
146
|
+
* A client that does not declare the tasks extension is refused with -32021
|
|
147
|
+
* before the tool runs (`guardTaskTools`), since it could not follow the task
|
|
148
|
+
* handle. `ambiguous` is ignored for a
|
|
149
|
+
* client that does not support elicitation.
|
|
150
|
+
*
|
|
151
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
152
|
+
*/
|
|
153
|
+
export const registerSimulateResearchQueryTool = (server) => {
|
|
154
|
+
server.registerTool(name, config, async (args, ctx) => {
|
|
155
|
+
const { topic, ambiguous } = SimulateResearchQuerySchema.parse(args);
|
|
156
|
+
// The clarification is a form, so only a form-capable client is asked.
|
|
157
|
+
const canAsk = clientSupportsFormElicitation(ctx);
|
|
158
|
+
// The clock time the stages started counting from, and the stage they
|
|
159
|
+
// started at: an ambiguous query restarts the count once it is answered.
|
|
160
|
+
let startedAt = Date.now();
|
|
161
|
+
let startStage = 0;
|
|
162
|
+
let clarification;
|
|
163
|
+
const needsClarification = () => ambiguous && canAsk && clarification === undefined;
|
|
164
|
+
return createTask({
|
|
165
|
+
stateAt: (now) => {
|
|
166
|
+
const stage = startStage + Math.floor((now - startedAt) / STAGE_DURATION);
|
|
167
|
+
// When the clock reached `stage`, the moment the task entered it.
|
|
168
|
+
const reached = (at) => startedAt + (at - startStage) * STAGE_DURATION;
|
|
169
|
+
if (needsClarification() && stage >= CLARIFY_AT) {
|
|
170
|
+
return {
|
|
171
|
+
since: reached(CLARIFY_AT),
|
|
172
|
+
status: "input_required",
|
|
173
|
+
statusMessage: `Found multiple interpretations for "${topic}". Requesting clarification...`,
|
|
174
|
+
inputRequests: {
|
|
175
|
+
clarification: inputRequired.elicit({
|
|
176
|
+
message: `The research query "${topic}" could have multiple interpretations. Please clarify what you're looking for:`,
|
|
177
|
+
requestedSchema: {
|
|
178
|
+
type: "object",
|
|
179
|
+
properties: {
|
|
180
|
+
interpretation: {
|
|
181
|
+
type: "string",
|
|
182
|
+
title: "Clarification",
|
|
183
|
+
description: "Which interpretation of the topic do you mean?",
|
|
184
|
+
oneOf: getInterpretationsForTopic(topic),
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
required: ["interpretation"],
|
|
188
|
+
},
|
|
189
|
+
}),
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
if (stage >= STAGES.length) {
|
|
194
|
+
return {
|
|
195
|
+
since: reached(STAGES.length),
|
|
196
|
+
status: "completed",
|
|
197
|
+
statusMessage: "Research complete",
|
|
198
|
+
result: generateResearchReport(topic, clarification),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
since: reached(stage),
|
|
203
|
+
status: "working",
|
|
204
|
+
statusMessage: clarification !== undefined && stage === CLARIFY_AT
|
|
205
|
+
? `Continuing with interpretation: "${clarification}"...`
|
|
206
|
+
: `${STAGES[stage]}...`,
|
|
207
|
+
};
|
|
208
|
+
},
|
|
209
|
+
receive: (responses, now) => {
|
|
210
|
+
clarification = clarificationFrom(responses, topic);
|
|
211
|
+
startedAt = now;
|
|
212
|
+
startStage = CLARIFY_AT;
|
|
213
|
+
},
|
|
214
|
+
}, { ttlMs: 5 * 60_000, pollIntervalMs: STAGE_DURATION });
|
|
215
|
+
});
|
|
216
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// The `toggle-simulated-logging` tool: starts or stops a timer that sends the
|
|
2
|
+
// client a random-leveled log message every five seconds, filtered by the
|
|
3
|
+
// level it set with `logging/setLevel`.
|
|
4
|
+
//
|
|
5
|
+
// 2025-era only, and deprecated: the 2026-07-28 revision removes
|
|
6
|
+
// `logging/setLevel` and unsolicited log messages (and deprecates logging),
|
|
7
|
+
// so the server factory registers this tool only for a 2025-era client.
|
|
8
|
+
// `trigger-log-messages` shows the 2026-07-28 replacement, a log level set
|
|
9
|
+
// per request. The timer belongs to the server instance serving this client,
|
|
10
|
+
// so no session map is needed (#4852).
|
|
11
|
+
import { beginSimulatedLogging } from "../server/logging.js";
|
|
12
|
+
// Tool configuration
|
|
13
|
+
const name = "toggle-simulated-logging";
|
|
14
|
+
const config = {
|
|
15
|
+
title: "Toggle Simulated Logging",
|
|
16
|
+
description: "Toggles simulated, random-leveled logging on or off. Deprecated: 2025-era clients only, since " +
|
|
17
|
+
"protocol version 2026-07-28 removes logging/setLevel; use trigger-log-messages with a per-request " +
|
|
18
|
+
"log level instead.",
|
|
19
|
+
inputSchema: {},
|
|
20
|
+
annotations: {
|
|
21
|
+
readOnlyHint: false,
|
|
22
|
+
destructiveHint: false,
|
|
23
|
+
idempotentHint: false,
|
|
24
|
+
openWorldHint: false,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Registers the `toggle-simulated-logging` tool.
|
|
29
|
+
*
|
|
30
|
+
* The registered tool enables or disables the sending of periodic, random-leveled
|
|
31
|
+
* logging messages to the connected client. Each call flips the state: it
|
|
32
|
+
* starts the messages when they are off and stops them when they are on. The
|
|
33
|
+
* messages also stop when the connection ends.
|
|
34
|
+
*
|
|
35
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
36
|
+
* @param {ServerContext} context - The instance's cleanup registry.
|
|
37
|
+
*/
|
|
38
|
+
export const registerToggleSimulatedLoggingTool = (server, context) => {
|
|
39
|
+
let stop;
|
|
40
|
+
context.onCleanup(() => {
|
|
41
|
+
stop?.();
|
|
42
|
+
stop = undefined;
|
|
43
|
+
});
|
|
44
|
+
server.registerTool(name, config, async () => {
|
|
45
|
+
let response;
|
|
46
|
+
if (stop) {
|
|
47
|
+
stop();
|
|
48
|
+
stop = undefined;
|
|
49
|
+
response = "Stopped simulated logging";
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
stop = beginSimulatedLogging(server);
|
|
53
|
+
response =
|
|
54
|
+
"Started simulated, random-leveled logging at a 5 second pace. Client's selected logging level will be respected. If an interval elapses and the message to be sent is below the selected level, it will not be sent. Thus at higher chosen logging levels, messages should arrive further apart.";
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
content: [{ type: "text", text: response }],
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// The `toggle-subscriber-updates` tool: starts or stops a timer that sends
|
|
2
|
+
// `notifications/resources/updated` for each resource the client subscribed
|
|
3
|
+
// to with `resources/subscribe`.
|
|
4
|
+
//
|
|
5
|
+
// 2025-era only, and deprecated: the 2026-07-28 revision removes
|
|
6
|
+
// `resources/subscribe` and unsolicited notifications, so the server factory
|
|
7
|
+
// registers this tool only for a 2025-era client. A 2026-07-28 client asks
|
|
8
|
+
// for updates in its `subscriptions/listen` filter, and
|
|
9
|
+
// `trigger-resource-update` sends one on demand. The timer belongs to the
|
|
10
|
+
// server instance serving this client, so no session map is needed (#4852).
|
|
11
|
+
import { beginSimulatedResourceUpdates } from "../resources/subscriptions.js";
|
|
12
|
+
// Tool configuration
|
|
13
|
+
const name = "toggle-subscriber-updates";
|
|
14
|
+
const config = {
|
|
15
|
+
title: "Toggle Subscriber Updates",
|
|
16
|
+
description: "Toggles simulated resource subscription updates on or off. Deprecated: 2025-era clients only, " +
|
|
17
|
+
"since protocol version 2026-07-28 removes resources/subscribe; use trigger-resource-update with a " +
|
|
18
|
+
"subscriptions/listen stream instead.",
|
|
19
|
+
inputSchema: {},
|
|
20
|
+
annotations: {
|
|
21
|
+
readOnlyHint: false,
|
|
22
|
+
destructiveHint: false,
|
|
23
|
+
idempotentHint: false,
|
|
24
|
+
openWorldHint: false,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Registers the `toggle-subscriber-updates` tool.
|
|
29
|
+
*
|
|
30
|
+
* The registered tool enables or disables the sending of periodic, simulated resource
|
|
31
|
+
* update messages to the connected client for any subscriptions it has made.
|
|
32
|
+
* Each call flips the state: it starts the updates when they are off and stops
|
|
33
|
+
* them when they are on. The updates also stop when the connection ends.
|
|
34
|
+
*
|
|
35
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
36
|
+
* @param {ServerContext} context - The instance's subscriptions and cleanup registry.
|
|
37
|
+
*/
|
|
38
|
+
export const registerToggleSubscriberUpdatesTool = (server, context) => {
|
|
39
|
+
let stop;
|
|
40
|
+
context.onCleanup(() => {
|
|
41
|
+
stop?.();
|
|
42
|
+
stop = undefined;
|
|
43
|
+
});
|
|
44
|
+
server.registerTool(name, config, async () => {
|
|
45
|
+
let response;
|
|
46
|
+
if (stop) {
|
|
47
|
+
stop();
|
|
48
|
+
stop = undefined;
|
|
49
|
+
response = "Stopped simulated resource updates";
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
stop = beginSimulatedResourceUpdates(server, context.subscribed);
|
|
53
|
+
response =
|
|
54
|
+
"Started simulated resource updated notifications at a 5 second pace. Client will receive updates for any resources it is subscribed to.";
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
content: [{ type: "text", text: response }],
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// The `trigger-elicitation-request-async` tool: a task on the Tasks extension
|
|
2
|
+
// (`io.modelcontextprotocol/tasks`) that needs the user's input before it can
|
|
3
|
+
// finish, on protocol version 2026-07-28 (#4852).
|
|
4
|
+
//
|
|
5
|
+
// On 2025-11-25 this tool sent the client a task-augmented
|
|
6
|
+
// `elicitation/create` request and polled the client's own task. The
|
|
7
|
+
// 2026-07-28 revision has no server-to-client requests, so the roles turn
|
|
8
|
+
// around: the call returns a handle to a server task whose status is
|
|
9
|
+
// `input_required`, carrying the elicitation. The client shows it to the user
|
|
10
|
+
// and answers with `tasks/update`, and the task completes with a summary.
|
|
11
|
+
import { acceptedContent, inputRequired, inputResponse, } from "@modelcontextprotocol/server";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
import { createTask } from "../server/tasks.js";
|
|
14
|
+
// Tool configuration
|
|
15
|
+
const name = "trigger-elicitation-request-async";
|
|
16
|
+
const config = {
|
|
17
|
+
title: "Trigger Async Elicitation Request Tool",
|
|
18
|
+
description: "Runs as a task on the io.modelcontextprotocol/tasks extension (protocol version 2026-07-28) that needs " +
|
|
19
|
+
"the user's input: the task waits in status input_required, carrying an elicitation request, until the " +
|
|
20
|
+
"client answers it with tasks/update, then completes with a summary of the answer.",
|
|
21
|
+
inputSchema: {},
|
|
22
|
+
annotations: {
|
|
23
|
+
readOnlyHint: false,
|
|
24
|
+
destructiveHint: false,
|
|
25
|
+
idempotentHint: false,
|
|
26
|
+
openWorldHint: false,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* The form the task asks the user to fill in. The same schema is sent as the
|
|
31
|
+
* requested schema and checks the answer, since the client's answer is
|
|
32
|
+
* untrusted: an accepted answer that does not fit leaves the task waiting.
|
|
33
|
+
*/
|
|
34
|
+
const FormSchema = z.object({
|
|
35
|
+
name: z
|
|
36
|
+
.string()
|
|
37
|
+
.min(1)
|
|
38
|
+
.meta({ title: "Your Name" })
|
|
39
|
+
.describe("Enter your name"),
|
|
40
|
+
favoriteColor: z
|
|
41
|
+
.enum(["red", "orange", "yellow", "green", "blue", "purple"])
|
|
42
|
+
.optional()
|
|
43
|
+
.meta({ title: "Favorite Color" })
|
|
44
|
+
.describe("What is your favorite color?"),
|
|
45
|
+
agreeToTerms: z
|
|
46
|
+
.boolean()
|
|
47
|
+
.optional()
|
|
48
|
+
.meta({ title: "Agree to Terms" })
|
|
49
|
+
.describe("Do you agree to the terms and conditions?"),
|
|
50
|
+
});
|
|
51
|
+
/** The completed task's summary of the user's answer. */
|
|
52
|
+
function summarize(action, content) {
|
|
53
|
+
const lines = [`Elicitation ${action}.`];
|
|
54
|
+
if (content) {
|
|
55
|
+
lines.push(`Name: ${content.name}`);
|
|
56
|
+
if (content.favoriteColor) {
|
|
57
|
+
lines.push(`Favorite color: ${content.favoriteColor}`);
|
|
58
|
+
}
|
|
59
|
+
if (content.agreeToTerms !== undefined) {
|
|
60
|
+
lines.push(`Agreed to terms: ${content.agreeToTerms}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { content: [{ type: "text", text: lines.join("\n") }] };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Registers the 'trigger-elicitation-request-async' tool, for 2026-07-28
|
|
67
|
+
* clients.
|
|
68
|
+
*
|
|
69
|
+
* A client that does not declare both the tasks extension and form-mode
|
|
70
|
+
* elicitation is refused with -32021 before the tool runs (`guardTaskTools`).
|
|
71
|
+
*
|
|
72
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
73
|
+
*/
|
|
74
|
+
export const registerTriggerElicitationRequestAsyncTool = (server) => {
|
|
75
|
+
server.registerTool(name, config, async () => {
|
|
76
|
+
const createdAt = Date.now();
|
|
77
|
+
let summary;
|
|
78
|
+
let answeredAt = createdAt;
|
|
79
|
+
// The key of the outstanding request. A key is never reused once
|
|
80
|
+
// answered, so a re-asked form gets the next one.
|
|
81
|
+
let attempt = 1;
|
|
82
|
+
const key = () => `form-${attempt}`;
|
|
83
|
+
return createTask({
|
|
84
|
+
stateAt: () => summary === undefined
|
|
85
|
+
? {
|
|
86
|
+
// Waiting since the form, or the last re-ask, went out
|
|
87
|
+
since: answeredAt,
|
|
88
|
+
status: "input_required",
|
|
89
|
+
statusMessage: "Waiting for the user's input",
|
|
90
|
+
inputRequests: {
|
|
91
|
+
[key()]: inputRequired.elicit({
|
|
92
|
+
message: "Please provide inputs for the following fields (async task demo):",
|
|
93
|
+
requestedSchema: FormSchema,
|
|
94
|
+
}),
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
: {
|
|
98
|
+
since: answeredAt,
|
|
99
|
+
status: "completed",
|
|
100
|
+
statusMessage: "Input received",
|
|
101
|
+
result: summary,
|
|
102
|
+
},
|
|
103
|
+
receive: (responses, now) => {
|
|
104
|
+
answeredAt = now;
|
|
105
|
+
const answer = inputResponse(responses, key());
|
|
106
|
+
if (answer.kind === "elicit" && answer.action !== "accept") {
|
|
107
|
+
summary = summarize(answer.action);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const content = answer.kind === "elicit"
|
|
111
|
+
? acceptedContent(responses, key(), FormSchema)
|
|
112
|
+
: undefined;
|
|
113
|
+
// An answer of another kind, or one that does not fit the form,
|
|
114
|
+
// spends the request: ask again under a new key.
|
|
115
|
+
if (content === undefined)
|
|
116
|
+
attempt++;
|
|
117
|
+
else
|
|
118
|
+
summary = summarize("accept", content);
|
|
119
|
+
},
|
|
120
|
+
}, { ttlMs: 10 * 60_000, pollIntervalMs: 1000 });
|
|
121
|
+
});
|
|
122
|
+
};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// The `trigger-elicitation-request` tool: asks the user to fill in a form
|
|
2
|
+
// that exercises every field type form-mode elicitation supports, and
|
|
3
|
+
// summarizes the answer.
|
|
4
|
+
//
|
|
5
|
+
// Written once for both protocol eras with a multi-round-trip request
|
|
6
|
+
// (#4852): the first call returns `input_required` carrying an
|
|
7
|
+
// `elicitation/create` request, the client shows the form and retries with
|
|
8
|
+
// the answer, and the retry summarizes it. A 2026-07-28 client does that
|
|
9
|
+
// in-band; for a 2025-era client the SDK's legacy shim sends a real
|
|
10
|
+
// `elicitation/create` request and re-enters the handler with the answer.
|
|
11
|
+
import { inputRequired, inputResponse, } from "@modelcontextprotocol/server";
|
|
12
|
+
// Tool configuration
|
|
13
|
+
const name = "trigger-elicitation-request";
|
|
14
|
+
const config = {
|
|
15
|
+
title: "Trigger Elicitation Request Tool",
|
|
16
|
+
description: "Trigger a Request from the Server for User Elicitation",
|
|
17
|
+
inputSchema: {},
|
|
18
|
+
annotations: {
|
|
19
|
+
readOnlyHint: false,
|
|
20
|
+
destructiveHint: false,
|
|
21
|
+
idempotentHint: false,
|
|
22
|
+
openWorldHint: false,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
/** The form: one field of each type form-mode elicitation supports. */
|
|
26
|
+
const requestedSchema = {
|
|
27
|
+
type: "object",
|
|
28
|
+
properties: {
|
|
29
|
+
name: {
|
|
30
|
+
title: "String",
|
|
31
|
+
type: "string",
|
|
32
|
+
description: "Your full, legal name",
|
|
33
|
+
},
|
|
34
|
+
check: {
|
|
35
|
+
title: "Boolean",
|
|
36
|
+
type: "boolean",
|
|
37
|
+
description: "Agree to the terms and conditions",
|
|
38
|
+
},
|
|
39
|
+
firstLine: {
|
|
40
|
+
title: "String with default",
|
|
41
|
+
type: "string",
|
|
42
|
+
description: "Favorite first line of a story",
|
|
43
|
+
default: "It was a dark and stormy night.",
|
|
44
|
+
},
|
|
45
|
+
email: {
|
|
46
|
+
title: "String with email format",
|
|
47
|
+
type: "string",
|
|
48
|
+
format: "email",
|
|
49
|
+
description: "Your email address (will be verified, and never shared with anyone else)",
|
|
50
|
+
},
|
|
51
|
+
homepage: {
|
|
52
|
+
type: "string",
|
|
53
|
+
format: "uri",
|
|
54
|
+
title: "String with uri format",
|
|
55
|
+
description: "Portfolio / personal website",
|
|
56
|
+
},
|
|
57
|
+
birthdate: {
|
|
58
|
+
title: "String with date format",
|
|
59
|
+
type: "string",
|
|
60
|
+
format: "date",
|
|
61
|
+
description: "Your date of birth",
|
|
62
|
+
},
|
|
63
|
+
integer: {
|
|
64
|
+
title: "Integer",
|
|
65
|
+
type: "integer",
|
|
66
|
+
description: "Your favorite integer (do not give us your phone number, pin, or other sensitive info)",
|
|
67
|
+
minimum: 1,
|
|
68
|
+
maximum: 100,
|
|
69
|
+
default: 42,
|
|
70
|
+
},
|
|
71
|
+
number: {
|
|
72
|
+
title: "Number in range 1-1000",
|
|
73
|
+
type: "number",
|
|
74
|
+
description: "Favorite number (there are no wrong answers)",
|
|
75
|
+
minimum: 0,
|
|
76
|
+
maximum: 1000,
|
|
77
|
+
default: 3.14,
|
|
78
|
+
},
|
|
79
|
+
untitledSingleSelectEnum: {
|
|
80
|
+
type: "string",
|
|
81
|
+
title: "Untitled Single Select Enum",
|
|
82
|
+
description: "Choose your favorite friend",
|
|
83
|
+
enum: ["Monica", "Rachel", "Joey", "Chandler", "Ross", "Phoebe"],
|
|
84
|
+
default: "Monica",
|
|
85
|
+
},
|
|
86
|
+
untitledMultipleSelectEnum: {
|
|
87
|
+
type: "array",
|
|
88
|
+
title: "Untitled Multiple Select Enum",
|
|
89
|
+
description: "Choose your favorite instruments",
|
|
90
|
+
minItems: 1,
|
|
91
|
+
maxItems: 3,
|
|
92
|
+
items: {
|
|
93
|
+
type: "string",
|
|
94
|
+
enum: ["Guitar", "Piano", "Violin", "Drums", "Bass"],
|
|
95
|
+
},
|
|
96
|
+
default: ["Guitar"],
|
|
97
|
+
},
|
|
98
|
+
titledSingleSelectEnum: {
|
|
99
|
+
type: "string",
|
|
100
|
+
title: "Titled Single Select Enum",
|
|
101
|
+
description: "Choose your favorite hero",
|
|
102
|
+
oneOf: [
|
|
103
|
+
{ const: "hero-1", title: "Superman" },
|
|
104
|
+
{ const: "hero-2", title: "Green Lantern" },
|
|
105
|
+
{ const: "hero-3", title: "Wonder Woman" },
|
|
106
|
+
],
|
|
107
|
+
default: "hero-1",
|
|
108
|
+
},
|
|
109
|
+
titledMultipleSelectEnum: {
|
|
110
|
+
type: "array",
|
|
111
|
+
title: "Titled Multiple Select Enum",
|
|
112
|
+
description: "Choose your favorite types of fish",
|
|
113
|
+
minItems: 1,
|
|
114
|
+
maxItems: 3,
|
|
115
|
+
items: {
|
|
116
|
+
anyOf: [
|
|
117
|
+
{ const: "fish-1", title: "Tuna" },
|
|
118
|
+
{ const: "fish-2", title: "Salmon" },
|
|
119
|
+
{ const: "fish-3", title: "Trout" },
|
|
120
|
+
],
|
|
121
|
+
},
|
|
122
|
+
default: ["fish-1"],
|
|
123
|
+
},
|
|
124
|
+
legacyTitledEnum: {
|
|
125
|
+
type: "string",
|
|
126
|
+
title: "Legacy Titled Single Select Enum",
|
|
127
|
+
description: "Choose your favorite type of pet",
|
|
128
|
+
enum: ["pet-1", "pet-2", "pet-3", "pet-4", "pet-5"],
|
|
129
|
+
enumNames: ["Cats", "Dogs", "Birds", "Fish", "Reptiles"],
|
|
130
|
+
default: "pet-1",
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
required: ["name"],
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Registers the 'trigger-elicitation-request' tool.
|
|
137
|
+
*
|
|
138
|
+
* The tool needs a client that supports form-mode elicitation. On a 2025-era
|
|
139
|
+
* connection the server factory registers it only for a client that declared
|
|
140
|
+
* `elicitation.form`, or an `elicitation` object with neither `form` nor
|
|
141
|
+
* `url`, which the spec treats as form mode for backwards compatibility; a
|
|
142
|
+
* client that declared only `elicitation.url` does not get it (#4985). On
|
|
143
|
+
* 2026-07-28 the SDK refuses the embedded request (-32021) of a client whose
|
|
144
|
+
* request did not declare form-mode elicitation.
|
|
145
|
+
*
|
|
146
|
+
* The first round sends the form. The retry reads the user's response, which
|
|
147
|
+
* may be acceptance with content, decline, or cancellation of the dialog, and
|
|
148
|
+
* returns a structured summary with the raw result for debugging.
|
|
149
|
+
*
|
|
150
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
151
|
+
*/
|
|
152
|
+
export const registerTriggerElicitationRequestTool = (server) => {
|
|
153
|
+
server.registerTool(name, config, async (_args, ctx) => {
|
|
154
|
+
const answer = inputResponse(ctx.mcpReq.inputResponses, "form");
|
|
155
|
+
// First round: show the user the form
|
|
156
|
+
if (answer.kind !== "elicit") {
|
|
157
|
+
return inputRequired({
|
|
158
|
+
inputRequests: {
|
|
159
|
+
form: inputRequired.elicit({
|
|
160
|
+
message: "Please provide inputs for the following fields:",
|
|
161
|
+
requestedSchema,
|
|
162
|
+
}),
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
// The answer as the client sent it, for the debugging output
|
|
167
|
+
const rawResult = {
|
|
168
|
+
action: answer.action,
|
|
169
|
+
...(answer.content !== undefined && { content: answer.content }),
|
|
170
|
+
};
|
|
171
|
+
// Handle different response actions
|
|
172
|
+
const content = [];
|
|
173
|
+
if (answer.action === "accept" && answer.content) {
|
|
174
|
+
content.push({
|
|
175
|
+
type: "text",
|
|
176
|
+
text: `✅ User provided the requested information!`,
|
|
177
|
+
});
|
|
178
|
+
// Only access answer.content when action is accept
|
|
179
|
+
const userData = answer.content;
|
|
180
|
+
const lines = [];
|
|
181
|
+
if (userData.name)
|
|
182
|
+
lines.push(`- Name: ${userData.name}`);
|
|
183
|
+
if (userData.check !== undefined)
|
|
184
|
+
lines.push(`- Agreed to terms: ${userData.check}`);
|
|
185
|
+
if (userData.color)
|
|
186
|
+
lines.push(`- Favorite Color: ${userData.color}`);
|
|
187
|
+
if (userData.email)
|
|
188
|
+
lines.push(`- Email: ${userData.email}`);
|
|
189
|
+
if (userData.homepage)
|
|
190
|
+
lines.push(`- Homepage: ${userData.homepage}`);
|
|
191
|
+
if (userData.birthdate)
|
|
192
|
+
lines.push(`- Birthdate: ${userData.birthdate}`);
|
|
193
|
+
if (userData.integer !== undefined)
|
|
194
|
+
lines.push(`- Favorite Integer: ${userData.integer}`);
|
|
195
|
+
if (userData.number !== undefined)
|
|
196
|
+
lines.push(`- Favorite Number: ${userData.number}`);
|
|
197
|
+
if (userData.petType)
|
|
198
|
+
lines.push(`- Pet Type: ${userData.petType}`);
|
|
199
|
+
content.push({
|
|
200
|
+
type: "text",
|
|
201
|
+
text: `User inputs:\n${lines.join("\n")}`,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
else if (answer.action === "decline") {
|
|
205
|
+
content.push({
|
|
206
|
+
type: "text",
|
|
207
|
+
text: `❌ User declined to provide the requested information.`,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
else if (answer.action === "cancel") {
|
|
211
|
+
content.push({
|
|
212
|
+
type: "text",
|
|
213
|
+
text: `⚠️ User cancelled the elicitation dialog.`,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
// Include raw result for debugging
|
|
217
|
+
content.push({
|
|
218
|
+
type: "text",
|
|
219
|
+
text: `\nRaw result: ${JSON.stringify(rawResult, null, 2)}`,
|
|
220
|
+
});
|
|
221
|
+
return { content };
|
|
222
|
+
});
|
|
223
|
+
};
|