@modelcontextprotocol/server-everything 2025.12.18 → 2026.1.26
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 +9 -158
- package/dist/docs/architecture.md +44 -0
- package/dist/docs/extension.md +23 -0
- package/dist/docs/features.md +103 -0
- package/dist/docs/how-it-works.md +45 -0
- package/dist/docs/instructions.md +28 -0
- package/dist/docs/startup.md +73 -0
- package/dist/docs/structure.md +182 -0
- package/dist/index.js +19 -14
- 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 +60 -0
- package/dist/prompts/simple.js +23 -0
- package/dist/resources/files.js +83 -0
- package/dist/resources/index.js +33 -0
- package/dist/resources/session.js +44 -0
- package/dist/resources/subscriptions.js +125 -0
- package/dist/resources/templates.js +171 -0
- package/dist/server/index.js +93 -0
- package/dist/server/logging.js +64 -0
- package/dist/server/roots.js +65 -0
- package/dist/tools/echo.js +29 -0
- package/dist/tools/get-annotated-message.js +81 -0
- package/dist/tools/get-env.js +28 -0
- package/dist/tools/get-resource-links.js +62 -0
- package/dist/tools/get-resource-reference.js +74 -0
- package/dist/tools/get-roots-list.js +71 -0
- package/dist/tools/get-structured-content.js +72 -0
- package/dist/tools/get-sum.js +40 -0
- package/dist/tools/get-tiny-image.js +41 -0
- package/dist/tools/gzip-file-as-resource.js +182 -0
- package/dist/tools/index.js +50 -0
- package/dist/tools/simulate-research-query.js +249 -0
- package/dist/tools/toggle-simulated-logging.js +41 -0
- package/dist/tools/toggle-subscriber-updates.js +44 -0
- package/dist/tools/trigger-elicitation-request-async.js +202 -0
- package/dist/tools/trigger-elicitation-request.js +210 -0
- package/dist/tools/trigger-long-running-operation.js +59 -0
- package/dist/tools/trigger-sampling-request-async.js +168 -0
- package/dist/tools/trigger-sampling-request.js +71 -0
- package/dist/{sse.js → transports/sse.js} +25 -17
- package/dist/transports/stdio.js +27 -0
- package/dist/transports/streamableHttp.js +206 -0
- package/package.json +10 -7
- package/dist/everything.js +0 -978
- package/dist/instructions.md +0 -23
- package/dist/stdio.js +0 -23
- package/dist/streamableHttp.js +0 -174
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ElicitResultSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
// Tool input schema
|
|
4
|
+
const SimulateResearchQuerySchema = z.object({
|
|
5
|
+
topic: z.string().describe("The research topic to investigate"),
|
|
6
|
+
ambiguous: z
|
|
7
|
+
.boolean()
|
|
8
|
+
.default(false)
|
|
9
|
+
.describe("Simulate an ambiguous query that requires clarification (triggers input_required status)"),
|
|
10
|
+
});
|
|
11
|
+
// Research stages
|
|
12
|
+
const STAGES = [
|
|
13
|
+
"Gathering sources",
|
|
14
|
+
"Analyzing content",
|
|
15
|
+
"Synthesizing findings",
|
|
16
|
+
"Generating report",
|
|
17
|
+
];
|
|
18
|
+
// Duration per stage in milliseconds
|
|
19
|
+
const STAGE_DURATION = 1000;
|
|
20
|
+
// Map to store research state per task
|
|
21
|
+
const researchStates = new Map();
|
|
22
|
+
/**
|
|
23
|
+
* Runs the background research process.
|
|
24
|
+
* Updates task status as it progresses through stages.
|
|
25
|
+
* If clarification is needed, attempts elicitation via sendRequest.
|
|
26
|
+
*
|
|
27
|
+
* Note: Elicitation only works on STDIO transport. On HTTP transport,
|
|
28
|
+
* sendRequest will fail and the task will use a default interpretation.
|
|
29
|
+
* Full HTTP support requires SDK PR #1210's elicitInputStream API.
|
|
30
|
+
*/
|
|
31
|
+
async function runResearchProcess(taskId, args, taskStore,
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
sendRequest) {
|
|
34
|
+
const state = researchStates.get(taskId);
|
|
35
|
+
if (!state)
|
|
36
|
+
return;
|
|
37
|
+
// Process each stage
|
|
38
|
+
for (let i = state.currentStage; i < STAGES.length; i++) {
|
|
39
|
+
state.currentStage = i;
|
|
40
|
+
// Check if task was cancelled externally
|
|
41
|
+
if (state.completed)
|
|
42
|
+
return;
|
|
43
|
+
// Update status message for current stage
|
|
44
|
+
await taskStore.updateTaskStatus(taskId, "working", `${STAGES[i]}...`);
|
|
45
|
+
// At synthesis stage (index 2), check if clarification is needed
|
|
46
|
+
if (i === 2 && state.ambiguous && !state.clarification) {
|
|
47
|
+
// Update status to show we're requesting input (spec SHOULD)
|
|
48
|
+
await taskStore.updateTaskStatus(taskId, "input_required", `Found multiple interpretations for "${state.topic}". Requesting clarification...`);
|
|
49
|
+
try {
|
|
50
|
+
// Try elicitation via sendRequest (works on STDIO, fails on HTTP)
|
|
51
|
+
const elicitResult = await sendRequest({
|
|
52
|
+
method: "elicitation/create",
|
|
53
|
+
params: {
|
|
54
|
+
message: `The research query "${state.topic}" could have multiple interpretations. Please clarify what you're looking for:`,
|
|
55
|
+
requestedSchema: {
|
|
56
|
+
type: "object",
|
|
57
|
+
properties: {
|
|
58
|
+
interpretation: {
|
|
59
|
+
type: "string",
|
|
60
|
+
title: "Clarification",
|
|
61
|
+
description: "Which interpretation of the topic do you mean?",
|
|
62
|
+
oneOf: getInterpretationsForTopic(state.topic),
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
required: ["interpretation"],
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
}, ElicitResultSchema);
|
|
69
|
+
// Process elicitation response
|
|
70
|
+
if (elicitResult.action === "accept" && elicitResult.content) {
|
|
71
|
+
state.clarification =
|
|
72
|
+
elicitResult.content
|
|
73
|
+
.interpretation || "User accepted without selection";
|
|
74
|
+
}
|
|
75
|
+
else if (elicitResult.action === "decline") {
|
|
76
|
+
state.clarification = "User declined - using default interpretation";
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
state.clarification = "User cancelled - using default interpretation";
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
// Elicitation failed (likely HTTP transport without streaming support)
|
|
84
|
+
// Use default interpretation and continue - task should still complete
|
|
85
|
+
console.warn(`Elicitation failed for task ${taskId} (HTTP transport?):`, error instanceof Error ? error.message : String(error));
|
|
86
|
+
state.clarification =
|
|
87
|
+
"technical (default - elicitation unavailable on HTTP)";
|
|
88
|
+
}
|
|
89
|
+
// Resume with working status (spec SHOULD)
|
|
90
|
+
await taskStore.updateTaskStatus(taskId, "working", `Continuing with interpretation: "${state.clarification}"...`);
|
|
91
|
+
// Continue processing (no return - just keep going through the loop)
|
|
92
|
+
}
|
|
93
|
+
// Simulate work for this stage
|
|
94
|
+
await new Promise((resolve) => setTimeout(resolve, STAGE_DURATION));
|
|
95
|
+
}
|
|
96
|
+
// All stages complete - generate result
|
|
97
|
+
state.completed = true;
|
|
98
|
+
const result = generateResearchReport(state);
|
|
99
|
+
state.result = result;
|
|
100
|
+
await taskStore.storeTaskResult(taskId, "completed", result);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Generates the final research report with educational content about tasks.
|
|
104
|
+
*/
|
|
105
|
+
function generateResearchReport(state) {
|
|
106
|
+
const topic = state.clarification
|
|
107
|
+
? `${state.topic} (${state.clarification})`
|
|
108
|
+
: state.topic;
|
|
109
|
+
const report = `# Research Report: ${topic}
|
|
110
|
+
|
|
111
|
+
## Research Parameters
|
|
112
|
+
- **Topic**: ${state.topic}
|
|
113
|
+
${state.clarification ? `- **Clarification**: ${state.clarification}` : ""}
|
|
114
|
+
|
|
115
|
+
## Synthesis
|
|
116
|
+
This research query was processed through ${STAGES.length} stages:
|
|
117
|
+
${STAGES.map((s, i) => `- Stage ${i + 1}: ${s} ✓`).join("\n")}
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## About This Demo (SEP-1686: Tasks)
|
|
122
|
+
|
|
123
|
+
This tool demonstrates MCP's task-based execution pattern for long-running operations:
|
|
124
|
+
|
|
125
|
+
**Task Lifecycle Demonstrated:**
|
|
126
|
+
1. \`tools/call\` with \`task\` parameter → Server returns \`CreateTaskResult\` (not the final result)
|
|
127
|
+
2. Client polls \`tasks/get\` → Server returns current status and \`statusMessage\`
|
|
128
|
+
3. Status progressed: \`working\` → ${state.clarification ? `\`input_required\` → \`working\` → ` : ""}\`completed\`
|
|
129
|
+
4. Client calls \`tasks/result\` → Server returns this final result
|
|
130
|
+
|
|
131
|
+
${state.clarification
|
|
132
|
+
? `**Elicitation Flow:**
|
|
133
|
+
When the query was ambiguous, the server sent an \`elicitation/create\` request
|
|
134
|
+
to the client. The task status changed to \`input_required\` while awaiting user input.
|
|
135
|
+
${state.clarification.includes("unavailable on HTTP")
|
|
136
|
+
? `
|
|
137
|
+
**Note:** Elicitation was skipped because this server is running over HTTP transport.
|
|
138
|
+
The current SDK's \`sendRequest\` only works over STDIO. Full HTTP elicitation support
|
|
139
|
+
requires SDK PR #1210's streaming \`elicitInputStream\` API.
|
|
140
|
+
`
|
|
141
|
+
: `After receiving clarification ("${state.clarification}"), the task resumed processing and completed.`}
|
|
142
|
+
`
|
|
143
|
+
: ""}
|
|
144
|
+
**Key Concepts:**
|
|
145
|
+
- Tasks enable "call now, fetch later" patterns
|
|
146
|
+
- \`statusMessage\` provides human-readable progress updates
|
|
147
|
+
- Tasks have TTL (time-to-live) for automatic cleanup
|
|
148
|
+
- \`pollInterval\` suggests how often to check status
|
|
149
|
+
- Elicitation requests can be sent directly during task execution
|
|
150
|
+
|
|
151
|
+
*This is a simulated research report from the Everything MCP Server.*
|
|
152
|
+
`;
|
|
153
|
+
return {
|
|
154
|
+
content: [
|
|
155
|
+
{
|
|
156
|
+
type: "text",
|
|
157
|
+
text: report,
|
|
158
|
+
},
|
|
159
|
+
],
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Registers the 'simulate-research-query' tool as a task-based tool.
|
|
164
|
+
*
|
|
165
|
+
* This tool demonstrates the MCP Tasks feature (SEP-1686) with a real-world scenario:
|
|
166
|
+
* a research tool that gathers and synthesizes information from multiple sources.
|
|
167
|
+
* If the query is ambiguous, it pauses to ask for clarification before completing.
|
|
168
|
+
*
|
|
169
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
170
|
+
*/
|
|
171
|
+
export const registerSimulateResearchQueryTool = (server) => {
|
|
172
|
+
// Check if client supports elicitation (needed for input_required flow)
|
|
173
|
+
const clientCapabilities = server.server.getClientCapabilities() || {};
|
|
174
|
+
const clientSupportsElicitation = clientCapabilities.elicitation !== undefined;
|
|
175
|
+
server.experimental.tasks.registerToolTask("simulate-research-query", {
|
|
176
|
+
title: "Simulate Research Query",
|
|
177
|
+
description: "Simulates a deep research operation that gathers, analyzes, and synthesizes information. " +
|
|
178
|
+
"Demonstrates MCP task-based operations with progress through multiple stages. " +
|
|
179
|
+
"If 'ambiguous' is true and client supports elicitation, sends an elicitation request for clarification.",
|
|
180
|
+
inputSchema: SimulateResearchQuerySchema,
|
|
181
|
+
execution: { taskSupport: "required" },
|
|
182
|
+
}, {
|
|
183
|
+
/**
|
|
184
|
+
* Creates a new research task and starts background processing.
|
|
185
|
+
*/
|
|
186
|
+
createTask: async (args, extra) => {
|
|
187
|
+
const validatedArgs = SimulateResearchQuerySchema.parse(args);
|
|
188
|
+
// Create the task in the store
|
|
189
|
+
const task = await extra.taskStore.createTask({
|
|
190
|
+
ttl: 300000, // 5 minutes
|
|
191
|
+
pollInterval: 1000,
|
|
192
|
+
});
|
|
193
|
+
// Initialize research state
|
|
194
|
+
const state = {
|
|
195
|
+
topic: validatedArgs.topic,
|
|
196
|
+
ambiguous: validatedArgs.ambiguous && clientSupportsElicitation,
|
|
197
|
+
currentStage: 0,
|
|
198
|
+
completed: false,
|
|
199
|
+
};
|
|
200
|
+
researchStates.set(task.taskId, state);
|
|
201
|
+
// Start background research (don't await - runs asynchronously)
|
|
202
|
+
// Pass sendRequest for elicitation (works on STDIO, gracefully degrades on HTTP)
|
|
203
|
+
runResearchProcess(task.taskId, validatedArgs, extra.taskStore, extra.sendRequest).catch((error) => {
|
|
204
|
+
console.error(`Research task ${task.taskId} failed:`, error);
|
|
205
|
+
extra.taskStore
|
|
206
|
+
.updateTaskStatus(task.taskId, "failed", String(error))
|
|
207
|
+
.catch(console.error);
|
|
208
|
+
});
|
|
209
|
+
return { task };
|
|
210
|
+
},
|
|
211
|
+
/**
|
|
212
|
+
* Returns the current status of the research task.
|
|
213
|
+
*/
|
|
214
|
+
getTask: async (args, extra) => {
|
|
215
|
+
return await extra.taskStore.getTask(extra.taskId);
|
|
216
|
+
},
|
|
217
|
+
/**
|
|
218
|
+
* Returns the task result.
|
|
219
|
+
* Elicitation is now handled directly in the background process.
|
|
220
|
+
*/
|
|
221
|
+
getTaskResult: async (args, extra) => {
|
|
222
|
+
// Return the stored result
|
|
223
|
+
const result = await extra.taskStore.getTaskResult(extra.taskId);
|
|
224
|
+
// Clean up state
|
|
225
|
+
researchStates.delete(extra.taskId);
|
|
226
|
+
return result;
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
/**
|
|
231
|
+
* Returns contextual interpretation options based on the topic.
|
|
232
|
+
*/
|
|
233
|
+
function getInterpretationsForTopic(topic) {
|
|
234
|
+
const lowerTopic = topic.toLowerCase();
|
|
235
|
+
// Example: contextual interpretations for "python"
|
|
236
|
+
if (lowerTopic.includes("python")) {
|
|
237
|
+
return [
|
|
238
|
+
{ const: "programming", title: "Python programming language" },
|
|
239
|
+
{ const: "snake", title: "Python snake species" },
|
|
240
|
+
{ const: "comedy", title: "Monty Python comedy group" },
|
|
241
|
+
];
|
|
242
|
+
}
|
|
243
|
+
// Default generic interpretations
|
|
244
|
+
return [
|
|
245
|
+
{ const: "technical", title: "Technical/scientific perspective" },
|
|
246
|
+
{ const: "historical", title: "Historical perspective" },
|
|
247
|
+
{ const: "current", title: "Current events/news perspective" },
|
|
248
|
+
];
|
|
249
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { beginSimulatedLogging, stopSimulatedLogging, } from "../server/logging.js";
|
|
2
|
+
// Tool configuration
|
|
3
|
+
const name = "toggle-simulated-logging";
|
|
4
|
+
const config = {
|
|
5
|
+
title: "Toggle Simulated Logging",
|
|
6
|
+
description: "Toggles simulated, random-leveled logging on or off.",
|
|
7
|
+
inputSchema: {},
|
|
8
|
+
};
|
|
9
|
+
// Track enabled clients by session id
|
|
10
|
+
const clients = new Set();
|
|
11
|
+
/**
|
|
12
|
+
* Registers the `toggle-simulated-logging` tool.
|
|
13
|
+
*
|
|
14
|
+
* The registered tool enables or disables the sending of periodic, random-leveled
|
|
15
|
+
* logging messages the connected client.
|
|
16
|
+
*
|
|
17
|
+
* When invoked, it either starts or stops simulated logging based on the session's
|
|
18
|
+
* current state. If logging for the specified session is active, it will be stopped;
|
|
19
|
+
* if it is inactive, logging will be started.
|
|
20
|
+
*
|
|
21
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
22
|
+
*/
|
|
23
|
+
export const registerToggleSimulatedLoggingTool = (server) => {
|
|
24
|
+
server.registerTool(name, config, async (_args, extra) => {
|
|
25
|
+
const sessionId = extra?.sessionId;
|
|
26
|
+
let response;
|
|
27
|
+
if (clients.has(sessionId)) {
|
|
28
|
+
stopSimulatedLogging(sessionId);
|
|
29
|
+
clients.delete(sessionId);
|
|
30
|
+
response = `Stopped simulated logging for session ${sessionId}`;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
beginSimulatedLogging(server, sessionId);
|
|
34
|
+
clients.add(sessionId);
|
|
35
|
+
response = `Started simulated, random-leveled logging for session ${sessionId} 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. `;
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
content: [{ type: "text", text: `${response}` }],
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { beginSimulatedResourceUpdates, stopSimulatedResourceUpdates, } from "../resources/subscriptions.js";
|
|
2
|
+
// Tool configuration
|
|
3
|
+
const name = "toggle-subscriber-updates";
|
|
4
|
+
const config = {
|
|
5
|
+
title: "Toggle Subscriber Updates",
|
|
6
|
+
description: "Toggles simulated resource subscription updates on or off.",
|
|
7
|
+
inputSchema: {},
|
|
8
|
+
};
|
|
9
|
+
// Track enabled clients by session id
|
|
10
|
+
const clients = new Set();
|
|
11
|
+
/**
|
|
12
|
+
* Registers the `toggle-subscriber-updates` tool.
|
|
13
|
+
*
|
|
14
|
+
* The registered tool enables or disables the sending of periodic, simulated resource
|
|
15
|
+
* update messages the connected client for any subscriptions they have made.
|
|
16
|
+
*
|
|
17
|
+
* When invoked, it either starts or stops simulated resource updates based on the session's
|
|
18
|
+
* current state. If simulated updates for the specified session is active, it will be stopped;
|
|
19
|
+
* if it is inactive, simulated updates will be started.
|
|
20
|
+
*
|
|
21
|
+
* The response provides feedback indicating whether simulated updates were started or stopped,
|
|
22
|
+
* including the session ID.
|
|
23
|
+
*
|
|
24
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
25
|
+
*/
|
|
26
|
+
export const registerToggleSubscriberUpdatesTool = (server) => {
|
|
27
|
+
server.registerTool(name, config, async (_args, extra) => {
|
|
28
|
+
const sessionId = extra?.sessionId;
|
|
29
|
+
let response;
|
|
30
|
+
if (clients.has(sessionId)) {
|
|
31
|
+
stopSimulatedResourceUpdates(sessionId);
|
|
32
|
+
clients.delete(sessionId);
|
|
33
|
+
response = `Stopped simulated resource updates for session ${sessionId}`;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
beginSimulatedResourceUpdates(server, sessionId);
|
|
37
|
+
clients.add(sessionId);
|
|
38
|
+
response = `Started simulated resource updated notifications for session ${sessionId} at a 5 second pace. Client will receive updates for any resources the it is subscribed to.`;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
content: [{ type: "text", text: `${response}` }],
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
};
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
// Tool configuration
|
|
3
|
+
const name = "trigger-elicitation-request-async";
|
|
4
|
+
const config = {
|
|
5
|
+
title: "Trigger Async Elicitation Request Tool",
|
|
6
|
+
description: "Trigger an async elicitation request that the CLIENT executes as a background task. " +
|
|
7
|
+
"Demonstrates bidirectional MCP tasks where the server sends an elicitation request and " +
|
|
8
|
+
"the client handles user input asynchronously, allowing the server to poll for completion.",
|
|
9
|
+
inputSchema: {},
|
|
10
|
+
};
|
|
11
|
+
// Poll interval in milliseconds
|
|
12
|
+
const POLL_INTERVAL = 1000;
|
|
13
|
+
// Maximum poll attempts before timeout (10 minutes for user input)
|
|
14
|
+
const MAX_POLL_ATTEMPTS = 600;
|
|
15
|
+
/**
|
|
16
|
+
* Registers the 'trigger-elicitation-request-async' tool.
|
|
17
|
+
*
|
|
18
|
+
* This tool demonstrates bidirectional MCP tasks for elicitation:
|
|
19
|
+
* - Server sends elicitation request to client with task metadata
|
|
20
|
+
* - Client creates a task and returns CreateTaskResult
|
|
21
|
+
* - Client prompts user for input (task status: input_required)
|
|
22
|
+
* - Server polls client's tasks/get endpoint for status
|
|
23
|
+
* - Server fetches final result from client's tasks/result endpoint
|
|
24
|
+
*
|
|
25
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
26
|
+
*/
|
|
27
|
+
export const registerTriggerElicitationRequestAsyncTool = (server) => {
|
|
28
|
+
// Check client capabilities
|
|
29
|
+
const clientCapabilities = server.server.getClientCapabilities() || {};
|
|
30
|
+
// Client must support elicitation AND tasks.requests.elicitation
|
|
31
|
+
const clientSupportsElicitation = clientCapabilities.elicitation !== undefined;
|
|
32
|
+
const clientTasksCapability = clientCapabilities.tasks;
|
|
33
|
+
const clientSupportsAsyncElicitation = clientTasksCapability?.requests?.elicitation?.create !== undefined;
|
|
34
|
+
if (clientSupportsElicitation && clientSupportsAsyncElicitation) {
|
|
35
|
+
server.registerTool(name, config, async (args, extra) => {
|
|
36
|
+
// Create the elicitation request WITH task metadata
|
|
37
|
+
// Using z.any() schema to avoid complex type matching with _meta
|
|
38
|
+
const request = {
|
|
39
|
+
method: "elicitation/create",
|
|
40
|
+
params: {
|
|
41
|
+
task: {
|
|
42
|
+
ttl: 600000, // 10 minutes (user input may take a while)
|
|
43
|
+
},
|
|
44
|
+
message: "Please provide inputs for the following fields (async task demo):",
|
|
45
|
+
requestedSchema: {
|
|
46
|
+
type: "object",
|
|
47
|
+
properties: {
|
|
48
|
+
name: {
|
|
49
|
+
title: "Your Name",
|
|
50
|
+
type: "string",
|
|
51
|
+
description: "Your full name",
|
|
52
|
+
},
|
|
53
|
+
favoriteColor: {
|
|
54
|
+
title: "Favorite Color",
|
|
55
|
+
type: "string",
|
|
56
|
+
description: "What is your favorite color?",
|
|
57
|
+
enum: ["Red", "Blue", "Green", "Yellow", "Purple"],
|
|
58
|
+
},
|
|
59
|
+
agreeToTerms: {
|
|
60
|
+
title: "Terms Agreement",
|
|
61
|
+
type: "boolean",
|
|
62
|
+
description: "Do you agree to the terms and conditions?",
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
required: ["name"],
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
// Send the elicitation request
|
|
70
|
+
// Client may return either:
|
|
71
|
+
// - ElicitResult (synchronous execution)
|
|
72
|
+
// - CreateTaskResult (task-based execution with { task } object)
|
|
73
|
+
const elicitResponse = await extra.sendRequest(request, z.union([
|
|
74
|
+
// CreateTaskResult - client created a task
|
|
75
|
+
z.object({
|
|
76
|
+
task: z.object({
|
|
77
|
+
taskId: z.string(),
|
|
78
|
+
status: z.string(),
|
|
79
|
+
pollInterval: z.number().optional(),
|
|
80
|
+
statusMessage: z.string().optional(),
|
|
81
|
+
}),
|
|
82
|
+
}),
|
|
83
|
+
// ElicitResult - synchronous execution
|
|
84
|
+
z.object({
|
|
85
|
+
action: z.string(),
|
|
86
|
+
content: z.any().optional(),
|
|
87
|
+
}),
|
|
88
|
+
]));
|
|
89
|
+
// Check if client returned CreateTaskResult (has task object)
|
|
90
|
+
const isTaskResult = "task" in elicitResponse && elicitResponse.task;
|
|
91
|
+
if (!isTaskResult) {
|
|
92
|
+
// Client executed synchronously - return the direct response
|
|
93
|
+
return {
|
|
94
|
+
content: [
|
|
95
|
+
{
|
|
96
|
+
type: "text",
|
|
97
|
+
text: `[SYNC] Client executed synchronously:\n${JSON.stringify(elicitResponse, null, 2)}`,
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const taskId = elicitResponse.task.taskId;
|
|
103
|
+
const statusMessages = [];
|
|
104
|
+
statusMessages.push(`Task created: ${taskId}`);
|
|
105
|
+
// Poll for task completion
|
|
106
|
+
let attempts = 0;
|
|
107
|
+
let taskStatus = elicitResponse.task.status;
|
|
108
|
+
let taskStatusMessage;
|
|
109
|
+
while (taskStatus !== "completed" &&
|
|
110
|
+
taskStatus !== "failed" &&
|
|
111
|
+
taskStatus !== "cancelled" &&
|
|
112
|
+
attempts < MAX_POLL_ATTEMPTS) {
|
|
113
|
+
// Wait before polling
|
|
114
|
+
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));
|
|
115
|
+
attempts++;
|
|
116
|
+
// Get task status from client
|
|
117
|
+
const pollResult = await extra.sendRequest({
|
|
118
|
+
method: "tasks/get",
|
|
119
|
+
params: { taskId },
|
|
120
|
+
}, z
|
|
121
|
+
.object({
|
|
122
|
+
status: z.string(),
|
|
123
|
+
statusMessage: z.string().optional(),
|
|
124
|
+
})
|
|
125
|
+
.passthrough());
|
|
126
|
+
taskStatus = pollResult.status;
|
|
127
|
+
taskStatusMessage = pollResult.statusMessage;
|
|
128
|
+
// Only log status changes or every 10 polls to avoid spam
|
|
129
|
+
if (attempts === 1 ||
|
|
130
|
+
attempts % 10 === 0 ||
|
|
131
|
+
taskStatus !== "input_required") {
|
|
132
|
+
statusMessages.push(`Poll ${attempts}: ${taskStatus}${taskStatusMessage ? ` - ${taskStatusMessage}` : ""}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// Check for timeout
|
|
136
|
+
if (attempts >= MAX_POLL_ATTEMPTS) {
|
|
137
|
+
return {
|
|
138
|
+
content: [
|
|
139
|
+
{
|
|
140
|
+
type: "text",
|
|
141
|
+
text: `[TIMEOUT] Task timed out after ${MAX_POLL_ATTEMPTS} poll attempts\n\nProgress:\n${statusMessages.join("\n")}`,
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
// Check for failure/cancellation
|
|
147
|
+
if (taskStatus === "failed" || taskStatus === "cancelled") {
|
|
148
|
+
return {
|
|
149
|
+
content: [
|
|
150
|
+
{
|
|
151
|
+
type: "text",
|
|
152
|
+
text: `[${taskStatus.toUpperCase()}] ${taskStatusMessage || "No message"}\n\nProgress:\n${statusMessages.join("\n")}`,
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
// Fetch the final result
|
|
158
|
+
const result = await extra.sendRequest({
|
|
159
|
+
method: "tasks/result",
|
|
160
|
+
params: { taskId },
|
|
161
|
+
}, z.any());
|
|
162
|
+
// Format the elicitation result
|
|
163
|
+
const content = [];
|
|
164
|
+
if (result.action === "accept" && result.content) {
|
|
165
|
+
content.push({
|
|
166
|
+
type: "text",
|
|
167
|
+
text: `[COMPLETED] User provided the requested information!`,
|
|
168
|
+
});
|
|
169
|
+
const userData = result.content;
|
|
170
|
+
const lines = [];
|
|
171
|
+
if (userData.name)
|
|
172
|
+
lines.push(`- Name: ${userData.name}`);
|
|
173
|
+
if (userData.favoriteColor)
|
|
174
|
+
lines.push(`- Favorite Color: ${userData.favoriteColor}`);
|
|
175
|
+
if (userData.agreeToTerms !== undefined)
|
|
176
|
+
lines.push(`- Agreed to terms: ${userData.agreeToTerms}`);
|
|
177
|
+
content.push({
|
|
178
|
+
type: "text",
|
|
179
|
+
text: `User inputs:\n${lines.join("\n")}`,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
else if (result.action === "decline") {
|
|
183
|
+
content.push({
|
|
184
|
+
type: "text",
|
|
185
|
+
text: `[DECLINED] User declined to provide the requested information.`,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
else if (result.action === "cancel") {
|
|
189
|
+
content.push({
|
|
190
|
+
type: "text",
|
|
191
|
+
text: `[CANCELLED] User cancelled the elicitation dialog.`,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
// Include progress and raw result for debugging
|
|
195
|
+
content.push({
|
|
196
|
+
type: "text",
|
|
197
|
+
text: `\nProgress:\n${statusMessages.join("\n")}\n\nRaw result: ${JSON.stringify(result, null, 2)}`,
|
|
198
|
+
});
|
|
199
|
+
return { content };
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
};
|