@nest-mcp/client 1.0.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/CHANGELOG.md +139 -0
- package/README.md +845 -69
- package/dist/ai-sdk/index.d.mts +14 -0
- package/dist/ai-sdk/index.d.mts.map +1 -0
- package/dist/ai-sdk/index.mjs +27 -0
- package/dist/ai-sdk/index.mjs.map +1 -0
- package/dist/bootstrap-BIAcDnHp.d.mts +394 -0
- package/dist/bootstrap-BIAcDnHp.d.mts.map +1 -0
- package/dist/index.d.mts +480 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1960 -0
- package/dist/index.mjs.map +1 -0
- package/dist/oauth/dynamic-registration.d.mts +92 -0
- package/dist/oauth/dynamic-registration.d.mts.map +1 -0
- package/dist/oauth/dynamic-registration.mjs +392 -0
- package/dist/oauth/dynamic-registration.mjs.map +1 -0
- package/dist/oauth/index.d.mts +422 -0
- package/dist/oauth/index.d.mts.map +1 -0
- package/dist/oauth/index.mjs +3349 -0
- package/dist/oauth/index.mjs.map +1 -0
- package/dist/scope-CxGBCDdr.mjs +22 -0
- package/dist/scope-CxGBCDdr.mjs.map +1 -0
- package/package.json +76 -50
- package/scripts/external-smoke.mjs +693 -0
- package/dist/decorators/inject-mcp-client.decorator.d.ts +0 -3
- package/dist/decorators/inject-mcp-client.decorator.js +0 -12
- package/dist/decorators/on-notification.decorator.d.ts +0 -7
- package/dist/decorators/on-notification.decorator.js +0 -13
- package/dist/health/mcp-client.health.d.ts +0 -20
- package/dist/health/mcp-client.health.js +0 -42
- package/dist/index.d.ts +0 -21
- package/dist/index.js +0 -64
- package/dist/interfaces/client-options.interface.d.ts +0 -139
- package/dist/interfaces/client-options.interface.js +0 -3
- package/dist/mcp-client.module.d.ts +0 -20
- package/dist/mcp-client.module.js +0 -188
- package/dist/mcp-client.service.d.ts +0 -558
- package/dist/mcp-client.service.js +0 -532
- package/dist/mcp-clients.service.d.ts +0 -39
- package/dist/mcp-clients.service.js +0 -104
- package/dist/testing/mock-client.d.ts +0 -67
- package/dist/testing/mock-client.js +0 -154
- package/dist/transport/apply-auth-headers.d.ts +0 -3
- package/dist/transport/apply-auth-headers.js +0 -13
- package/dist/transport/client-transport.factory.d.ts +0 -4
- package/dist/transport/client-transport.factory.js +0 -21
- package/dist/transport/sse-client.transport.d.ts +0 -4
- package/dist/transport/sse-client.transport.js +0 -15
- package/dist/transport/stdio-client.transport.d.ts +0 -4
- package/dist/transport/stdio-client.transport.js +0 -14
- package/dist/transport/streamable-client.transport.d.ts +0 -4
- package/dist/transport/streamable-client.transport.js +0 -17
- package/dist/utils/format-error-message.d.ts +0 -2
- package/dist/utils/format-error-message.js +0 -7
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { access } from "node:fs/promises";
|
|
3
|
+
import { isIP } from "node:net";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
import { OAuthErrorCode, SdkErrorCode } from "@modelcontextprotocol/client";
|
|
7
|
+
import { getDefaultEnvironment } from "@modelcontextprotocol/client/stdio";
|
|
8
|
+
|
|
9
|
+
const SERVER_NAME = "external-smoke";
|
|
10
|
+
const DEFAULT_TIMEOUT_MS = 15_000;
|
|
11
|
+
const MAX_TIMEOUT_MS = 120_000;
|
|
12
|
+
const MODERN_PROTOCOL_START = "2026-07-28";
|
|
13
|
+
const BUILD_TERMINATION_GRACE_MS = 250;
|
|
14
|
+
const ALLOWED_ERROR_CODES = new Set([
|
|
15
|
+
...Object.values(SdkErrorCode),
|
|
16
|
+
...Object.values(OAuthErrorCode),
|
|
17
|
+
"ABORT_ERR",
|
|
18
|
+
"MCP_CLIENT_RUNTIME_CLOSED",
|
|
19
|
+
"MCP_CLIENT_SERVER_EXISTS",
|
|
20
|
+
"MCP_CLIENT_SERVER_NOT_FOUND",
|
|
21
|
+
"MCP_CLIENT_NOT_CONNECTED",
|
|
22
|
+
"MCP_CLIENT_SHUTDOWN_TIMEOUT",
|
|
23
|
+
]);
|
|
24
|
+
const redactionValues = new Set();
|
|
25
|
+
const HELP = `Usage:
|
|
26
|
+
pnpm --filter @nest-mcp/client smoke:external
|
|
27
|
+
|
|
28
|
+
The command reads configuration only from environment variables. It connects, performs the
|
|
29
|
+
protocol-appropriate liveness check, lists each advertised capability, optionally invokes one
|
|
30
|
+
tool, and prints a secret-free summary.
|
|
31
|
+
|
|
32
|
+
Common:
|
|
33
|
+
MCP_SMOKE_TRANSPORT http or stdio (required)
|
|
34
|
+
MCP_SMOKE_PROTOCOL auto, legacy, or a modern date >= 2026-07-28 (default: auto)
|
|
35
|
+
MCP_SMOKE_TIMEOUT_MS active-work timeout plus shutdown bound (default: 15000, max: 120000)
|
|
36
|
+
MCP_SMOKE_TOOL_NAME optional tool to invoke
|
|
37
|
+
MCP_SMOKE_TOOL_ARGUMENTS_JSON JSON object for the optional tool (default: {})
|
|
38
|
+
|
|
39
|
+
HTTP:
|
|
40
|
+
MCP_SMOKE_URL Streamable HTTP endpoint (required)
|
|
41
|
+
MCP_SMOKE_AUTH none, bearer, or oauth-client-credentials (default: none)
|
|
42
|
+
MCP_SMOKE_BEARER_TOKEN required for bearer
|
|
43
|
+
MCP_SMOKE_OAUTH_CLIENT_ID required for oauth-client-credentials
|
|
44
|
+
MCP_SMOKE_OAUTH_CLIENT_SECRET required for oauth-client-credentials
|
|
45
|
+
MCP_SMOKE_OAUTH_EXPECTED_ISSUER required issuer binding for oauth-client-credentials
|
|
46
|
+
MCP_SMOKE_OAUTH_SCOPE optional space-separated scopes
|
|
47
|
+
MCP_SMOKE_HTTP_HEADERS_JSON optional JSON object of additional string headers
|
|
48
|
+
MCP_SMOKE_ALLOW_INSECURE_HTTP=true permits non-loopback http:// targets
|
|
49
|
+
|
|
50
|
+
stdio:
|
|
51
|
+
MCP_SMOKE_COMMAND executable to spawn (required)
|
|
52
|
+
MCP_SMOKE_ARGS_JSON optional JSON array of string arguments
|
|
53
|
+
MCP_SMOKE_CWD optional child working directory
|
|
54
|
+
MCP_SMOKE_STDIO_ENV_NAMES comma-separated environment variable names to pass to the child
|
|
55
|
+
MCP_SMOKE_STDIO_STDERR ignore (default) or inherit; inherited output bypasses redaction
|
|
56
|
+
|
|
57
|
+
Safety:
|
|
58
|
+
The command refuses to run when CI is set. MCP_SMOKE_ALLOW_CI=true is an explicit override for
|
|
59
|
+
trusted, manually configured CI jobs. It is not part of the repository's default test/verify path.
|
|
60
|
+
`;
|
|
61
|
+
|
|
62
|
+
class SmokeConfigurationError extends Error {
|
|
63
|
+
constructor(message) {
|
|
64
|
+
super(message);
|
|
65
|
+
this.name = "SmokeConfigurationError";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function main() {
|
|
70
|
+
const commandArguments = process.argv.slice(2).filter((argument) => argument !== "--");
|
|
71
|
+
if (commandArguments.includes("--help") || commandArguments.includes("-h")) {
|
|
72
|
+
process.stdout.write(HELP);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (commandArguments.length > 0) {
|
|
76
|
+
throw new SmokeConfigurationError(
|
|
77
|
+
`Unexpected command argument. This command accepts environment variables only; use --help.`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
assertCiOptIn(process.env);
|
|
82
|
+
requireChoice(process.env, "MCP_SMOKE_TRANSPORT", new Set(["http", "stdio"]));
|
|
83
|
+
const timeoutMs = readTimeout(process.env.MCP_SMOKE_TIMEOUT_MS);
|
|
84
|
+
const abortController = new AbortController();
|
|
85
|
+
const timeout = setTimeout(() => {
|
|
86
|
+
abortController.abort(new Error("External MCP smoke test timed out."));
|
|
87
|
+
}, timeoutMs);
|
|
88
|
+
const abortForSignal = () => {
|
|
89
|
+
abortController.abort(new Error("External MCP smoke test interrupted."));
|
|
90
|
+
};
|
|
91
|
+
process.once("SIGINT", abortForSignal);
|
|
92
|
+
process.once("SIGTERM", abortForSignal);
|
|
93
|
+
|
|
94
|
+
let runtime;
|
|
95
|
+
let failure;
|
|
96
|
+
let summary;
|
|
97
|
+
try {
|
|
98
|
+
const { ClientCredentialsProvider, McpClientRuntime } = await loadClientApi(
|
|
99
|
+
abortController.signal,
|
|
100
|
+
);
|
|
101
|
+
abortController.signal.throwIfAborted();
|
|
102
|
+
const configuration = loadConfiguration(
|
|
103
|
+
process.env,
|
|
104
|
+
redactionValues,
|
|
105
|
+
ClientCredentialsProvider,
|
|
106
|
+
timeoutMs,
|
|
107
|
+
);
|
|
108
|
+
abortController.signal.throwIfAborted();
|
|
109
|
+
runtime = new McpClientRuntime({
|
|
110
|
+
clientInfo: { name: "@nest-mcp/client-external-smoke", version: "1.0.0" },
|
|
111
|
+
shutdownTimeoutMs: timeoutMs,
|
|
112
|
+
servers: [
|
|
113
|
+
{
|
|
114
|
+
name: SERVER_NAME,
|
|
115
|
+
transport: configuration.transport,
|
|
116
|
+
clientOptions: {
|
|
117
|
+
versionNegotiation: { mode: configuration.negotiationMode },
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const requestOptions = {
|
|
124
|
+
signal: abortController.signal,
|
|
125
|
+
timeout: timeoutMs,
|
|
126
|
+
maxTotalTimeout: timeoutMs,
|
|
127
|
+
};
|
|
128
|
+
await runtime.connect(SERVER_NAME, requestOptions);
|
|
129
|
+
let snapshot = runtime.snapshot(SERVER_NAME);
|
|
130
|
+
const liveness = await probeProtocolLiveness(runtime, snapshot.protocolEra, requestOptions);
|
|
131
|
+
snapshot = runtime.snapshot(SERVER_NAME);
|
|
132
|
+
const probes = await probeAdvertisedCapabilities(
|
|
133
|
+
runtime,
|
|
134
|
+
snapshot.serverCapabilities,
|
|
135
|
+
configuration.tool,
|
|
136
|
+
requestOptions,
|
|
137
|
+
liveness,
|
|
138
|
+
);
|
|
139
|
+
summary = {
|
|
140
|
+
ok: true,
|
|
141
|
+
transport: configuration.transport.kind,
|
|
142
|
+
negotiation: configuration.negotiationLabel,
|
|
143
|
+
state: snapshot.state,
|
|
144
|
+
protocolEra: snapshot.protocolEra ?? null,
|
|
145
|
+
protocolVersion: snapshot.negotiatedProtocolVersion ?? null,
|
|
146
|
+
sessionIdAssigned: snapshot.sessionId !== undefined,
|
|
147
|
+
probes,
|
|
148
|
+
};
|
|
149
|
+
} catch (error) {
|
|
150
|
+
failure = error;
|
|
151
|
+
} finally {
|
|
152
|
+
clearTimeout(timeout);
|
|
153
|
+
if (runtime !== undefined) {
|
|
154
|
+
try {
|
|
155
|
+
await runtime.close();
|
|
156
|
+
} catch (error) {
|
|
157
|
+
failure =
|
|
158
|
+
failure === undefined
|
|
159
|
+
? error
|
|
160
|
+
: new AggregateError(
|
|
161
|
+
[failure, error],
|
|
162
|
+
"The smoke test and runtime cleanup both failed.",
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (failure === undefined && abortController.signal.aborted) {
|
|
167
|
+
failure = abortReason(abortController.signal);
|
|
168
|
+
}
|
|
169
|
+
process.removeListener("SIGINT", abortForSignal);
|
|
170
|
+
process.removeListener("SIGTERM", abortForSignal);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (failure !== undefined) throw failure;
|
|
174
|
+
process.stdout.write(`${JSON.stringify(summary, null, 2)}\n`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function loadClientApi(signal) {
|
|
178
|
+
const distribution = new URL("../dist/index.mjs", import.meta.url);
|
|
179
|
+
const source = new URL("../src/index.ts", import.meta.url);
|
|
180
|
+
if (await pathExists(source)) {
|
|
181
|
+
await buildDevelopmentPackage(signal);
|
|
182
|
+
} else if (!(await pathExists(distribution))) {
|
|
183
|
+
throw new SmokeConfigurationError(
|
|
184
|
+
"The package is incomplete: dist/index.mjs and the development source are both missing.",
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
return import(distribution.href);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function pathExists(url) {
|
|
191
|
+
try {
|
|
192
|
+
await access(url);
|
|
193
|
+
return true;
|
|
194
|
+
} catch {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function buildDevelopmentPackage(signal) {
|
|
200
|
+
const workspaceRoot = fileURLToPath(new URL("../../..", import.meta.url));
|
|
201
|
+
await runPnpmBuild(workspaceRoot, "@nest-mcp/core", signal);
|
|
202
|
+
await runPnpmBuild(workspaceRoot, "@nest-mcp/client", signal);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function runPnpmBuild(workspaceRoot, packageName, signal) {
|
|
206
|
+
if (signal.aborted) return Promise.reject(abortReason(signal));
|
|
207
|
+
return new Promise((resolve, reject) => {
|
|
208
|
+
const child = spawn("pnpm", ["--filter", packageName, "run", "build"], {
|
|
209
|
+
cwd: workspaceRoot,
|
|
210
|
+
detached: process.platform !== "win32",
|
|
211
|
+
env: getDefaultEnvironment(),
|
|
212
|
+
stdio: "inherit",
|
|
213
|
+
});
|
|
214
|
+
let abortStarted = false;
|
|
215
|
+
let childExited = false;
|
|
216
|
+
let escalationComplete = false;
|
|
217
|
+
let settled = false;
|
|
218
|
+
let forceKillTimeout;
|
|
219
|
+
|
|
220
|
+
const cleanup = () => {
|
|
221
|
+
signal.removeEventListener("abort", onAbort);
|
|
222
|
+
};
|
|
223
|
+
const settle = (error) => {
|
|
224
|
+
if (settled) return;
|
|
225
|
+
settled = true;
|
|
226
|
+
cleanup();
|
|
227
|
+
if (forceKillTimeout !== undefined) clearTimeout(forceKillTimeout);
|
|
228
|
+
if (error === undefined) resolve();
|
|
229
|
+
else reject(error);
|
|
230
|
+
};
|
|
231
|
+
const finishAbortedBuild = () => {
|
|
232
|
+
if (childExited && escalationComplete) settle(abortReason(signal));
|
|
233
|
+
};
|
|
234
|
+
const onAbort = () => {
|
|
235
|
+
if (abortStarted || settled) return;
|
|
236
|
+
abortStarted = true;
|
|
237
|
+
terminateBuildProcess(child, "SIGTERM");
|
|
238
|
+
forceKillTimeout = setTimeout(() => {
|
|
239
|
+
terminateBuildProcess(child, "SIGKILL");
|
|
240
|
+
escalationComplete = true;
|
|
241
|
+
finishAbortedBuild();
|
|
242
|
+
}, BUILD_TERMINATION_GRACE_MS);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
246
|
+
if (signal.aborted) onAbort();
|
|
247
|
+
child.once("spawn", () => {
|
|
248
|
+
if (signal.aborted) terminateBuildProcess(child, "SIGTERM");
|
|
249
|
+
});
|
|
250
|
+
child.once("error", () => {
|
|
251
|
+
settle(
|
|
252
|
+
signal.aborted
|
|
253
|
+
? abortReason(signal)
|
|
254
|
+
: new SmokeConfigurationError(
|
|
255
|
+
"The development build could not start; install pnpm or run the package build manually.",
|
|
256
|
+
),
|
|
257
|
+
);
|
|
258
|
+
});
|
|
259
|
+
child.once("exit", (code) => {
|
|
260
|
+
childExited = true;
|
|
261
|
+
if (signal.aborted) {
|
|
262
|
+
if (!abortStarted) onAbort();
|
|
263
|
+
finishAbortedBuild();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (code === 0) {
|
|
267
|
+
settle();
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
settle(new SmokeConfigurationError("The development package build failed."));
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function terminateBuildProcess(child, signal) {
|
|
276
|
+
const pid = child.pid;
|
|
277
|
+
if (pid === undefined) return;
|
|
278
|
+
try {
|
|
279
|
+
if (process.platform === "win32") child.kill(signal);
|
|
280
|
+
else process.kill(-pid, signal);
|
|
281
|
+
} catch {
|
|
282
|
+
try {
|
|
283
|
+
child.kill(signal);
|
|
284
|
+
} catch {
|
|
285
|
+
// The exact child already exited between the PID check and termination request.
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function abortReason(signal) {
|
|
291
|
+
return signal.reason instanceof Error
|
|
292
|
+
? signal.reason
|
|
293
|
+
: new Error("External MCP smoke test aborted.");
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function loadConfiguration(environment, secretValues, ClientCredentialsProvider, timeoutMs) {
|
|
297
|
+
const { mode: negotiationMode, label: negotiationLabel } = readNegotiationMode(
|
|
298
|
+
environment.MCP_SMOKE_PROTOCOL,
|
|
299
|
+
);
|
|
300
|
+
const transportKind = requireChoice(
|
|
301
|
+
environment,
|
|
302
|
+
"MCP_SMOKE_TRANSPORT",
|
|
303
|
+
new Set(["http", "stdio"]),
|
|
304
|
+
);
|
|
305
|
+
const transport =
|
|
306
|
+
transportKind === "http"
|
|
307
|
+
? loadHttpTransport(environment, secretValues, ClientCredentialsProvider)
|
|
308
|
+
: loadStdioTransport(environment, secretValues);
|
|
309
|
+
const toolName = optionalNonEmpty(environment.MCP_SMOKE_TOOL_NAME);
|
|
310
|
+
const toolArgumentsRaw = environment.MCP_SMOKE_TOOL_ARGUMENTS_JSON;
|
|
311
|
+
if (toolName === undefined && optionalNonEmpty(toolArgumentsRaw) !== undefined) {
|
|
312
|
+
throw new SmokeConfigurationError(
|
|
313
|
+
"MCP_SMOKE_TOOL_ARGUMENTS_JSON requires MCP_SMOKE_TOOL_NAME.",
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
let tool;
|
|
317
|
+
if (toolName !== undefined) {
|
|
318
|
+
const toolArguments = readJsonObject(toolArgumentsRaw, "MCP_SMOKE_TOOL_ARGUMENTS_JSON", {});
|
|
319
|
+
registerScalarSecrets(toolArguments, secretValues);
|
|
320
|
+
tool = { name: toolName, arguments: toolArguments };
|
|
321
|
+
}
|
|
322
|
+
if (toolArgumentsRaw !== undefined) secretValues.add(toolArgumentsRaw);
|
|
323
|
+
|
|
324
|
+
return {
|
|
325
|
+
negotiationLabel,
|
|
326
|
+
negotiationMode,
|
|
327
|
+
timeoutMs,
|
|
328
|
+
tool,
|
|
329
|
+
transport,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function loadHttpTransport(environment, secretValues, ClientCredentialsProvider) {
|
|
334
|
+
const rawUrl = requireEnvironment(environment, "MCP_SMOKE_URL");
|
|
335
|
+
secretValues.add(rawUrl);
|
|
336
|
+
const url = readUrl(rawUrl, "MCP_SMOKE_URL");
|
|
337
|
+
secretValues.add(url.href);
|
|
338
|
+
if (url.username !== "" || url.password !== "") {
|
|
339
|
+
throw new SmokeConfigurationError("MCP_SMOKE_URL must not contain URL credentials.");
|
|
340
|
+
}
|
|
341
|
+
if (
|
|
342
|
+
url.protocol === "http:" &&
|
|
343
|
+
!isLoopbackHostname(url.hostname) &&
|
|
344
|
+
!isTrue(environment.MCP_SMOKE_ALLOW_INSECURE_HTTP)
|
|
345
|
+
) {
|
|
346
|
+
throw new SmokeConfigurationError(
|
|
347
|
+
"Non-loopback http:// targets require MCP_SMOKE_ALLOW_INSECURE_HTTP=true.",
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const authMode = optionalNonEmpty(environment.MCP_SMOKE_AUTH) ?? "none";
|
|
352
|
+
if (!new Set(["none", "bearer", "oauth-client-credentials"]).has(authMode)) {
|
|
353
|
+
throw new SmokeConfigurationError(
|
|
354
|
+
"MCP_SMOKE_AUTH must be none, bearer, or oauth-client-credentials.",
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
const headers = readHeaders(environment.MCP_SMOKE_HTTP_HEADERS_JSON, secretValues);
|
|
358
|
+
if (authMode !== "none" && headers.has("authorization")) {
|
|
359
|
+
throw new SmokeConfigurationError(
|
|
360
|
+
"Do not combine an Authorization header with MCP_SMOKE_AUTH.",
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
let authProvider;
|
|
365
|
+
if (authMode === "bearer") {
|
|
366
|
+
const token = requireEnvironment(environment, "MCP_SMOKE_BEARER_TOKEN");
|
|
367
|
+
secretValues.add(token);
|
|
368
|
+
authProvider = { token: async () => token };
|
|
369
|
+
}
|
|
370
|
+
if (authMode === "oauth-client-credentials") {
|
|
371
|
+
const clientId = requireEnvironment(environment, "MCP_SMOKE_OAUTH_CLIENT_ID");
|
|
372
|
+
const clientSecret = requireEnvironment(environment, "MCP_SMOKE_OAUTH_CLIENT_SECRET");
|
|
373
|
+
const expectedIssuer = requireEnvironment(environment, "MCP_SMOKE_OAUTH_EXPECTED_ISSUER");
|
|
374
|
+
const issuerUrl = readUrl(expectedIssuer, "MCP_SMOKE_OAUTH_EXPECTED_ISSUER");
|
|
375
|
+
if (issuerUrl.username !== "" || issuerUrl.password !== "") {
|
|
376
|
+
throw new SmokeConfigurationError(
|
|
377
|
+
"MCP_SMOKE_OAUTH_EXPECTED_ISSUER must not contain URL credentials.",
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
if (issuerUrl.protocol === "http:" && !isLoopbackHostname(issuerUrl.hostname)) {
|
|
381
|
+
throw new SmokeConfigurationError(
|
|
382
|
+
"MCP_SMOKE_OAUTH_EXPECTED_ISSUER must use https:// unless it is loopback.",
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
secretValues.add(clientId);
|
|
386
|
+
secretValues.add(clientSecret);
|
|
387
|
+
secretValues.add(expectedIssuer);
|
|
388
|
+
authProvider = new ClientCredentialsProvider({
|
|
389
|
+
clientId,
|
|
390
|
+
clientSecret,
|
|
391
|
+
expectedIssuer,
|
|
392
|
+
...(optionalNonEmpty(environment.MCP_SMOKE_OAUTH_SCOPE) === undefined
|
|
393
|
+
? {}
|
|
394
|
+
: { scope: environment.MCP_SMOKE_OAUTH_SCOPE }),
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return {
|
|
399
|
+
kind: "http",
|
|
400
|
+
url,
|
|
401
|
+
...(authProvider === undefined ? {} : { authProvider }),
|
|
402
|
+
...(headers.size === 0 ? {} : { requestInit: { headers } }),
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function loadStdioTransport(environment, secretValues) {
|
|
407
|
+
const command = requireEnvironment(environment, "MCP_SMOKE_COMMAND");
|
|
408
|
+
const args = readJsonStringArray(environment.MCP_SMOKE_ARGS_JSON, "MCP_SMOKE_ARGS_JSON");
|
|
409
|
+
const cwd = optionalNonEmpty(environment.MCP_SMOKE_CWD);
|
|
410
|
+
const stderr = optionalNonEmpty(environment.MCP_SMOKE_STDIO_STDERR) ?? "ignore";
|
|
411
|
+
if (stderr !== "ignore" && stderr !== "inherit") {
|
|
412
|
+
throw new SmokeConfigurationError("MCP_SMOKE_STDIO_STDERR must be ignore or inherit.");
|
|
413
|
+
}
|
|
414
|
+
const environmentNames = readEnvironmentNames(environment.MCP_SMOKE_STDIO_ENV_NAMES);
|
|
415
|
+
const childEnvironment = getDefaultEnvironment();
|
|
416
|
+
for (const name of environmentNames) {
|
|
417
|
+
const value = requireEnvironment(environment, name);
|
|
418
|
+
childEnvironment[name] = value;
|
|
419
|
+
secretValues.add(value);
|
|
420
|
+
}
|
|
421
|
+
for (const argument of args) secretValues.add(argument);
|
|
422
|
+
|
|
423
|
+
return {
|
|
424
|
+
kind: "stdio",
|
|
425
|
+
command,
|
|
426
|
+
args,
|
|
427
|
+
env: childEnvironment,
|
|
428
|
+
stderr,
|
|
429
|
+
...(cwd === undefined ? {} : { cwd }),
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
async function probeProtocolLiveness(runtime, protocolEra, requestOptions) {
|
|
434
|
+
if (protocolEra === "legacy") {
|
|
435
|
+
await runtime.ping(SERVER_NAME, requestOptions);
|
|
436
|
+
return { discover: null, ping: true };
|
|
437
|
+
}
|
|
438
|
+
if (protocolEra === "modern") {
|
|
439
|
+
await runtime.discover(SERVER_NAME, requestOptions);
|
|
440
|
+
return { discover: true, ping: null };
|
|
441
|
+
}
|
|
442
|
+
throw new Error("The connected MCP server did not report a protocol era.");
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
async function probeAdvertisedCapabilities(runtime, capabilities, tool, requestOptions, liveness) {
|
|
446
|
+
const probes = {
|
|
447
|
+
discover: liveness.discover,
|
|
448
|
+
ping: liveness.ping,
|
|
449
|
+
tools: null,
|
|
450
|
+
resources: null,
|
|
451
|
+
prompts: null,
|
|
452
|
+
toolInvoked: false,
|
|
453
|
+
};
|
|
454
|
+
let listedTools;
|
|
455
|
+
if (hasCapability(capabilities, "tools") || tool !== undefined) {
|
|
456
|
+
listedTools = await runtime.listTools(SERVER_NAME, undefined, requestOptions);
|
|
457
|
+
probes.tools = listedTools.tools.length;
|
|
458
|
+
}
|
|
459
|
+
if (hasCapability(capabilities, "resources")) {
|
|
460
|
+
const result = await runtime.listResources(SERVER_NAME, undefined, requestOptions);
|
|
461
|
+
probes.resources = result.resources.length;
|
|
462
|
+
}
|
|
463
|
+
if (hasCapability(capabilities, "prompts")) {
|
|
464
|
+
const result = await runtime.listPrompts(SERVER_NAME, undefined, requestOptions);
|
|
465
|
+
probes.prompts = result.prompts.length;
|
|
466
|
+
}
|
|
467
|
+
if (tool !== undefined) {
|
|
468
|
+
if (!listedTools?.tools.some((entry) => entry.name === tool.name)) {
|
|
469
|
+
throw new SmokeConfigurationError(
|
|
470
|
+
"MCP_SMOKE_TOOL_NAME is not present in the server's tools list.",
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
const result = await runtime.callTool(
|
|
474
|
+
SERVER_NAME,
|
|
475
|
+
{ name: tool.name, arguments: tool.arguments },
|
|
476
|
+
requestOptions,
|
|
477
|
+
);
|
|
478
|
+
if (result.isError === true) {
|
|
479
|
+
throw new Error("The configured smoke-test tool returned isError=true.");
|
|
480
|
+
}
|
|
481
|
+
probes.toolInvoked = true;
|
|
482
|
+
}
|
|
483
|
+
return probes;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function hasCapability(capabilities, name) {
|
|
487
|
+
return capabilities !== undefined && capabilities[name] !== undefined;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function readHeaders(raw, secretValues) {
|
|
491
|
+
const values = readJsonObject(raw, "MCP_SMOKE_HTTP_HEADERS_JSON", {});
|
|
492
|
+
const headers = new Headers();
|
|
493
|
+
for (const [name, value] of Object.entries(values)) {
|
|
494
|
+
if (typeof value !== "string") {
|
|
495
|
+
throw new SmokeConfigurationError("MCP_SMOKE_HTTP_HEADERS_JSON values must all be strings.");
|
|
496
|
+
}
|
|
497
|
+
try {
|
|
498
|
+
headers.set(name, value);
|
|
499
|
+
} catch {
|
|
500
|
+
throw new SmokeConfigurationError("MCP_SMOKE_HTTP_HEADERS_JSON contains an invalid header.");
|
|
501
|
+
}
|
|
502
|
+
secretValues.add(value);
|
|
503
|
+
}
|
|
504
|
+
if (raw !== undefined) secretValues.add(raw);
|
|
505
|
+
return headers;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function readJsonObject(raw, name, fallback) {
|
|
509
|
+
if (optionalNonEmpty(raw) === undefined) return fallback;
|
|
510
|
+
let parsed;
|
|
511
|
+
try {
|
|
512
|
+
parsed = JSON.parse(raw);
|
|
513
|
+
} catch {
|
|
514
|
+
throw new SmokeConfigurationError(`${name} must contain valid JSON.`);
|
|
515
|
+
}
|
|
516
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
517
|
+
throw new SmokeConfigurationError(`${name} must contain a JSON object.`);
|
|
518
|
+
}
|
|
519
|
+
return parsed;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function registerScalarSecrets(value, secretValues) {
|
|
523
|
+
const pending = [value];
|
|
524
|
+
while (pending.length > 0) {
|
|
525
|
+
const candidate = pending.pop();
|
|
526
|
+
if (candidate === null) {
|
|
527
|
+
secretValues.add("null");
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
if (Array.isArray(candidate)) {
|
|
531
|
+
for (const entry of candidate) pending.push(entry);
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (typeof candidate === "object") {
|
|
535
|
+
for (const entry of Object.values(candidate)) pending.push(entry);
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
if (
|
|
539
|
+
typeof candidate === "string" ||
|
|
540
|
+
typeof candidate === "number" ||
|
|
541
|
+
typeof candidate === "boolean"
|
|
542
|
+
) {
|
|
543
|
+
secretValues.add(String(candidate));
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function readJsonStringArray(raw, name) {
|
|
549
|
+
if (optionalNonEmpty(raw) === undefined) return [];
|
|
550
|
+
let parsed;
|
|
551
|
+
try {
|
|
552
|
+
parsed = JSON.parse(raw);
|
|
553
|
+
} catch {
|
|
554
|
+
throw new SmokeConfigurationError(`${name} must contain valid JSON.`);
|
|
555
|
+
}
|
|
556
|
+
if (!Array.isArray(parsed) || !parsed.every((entry) => typeof entry === "string")) {
|
|
557
|
+
throw new SmokeConfigurationError(`${name} must contain a JSON array of strings.`);
|
|
558
|
+
}
|
|
559
|
+
return parsed;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
function readEnvironmentNames(raw) {
|
|
563
|
+
if (optionalNonEmpty(raw) === undefined) return [];
|
|
564
|
+
const names = raw.split(",").map((name) => name.trim());
|
|
565
|
+
if (names.some((name) => !/^[A-Za-z_][A-Za-z0-9_]*$/u.test(name))) {
|
|
566
|
+
throw new SmokeConfigurationError(
|
|
567
|
+
"MCP_SMOKE_STDIO_ENV_NAMES must be a comma-separated list of environment names.",
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
return [...new Set(names)];
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function readNegotiationMode(raw) {
|
|
574
|
+
const value = optionalNonEmpty(raw) ?? "auto";
|
|
575
|
+
if (value === "auto" || value === "legacy") return { mode: value, label: value };
|
|
576
|
+
if (!isCalendarDate(value) || value < MODERN_PROTOCOL_START) {
|
|
577
|
+
throw new SmokeConfigurationError(
|
|
578
|
+
`MCP_SMOKE_PROTOCOL must be auto, legacy, or a valid calendar date on or after ${MODERN_PROTOCOL_START}.`,
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
return { mode: { pin: value }, label: `pin:${value}` };
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function isCalendarDate(value) {
|
|
585
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/u.exec(value);
|
|
586
|
+
if (match === null) return false;
|
|
587
|
+
const year = Number(match[1]);
|
|
588
|
+
const month = Number(match[2]);
|
|
589
|
+
const day = Number(match[3]);
|
|
590
|
+
if (month < 1 || month > 12 || day < 1) return false;
|
|
591
|
+
const daysInMonth = [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
592
|
+
return day <= daysInMonth[month - 1];
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function isLeapYear(year) {
|
|
596
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function readTimeout(raw) {
|
|
600
|
+
if (optionalNonEmpty(raw) === undefined) return DEFAULT_TIMEOUT_MS;
|
|
601
|
+
const value = Number(raw);
|
|
602
|
+
if (!Number.isSafeInteger(value) || value < 100 || value > MAX_TIMEOUT_MS) {
|
|
603
|
+
throw new SmokeConfigurationError(
|
|
604
|
+
`MCP_SMOKE_TIMEOUT_MS must be an integer from 100 to ${String(MAX_TIMEOUT_MS)}.`,
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
return value;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
function readUrl(raw, name) {
|
|
611
|
+
let url;
|
|
612
|
+
try {
|
|
613
|
+
url = new URL(raw);
|
|
614
|
+
} catch {
|
|
615
|
+
throw new SmokeConfigurationError(`${name} must be an absolute http:// or https:// URL.`);
|
|
616
|
+
}
|
|
617
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
618
|
+
throw new SmokeConfigurationError(`${name} must be an absolute http:// or https:// URL.`);
|
|
619
|
+
}
|
|
620
|
+
return url;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function isLoopbackHostname(hostname) {
|
|
624
|
+
if (hostname === "localhost") return true;
|
|
625
|
+
const unbracketed =
|
|
626
|
+
hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname;
|
|
627
|
+
const ipVersion = isIP(unbracketed);
|
|
628
|
+
if (ipVersion === 4) return unbracketed.split(".", 1)[0] === "127";
|
|
629
|
+
return ipVersion === 6 && unbracketed === "::1";
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
function requireChoice(environment, name, choices) {
|
|
633
|
+
const value = requireEnvironment(environment, name);
|
|
634
|
+
if (!choices.has(value)) {
|
|
635
|
+
throw new SmokeConfigurationError(`${name} must be one of: ${[...choices].join(", ")}.`);
|
|
636
|
+
}
|
|
637
|
+
return value;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function requireEnvironment(environment, name) {
|
|
641
|
+
const value = environment[name];
|
|
642
|
+
if (optionalNonEmpty(value) === undefined) {
|
|
643
|
+
throw new SmokeConfigurationError(`Missing required environment variable: ${name}.`);
|
|
644
|
+
}
|
|
645
|
+
return value;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function optionalNonEmpty(value) {
|
|
649
|
+
return typeof value === "string" && value.trim().length > 0 ? value : undefined;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function assertCiOptIn(environment) {
|
|
653
|
+
if (optionalNonEmpty(environment.CI) !== undefined && !isTrue(environment.MCP_SMOKE_ALLOW_CI)) {
|
|
654
|
+
throw new SmokeConfigurationError(
|
|
655
|
+
"External MCP smoke tests are disabled when CI is set; use MCP_SMOKE_ALLOW_CI=true only in a trusted opt-in job.",
|
|
656
|
+
);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function isTrue(value) {
|
|
661
|
+
return value === "true" || value === "1";
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
function errorSummary(error, secretValues) {
|
|
665
|
+
if (!(error instanceof Error)) {
|
|
666
|
+
return { name: "Error", message: "External MCP smoke test failed with a non-Error value." };
|
|
667
|
+
}
|
|
668
|
+
const summary = {
|
|
669
|
+
name: error.name,
|
|
670
|
+
message: redact(error.message || "External MCP smoke test failed.", secretValues),
|
|
671
|
+
};
|
|
672
|
+
const code = Reflect.get(error, "code");
|
|
673
|
+
if (typeof code === "string" && ALLOWED_ERROR_CODES.has(code)) summary.code = code;
|
|
674
|
+
return summary;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
function redact(value, secretValues) {
|
|
678
|
+
let redacted = value;
|
|
679
|
+
const ordered = [...secretValues]
|
|
680
|
+
.filter((secret) => typeof secret === "string" && secret.length > 0)
|
|
681
|
+
.toSorted((left, right) => right.length - left.length);
|
|
682
|
+
for (const secret of ordered) redacted = redacted.split(secret).join("[REDACTED]");
|
|
683
|
+
return redacted;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
try {
|
|
687
|
+
await main();
|
|
688
|
+
} catch (error) {
|
|
689
|
+
process.stderr.write(
|
|
690
|
+
`${JSON.stringify({ ok: false, error: errorSummary(error, redactionValues) }, null, 2)}\n`,
|
|
691
|
+
);
|
|
692
|
+
process.exitCode = 1;
|
|
693
|
+
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMcpClientToken = getMcpClientToken;
|
|
4
|
-
exports.InjectMcpClient = InjectMcpClient;
|
|
5
|
-
const common_1 = require("@nestjs/common");
|
|
6
|
-
function getMcpClientToken(name) {
|
|
7
|
-
return `MCP_CLIENT_${name}`;
|
|
8
|
-
}
|
|
9
|
-
function InjectMcpClient(name) {
|
|
10
|
-
return (0, common_1.Inject)(getMcpClientToken(name));
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=inject-mcp-client.decorator.js.map
|