@modelcontextprotocol/server-everything 1.0.0 → 2.0.1
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 +37 -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 +26 -6
- package/dist/transports/stdio.js +42 -22
- package/dist/transports/streamableHttp.js +110 -14
- package/package.json +12 -5
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// The server side of the Tasks extension (`io.modelcontextprotocol/tasks`,
|
|
2
|
+
// SEP-2663) on the 2026-07-28 revision (#4852).
|
|
3
|
+
//
|
|
4
|
+
// A tool that runs as a task answers `tools/call` with a task handle
|
|
5
|
+
// (`resultType: "task"`) instead of its result. The client then polls
|
|
6
|
+
// `tasks/get` for the task's status, answers an `input_required` status with
|
|
7
|
+
// `tasks/update`, and may stop the task with `tasks/cancel`; the final result
|
|
8
|
+
// arrives inside the `completed` status. The SDK has no server implementation
|
|
9
|
+
// of the extension (modelcontextprotocol/ext-tasks#30), so this module is one,
|
|
10
|
+
// for the everything server's three task tools.
|
|
11
|
+
//
|
|
12
|
+
// A task outlives the request that created it, and on the 2026-07-28 HTTP
|
|
13
|
+
// path every request is served by a fresh server instance, so tasks live in a
|
|
14
|
+
// process-wide store keyed by the task id. The id is a random UUID the server
|
|
15
|
+
// mints: it is the handle, and knowing it is what lets a client reach the
|
|
16
|
+
// task. Nothing runs in the background. A task's status is worked out from the
|
|
17
|
+
// clock when it is read, so there are no timers to stop; a task past its TTL
|
|
18
|
+
// is dropped the next time the store is touched.
|
|
19
|
+
//
|
|
20
|
+
// The extension is served on 2026-07-28 only. The 2025-11-25 revision had its
|
|
21
|
+
// own, different task methods in the core protocol; they are not served.
|
|
22
|
+
import { randomUUID } from "node:crypto";
|
|
23
|
+
import { CLIENT_CAPABILITIES_META_KEY, MissingRequiredClientCapabilityError, ProtocolError, ProtocolErrorCode, } from "@modelcontextprotocol/server";
|
|
24
|
+
import { z } from "zod";
|
|
25
|
+
/** The extension's identifier, as declared in `capabilities.extensions`. */
|
|
26
|
+
export const TASKS_EXTENSION = "io.modelcontextprotocol/tasks";
|
|
27
|
+
/** Every live task, by id, across every server instance in the process. */
|
|
28
|
+
const tasks = new Map();
|
|
29
|
+
/**
|
|
30
|
+
* The most tasks the process keeps. A task holds its tool's arguments (each
|
|
31
|
+
* task tool bounds their length) and the client's answers (bounded by the
|
|
32
|
+
* transports' request size) until its TTL, so without a cap a burst of calls
|
|
33
|
+
* could exhaust memory first. A live task is kept until its TTL, since the
|
|
34
|
+
* extension lets a server discard one only after that, so at the cap a new
|
|
35
|
+
* task is refused rather than an old one dropped.
|
|
36
|
+
*/
|
|
37
|
+
export const MAX_TASKS = 100;
|
|
38
|
+
/**
|
|
39
|
+
* The most bytes of answers, serialized, one `tasks/update` may hand a task.
|
|
40
|
+
* A task keeps its answers until its TTL, and a transport accepts messages
|
|
41
|
+
* far larger than any of these tasks needs (10 MiB on stdio), so with
|
|
42
|
+
* `MAX_TASKS` this bounds what the store holds.
|
|
43
|
+
*/
|
|
44
|
+
export const MAX_ANSWER_BYTES = 64 * 1024;
|
|
45
|
+
/** Drop every task past its TTL. */
|
|
46
|
+
function sweep(now) {
|
|
47
|
+
for (const [taskId, task] of tasks) {
|
|
48
|
+
if (task.createdAt + task.ttlMs <= now)
|
|
49
|
+
tasks.delete(taskId);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** The task with this id, or a -32602 error naming it as unknown. */
|
|
53
|
+
function find(taskId, now) {
|
|
54
|
+
sweep(now);
|
|
55
|
+
const task = tasks.get(taskId);
|
|
56
|
+
if (!task) {
|
|
57
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Unknown task: ${taskId}`);
|
|
58
|
+
}
|
|
59
|
+
return task;
|
|
60
|
+
}
|
|
61
|
+
/** The task's state now, honoring a cancellation. */
|
|
62
|
+
function stateOf(task, now) {
|
|
63
|
+
return task.cancelledAt !== undefined
|
|
64
|
+
? {
|
|
65
|
+
status: "cancelled",
|
|
66
|
+
statusMessage: "Cancelled by the client",
|
|
67
|
+
since: task.cancelledAt,
|
|
68
|
+
}
|
|
69
|
+
: task.behavior.stateAt(now);
|
|
70
|
+
}
|
|
71
|
+
/** The wire form of a task (the `DetailedTask` of the extension's schema). */
|
|
72
|
+
function describe(task, now) {
|
|
73
|
+
const state = stateOf(task, now);
|
|
74
|
+
return {
|
|
75
|
+
taskId: task.taskId,
|
|
76
|
+
status: state.status,
|
|
77
|
+
...(state.statusMessage !== undefined && {
|
|
78
|
+
statusMessage: state.statusMessage,
|
|
79
|
+
}),
|
|
80
|
+
createdAt: new Date(task.createdAt).toISOString(),
|
|
81
|
+
lastUpdatedAt: new Date(Math.max(task.createdAt, state.since)).toISOString(),
|
|
82
|
+
ttlMs: task.ttlMs,
|
|
83
|
+
pollIntervalMs: task.pollIntervalMs,
|
|
84
|
+
...(state.status === "input_required" && {
|
|
85
|
+
inputRequests: state.inputRequests,
|
|
86
|
+
}),
|
|
87
|
+
...(state.status === "completed" && { result: state.result }),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Store a new task and return the `tools/call` result that hands its handle
|
|
92
|
+
* to the client: the task's fields, with `resultType: "task"`.
|
|
93
|
+
*
|
|
94
|
+
* @param behavior - How the task progresses.
|
|
95
|
+
* @param options - Its time-to-live and the polling interval to suggest.
|
|
96
|
+
*/
|
|
97
|
+
export function createTask(behavior, options) {
|
|
98
|
+
const now = Date.now();
|
|
99
|
+
sweep(now);
|
|
100
|
+
if (tasks.size >= MAX_TASKS) {
|
|
101
|
+
return {
|
|
102
|
+
content: [
|
|
103
|
+
{
|
|
104
|
+
type: "text",
|
|
105
|
+
text: `The server is already running ${MAX_TASKS} tasks, its limit. Try again once one has expired.`,
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
isError: true,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
const task = {
|
|
112
|
+
taskId: randomUUID(),
|
|
113
|
+
createdAt: now,
|
|
114
|
+
...options,
|
|
115
|
+
behavior,
|
|
116
|
+
};
|
|
117
|
+
tasks.set(task.taskId, task);
|
|
118
|
+
// The SDK passes a tool result's `resultType` through to the wire on
|
|
119
|
+
// 2026-07-28, which is how a task handle replaces the tool's result.
|
|
120
|
+
return { content: [], resultType: "task", ...describe(task, now) };
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The capabilities the client declared in this request's `_meta` envelope,
|
|
124
|
+
* where a 2026-07-28 client sends them with every request.
|
|
125
|
+
*/
|
|
126
|
+
export function clientCapabilitiesOf(ctx) {
|
|
127
|
+
// The SDK's published typing of the envelope has no members, so it is read
|
|
128
|
+
// as the plain JSON object it is.
|
|
129
|
+
const envelope = ctx.mcpReq.envelope;
|
|
130
|
+
const capabilities = isObject(envelope)
|
|
131
|
+
? envelope[CLIENT_CAPABILITIES_META_KEY]
|
|
132
|
+
: undefined;
|
|
133
|
+
return isObject(capabilities) ? capabilities : undefined;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Whether the client that sent this request supports form-mode elicitation:
|
|
137
|
+
* `elicitation.form`, or an `elicitation` object with neither mode, which the
|
|
138
|
+
* spec reads as form mode (#4985).
|
|
139
|
+
*/
|
|
140
|
+
export function clientSupportsFormElicitation(ctx) {
|
|
141
|
+
const elicitation = clientCapabilitiesOf(ctx)?.elicitation;
|
|
142
|
+
return (isObject(elicitation) &&
|
|
143
|
+
(elicitation.form !== undefined || elicitation.url === undefined));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Whether the client that sent this request declared the tasks extension, so
|
|
147
|
+
* it can follow a task handle. A tool may answer with a task only then.
|
|
148
|
+
*/
|
|
149
|
+
export function clientSupportsTasks(ctx) {
|
|
150
|
+
const extensions = clientCapabilitiesOf(ctx)?.extensions;
|
|
151
|
+
return isObject(extensions) && extensions[TASKS_EXTENSION] !== undefined;
|
|
152
|
+
}
|
|
153
|
+
function isObject(value) {
|
|
154
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
155
|
+
}
|
|
156
|
+
/** The tasks extension, as a client declares it. */
|
|
157
|
+
const TASKS_CAPABILITY = {
|
|
158
|
+
extensions: { [TASKS_EXTENSION]: {} },
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* What each task tool needs the client to have declared: the tasks
|
|
162
|
+
* extension, to follow the handle, and the capability its task asks for
|
|
163
|
+
* input with. The SDK checks the input requests a tool returns itself, but
|
|
164
|
+
* not the ones a task carries in its status, so they are checked here.
|
|
165
|
+
*/
|
|
166
|
+
const TASK_TOOL_REQUIREMENTS = {
|
|
167
|
+
"simulate-research-query": (ctx) => missingTasks(ctx),
|
|
168
|
+
"trigger-sampling-request-async": (ctx) => merged(missingTasks(ctx), clientCapabilitiesOf(ctx)?.sampling === undefined
|
|
169
|
+
? { sampling: {} }
|
|
170
|
+
: undefined),
|
|
171
|
+
"trigger-elicitation-request-async": (ctx) => merged(missingTasks(ctx), clientSupportsFormElicitation(ctx)
|
|
172
|
+
? undefined
|
|
173
|
+
: { elicitation: { form: {} } }),
|
|
174
|
+
};
|
|
175
|
+
/** The tasks extension, if this request's client did not declare it. */
|
|
176
|
+
function missingTasks(ctx) {
|
|
177
|
+
return clientSupportsTasks(ctx) ? undefined : TASKS_CAPABILITY;
|
|
178
|
+
}
|
|
179
|
+
/** Both sets of missing capabilities, or undefined when neither is. */
|
|
180
|
+
function merged(a, b) {
|
|
181
|
+
return a || b ? { ...a, ...b } : undefined;
|
|
182
|
+
}
|
|
183
|
+
/** The -32021 error naming the capabilities the client did not declare. */
|
|
184
|
+
function missingCapabilities(requiredCapabilities) {
|
|
185
|
+
return new MissingRequiredClientCapabilityError({ requiredCapabilities }, `Missing required client capability: ${Object.keys(requiredCapabilities).join(", ")}`);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Refuse a call to a task tool from a client that did not declare what the
|
|
189
|
+
* tool needs, with the JSON-RPC error -32021 naming it, as the extension
|
|
190
|
+
* requires: such a client cannot be served without a task handle.
|
|
191
|
+
*
|
|
192
|
+
* `McpServer` reports anything a tool handler throws as an `isError` tool
|
|
193
|
+
* result, so the check cannot live in the tools. Instead it wraps the
|
|
194
|
+
* `tools/call` handler `McpServer` installs, which the SDK then runs behind
|
|
195
|
+
* its own era checks, so the error reaches the client as an error. Call this
|
|
196
|
+
* before the first tool is registered, on a server constructed without a
|
|
197
|
+
* `tools` capability: `McpServer` installs the handler at the first
|
|
198
|
+
* registration then, or in its constructor when `tools` is declared.
|
|
199
|
+
*
|
|
200
|
+
* @param {McpServer} server - The 2026-07-28 server whose task tools to guard.
|
|
201
|
+
*/
|
|
202
|
+
export function guardTaskTools(server) {
|
|
203
|
+
const lowLevel = server.server;
|
|
204
|
+
const setRequestHandler = lowLevel.setRequestHandler.bind(lowLevel);
|
|
205
|
+
// The SDK's overloads cannot be restated for a wrapper; this one only
|
|
206
|
+
// inspects the arguments and passes them through unchanged.
|
|
207
|
+
lowLevel.setRequestHandler = ((...args) => {
|
|
208
|
+
const handler = args.at(-1);
|
|
209
|
+
if (args[0] === "tools/call" && typeof handler === "function") {
|
|
210
|
+
args[args.length - 1] = (request, ctx) => {
|
|
211
|
+
const missing = TASK_TOOL_REQUIREMENTS[request.params.name]?.(ctx);
|
|
212
|
+
if (missing)
|
|
213
|
+
throw missingCapabilities(missing);
|
|
214
|
+
return handler(request, ctx);
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
return setRequestHandler(...args);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
/** Refuse a tasks/* request from a client without the extension (-32021). */
|
|
221
|
+
function requireTasks(ctx) {
|
|
222
|
+
const missing = missingTasks(ctx);
|
|
223
|
+
if (missing)
|
|
224
|
+
throw missingCapabilities(missing);
|
|
225
|
+
}
|
|
226
|
+
/** Forget every task. For tests. */
|
|
227
|
+
export function clearTasks() {
|
|
228
|
+
tasks.clear();
|
|
229
|
+
}
|
|
230
|
+
const TaskIdParams = z.object({
|
|
231
|
+
taskId: z.string().describe("The task identifier"),
|
|
232
|
+
});
|
|
233
|
+
const TaskResult = z.looseObject({});
|
|
234
|
+
const EmptyResult = z.object({});
|
|
235
|
+
/**
|
|
236
|
+
* Serve the extension's methods on a 2026-07-28 server: `tasks/get`,
|
|
237
|
+
* `tasks/update` and `tasks/cancel`. A request from a client that does not
|
|
238
|
+
* declare the extension is refused with -32021. An unknown or expired task
|
|
239
|
+
* id is refused with -32602 (invalid params). A cancellation is acknowledged
|
|
240
|
+
* whatever the task's status, and changes only a task still running. An update's answers to requests that are not outstanding are
|
|
241
|
+
* ignored. Each input request a task makes has a key of its own, never reused
|
|
242
|
+
* once answered (the extension requires it), so a re-asked request gets a
|
|
243
|
+
* new key.
|
|
244
|
+
*
|
|
245
|
+
* @param {McpServer} server - The 2026-07-28 server to serve them on.
|
|
246
|
+
*/
|
|
247
|
+
export function registerTasksExtension(server) {
|
|
248
|
+
server.server.setRequestHandler("tasks/get", { params: TaskIdParams, result: TaskResult }, async ({ taskId }, ctx) => {
|
|
249
|
+
requireTasks(ctx);
|
|
250
|
+
const now = Date.now();
|
|
251
|
+
return describe(find(taskId, now), now);
|
|
252
|
+
});
|
|
253
|
+
server.server.setRequestHandler("tasks/update", { params: TaskIdParams, result: EmptyResult }, async ({ taskId }, ctx) => {
|
|
254
|
+
requireTasks(ctx);
|
|
255
|
+
// The SDK lifts `inputResponses` out of the params of every request
|
|
256
|
+
// (it is a reserved retry field on 2026-07-28) into the context. The
|
|
257
|
+
// extension requires it, so a request without it is malformed.
|
|
258
|
+
const inputResponses = ctx.mcpReq.inputResponses;
|
|
259
|
+
if (inputResponses === undefined) {
|
|
260
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Invalid params for tasks/update: inputResponses is required");
|
|
261
|
+
}
|
|
262
|
+
const now = Date.now();
|
|
263
|
+
const task = find(taskId, now);
|
|
264
|
+
const state = stateOf(task, now);
|
|
265
|
+
// Only answers to requests outstanding now are passed on. The rest (a
|
|
266
|
+
// key never issued, one already answered, or any answer to a task not
|
|
267
|
+
// waiting for input) are ignored, as the extension asks, so a client
|
|
268
|
+
// that resends or batches responses is not failed for it.
|
|
269
|
+
const outstanding = state.status === "input_required"
|
|
270
|
+
? Object.keys(state.inputRequests)
|
|
271
|
+
: [];
|
|
272
|
+
const answered = Object.fromEntries(Object.entries(inputResponses).filter(([key]) => outstanding.includes(key)));
|
|
273
|
+
const size = JSON.stringify(answered).length;
|
|
274
|
+
if (size > MAX_ANSWER_BYTES) {
|
|
275
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Invalid params for tasks/update: the answers are ${size} bytes, over the ${MAX_ANSWER_BYTES}-byte limit; the task is unchanged`);
|
|
276
|
+
}
|
|
277
|
+
if (Object.keys(answered).length > 0) {
|
|
278
|
+
task.behavior.receive(answered, now);
|
|
279
|
+
}
|
|
280
|
+
return {};
|
|
281
|
+
});
|
|
282
|
+
server.server.setRequestHandler("tasks/cancel", { params: TaskIdParams, result: EmptyResult }, async ({ taskId }, ctx) => {
|
|
283
|
+
requireTasks(ctx);
|
|
284
|
+
const now = Date.now();
|
|
285
|
+
const task = find(taskId, now);
|
|
286
|
+
// Cancellation is cooperative: the request is acknowledged whatever
|
|
287
|
+
// the task's status, and a task that has already finished stays as it
|
|
288
|
+
// ended.
|
|
289
|
+
const { status } = stateOf(task, now);
|
|
290
|
+
if (status !== "completed" && status !== "cancelled") {
|
|
291
|
+
task.cancelledAt = now;
|
|
292
|
+
}
|
|
293
|
+
return {};
|
|
294
|
+
});
|
|
295
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// The `get-header-params` tool: demonstrates two 2026-07-28 tool-definition
|
|
2
|
+
// features (#4852).
|
|
3
|
+
//
|
|
4
|
+
// - Its `inputSchema` and `outputSchema` are plain JSON Schema 2020-12
|
|
5
|
+
// documents (`$schema` named), passed through `fromJsonSchema` rather than
|
|
6
|
+
// built from Zod, and validated by the SDK's JSON Schema validator.
|
|
7
|
+
// - Its `region` argument carries `x-mcp-header: "Region"`. Over Streamable
|
|
8
|
+
// HTTP a 2026-07-28 client mirrors that argument into an
|
|
9
|
+
// `Mcp-Param-Region` request header, so infrastructure in front of the
|
|
10
|
+
// server (a load balancer, a gateway) can route on it without reading the
|
|
11
|
+
// body, and the server rejects a call whose header is missing or disagrees
|
|
12
|
+
// with the body (-32020). The tool reports the header it was sent, which is
|
|
13
|
+
// null on stdio and for a 2025-era client, which mirrors nothing.
|
|
14
|
+
import { fromJsonSchema, } from "@modelcontextprotocol/server";
|
|
15
|
+
/**
|
|
16
|
+
* The keyword that asks a 2026-07-28 client to mirror an argument into the
|
|
17
|
+
* `Mcp-Param-<value>` header (SEP-2243). It is an extension keyword, so the
|
|
18
|
+
* JSON Schema type has no member for it; it is spread into the property.
|
|
19
|
+
*/
|
|
20
|
+
const mirroredInHeader = { "x-mcp-header": "Region" };
|
|
21
|
+
/** The input: a region, mirrored into a header, and an optional count. */
|
|
22
|
+
const inputSchema = {
|
|
23
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
24
|
+
type: "object",
|
|
25
|
+
properties: {
|
|
26
|
+
region: {
|
|
27
|
+
type: "string",
|
|
28
|
+
description: "A region name, e.g. eu-west. Mirrored into the Mcp-Param-Region HTTP header.",
|
|
29
|
+
...mirroredInHeader,
|
|
30
|
+
},
|
|
31
|
+
repeat: {
|
|
32
|
+
type: "integer",
|
|
33
|
+
minimum: 1,
|
|
34
|
+
maximum: 3,
|
|
35
|
+
default: 1,
|
|
36
|
+
description: "How many times to repeat the region in the result (1-3)",
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
required: ["region"],
|
|
40
|
+
additionalProperties: false,
|
|
41
|
+
};
|
|
42
|
+
/** The output: the region from the body, and the header that carried it. */
|
|
43
|
+
const outputSchema = {
|
|
44
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
45
|
+
type: "object",
|
|
46
|
+
properties: {
|
|
47
|
+
region: { type: "string" },
|
|
48
|
+
// Nullable as anyOf with one type per branch, not `type: ["string",
|
|
49
|
+
// "null"]`: some clients map tool schemas onto a dialect that allows a
|
|
50
|
+
// single `type` per property (the OpenAPI subset of Gemini function
|
|
51
|
+
// declarations, for one) and reject or drop the array form.
|
|
52
|
+
regionHeader: {
|
|
53
|
+
anyOf: [{ type: "string" }, { type: "null" }],
|
|
54
|
+
description: "The Mcp-Param-Region header the request carried, if any",
|
|
55
|
+
},
|
|
56
|
+
repeated: { type: "array", items: { type: "string" } },
|
|
57
|
+
},
|
|
58
|
+
required: ["region", "regionHeader", "repeated"],
|
|
59
|
+
additionalProperties: false,
|
|
60
|
+
};
|
|
61
|
+
// Tool configuration
|
|
62
|
+
const name = "get-header-params";
|
|
63
|
+
const config = {
|
|
64
|
+
title: "Get Header Params Tool",
|
|
65
|
+
description: "Returns its region argument and the Mcp-Param-Region HTTP header it arrived in. Demonstrates " +
|
|
66
|
+
"x-mcp-header (a 2026-07-28 Streamable HTTP client mirrors the argument into the header) and " +
|
|
67
|
+
"JSON Schema 2020-12 input and output schemas.",
|
|
68
|
+
inputSchema: fromJsonSchema(inputSchema),
|
|
69
|
+
outputSchema: fromJsonSchema(outputSchema),
|
|
70
|
+
annotations: {
|
|
71
|
+
readOnlyHint: true,
|
|
72
|
+
destructiveHint: false,
|
|
73
|
+
idempotentHint: true,
|
|
74
|
+
openWorldHint: false,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Registers the 'get-header-params' tool.
|
|
79
|
+
*
|
|
80
|
+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
81
|
+
*/
|
|
82
|
+
export const registerGetHeaderParamsTool = (server) => {
|
|
83
|
+
server.registerTool(name, config, async ({ region, repeat }, ctx) => {
|
|
84
|
+
const regionHeader = ctx.http?.req?.headers.get("mcp-param-region") ?? null;
|
|
85
|
+
const structuredContent = {
|
|
86
|
+
region,
|
|
87
|
+
regionHeader,
|
|
88
|
+
repeated: Array.from({ length: repeat ?? 1 }, () => region),
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
content: [{ type: "text", text: JSON.stringify(structuredContent) }],
|
|
92
|
+
structuredContent,
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
};
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
// The `get-roots-list` tool: asks the client for its roots and lists them.
|
|
2
|
+
//
|
|
3
|
+
// Written once for both protocol eras with a multi-round-trip request
|
|
4
|
+
// (#4852): the first call returns `input_required` carrying a `roots/list`
|
|
5
|
+
// request, the client answers it and retries, and the retry lists the
|
|
6
|
+
// answer. A 2026-07-28 client does that in-band; for a 2025-era client the
|
|
7
|
+
// SDK's legacy shim sends a real `roots/list` request and re-enters the
|
|
8
|
+
// handler with the answer. Roots is deprecated as of 2026-07-28 (SEP-2577).
|
|
9
|
+
import { inputRequired, inputResponse, } from "@modelcontextprotocol/server";
|
|
2
10
|
// Tool configuration
|
|
3
11
|
const name = "get-roots-list";
|
|
4
12
|
const config = {
|
|
5
13
|
title: "Get Roots List Tool",
|
|
6
|
-
description: "Lists the current MCP roots provided by the client. Demonstrates the roots protocol capability even though this server doesn't access files."
|
|
14
|
+
description: "Lists the current MCP roots provided by the client. Demonstrates the roots protocol capability even though this server doesn't access files. " +
|
|
15
|
+
"Deprecated: Roots is deprecated as of protocol version 2026-07-28. The server asks for the roots with a multi-round-trip request on every protocol version.",
|
|
7
16
|
inputSchema: {},
|
|
8
17
|
annotations: {
|
|
9
18
|
readOnlyHint: true,
|
|
@@ -15,63 +24,53 @@ const config = {
|
|
|
15
24
|
/**
|
|
16
25
|
* Registers the 'get-roots-list' tool.
|
|
17
26
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* When supported, the server automatically retrieves and formats the current list of roots from the
|
|
24
|
-
* client upon connection and whenever the client sends a `roots/list_changed` notification.
|
|
25
|
-
*
|
|
26
|
-
* Therefore, this tool displays the roots that the server currently knows about for the connected
|
|
27
|
-
* client. If for some reason the server never got the initial roots list, the tool will request the
|
|
28
|
-
* list from the client again.
|
|
27
|
+
* The tool needs a client that supports Roots: on a 2025-era connection the
|
|
28
|
+
* server factory registers it only for one that declared the capability, and
|
|
29
|
+
* on 2026-07-28 the SDK refuses the embedded request (-32021) of a client
|
|
30
|
+
* whose request did not declare it.
|
|
29
31
|
*
|
|
30
32
|
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
31
33
|
*/
|
|
32
34
|
export const registerGetRootsListTool = (server) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
content: [
|
|
46
|
-
{
|
|
47
|
-
type: "text",
|
|
48
|
-
text: "The client supports roots but no roots are currently configured.\n\n" +
|
|
49
|
-
"This could mean:\n" +
|
|
50
|
-
"1. The client hasn't provided any roots yet\n" +
|
|
51
|
-
"2. The client provided an empty roots list\n" +
|
|
52
|
-
"3. The roots configuration is still being loaded",
|
|
53
|
-
},
|
|
54
|
-
],
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
// Create formatted response if there is a list of roots
|
|
58
|
-
const rootsList = currentRoots
|
|
59
|
-
? currentRoots
|
|
60
|
-
.map((root, index) => {
|
|
61
|
-
return `${index + 1}. ${root.name || "Unnamed Root"}\n URI: ${root.uri}`;
|
|
62
|
-
})
|
|
63
|
-
.join("\n\n")
|
|
64
|
-
: "No roots found";
|
|
35
|
+
server.registerTool(name, config, async (_args, ctx) => {
|
|
36
|
+
const answer = inputResponse(ctx.mcpReq.inputResponses, "roots");
|
|
37
|
+
// First round: ask the client for its roots
|
|
38
|
+
if (answer.kind !== "roots") {
|
|
39
|
+
return inputRequired({
|
|
40
|
+
inputRequests: { roots: inputRequired.listRoots() },
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
const currentRoots = answer.roots;
|
|
44
|
+
// Respond if client supports roots but doesn't have any configured
|
|
45
|
+
if (currentRoots.length === 0) {
|
|
65
46
|
return {
|
|
66
47
|
content: [
|
|
67
48
|
{
|
|
68
49
|
type: "text",
|
|
69
|
-
text:
|
|
70
|
-
"
|
|
71
|
-
"The
|
|
50
|
+
text: "The client supports roots but no roots are currently configured.\n\n" +
|
|
51
|
+
"This could mean:\n" +
|
|
52
|
+
"1. The client hasn't provided any roots yet\n" +
|
|
53
|
+
"2. The client provided an empty roots list\n" +
|
|
54
|
+
"3. The roots configuration is still being loaded",
|
|
72
55
|
},
|
|
73
56
|
],
|
|
74
57
|
};
|
|
75
|
-
}
|
|
76
|
-
|
|
58
|
+
}
|
|
59
|
+
// Create formatted response if there is a list of roots
|
|
60
|
+
const rootsList = currentRoots
|
|
61
|
+
.map((root, index) => {
|
|
62
|
+
return `${index + 1}. ${root.name || "Unnamed Root"}\n URI: ${root.uri}`;
|
|
63
|
+
})
|
|
64
|
+
.join("\n\n");
|
|
65
|
+
return {
|
|
66
|
+
content: [
|
|
67
|
+
{
|
|
68
|
+
type: "text",
|
|
69
|
+
text: `Current MCP Roots (${currentRoots.length} total):\n\n${rootsList}\n\n` +
|
|
70
|
+
"Note: This server demonstrates the roots protocol capability but doesn't actually access files. " +
|
|
71
|
+
"The roots are provided by the MCP client and can be used by servers that need file system access.",
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
};
|
|
75
|
+
});
|
|
77
76
|
};
|
|
@@ -1,5 +1,17 @@
|
|
|
1
|
+
// The `gzip-file-as-resource` tool: fetches a file, gzips it, and returns the
|
|
2
|
+
// result as an embedded resource or as a link to a resource the client can
|
|
3
|
+
// read later.
|
|
4
|
+
//
|
|
5
|
+
// Where that later resource lives depends on the protocol era (#4852). For a
|
|
6
|
+
// 2025-era client it is registered on the server instance serving the
|
|
7
|
+
// client's connection or session (resources/session.ts), and lasts as long as
|
|
8
|
+
// that does. The 2026-07-28 revision has no sessions, and on its HTTP path
|
|
9
|
+
// each request gets a fresh instance, so for a 2026-07-28 client the resource
|
|
10
|
+
// is stored under a URI with a server-minted id that every instance can serve
|
|
11
|
+
// (resources/minted.ts), and expires after ten minutes.
|
|
1
12
|
import { z } from "zod";
|
|
2
13
|
import { gzipSync } from "node:zlib";
|
|
14
|
+
import { mintBlobResource } from "../resources/minted.js";
|
|
3
15
|
import { getSessionResourceURI, registerSessionResource, } from "../resources/session.js";
|
|
4
16
|
// Maximum input file size - 10 MB default
|
|
5
17
|
const GZIP_MAX_FETCH_SIZE = Number(process.env.GZIP_MAX_FETCH_SIZE ?? String(10 * 1024 * 1024));
|
|
@@ -12,7 +24,11 @@ const GZIP_ALLOWED_DOMAINS = (process.env.GZIP_ALLOWED_DOMAINS ?? "")
|
|
|
12
24
|
.filter((d) => d.length > 0);
|
|
13
25
|
// Tool input schema
|
|
14
26
|
const GZipFileAsResourceSchema = z.object({
|
|
15
|
-
name: z
|
|
27
|
+
name: z
|
|
28
|
+
.string()
|
|
29
|
+
.max(255)
|
|
30
|
+
.describe("Name of the output file (at most 255 characters)")
|
|
31
|
+
.default("README.md.gz"),
|
|
16
32
|
data: z
|
|
17
33
|
.url()
|
|
18
34
|
.describe("URL or data URI of the file content to compress")
|
|
@@ -26,7 +42,7 @@ const GZipFileAsResourceSchema = z.object({
|
|
|
26
42
|
const name = "gzip-file-as-resource";
|
|
27
43
|
const config = {
|
|
28
44
|
title: "GZip File as Resource Tool",
|
|
29
|
-
description: "Compresses a single file using gzip compression. Depending upon the selected output type, returns either the compressed data as a gzipped resource or a resource link, allowing it to be downloaded in a subsequent request during the current session.",
|
|
45
|
+
description: "Compresses a single file using gzip compression. Depending upon the selected output type, returns either the compressed data as a gzipped resource or a resource link, allowing it to be downloaded in a subsequent request: during the current session for a 2025-era client, or for ten minutes at a minted URI on protocol version 2026-07-28.",
|
|
30
46
|
inputSchema: GZipFileAsResourceSchema,
|
|
31
47
|
annotations: {
|
|
32
48
|
readOnlyHint: false,
|
|
@@ -48,9 +64,10 @@ const config = {
|
|
|
48
64
|
* If an unrecognized `outputType` is provided, the tool throws an error.
|
|
49
65
|
*
|
|
50
66
|
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
|
67
|
+
* @param {ServerContext} context - The protocol era the instance serves.
|
|
51
68
|
* @throws {Error} Throws an error if an unknown output type is specified.
|
|
52
69
|
*/
|
|
53
|
-
export const registerGZipFileAsResourceTool = (server) => {
|
|
70
|
+
export const registerGZipFileAsResourceTool = (server, context) => {
|
|
54
71
|
server.registerTool(name, config, async (args) => {
|
|
55
72
|
const { name, data: dataUri, outputType, } = GZipFileAsResourceSchema.parse(args);
|
|
56
73
|
// Validate data uri
|
|
@@ -68,8 +85,6 @@ export const registerGZipFileAsResourceTool = (server) => {
|
|
|
68
85
|
const blob = compressedBuffer.toString("base64");
|
|
69
86
|
const mimeType = "application/gzip";
|
|
70
87
|
const resource = { uri, name, mimeType };
|
|
71
|
-
// Register resource, get resource link in return
|
|
72
|
-
const resourceLink = registerSessionResource(server, resource, "blob", blob);
|
|
73
88
|
// Return the resource or a resource link that can be used to access this resource later
|
|
74
89
|
if (outputType === "resource") {
|
|
75
90
|
return {
|
|
@@ -82,6 +97,10 @@ export const registerGZipFileAsResourceTool = (server) => {
|
|
|
82
97
|
};
|
|
83
98
|
}
|
|
84
99
|
else if (outputType === "resourceLink") {
|
|
100
|
+
// Make the resource readable later, where this era can reach it
|
|
101
|
+
const resourceLink = context.era === "modern"
|
|
102
|
+
? mintBlobResource(name, mimeType, blob)
|
|
103
|
+
: registerSessionResource(server, resource, "blob", blob);
|
|
85
104
|
return {
|
|
86
105
|
content: [resourceLink],
|
|
87
106
|
};
|