@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,47 @@
|
|
|
1
|
+
// The per-instance context the server factory hands to the features that
|
|
2
|
+
// need more than the `McpServer` itself (#4852).
|
|
3
|
+
//
|
|
4
|
+
// Before the 2026-07-28 revision, features that kept state across calls
|
|
5
|
+
// (simulated logging, resource subscriptions, the roots cache, the URL
|
|
6
|
+
// elicitation error path) held it in module-level maps keyed by the
|
|
7
|
+
// transport's session id. The 2026-07-28 revision has no sessions, so that
|
|
8
|
+
// state now lives here, on the server instance the factory built: one per
|
|
9
|
+
// stdio connection, one per 2025-era Streamable HTTP session, and one per
|
|
10
|
+
// request on the 2026-07-28 HTTP path, where nothing survives between
|
|
11
|
+
// requests unless the server mints a handle for it (as the tasks store does).
|
|
12
|
+
//
|
|
13
|
+
// The context also carries the protocol era the instance serves, because a
|
|
14
|
+
// few features differ by era, and the publisher that announces list and
|
|
15
|
+
// resource changes, because where a change notification goes differs by
|
|
16
|
+
// transport: on stdio and on a 2025-era session the instance sends it
|
|
17
|
+
// itself, while on the 2026-07-28 HTTP path the instance lives for one
|
|
18
|
+
// request and the change goes out on the handler's subscription bus.
|
|
19
|
+
/**
|
|
20
|
+
* A publisher that sends each change from `server` itself. On a 2025-era
|
|
21
|
+
* connection the notification goes to the client unsolicited; on a
|
|
22
|
+
* 2026-07-28 stdio connection `serveStdio` routes it onto the open
|
|
23
|
+
* `subscriptions/listen` streams that asked for it.
|
|
24
|
+
*
|
|
25
|
+
* `resourceUpdated` is sent only for a URI `isSubscribed` accepts: a 2025-era
|
|
26
|
+
* client subscribes per resource, and on 2026-07-28 the listen router does
|
|
27
|
+
* the filtering, so the server passes `() => true` there.
|
|
28
|
+
*/
|
|
29
|
+
export function instancePublisher(server, isSubscribed) {
|
|
30
|
+
const report = (error) => console.error("Change notification failed:", error);
|
|
31
|
+
// The low-level sends return their promise; McpServer's own helpers start
|
|
32
|
+
// the same send and drop it, so a failed one would go unhandled.
|
|
33
|
+
const send = (notify) => {
|
|
34
|
+
if (server.isConnected())
|
|
35
|
+
notify().catch(report);
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
toolsChanged: () => send(() => server.server.sendToolListChanged()),
|
|
39
|
+
promptsChanged: () => send(() => server.server.sendPromptListChanged()),
|
|
40
|
+
resourcesChanged: () => send(() => server.server.sendResourceListChanged()),
|
|
41
|
+
resourceUpdated: (uri) => {
|
|
42
|
+
if (isSubscribed(uri)) {
|
|
43
|
+
server.server.sendResourceUpdated({ uri }).catch(report);
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
package/dist/server/index.js
CHANGED
|
@@ -1,31 +1,73 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// The everything server's factory: builds one fully registered `McpServer`
|
|
2
|
+
// for the protocol era it is asked to serve (#4852).
|
|
3
|
+
//
|
|
4
|
+
// One factory backs every transport and both eras. `serveStdio` calls it once
|
|
5
|
+
// per stdio connection; the Streamable HTTP transport calls it once per
|
|
6
|
+
// 2025-era session and once per 2026-07-28 request; the SSE transport once
|
|
7
|
+
// per (2025-era) connection. Everything that must outlive a single call lives
|
|
8
|
+
// on the instance it builds, in the context below, never in a map keyed by a
|
|
9
|
+
// session id: the 2026-07-28 revision has no sessions.
|
|
10
|
+
//
|
|
11
|
+
// What differs by era is wired here, in one place:
|
|
12
|
+
// - 2025-era: the client's capabilities arrive in `initialize`, so the tools
|
|
13
|
+
// that need a client capability are registered once it has initialized, and
|
|
14
|
+
// the roots list is synced then. The features the 2026-07-28 revision
|
|
15
|
+
// removes (`logging/setLevel`-paced simulated logging, `resources/subscribe`
|
|
16
|
+
// timed updates) are registered here only, labelled deprecated.
|
|
17
|
+
// - 2026-07-28: there is no `initialize`, and each request carries the
|
|
18
|
+
// client's capabilities in its `_meta`, so the capability-gated tools are
|
|
19
|
+
// registered up front and the SDK refuses an embedded request the client
|
|
20
|
+
// did not declare (-32021). The tasks extension is served here only.
|
|
21
|
+
import { createRequestStateCodec, McpServer, } from "@modelcontextprotocol/server";
|
|
22
|
+
import { setSubscriptionHandlers } from "../resources/subscriptions.js";
|
|
23
|
+
import { registerConditionalTools, registerLegacyTools, registerModernTools, registerTools, } from "../tools/index.js";
|
|
5
24
|
import { registerResources, readInstructions } from "../resources/index.js";
|
|
6
25
|
import { registerPrompts } from "../prompts/index.js";
|
|
7
|
-
import {
|
|
26
|
+
import { guardTaskTools, registerTasksExtension, TASKS_EXTENSION, } from "./tasks.js";
|
|
8
27
|
import { syncRoots } from "./roots.js";
|
|
28
|
+
import { instancePublisher, } from "./context.js";
|
|
9
29
|
import { SERVER_VERSION } from "../version.js";
|
|
30
|
+
/**
|
|
31
|
+
* The process-wide `requestState` codec. The key is per process: on the
|
|
32
|
+
* 2026-07-28 HTTP path a later round may be served by a fresh instance, so
|
|
33
|
+
* the key cannot belong to one. A deployment of several processes would load
|
|
34
|
+
* a shared secret here instead.
|
|
35
|
+
*/
|
|
36
|
+
export const roundStateCodec = createRequestStateCodec({
|
|
37
|
+
key: crypto.getRandomValues(new Uint8Array(32)),
|
|
38
|
+
ttlSeconds: 600,
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* Cache hints for the 2026-07-28 cacheable results (`ttlMs` / `cacheScope`).
|
|
42
|
+
* The lists are the same for every client and change only with a
|
|
43
|
+
* `list_changed` notification, so a shared cache may hold them for a minute;
|
|
44
|
+
* `server/discover` changes only when the server is upgraded. 2025-era
|
|
45
|
+
* responses never carry these fields. A static document resource sets a
|
|
46
|
+
* longer hint of its own (resources/files.ts).
|
|
47
|
+
*/
|
|
48
|
+
export const CACHE_HINTS = {
|
|
49
|
+
"server/discover": { ttlMs: 5 * 60_000, cacheScope: "public" },
|
|
50
|
+
"tools/list": { ttlMs: 60_000, cacheScope: "public" },
|
|
51
|
+
"prompts/list": { ttlMs: 60_000, cacheScope: "public" },
|
|
52
|
+
"resources/list": { ttlMs: 60_000, cacheScope: "public" },
|
|
53
|
+
"resources/templates/list": { ttlMs: 60_000, cacheScope: "public" },
|
|
54
|
+
};
|
|
10
55
|
/**
|
|
11
56
|
* Server Factory
|
|
12
57
|
*
|
|
13
|
-
*
|
|
14
|
-
* registers tools, resources
|
|
15
|
-
*
|
|
16
|
-
* @returns {ServerFactoryResponse} An object containing the server instance, and a `cleanup`
|
|
17
|
-
* function for handling server-side cleanup when a session ends.
|
|
58
|
+
* Initializes a `McpServer` with its capabilities and instructions, and
|
|
59
|
+
* registers the tools, resources and prompts for the era it serves.
|
|
18
60
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
61
|
+
* @param {ServerOptions} options - The era to serve and where its change
|
|
62
|
+
* notifications go.
|
|
63
|
+
* @returns {ServerFactoryResponse} The server instance, and a `cleanup`
|
|
64
|
+
* function that ends its per-connection work when the connection closes.
|
|
22
65
|
*/
|
|
23
|
-
export const createServer = () => {
|
|
66
|
+
export const createServer = (options = {}) => {
|
|
67
|
+
const era = options.era ?? "legacy";
|
|
24
68
|
// Read the server instructions
|
|
25
69
|
const instructions = readInstructions();
|
|
26
|
-
|
|
27
|
-
const taskStore = new InMemoryTaskStore();
|
|
28
|
-
const taskMessageQueue = new InMemoryTaskMessageQueue();
|
|
70
|
+
const cleanups = [];
|
|
29
71
|
let initializeTimeout = null;
|
|
30
72
|
// Create the server
|
|
31
73
|
const server = new McpServer({
|
|
@@ -34,9 +76,10 @@ export const createServer = () => {
|
|
|
34
76
|
version: SERVER_VERSION,
|
|
35
77
|
}, {
|
|
36
78
|
capabilities: {
|
|
37
|
-
tools: {
|
|
38
|
-
|
|
39
|
-
|
|
79
|
+
// `tools: { listChanged: true }` is advertised by the first
|
|
80
|
+
// registerTool. Declaring it here would install McpServer's
|
|
81
|
+
// tools/call handler in the constructor, before guardTaskTools can
|
|
82
|
+
// wrap it.
|
|
40
83
|
prompts: {
|
|
41
84
|
listChanged: true,
|
|
42
85
|
},
|
|
@@ -44,50 +87,65 @@ export const createServer = () => {
|
|
|
44
87
|
subscribe: true,
|
|
45
88
|
listChanged: true,
|
|
46
89
|
},
|
|
90
|
+
// Deprecated as of 2026-07-28 (SEP-2577), and still served on both
|
|
91
|
+
// eras: 2025-era clients set the level with `logging/setLevel`,
|
|
92
|
+
// 2026-07-28 clients per request in `_meta`.
|
|
47
93
|
logging: {},
|
|
48
|
-
tasks
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
requests: {
|
|
52
|
-
tools: {
|
|
53
|
-
call: {},
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
},
|
|
94
|
+
// The tasks extension (`io.modelcontextprotocol/tasks`) is
|
|
95
|
+
// advertised on 2026-07-28 only, where it is served.
|
|
96
|
+
...(era === "modern" && { extensions: { [TASKS_EXTENSION]: {} } }),
|
|
57
97
|
},
|
|
58
98
|
instructions,
|
|
59
|
-
|
|
60
|
-
|
|
99
|
+
cacheHints: CACHE_HINTS,
|
|
100
|
+
requestState: { verify: roundStateCodec.verify },
|
|
61
101
|
});
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// Register conditional tools now that client capabilities are known.
|
|
73
|
-
// This finishes before the `notifications/initialized` handler finishes.
|
|
74
|
-
registerConditionalTools(server);
|
|
75
|
-
// Sync roots if the client supports them.
|
|
76
|
-
// This is delayed until after the `notifications/initialized` handler finishes,
|
|
77
|
-
// otherwise, the request gets lost.
|
|
78
|
-
const sessionId = server.server.transport?.sessionId;
|
|
79
|
-
initializeTimeout = setTimeout(() => syncRoots(server, sessionId), 350);
|
|
102
|
+
// Resources a 2025-era client has subscribed to on this instance. On
|
|
103
|
+
// 2026-07-28 the client names them in its subscriptions/listen filter and
|
|
104
|
+
// the serving entry delivers only what it asked for.
|
|
105
|
+
const subscribed = new Set();
|
|
106
|
+
const context = {
|
|
107
|
+
era,
|
|
108
|
+
subscribed,
|
|
109
|
+
publish: options.publish ??
|
|
110
|
+
instancePublisher(server, (uri) => era === "modern" ? true : subscribed.has(uri)),
|
|
111
|
+
onCleanup: (fn) => cleanups.push(fn),
|
|
80
112
|
};
|
|
113
|
+
// A task tool refuses, with -32021, a client that has not declared what it
|
|
114
|
+
// needs; this wraps the tools/call handler the first registration installs.
|
|
115
|
+
if (era === "modern")
|
|
116
|
+
guardTaskTools(server);
|
|
117
|
+
// Register the tools, resources and prompts every era gets
|
|
118
|
+
registerTools(server, context);
|
|
119
|
+
registerResources(server, context);
|
|
120
|
+
registerPrompts(server);
|
|
121
|
+
if (era === "modern") {
|
|
122
|
+
// Each request carries the client's capabilities, so the tools that need
|
|
123
|
+
// one are registered up front, along with the tasks extension.
|
|
124
|
+
registerModernTools(server, context);
|
|
125
|
+
registerTasksExtension(server);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
registerLegacyTools(server, context);
|
|
129
|
+
setSubscriptionHandlers(server, context.subscribed);
|
|
130
|
+
// Perform post-initialization operations
|
|
131
|
+
server.server.oninitialized = async () => {
|
|
132
|
+
// Register conditional tools now that client capabilities are known.
|
|
133
|
+
// This finishes before the `notifications/initialized` handler finishes.
|
|
134
|
+
registerConditionalTools(server, context);
|
|
135
|
+
// Sync roots if the client supports them.
|
|
136
|
+
// This is delayed until after the `notifications/initialized` handler finishes,
|
|
137
|
+
// otherwise, the request gets lost.
|
|
138
|
+
initializeTimeout = setTimeout(() => syncRoots(server), 350);
|
|
139
|
+
};
|
|
140
|
+
}
|
|
81
141
|
// Return the ServerFactoryResponse
|
|
82
142
|
return {
|
|
83
143
|
server,
|
|
84
|
-
cleanup: (
|
|
144
|
+
cleanup: () => {
|
|
85
145
|
// Stop any simulated logging or resource updates that may have been initiated.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
// Clean up task store timers
|
|
90
|
-
taskStore.cleanup();
|
|
146
|
+
for (const fn of cleanups.splice(0))
|
|
147
|
+
fn();
|
|
148
|
+
subscribed.clear();
|
|
91
149
|
if (initializeTimeout)
|
|
92
150
|
clearTimeout(initializeTimeout);
|
|
93
151
|
},
|
package/dist/server/logging.js
CHANGED
|
@@ -1,70 +1,43 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
1
|
+
// Simulated logging for `toggle-simulated-logging`: a timer that sends a
|
|
2
|
+
// random-leveled `notifications/message` every five seconds, filtered by the
|
|
3
|
+
// level the client set with `logging/setLevel`.
|
|
4
|
+
//
|
|
5
|
+
// This is 2025-era behavior. The 2026-07-28 revision removes
|
|
6
|
+
// `logging/setLevel` and unsolicited log messages: a log message is sent only
|
|
7
|
+
// during a request, at the level that request names in its `_meta`
|
|
8
|
+
// (`trigger-log-messages` shows that). Logging itself is deprecated as of
|
|
9
|
+
// 2026-07-28 (SEP-2577). The timer belongs to one server instance, the one
|
|
10
|
+
// serving the client's connection or session, so no session map is needed
|
|
11
|
+
// (#4852).
|
|
12
|
+
/** One message per logging level, from least to most severe. */
|
|
13
|
+
const MESSAGES = [
|
|
14
|
+
{ level: "debug", data: "Debug-level message" },
|
|
15
|
+
{ level: "info", data: "Info-level message" },
|
|
16
|
+
{ level: "notice", data: "Notice-level message" },
|
|
17
|
+
{ level: "warning", data: "Warning-level message" },
|
|
18
|
+
{ level: "error", data: "Error-level message" },
|
|
19
|
+
{ level: "critical", data: "Critical-level message" },
|
|
20
|
+
{ level: "alert", data: "Alert level-message" },
|
|
21
|
+
{ level: "emergency", data: "Emergency-level message" },
|
|
22
|
+
];
|
|
3
23
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
24
|
+
* Start sending `server`'s client a random-leveled log message now and then
|
|
25
|
+
* every five seconds. The SDK drops a message below the level the client set.
|
|
6
26
|
*
|
|
7
|
-
* @param {McpServer} server - The server
|
|
8
|
-
* @
|
|
9
|
-
* the session ID will be appended to log messages.
|
|
27
|
+
* @param {McpServer} server - The server whose client receives the messages.
|
|
28
|
+
* @returns {() => void} Stops the messages.
|
|
10
29
|
*/
|
|
11
|
-
export const beginSimulatedLogging = (server
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
data: `Critical-level message${maybeAppendSessionId}`,
|
|
25
|
-
},
|
|
26
|
-
{ level: "alert", data: `Alert level-message${maybeAppendSessionId}` },
|
|
27
|
-
{
|
|
28
|
-
level: "emergency",
|
|
29
|
-
data: `Emergency-level message${maybeAppendSessionId}`,
|
|
30
|
-
},
|
|
31
|
-
];
|
|
32
|
-
/**
|
|
33
|
-
* Send a simulated logging message to the client
|
|
34
|
-
*/
|
|
35
|
-
const sendSimulatedLoggingMessage = async (sessionId) => {
|
|
36
|
-
// By using the `sendLoggingMessage` function to send the message, we
|
|
37
|
-
// ensure that the client's chosen logging level will be respected
|
|
38
|
-
await server.sendLoggingMessage(messages[Math.floor(Math.random() * messages.length)], sessionId);
|
|
39
|
-
};
|
|
40
|
-
// Set the interval to send later logging messages to this client
|
|
41
|
-
/* v8 ignore else -- toggle-simulated-logging tracks the sessions it started and never begins one twice */
|
|
42
|
-
if (!logsUpdateIntervals.has(sessionId)) {
|
|
43
|
-
// Send once immediately, then every 5 seconds. Fire-and-forget: this
|
|
44
|
-
// function is synchronous by contract (it starts a background simulation),
|
|
45
|
-
// so neither send can be awaited. A send that fails (e.g. the transport
|
|
46
|
-
// closed before `stopSimulatedLogging`) is logged rather than left to
|
|
47
|
-
// become an unhandled rejection.
|
|
48
|
-
const send = () => sendSimulatedLoggingMessage(sessionId).catch((error) => console.error("Simulated logging message failed:", error));
|
|
49
|
-
void send();
|
|
50
|
-
// Send a randomly-leveled log message every 5 seconds
|
|
51
|
-
logsUpdateIntervals.set(sessionId, setInterval(() => void send(), 5000));
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
/**
|
|
55
|
-
* Stops the simulated logging process for a given session.
|
|
56
|
-
*
|
|
57
|
-
* This function halts the periodic logging updates associated with the specified
|
|
58
|
-
* session ID by clearing the interval and removing the session's tracking
|
|
59
|
-
* reference. Session ID can be undefined for stdio.
|
|
60
|
-
*
|
|
61
|
-
* @param {string} [sessionId] - The optional unique identifier of the session.
|
|
62
|
-
*/
|
|
63
|
-
export const stopSimulatedLogging = (sessionId) => {
|
|
64
|
-
// Remove active intervals
|
|
65
|
-
if (logsUpdateIntervals.has(sessionId)) {
|
|
66
|
-
const logsUpdateInterval = logsUpdateIntervals.get(sessionId);
|
|
67
|
-
clearInterval(logsUpdateInterval);
|
|
68
|
-
logsUpdateIntervals.delete(sessionId);
|
|
69
|
-
}
|
|
30
|
+
export const beginSimulatedLogging = (server) => {
|
|
31
|
+
// Fire-and-forget: this runs on a timer with no caller to await it. A send
|
|
32
|
+
// that fails (e.g. the transport closed before logging was stopped) is
|
|
33
|
+
// logged rather than left to become an unhandled rejection.
|
|
34
|
+
const send = () => server
|
|
35
|
+
.sendLoggingMessage(MESSAGES[Math.floor(Math.random() * MESSAGES.length)],
|
|
36
|
+
// The SDK keeps the level the client set per transport session, so
|
|
37
|
+
// the filter is looked up by this connection's session id.
|
|
38
|
+
server.server.transport?.sessionId)
|
|
39
|
+
.catch((error) => console.error("Simulated logging message failed:", error));
|
|
40
|
+
void send();
|
|
41
|
+
const interval = setInterval(() => void send(), 5000);
|
|
42
|
+
return () => clearInterval(interval);
|
|
70
43
|
};
|
package/dist/server/roots.js
CHANGED
|
@@ -1,70 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
//
|
|
3
|
-
|
|
1
|
+
// The 2025-era roots sync: after `initialize`, a server whose client supports
|
|
2
|
+
// Roots asks for the list with `roots/list`, and asks again each time the
|
|
3
|
+
// client sends `notifications/roots/list_changed`, reporting each answer to
|
|
4
|
+
// the client as a log message.
|
|
5
|
+
//
|
|
6
|
+
// 2025-era only, and deprecated: the 2026-07-28 revision removes
|
|
7
|
+
// server-to-client requests and `notifications/roots/list_changed`, and
|
|
8
|
+
// deprecates Roots. There a server asks for roots in-band, by returning an
|
|
9
|
+
// `input_required` result from a call, which `get-roots-list` does on both
|
|
10
|
+
// eras (#4852). Nothing is cached: the answer is only reported.
|
|
11
|
+
/** The servers whose `roots/list_changed` handler is installed. */
|
|
12
|
+
const watching = new WeakSet();
|
|
4
13
|
/**
|
|
5
|
-
*
|
|
14
|
+
* Ask the client for its roots and report the answer, and re-ask on each
|
|
15
|
+
* `notifications/roots/list_changed`. Does nothing for a client without Roots.
|
|
16
|
+
* Safe to call more than once: the notification handler is installed once.
|
|
6
17
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
18
|
+
* A failed request is logged to stderr, not thrown, since the caller is a
|
|
19
|
+
* timer with nothing to report the failure to.
|
|
9
20
|
*
|
|
10
|
-
*
|
|
11
|
-
* notification handler. This ensures that updates are automatically fetched and handled
|
|
12
|
-
* in real-time.
|
|
13
|
-
*
|
|
14
|
-
* This function is idempotent. It should only request roots from the client once per session,
|
|
15
|
-
* returning the cached version thereafter.
|
|
16
|
-
*
|
|
17
|
-
* @param {McpServer} server - An instance of the MCP server used to communicate with the client.
|
|
18
|
-
* @param {string} [sessionId] - An optional session id used to associate the roots list with a specific client session.
|
|
19
|
-
*
|
|
20
|
-
* @throws {Error} In case of a failure to request the roots from the client, an error log message is sent.
|
|
21
|
+
* @param {McpServer} server - The 2025-era server whose client is asked.
|
|
21
22
|
*/
|
|
22
|
-
export const syncRoots = async (server
|
|
23
|
+
export const syncRoots = async (server) => {
|
|
23
24
|
const clientCapabilities = server.server.getClientCapabilities() || {};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}, sessionId);
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
await server.sendLoggingMessage({
|
|
45
|
-
level: "info",
|
|
46
|
-
logger: "everything-server",
|
|
47
|
-
data: "Client returned no roots set",
|
|
48
|
-
}, sessionId);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
catch (error) {
|
|
52
|
-
console.error(`Failed to request roots from client ${sessionId}: ${
|
|
53
|
-
/* v8 ignore start -- the SDK always rejects listRoots() with an Error (McpError) */
|
|
54
|
-
error instanceof Error ? error.message : String(error)
|
|
55
|
-
/* v8 ignore stop */
|
|
56
|
-
}`);
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
// If the roots have not been synced for this client,
|
|
60
|
-
// set notification handler and request initial roots
|
|
61
|
-
if (!roots.has(sessionId)) {
|
|
62
|
-
// Set the list changed notification handler
|
|
63
|
-
server.server.setNotificationHandler(RootsListChangedNotificationSchema, requestRoots);
|
|
64
|
-
// Request the initial roots list immediately
|
|
65
|
-
await requestRoots();
|
|
25
|
+
if (clientCapabilities.roots === undefined)
|
|
26
|
+
return;
|
|
27
|
+
const requestRoots = async () => {
|
|
28
|
+
try {
|
|
29
|
+
const response = await server.server.listRoots();
|
|
30
|
+
await server.sendLoggingMessage({
|
|
31
|
+
level: "info",
|
|
32
|
+
logger: "everything-server",
|
|
33
|
+
data: `Roots updated: ${response.roots.length} root(s) received from client`,
|
|
34
|
+
}, server.server.transport?.sessionId);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
console.error(`Failed to request roots from client: ${
|
|
38
|
+
/* v8 ignore start -- the SDK always rejects listRoots() with an Error (ProtocolError or SdkError) */
|
|
39
|
+
error instanceof Error ? error.message : String(error)
|
|
40
|
+
/* v8 ignore stop */
|
|
41
|
+
}`);
|
|
66
42
|
}
|
|
67
|
-
|
|
68
|
-
|
|
43
|
+
};
|
|
44
|
+
if (!watching.has(server)) {
|
|
45
|
+
watching.add(server);
|
|
46
|
+
server.server.setNotificationHandler("notifications/roots/list_changed", requestRoots);
|
|
69
47
|
}
|
|
48
|
+
await requestRoots();
|
|
70
49
|
};
|