@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,57 @@
|
|
|
1
|
+
// The `trigger-list-changed` tool: announces that the server's tool, prompt
|
|
2
|
+
// or resource list changed, so a client can watch the notification arrive.
|
|
3
|
+
//
|
|
4
|
+
// Where the notification goes depends on the protocol era (#4852). A
|
|
5
|
+
// 2025-era client gets it unsolicited. A 2026-07-28 client gets it only on a
|
|
6
|
+
// `subscriptions/listen` stream whose filter asked for that list
|
|
7
|
+
// (`toolsListChanged`, `promptsListChanged` or `resourcesListChanged`). The
|
|
8
|
+
// server context's publisher picks the route: the instance itself on stdio
|
|
9
|
+
// and on a 2025-era session, the handler's subscription bus on the 2026-07-28
|
|
10
|
+
// HTTP path.
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
// Tool input schema
|
|
13
|
+
const TriggerListChangedSchema = z.object({
|
|
14
|
+
list: z
|
|
15
|
+
.enum(["tools", "prompts", "resources"])
|
|
16
|
+
.describe("Which list to announce as changed"),
|
|
17
|
+
});
|
|
18
|
+
// Tool configuration
|
|
19
|
+
const name = "trigger-list-changed";
|
|
20
|
+
const config = {
|
|
21
|
+
title: "Trigger List Changed Tool",
|
|
22
|
+
description: "Sends a notifications/<list>/list_changed for the tool, prompt or resource list. On protocol version " +
|
|
23
|
+
"2026-07-28 it is delivered on each subscriptions/listen stream that asked for that list; a 2025-era " +
|
|
24
|
+
"client receives it unsolicited.",
|
|
25
|
+
inputSchema: TriggerListChangedSchema,
|
|
26
|
+
annotations: {
|
|
27
|
+
readOnlyHint: true,
|
|
28
|
+
destructiveHint: false,
|
|
29
|
+
idempotentHint: false,
|
|
30
|
+
openWorldHint: false,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Registers the 'trigger-list-changed' 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 registerTriggerListChangedTool = (server, context) => {
|
|
40
|
+
server.registerTool(name, config, async (args) => {
|
|
41
|
+
const { list } = TriggerListChangedSchema.parse(args);
|
|
42
|
+
if (list === "tools")
|
|
43
|
+
context.publish.toolsChanged();
|
|
44
|
+
else if (list === "prompts")
|
|
45
|
+
context.publish.promptsChanged();
|
|
46
|
+
else
|
|
47
|
+
context.publish.resourcesChanged();
|
|
48
|
+
return {
|
|
49
|
+
content: [
|
|
50
|
+
{
|
|
51
|
+
type: "text",
|
|
52
|
+
text: `Sent notifications/${list}/list_changed`,
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// The `trigger-log-messages` tool: sends one log message at each logging
|
|
2
|
+
// level during the call, so a client can see which ones its level lets
|
|
3
|
+
// through.
|
|
4
|
+
//
|
|
5
|
+
// The level is set differently on each protocol era (#4852). A 2026-07-28
|
|
6
|
+
// client names it per request, in the request's `_meta`
|
|
7
|
+
// (`io.modelcontextprotocol/logLevel`), and a request that names none gets no
|
|
8
|
+
// log messages at all. A 2025-era client sets it for the whole session with
|
|
9
|
+
// `logging/setLevel`. The SDK applies whichever filter the request has, so
|
|
10
|
+
// the handler is the same for both. Logging is deprecated as of 2026-07-28
|
|
11
|
+
// (SEP-2577); this replaces `toggle-simulated-logging`, whose timer relies on
|
|
12
|
+
// `logging/setLevel` and unsolicited messages, which 2026-07-28 removes.
|
|
13
|
+
/** Every logging level, from least to most severe. */
|
|
14
|
+
export const LOGGING_LEVELS = [
|
|
15
|
+
"debug",
|
|
16
|
+
"info",
|
|
17
|
+
"notice",
|
|
18
|
+
"warning",
|
|
19
|
+
"error",
|
|
20
|
+
"critical",
|
|
21
|
+
"alert",
|
|
22
|
+
"emergency",
|
|
23
|
+
];
|
|
24
|
+
// Tool configuration
|
|
25
|
+
const name = "trigger-log-messages";
|
|
26
|
+
const config = {
|
|
27
|
+
title: "Trigger Log Messages Tool",
|
|
28
|
+
description: "Sends one log message at each logging level during the call. On protocol version 2026-07-28 only " +
|
|
29
|
+
"the messages at or above the level the request names in _meta (io.modelcontextprotocol/logLevel) " +
|
|
30
|
+
"are sent, and none if it names no level; a 2025-era client gets those at or above the level it set " +
|
|
31
|
+
"with logging/setLevel. Deprecated: logging is deprecated as of protocol version 2026-07-28.",
|
|
32
|
+
inputSchema: {},
|
|
33
|
+
annotations: {
|
|
34
|
+
readOnlyHint: true,
|
|
35
|
+
destructiveHint: false,
|
|
36
|
+
idempotentHint: true,
|
|
37
|
+
openWorldHint: false,
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Registers the 'trigger-log-messages' tool.
|
|
42
|
+
*
|
|
43
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
44
|
+
*/
|
|
45
|
+
export const registerTriggerLogMessagesTool = (server) => {
|
|
46
|
+
server.registerTool(name, config, async (_args, ctx) => {
|
|
47
|
+
for (const level of LOGGING_LEVELS) {
|
|
48
|
+
await ctx.mcpReq.log(level, `${level}-level message`, "everything");
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
content: [
|
|
52
|
+
{
|
|
53
|
+
type: "text",
|
|
54
|
+
text: `Sent one log message at each of ${LOGGING_LEVELS.length} levels (${LOGGING_LEVELS.join(", ")}). ` +
|
|
55
|
+
"The ones below this request's log level were dropped.",
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
// Tool input schema
|
|
3
|
+
const TriggerLongRunningOperationSchema = z.object({
|
|
4
|
+
duration: z
|
|
5
|
+
.number()
|
|
6
|
+
.default(10)
|
|
7
|
+
.describe("Duration of the operation in seconds"),
|
|
8
|
+
steps: z.number().default(5).describe("Number of steps in the operation"),
|
|
9
|
+
});
|
|
10
|
+
// Tool configuration
|
|
11
|
+
const name = "trigger-long-running-operation";
|
|
12
|
+
const config = {
|
|
13
|
+
title: "Trigger Long Running Operation Tool",
|
|
14
|
+
description: "Demonstrates a long running operation with progress updates.",
|
|
15
|
+
inputSchema: TriggerLongRunningOperationSchema,
|
|
16
|
+
annotations: {
|
|
17
|
+
readOnlyHint: true,
|
|
18
|
+
destructiveHint: false,
|
|
19
|
+
idempotentHint: true,
|
|
20
|
+
openWorldHint: false,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Registers the 'trigger-tong-running-operation' tool.
|
|
25
|
+
*
|
|
26
|
+
* The registered tool starts a long-running operation defined by a specific duration and
|
|
27
|
+
* number of steps.
|
|
28
|
+
*
|
|
29
|
+
* Progress notifications are sent back to the client at each step if a `progressToken`
|
|
30
|
+
* is provided in the metadata.
|
|
31
|
+
*
|
|
32
|
+
* At the end of the operation, the tool returns a message indicating the completion of the
|
|
33
|
+
* operation, including the total duration and steps.
|
|
34
|
+
*
|
|
35
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
36
|
+
*/
|
|
37
|
+
export const registerTriggerLongRunningOperationTool = (server) => {
|
|
38
|
+
server.registerTool(name, config, async (args, ctx) => {
|
|
39
|
+
const validatedArgs = TriggerLongRunningOperationSchema.parse(args);
|
|
40
|
+
const { duration, steps } = validatedArgs;
|
|
41
|
+
const stepDuration = duration / steps;
|
|
42
|
+
const progressToken = ctx.mcpReq._meta?.progressToken;
|
|
43
|
+
for (let i = 1; i < steps + 1; i++) {
|
|
44
|
+
await new Promise((resolve) => setTimeout(resolve, stepDuration * 1000));
|
|
45
|
+
if (progressToken !== undefined) {
|
|
46
|
+
await server.server.notification({
|
|
47
|
+
method: "notifications/progress",
|
|
48
|
+
params: {
|
|
49
|
+
progress: i,
|
|
50
|
+
total: steps,
|
|
51
|
+
progressToken,
|
|
52
|
+
},
|
|
53
|
+
}, { relatedRequestId: ctx.mcpReq.id });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
content: [
|
|
58
|
+
{
|
|
59
|
+
type: "text",
|
|
60
|
+
text: `Long running operation completed. Duration: ${duration} seconds, Steps: ${steps}.`,
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
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";
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
import { createTask } from "../server/tasks.js";
|
|
15
|
+
// Tool input schema
|
|
16
|
+
const TriggerSamplingRequestAsyncSchema = z.object({
|
|
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)"),
|
|
21
|
+
maxTokens: z
|
|
22
|
+
.number()
|
|
23
|
+
.default(100)
|
|
24
|
+
.describe("Maximum number of tokens to generate"),
|
|
25
|
+
});
|
|
26
|
+
// Tool configuration
|
|
27
|
+
const name = "trigger-sampling-request-async";
|
|
28
|
+
const config = {
|
|
29
|
+
title: "Trigger Async Sampling Request Tool",
|
|
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.",
|
|
34
|
+
inputSchema: TriggerSamplingRequestAsyncSchema,
|
|
35
|
+
annotations: {
|
|
36
|
+
readOnlyHint: false,
|
|
37
|
+
destructiveHint: false,
|
|
38
|
+
idempotentHint: false,
|
|
39
|
+
openWorldHint: true,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Registers the 'trigger-sampling-request-async' tool, for 2026-07-28 clients.
|
|
44
|
+
*
|
|
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.
|
|
48
|
+
*
|
|
49
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
50
|
+
*/
|
|
51
|
+
export const registerTriggerSamplingRequestAsyncTool = (server) => {
|
|
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
|
+
}),
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
: {
|
|
86
|
+
since: answeredAt,
|
|
87
|
+
status: "completed",
|
|
88
|
+
statusMessage: "Sampling complete",
|
|
89
|
+
result: {
|
|
90
|
+
content: [
|
|
91
|
+
{
|
|
92
|
+
type: "text",
|
|
93
|
+
text: `LLM sampling result: \n${JSON.stringify(result, null, 2)}`,
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
},
|
|
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
|
+
});
|
|
110
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
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";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
// Tool input schema
|
|
14
|
+
const TriggerSamplingRequestSchema = z.object({
|
|
15
|
+
prompt: z.string().describe("The prompt to send to the LLM"),
|
|
16
|
+
maxTokens: z
|
|
17
|
+
.number()
|
|
18
|
+
.default(100)
|
|
19
|
+
.describe("Maximum number of tokens to generate"),
|
|
20
|
+
});
|
|
21
|
+
// Tool configuration
|
|
22
|
+
const name = "trigger-sampling-request";
|
|
23
|
+
const config = {
|
|
24
|
+
title: "Trigger Sampling Request Tool",
|
|
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.",
|
|
27
|
+
inputSchema: TriggerSamplingRequestSchema,
|
|
28
|
+
annotations: {
|
|
29
|
+
readOnlyHint: false,
|
|
30
|
+
destructiveHint: false,
|
|
31
|
+
idempotentHint: false,
|
|
32
|
+
openWorldHint: true,
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Registers the 'trigger-sampling-request' tool.
|
|
37
|
+
*
|
|
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.
|
|
42
|
+
*
|
|
43
|
+
* The first round returns the `sampling/createMessage` request built from the
|
|
44
|
+
* prompt and token limit; the retry formats the client's result.
|
|
45
|
+
*
|
|
46
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
47
|
+
*/
|
|
48
|
+
export const registerTriggerSamplingRequestTool = (server) => {
|
|
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
|
+
},
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
systemPrompt: "You are a helpful test server.",
|
|
67
|
+
maxTokens,
|
|
68
|
+
temperature: 0.7,
|
|
69
|
+
}),
|
|
70
|
+
},
|
|
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
|
+
});
|
|
83
|
+
};
|