@modelcontextprotocol/server-everything 1.0.0 → 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 +35 -20
- package/dist/docs/architecture.md +6 -5
- package/dist/docs/extension.md +5 -3
- package/dist/docs/features.md +65 -71
- package/dist/docs/how-it-works.md +31 -16
- package/dist/docs/instructions.md +14 -7
- package/dist/docs/startup.md +28 -33
- package/dist/docs/structure.md +63 -29
- package/dist/prompts/args.js +2 -2
- package/dist/prompts/completions.js +3 -3
- package/dist/prompts/resource.js +3 -2
- package/dist/resources/files.js +9 -1
- package/dist/resources/index.js +7 -2
- package/dist/resources/minted.js +119 -0
- package/dist/resources/subscriptions.js +44 -122
- package/dist/resources/templates.js +5 -4
- package/dist/server/context.js +47 -0
- package/dist/server/index.js +113 -55
- package/dist/server/logging.js +39 -66
- package/dist/server/roots.js +41 -62
- package/dist/server/tasks.js +295 -0
- package/dist/tools/get-header-params.js +95 -0
- package/dist/tools/get-roots-list.js +49 -50
- package/dist/tools/gzip-file-as-resource.js +24 -5
- package/dist/tools/index.js +72 -14
- package/dist/tools/simulate-research-query.js +166 -201
- package/dist/tools/toggle-simulated-logging.js +33 -20
- package/dist/tools/toggle-subscriber-updates.js +33 -23
- package/dist/tools/trigger-elicitation-request-async.js +99 -204
- package/dist/tools/trigger-elicitation-request.js +197 -196
- 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 +3 -3
- 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 +79 -143
- package/dist/tools/trigger-sampling-request.js +51 -45
- package/dist/tools/trigger-url-elicitation.js +148 -111
- package/dist/transports/sse.js +11 -2
- package/dist/transports/stdio.js +42 -22
- package/dist/transports/streamableHttp.js +96 -10
- package/package.json +10 -3
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// The `trigger-multi-step-elicitation` tool: asks the user two questions in
|
|
2
|
+
// sequence, each depending on the last, to show `requestState` carrying a
|
|
3
|
+
// multi-round-trip flow from one round to the next (#4852).
|
|
4
|
+
//
|
|
5
|
+
// Each retry of a multi-round-trip request carries only that round's answers,
|
|
6
|
+
// and on the 2026-07-28 HTTP path a later round may be served by a fresh
|
|
7
|
+
// server instance, so nothing on the server remembers earlier rounds. What
|
|
8
|
+
// the flow has learned travels in `requestState` instead: an opaque string
|
|
9
|
+
// the server returns with `input_required` and the client echoes back on the
|
|
10
|
+
// retry. It is attacker-controlled on the way back, so the server signs it
|
|
11
|
+
// and gives it an expiry (`roundStateCodec`); the SDK rejects a tampered or
|
|
12
|
+
// expired state with -32602 before the handler runs. The handler switches on
|
|
13
|
+
// the phase the state names rather than on which answers arrived.
|
|
14
|
+
//
|
|
15
|
+
// Written once for both protocol eras: for a 2025-era client the SDK's legacy
|
|
16
|
+
// shim sends each round as a real `elicitation/create` request and replays
|
|
17
|
+
// the state between rounds.
|
|
18
|
+
import { acceptedContent, inputRequired, inputResponse, } from "@modelcontextprotocol/server";
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
import { roundStateCodec } from "../server/index.js";
|
|
21
|
+
// Tool configuration
|
|
22
|
+
const name = "trigger-multi-step-elicitation";
|
|
23
|
+
const config = {
|
|
24
|
+
title: "Trigger Multi-Step Elicitation Tool",
|
|
25
|
+
description: "Asks the user for their name, then for a favorite color addressed to them by that name, carrying " +
|
|
26
|
+
"the first answer to the second round in requestState. Demonstrates multi-round-trip requests " +
|
|
27
|
+
"(input_required) and requestState on protocol version 2026-07-28; 2025-era clients get the same " +
|
|
28
|
+
"flow as elicitation requests.",
|
|
29
|
+
inputSchema: {},
|
|
30
|
+
annotations: {
|
|
31
|
+
readOnlyHint: false,
|
|
32
|
+
destructiveHint: false,
|
|
33
|
+
idempotentHint: false,
|
|
34
|
+
openWorldHint: false,
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
/** The first question's answer: a name that is not blank. */
|
|
38
|
+
const NameSchema = z.object({
|
|
39
|
+
name: z.string().trim().min(1).describe("Your name"),
|
|
40
|
+
});
|
|
41
|
+
/** The second question's answer. */
|
|
42
|
+
const ColorSchema = z.object({
|
|
43
|
+
color: z
|
|
44
|
+
.enum(["red", "green", "blue"])
|
|
45
|
+
.describe("Your favorite of the three"),
|
|
46
|
+
});
|
|
47
|
+
/** Ask for the name: the first round, and a repeat of it after a bad answer. */
|
|
48
|
+
const askForName = async (message) => inputRequired({
|
|
49
|
+
inputRequests: {
|
|
50
|
+
name: inputRequired.elicit({ message, requestedSchema: NameSchema }),
|
|
51
|
+
},
|
|
52
|
+
requestState: await roundStateCodec.mint({ step: "awaiting-name" }),
|
|
53
|
+
});
|
|
54
|
+
/** Ask for the color, carrying the name the first round proved. */
|
|
55
|
+
const askForColor = async (name) => inputRequired({
|
|
56
|
+
inputRequests: {
|
|
57
|
+
color: inputRequired.elicit({
|
|
58
|
+
message: `Thanks, ${name}. Which color do you like best?`,
|
|
59
|
+
requestedSchema: ColorSchema,
|
|
60
|
+
}),
|
|
61
|
+
},
|
|
62
|
+
requestState: await roundStateCodec.mint({ step: "awaiting-color", name }),
|
|
63
|
+
});
|
|
64
|
+
/** The result for a user who declined or cancelled a question. */
|
|
65
|
+
const stopped = (action, step) => ({
|
|
66
|
+
content: [
|
|
67
|
+
{ type: "text", text: `Stopped: the user chose ${action} at ${step}.` },
|
|
68
|
+
],
|
|
69
|
+
isError: true,
|
|
70
|
+
});
|
|
71
|
+
/**
|
|
72
|
+
* Registers the 'trigger-multi-step-elicitation' tool.
|
|
73
|
+
*
|
|
74
|
+
* The tool needs a client that supports form-mode elicitation, so it is gated
|
|
75
|
+
* like `trigger-elicitation-request`. Each entry reads the phase from the
|
|
76
|
+
* verified `requestState`:
|
|
77
|
+
* - none: the first call, which asks for the name;
|
|
78
|
+
* - `awaiting-name`: reads the name, asking again if it is missing or blank,
|
|
79
|
+
* then asks for the color with the name in the next state;
|
|
80
|
+
* - `awaiting-color`: reads the color, asking again if it is not one of the
|
|
81
|
+
* choices, then returns both answers.
|
|
82
|
+
* A declined or cancelled question ends the flow with an error result.
|
|
83
|
+
*
|
|
84
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
85
|
+
*/
|
|
86
|
+
export const registerTriggerMultiStepElicitationTool = (server) => {
|
|
87
|
+
server.registerTool(name, config, async (_args, ctx) => {
|
|
88
|
+
const state = ctx.mcpReq.requestState();
|
|
89
|
+
const responses = ctx.mcpReq.inputResponses;
|
|
90
|
+
switch (state?.step) {
|
|
91
|
+
case "awaiting-name": {
|
|
92
|
+
const view = inputResponse(responses, "name");
|
|
93
|
+
if (view.kind === "elicit" && view.action !== "accept") {
|
|
94
|
+
return stopped(view.action, "the name question");
|
|
95
|
+
}
|
|
96
|
+
const answer = acceptedContent(responses, "name", NameSchema);
|
|
97
|
+
if (answer === undefined) {
|
|
98
|
+
return askForName("A name is required. What is your name?");
|
|
99
|
+
}
|
|
100
|
+
return askForColor(answer.name);
|
|
101
|
+
}
|
|
102
|
+
case "awaiting-color": {
|
|
103
|
+
const view = inputResponse(responses, "color");
|
|
104
|
+
if (view.kind === "elicit" && view.action !== "accept") {
|
|
105
|
+
return stopped(view.action, "the color question");
|
|
106
|
+
}
|
|
107
|
+
const answer = acceptedContent(responses, "color", ColorSchema);
|
|
108
|
+
const name = String(state.name);
|
|
109
|
+
if (answer === undefined)
|
|
110
|
+
return askForColor(name);
|
|
111
|
+
return {
|
|
112
|
+
content: [
|
|
113
|
+
{
|
|
114
|
+
type: "text",
|
|
115
|
+
text: `${name}'s favorite color is ${answer.color}.`,
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
structuredContent: { name, color: answer.color },
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
default:
|
|
122
|
+
return askForName("What is your name?");
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// The `trigger-resource-update` tool: announces that a resource changed, so a
|
|
2
|
+
// client can watch `notifications/resources/updated` arrive for a resource it
|
|
3
|
+
// asked to be told about.
|
|
4
|
+
//
|
|
5
|
+
// How the client asks depends on the protocol era (#4852). A 2026-07-28
|
|
6
|
+
// client names the URI in the `resourceSubscriptions` of its
|
|
7
|
+
// `subscriptions/listen` filter, and the notification is delivered only on
|
|
8
|
+
// such a stream. A 2025-era client calls `resources/subscribe`, and the
|
|
9
|
+
// notification is sent only once it has. This replaces the timed updates of
|
|
10
|
+
// `toggle-subscriber-updates` (2025-era only) with one sent on demand.
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
// Tool input schema
|
|
13
|
+
const TriggerResourceUpdateSchema = z.object({
|
|
14
|
+
uri: z
|
|
15
|
+
.string()
|
|
16
|
+
.describe("The URI of the resource to announce as updated, e.g. demo://resource/dynamic/text/1"),
|
|
17
|
+
});
|
|
18
|
+
// Tool configuration
|
|
19
|
+
const name = "trigger-resource-update";
|
|
20
|
+
const config = {
|
|
21
|
+
title: "Trigger Resource Update Tool",
|
|
22
|
+
description: "Sends a notifications/resources/updated for a resource URI. On protocol version 2026-07-28 it is " +
|
|
23
|
+
"delivered on each subscriptions/listen stream whose resourceSubscriptions name the URI; a 2025-era " +
|
|
24
|
+
"client receives it once it has called resources/subscribe for the URI.",
|
|
25
|
+
inputSchema: TriggerResourceUpdateSchema,
|
|
26
|
+
annotations: {
|
|
27
|
+
readOnlyHint: true,
|
|
28
|
+
destructiveHint: false,
|
|
29
|
+
idempotentHint: false,
|
|
30
|
+
openWorldHint: false,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Registers the 'trigger-resource-update' tool.
|
|
35
|
+
*
|
|
36
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
37
|
+
* @param {ServerContext} context - Where the instance's change notifications go.
|
|
38
|
+
*/
|
|
39
|
+
export const registerTriggerResourceUpdateTool = (server, context) => {
|
|
40
|
+
server.registerTool(name, config, async (args) => {
|
|
41
|
+
const { uri } = TriggerResourceUpdateSchema.parse(args);
|
|
42
|
+
context.publish.resourceUpdated(uri);
|
|
43
|
+
return {
|
|
44
|
+
content: [
|
|
45
|
+
{
|
|
46
|
+
type: "text",
|
|
47
|
+
text: `Sent notifications/resources/updated for ${uri} to each subscriber`,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
};
|
|
@@ -1,7 +1,23 @@
|
|
|
1
|
+
// The `trigger-sampling-request-async` tool: a task on the Tasks extension
|
|
2
|
+
// (`io.modelcontextprotocol/tasks`) that needs an LLM completion from the
|
|
3
|
+
// client before it can finish, on protocol version 2026-07-28 (#4852).
|
|
4
|
+
//
|
|
5
|
+
// On 2025-11-25 this tool sent the client a task-augmented
|
|
6
|
+
// `sampling/createMessage` 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 sampling request. The client runs it and
|
|
10
|
+
// answers with `tasks/update`, and the task completes with the result.
|
|
11
|
+
// Sampling is deprecated as of 2026-07-28 (SEP-2577).
|
|
12
|
+
import { inputRequired, inputResponse, } from "@modelcontextprotocol/server";
|
|
1
13
|
import { z } from "zod";
|
|
14
|
+
import { createTask } from "../server/tasks.js";
|
|
2
15
|
// Tool input schema
|
|
3
16
|
const TriggerSamplingRequestAsyncSchema = z.object({
|
|
4
|
-
prompt: z
|
|
17
|
+
prompt: z
|
|
18
|
+
.string()
|
|
19
|
+
.max(10000)
|
|
20
|
+
.describe("The prompt to send to the LLM (at most 10,000 characters; the task keeps it until it expires)"),
|
|
5
21
|
maxTokens: z
|
|
6
22
|
.number()
|
|
7
23
|
.default(100)
|
|
@@ -11,9 +27,10 @@ const TriggerSamplingRequestAsyncSchema = z.object({
|
|
|
11
27
|
const name = "trigger-sampling-request-async";
|
|
12
28
|
const config = {
|
|
13
29
|
title: "Trigger Async Sampling Request Tool",
|
|
14
|
-
description: "
|
|
15
|
-
"
|
|
16
|
-
"
|
|
30
|
+
description: "Runs as a task on the io.modelcontextprotocol/tasks extension (protocol version 2026-07-28) that needs an " +
|
|
31
|
+
"LLM completion: the task waits in status input_required, carrying a sampling request, until the client " +
|
|
32
|
+
"answers it with tasks/update, then completes with the result. Deprecated: Sampling is deprecated as of " +
|
|
33
|
+
"protocol version 2026-07-28.",
|
|
17
34
|
inputSchema: TriggerSamplingRequestAsyncSchema,
|
|
18
35
|
annotations: {
|
|
19
36
|
readOnlyHint: false,
|
|
@@ -22,153 +39,72 @@ const config = {
|
|
|
22
39
|
openWorldHint: true,
|
|
23
40
|
},
|
|
24
41
|
};
|
|
25
|
-
// Poll interval in milliseconds
|
|
26
|
-
const POLL_INTERVAL = 1000;
|
|
27
|
-
// Maximum poll attempts before timeout
|
|
28
|
-
const MAX_POLL_ATTEMPTS = 60;
|
|
29
42
|
/**
|
|
30
|
-
* Registers the 'trigger-sampling-request-async' tool.
|
|
43
|
+
* Registers the 'trigger-sampling-request-async' tool, for 2026-07-28 clients.
|
|
31
44
|
*
|
|
32
|
-
*
|
|
33
|
-
* -
|
|
34
|
-
*
|
|
35
|
-
* - Server polls client's tasks/get endpoint for status
|
|
36
|
-
* - Server fetches final result from client's tasks/result endpoint
|
|
45
|
+
* A client that does not declare both the tasks extension and sampling is
|
|
46
|
+
* refused with -32021 before the tool runs (`guardTaskTools`): the SDK does
|
|
47
|
+
* not check the requests a task carries in its status.
|
|
37
48
|
*
|
|
38
49
|
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
39
50
|
*/
|
|
40
51
|
export const registerTriggerSamplingRequestAsyncTool = (server) => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
server.registerTool(name, config, async (args) => {
|
|
53
|
+
const { prompt, maxTokens } = TriggerSamplingRequestAsyncSchema.parse(args);
|
|
54
|
+
const createdAt = Date.now();
|
|
55
|
+
let result;
|
|
56
|
+
let answeredAt = createdAt;
|
|
57
|
+
// The key of the outstanding request. A key is never reused once
|
|
58
|
+
// answered, so a re-asked request gets the next one.
|
|
59
|
+
let attempt = 1;
|
|
60
|
+
const key = () => `sampling-${attempt}`;
|
|
61
|
+
return createTask({
|
|
62
|
+
stateAt: () => result === undefined
|
|
63
|
+
? {
|
|
64
|
+
// Waiting since the request, or the last re-ask, went out
|
|
65
|
+
since: answeredAt,
|
|
66
|
+
status: "input_required",
|
|
67
|
+
statusMessage: "Waiting for the client's LLM to respond",
|
|
68
|
+
inputRequests: {
|
|
69
|
+
[key()]: inputRequired.createMessage({
|
|
70
|
+
messages: [
|
|
71
|
+
{
|
|
72
|
+
role: "user",
|
|
73
|
+
content: {
|
|
74
|
+
type: "text",
|
|
75
|
+
text: `Resource ${name} context: ${prompt}`,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
systemPrompt: "You are a helpful test server.",
|
|
80
|
+
maxTokens,
|
|
81
|
+
temperature: 0.7,
|
|
82
|
+
}),
|
|
58
83
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
84
|
+
}
|
|
85
|
+
: {
|
|
86
|
+
since: answeredAt,
|
|
87
|
+
status: "completed",
|
|
88
|
+
statusMessage: "Sampling complete",
|
|
89
|
+
result: {
|
|
90
|
+
content: [
|
|
91
|
+
{
|
|
63
92
|
type: "text",
|
|
64
|
-
text: `
|
|
93
|
+
text: `LLM sampling result: \n${JSON.stringify(result, null, 2)}`,
|
|
65
94
|
},
|
|
66
|
-
|
|
67
|
-
],
|
|
68
|
-
systemPrompt: "You are a helpful test server.",
|
|
69
|
-
maxTokens,
|
|
70
|
-
temperature: 0.7,
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
// Send the sampling request
|
|
74
|
-
// Client may return either:
|
|
75
|
-
// - CreateMessageResult (synchronous execution)
|
|
76
|
-
// - CreateTaskResult (task-based execution with { task } object)
|
|
77
|
-
const samplingResponse = await extra.sendRequest(request, z.union([
|
|
78
|
-
// CreateTaskResult - client created a task
|
|
79
|
-
z.object({
|
|
80
|
-
task: z.object({
|
|
81
|
-
taskId: z.string(),
|
|
82
|
-
status: z.string(),
|
|
83
|
-
pollInterval: z.number().optional(),
|
|
84
|
-
statusMessage: z.string().optional(),
|
|
85
|
-
}),
|
|
86
|
-
}),
|
|
87
|
-
// CreateMessageResult - synchronous execution
|
|
88
|
-
z.object({
|
|
89
|
-
role: z.string(),
|
|
90
|
-
content: z.any(),
|
|
91
|
-
model: z.string(),
|
|
92
|
-
stopReason: z.string().optional(),
|
|
93
|
-
}),
|
|
94
|
-
]));
|
|
95
|
-
// Check if client returned CreateTaskResult (has task object)
|
|
96
|
-
const isTaskResult = "task" in samplingResponse && samplingResponse.task;
|
|
97
|
-
if (!isTaskResult) {
|
|
98
|
-
// Client executed synchronously - return the direct response
|
|
99
|
-
return {
|
|
100
|
-
content: [
|
|
101
|
-
{
|
|
102
|
-
type: "text",
|
|
103
|
-
text: `[SYNC] Client executed synchronously:\n${JSON.stringify(samplingResponse, null, 2)}`,
|
|
104
|
-
},
|
|
105
|
-
],
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
const taskId = samplingResponse.task.taskId;
|
|
109
|
-
const statusMessages = [];
|
|
110
|
-
statusMessages.push(`Task created: ${taskId}`);
|
|
111
|
-
// Poll for task completion
|
|
112
|
-
let attempts = 0;
|
|
113
|
-
let taskStatus = samplingResponse.task.status;
|
|
114
|
-
// A task that is already finished when created is never polled, so
|
|
115
|
-
// its status message has to come from the CreateTaskResult
|
|
116
|
-
let taskStatusMessage = samplingResponse.task.statusMessage;
|
|
117
|
-
while (taskStatus !== "completed" &&
|
|
118
|
-
taskStatus !== "failed" &&
|
|
119
|
-
taskStatus !== "cancelled" &&
|
|
120
|
-
attempts < MAX_POLL_ATTEMPTS) {
|
|
121
|
-
// Wait before polling
|
|
122
|
-
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));
|
|
123
|
-
attempts++;
|
|
124
|
-
// Get task status from client
|
|
125
|
-
const pollResult = await extra.sendRequest({
|
|
126
|
-
method: "tasks/get",
|
|
127
|
-
params: { taskId },
|
|
128
|
-
}, z.looseObject({
|
|
129
|
-
status: z.string(),
|
|
130
|
-
statusMessage: z.string().optional(),
|
|
131
|
-
}));
|
|
132
|
-
taskStatus = pollResult.status;
|
|
133
|
-
taskStatusMessage = pollResult.statusMessage;
|
|
134
|
-
statusMessages.push(`Poll ${attempts}: ${taskStatus}${taskStatusMessage ? ` - ${taskStatusMessage}` : ""}`);
|
|
135
|
-
}
|
|
136
|
-
// Check for timeout
|
|
137
|
-
if (attempts >= MAX_POLL_ATTEMPTS) {
|
|
138
|
-
return {
|
|
139
|
-
content: [
|
|
140
|
-
{
|
|
141
|
-
type: "text",
|
|
142
|
-
text: `[TIMEOUT] Task timed out after ${MAX_POLL_ATTEMPTS} poll attempts\n\nProgress:\n${statusMessages.join("\n")}`,
|
|
143
|
-
},
|
|
144
|
-
],
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
// Check for failure/cancellation
|
|
148
|
-
if (taskStatus === "failed" || taskStatus === "cancelled") {
|
|
149
|
-
return {
|
|
150
|
-
content: [
|
|
151
|
-
{
|
|
152
|
-
type: "text",
|
|
153
|
-
text: `[${taskStatus.toUpperCase()}] ${taskStatusMessage || "No message"}\n\nProgress:\n${statusMessages.join("\n")}`,
|
|
154
|
-
},
|
|
155
|
-
],
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
// Fetch the final result
|
|
159
|
-
const result = await extra.sendRequest({
|
|
160
|
-
method: "tasks/result",
|
|
161
|
-
params: { taskId },
|
|
162
|
-
}, z.any());
|
|
163
|
-
// Return the result with status history
|
|
164
|
-
return {
|
|
165
|
-
content: [
|
|
166
|
-
{
|
|
167
|
-
type: "text",
|
|
168
|
-
text: `[COMPLETED] Async sampling completed!\n\n**Progress:**\n${statusMessages.join("\n")}\n\n**Result:**\n${JSON.stringify(result, null, 2)}`,
|
|
95
|
+
],
|
|
169
96
|
},
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
97
|
+
},
|
|
98
|
+
receive: (responses, now) => {
|
|
99
|
+
answeredAt = now;
|
|
100
|
+
const answer = inputResponse(responses, key());
|
|
101
|
+
// An answer of another kind spends the request: ask again under a
|
|
102
|
+
// new key.
|
|
103
|
+
if (answer.kind === "sampling")
|
|
104
|
+
result = answer.result;
|
|
105
|
+
else
|
|
106
|
+
attempt++;
|
|
107
|
+
},
|
|
108
|
+
}, { ttlMs: 10 * 60_000, pollIntervalMs: 1000 });
|
|
109
|
+
});
|
|
174
110
|
};
|
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
// The `trigger-sampling-request` tool: asks the client's LLM for a completion
|
|
2
|
+
// and returns what it sent back.
|
|
3
|
+
//
|
|
4
|
+
// Written once for both protocol eras with a multi-round-trip request
|
|
5
|
+
// (#4852): the first call returns `input_required` carrying a
|
|
6
|
+
// `sampling/createMessage` request, the client runs it and retries, and the
|
|
7
|
+
// retry returns the result. A 2026-07-28 client does that in-band; for a
|
|
8
|
+
// 2025-era client the SDK's legacy shim sends a real `sampling/createMessage`
|
|
9
|
+
// request and re-enters the handler with the answer. Sampling is deprecated
|
|
10
|
+
// as of 2026-07-28 (SEP-2577).
|
|
11
|
+
import { inputRequired, inputResponse, } from "@modelcontextprotocol/server";
|
|
2
12
|
import { z } from "zod";
|
|
3
13
|
// Tool input schema
|
|
4
14
|
const TriggerSamplingRequestSchema = z.object({
|
|
@@ -12,7 +22,8 @@ const TriggerSamplingRequestSchema = z.object({
|
|
|
12
22
|
const name = "trigger-sampling-request";
|
|
13
23
|
const config = {
|
|
14
24
|
title: "Trigger Sampling Request Tool",
|
|
15
|
-
description: "Trigger a Request from the Server for LLM Sampling"
|
|
25
|
+
description: "Trigger a Request from the Server for LLM Sampling. Deprecated: Sampling is deprecated as of protocol " +
|
|
26
|
+
"version 2026-07-28. The server asks with a multi-round-trip request on every protocol version.",
|
|
16
27
|
inputSchema: TriggerSamplingRequestSchema,
|
|
17
28
|
annotations: {
|
|
18
29
|
readOnlyHint: false,
|
|
@@ -24,54 +35,49 @@ const config = {
|
|
|
24
35
|
/**
|
|
25
36
|
* Registers the 'trigger-sampling-request' tool.
|
|
26
37
|
*
|
|
27
|
-
*
|
|
38
|
+
* The tool needs a client that supports sampling: on a 2025-era connection
|
|
39
|
+
* the server factory registers it only for one that declared the capability,
|
|
40
|
+
* and on 2026-07-28 the SDK refuses the embedded request (-32021) of a client
|
|
41
|
+
* whose request did not declare it.
|
|
28
42
|
*
|
|
29
|
-
* The
|
|
30
|
-
*
|
|
31
|
-
* - Constructs a `sampling/createMessage` request object using provided prompt and maximum tokens.
|
|
32
|
-
* - Sends the request to the server for sampling.
|
|
33
|
-
* - Formats and returns the sampling result content to the client.
|
|
43
|
+
* The first round returns the `sampling/createMessage` request built from the
|
|
44
|
+
* prompt and token limit; the retry formats the client's result.
|
|
34
45
|
*
|
|
35
46
|
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
36
47
|
*/
|
|
37
48
|
export const registerTriggerSamplingRequestTool = (server) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
content: {
|
|
54
|
-
type: "text",
|
|
55
|
-
text: `Resource ${name} context: ${prompt}`,
|
|
49
|
+
server.registerTool(name, config, async (args, ctx) => {
|
|
50
|
+
const { prompt, maxTokens } = TriggerSamplingRequestSchema.parse(args);
|
|
51
|
+
const answer = inputResponse(ctx.mcpReq.inputResponses, "sampling");
|
|
52
|
+
// First round: ask the client to sample
|
|
53
|
+
if (answer.kind !== "sampling") {
|
|
54
|
+
return inputRequired({
|
|
55
|
+
inputRequests: {
|
|
56
|
+
sampling: inputRequired.createMessage({
|
|
57
|
+
messages: [
|
|
58
|
+
{
|
|
59
|
+
role: "user",
|
|
60
|
+
content: {
|
|
61
|
+
type: "text",
|
|
62
|
+
text: `Resource ${name} context: ${prompt}`,
|
|
63
|
+
},
|
|
56
64
|
},
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
],
|
|
66
|
+
systemPrompt: "You are a helpful test server.",
|
|
67
|
+
maxTokens,
|
|
68
|
+
temperature: 0.7,
|
|
69
|
+
}),
|
|
62
70
|
},
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
});
|
|
76
|
-
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
// Return the result to the client
|
|
74
|
+
return {
|
|
75
|
+
content: [
|
|
76
|
+
{
|
|
77
|
+
type: "text",
|
|
78
|
+
text: `LLM sampling result: \n${JSON.stringify(answer.result, null, 2)}`,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
};
|
|
82
|
+
});
|
|
77
83
|
};
|