@wincode/mcp 0.0.0-stage → 0.1.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 +57 -2
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +34 -4
- package/src/client.ts +370 -0
- package/src/config/resolve.ts +722 -0
- package/src/config/schema.ts +96 -0
- package/src/config.ts +31 -0
- package/src/identifiers.ts +6 -0
- package/src/index.ts +10 -0
- package/src/management.ts +257 -0
- package/src/manifest.ts +102 -0
- package/src/oauth.ts +347 -0
- package/src/plugin/config.ts +40 -0
- package/src/plugin/index.ts +501 -0
- package/src/registry.ts +1455 -0
- package/src/result.ts +504 -0
- package/src/sanitize.ts +32 -0
- package/src/tool-identity.ts +24 -0
- package/src/utf8-byte-length.ts +4 -0
- package/test/client.test.ts +642 -0
- package/test/config-resolve.test.ts +376 -0
- package/test/management.test.ts +186 -0
- package/test/manifest.test.ts +17 -0
- package/test/oauth.test.ts +106 -0
- package/test/plugin-config.test.ts +117 -0
- package/test/result.test.ts +291 -0
- package/test/sanitize.test.ts +103 -0
- package/test/tool-identity.test.ts +27 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,722 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
isPlainObject,
|
|
4
|
+
isUndefined,
|
|
5
|
+
omitUndefined,
|
|
6
|
+
resolveConfigEnvironmentVariables,
|
|
7
|
+
} from "@wincode/utils";
|
|
8
|
+
import type { ZodError } from "zod";
|
|
9
|
+
import {
|
|
10
|
+
DEFAULT_MCP_TIMEOUTS,
|
|
11
|
+
type McpExposure,
|
|
12
|
+
type McpTimeouts,
|
|
13
|
+
mcpServerNameSchema,
|
|
14
|
+
type RawMcpServerConfig,
|
|
15
|
+
type ResolvedMcpServerConfig,
|
|
16
|
+
rawServerSchema,
|
|
17
|
+
resolvedServerSchema,
|
|
18
|
+
} from "./schema";
|
|
19
|
+
|
|
20
|
+
const PROJECT_OVERLAY_FIELDS = new Set(["enabled", "exposure", "toolExposure"]);
|
|
21
|
+
|
|
22
|
+
export type McpConfigOrigin = Readonly<{
|
|
23
|
+
path: string;
|
|
24
|
+
scope: string;
|
|
25
|
+
}>;
|
|
26
|
+
|
|
27
|
+
export type McpConfigSource = McpConfigOrigin &
|
|
28
|
+
Readonly<{ document: Readonly<Record<string, unknown>> }>;
|
|
29
|
+
|
|
30
|
+
export type McpConfigDiagnosticCode =
|
|
31
|
+
| "duplicate-config"
|
|
32
|
+
| "parse-error"
|
|
33
|
+
| "read-error"
|
|
34
|
+
| "unsafe-key"
|
|
35
|
+
| "invalid-field"
|
|
36
|
+
| "invalid-scope"
|
|
37
|
+
| "invalid-server"
|
|
38
|
+
| "invalid-timeout"
|
|
39
|
+
| "invalid-url"
|
|
40
|
+
| "missing-env"
|
|
41
|
+
| "unsupported-auth"
|
|
42
|
+
| "command-failed"
|
|
43
|
+
| "unsupported-transport"
|
|
44
|
+
| "unsupported-exposure";
|
|
45
|
+
|
|
46
|
+
export type McpConfigDiagnostic = McpConfigOrigin &
|
|
47
|
+
Readonly<{
|
|
48
|
+
code: McpConfigDiagnosticCode;
|
|
49
|
+
message: string;
|
|
50
|
+
serverName?: string;
|
|
51
|
+
}>;
|
|
52
|
+
|
|
53
|
+
export type McpConfigSnapshot = Readonly<{
|
|
54
|
+
diagnostics: readonly McpConfigDiagnostic[];
|
|
55
|
+
document: Readonly<Record<string, unknown>>;
|
|
56
|
+
sourceFor(path: readonly string[]): McpConfigOrigin | undefined;
|
|
57
|
+
sources: readonly McpConfigSource[];
|
|
58
|
+
}>;
|
|
59
|
+
|
|
60
|
+
export type McpConfigResult = Readonly<{
|
|
61
|
+
diagnostics: readonly McpConfigDiagnostic[];
|
|
62
|
+
invalidServers?: Readonly<Record<string, InvalidMcpServerConfig>>;
|
|
63
|
+
serverSources?: Readonly<Record<string, McpConfigOrigin>>;
|
|
64
|
+
servers: Readonly<Record<string, ResolvedMcpServerConfig>>;
|
|
65
|
+
}>;
|
|
66
|
+
|
|
67
|
+
export type InvalidMcpServerConfig = Readonly<{
|
|
68
|
+
error: string;
|
|
69
|
+
name: string;
|
|
70
|
+
transport: "local" | "remote";
|
|
71
|
+
}>;
|
|
72
|
+
|
|
73
|
+
type EffectiveServer = Readonly<{
|
|
74
|
+
raw: Readonly<Record<string, unknown>>;
|
|
75
|
+
source: McpConfigSource;
|
|
76
|
+
}>;
|
|
77
|
+
|
|
78
|
+
type ResolutionContext = Readonly<{
|
|
79
|
+
diagnostics: McpConfigDiagnostic[];
|
|
80
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
81
|
+
name: string;
|
|
82
|
+
commandOutputs: Map<string, string>;
|
|
83
|
+
source: McpConfigSource;
|
|
84
|
+
workspace: string;
|
|
85
|
+
}>;
|
|
86
|
+
|
|
87
|
+
const MAX_COMMAND_OUTPUT_BYTES = 64 * 1024;
|
|
88
|
+
|
|
89
|
+
const serverPath = (name: string, field: readonly string[] = []): string =>
|
|
90
|
+
["mcpServers", name, ...field].join(".");
|
|
91
|
+
|
|
92
|
+
const addDiagnostic = (
|
|
93
|
+
diagnostics: McpConfigDiagnostic[],
|
|
94
|
+
origin: McpConfigOrigin,
|
|
95
|
+
code: McpConfigDiagnosticCode,
|
|
96
|
+
message: string,
|
|
97
|
+
suffix: string,
|
|
98
|
+
serverName?: string
|
|
99
|
+
): void => {
|
|
100
|
+
diagnostics.push({
|
|
101
|
+
code,
|
|
102
|
+
message,
|
|
103
|
+
path: `${origin.path}:${suffix}`,
|
|
104
|
+
scope: origin.scope,
|
|
105
|
+
...omitUndefined({ serverName }),
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const mergePatch = (
|
|
110
|
+
base: Readonly<Record<string, unknown>>,
|
|
111
|
+
patch: Readonly<Record<string, unknown>>
|
|
112
|
+
): Readonly<Record<string, unknown>> => {
|
|
113
|
+
const merged: Record<string, unknown> = Object.assign(
|
|
114
|
+
Object.create(null),
|
|
115
|
+
base
|
|
116
|
+
);
|
|
117
|
+
for (const [key, incoming] of Object.entries(patch)) {
|
|
118
|
+
const current = merged[key];
|
|
119
|
+
merged[key] =
|
|
120
|
+
isPlainObject(current) && isPlainObject(incoming)
|
|
121
|
+
? mergePatch(current, incoming)
|
|
122
|
+
: incoming;
|
|
123
|
+
}
|
|
124
|
+
return merged;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const isTransportDefinition = (
|
|
128
|
+
server: Readonly<Record<string, unknown>>
|
|
129
|
+
): boolean => Object.hasOwn(server, "command") || Object.hasOwn(server, "url");
|
|
130
|
+
|
|
131
|
+
const readServerEntries = (
|
|
132
|
+
source: McpConfigSource,
|
|
133
|
+
diagnostics: McpConfigDiagnostic[]
|
|
134
|
+
): Readonly<Record<string, unknown>> | undefined => {
|
|
135
|
+
const candidate = source.document.mcpServers;
|
|
136
|
+
if (isUndefined(candidate)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (!isPlainObject(candidate)) {
|
|
140
|
+
addDiagnostic(
|
|
141
|
+
diagnostics,
|
|
142
|
+
source,
|
|
143
|
+
"invalid-scope",
|
|
144
|
+
"mcpServers must be an object",
|
|
145
|
+
"mcpServers"
|
|
146
|
+
);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
return candidate;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const applyGlobalServer = (
|
|
153
|
+
servers: Map<string, EffectiveServer>,
|
|
154
|
+
source: McpConfigSource,
|
|
155
|
+
name: string,
|
|
156
|
+
candidate: unknown,
|
|
157
|
+
diagnostics: McpConfigDiagnostic[]
|
|
158
|
+
): void => {
|
|
159
|
+
if (!mcpServerNameSchema.safeParse(name).success) {
|
|
160
|
+
addDiagnostic(
|
|
161
|
+
diagnostics,
|
|
162
|
+
source,
|
|
163
|
+
"invalid-server",
|
|
164
|
+
"Server names may contain only letters, digits, underscores, and hyphens.",
|
|
165
|
+
serverPath(name),
|
|
166
|
+
name
|
|
167
|
+
);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (!isPlainObject(candidate)) {
|
|
171
|
+
addDiagnostic(
|
|
172
|
+
diagnostics,
|
|
173
|
+
source,
|
|
174
|
+
"invalid-server",
|
|
175
|
+
"Server entry must be an object",
|
|
176
|
+
serverPath(name),
|
|
177
|
+
name
|
|
178
|
+
);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const existing = servers.get(name);
|
|
182
|
+
if (source.scope === "plugin") {
|
|
183
|
+
if (existing !== undefined) {
|
|
184
|
+
addDiagnostic(
|
|
185
|
+
diagnostics,
|
|
186
|
+
source,
|
|
187
|
+
"invalid-server",
|
|
188
|
+
"MCP Server registration names must be unique across Plugins.",
|
|
189
|
+
serverPath(name),
|
|
190
|
+
name
|
|
191
|
+
);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
servers.set(name, { raw: candidate, source });
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (existing?.source.scope === "plugin") {
|
|
198
|
+
servers.set(name, {
|
|
199
|
+
raw: isTransportDefinition(candidate)
|
|
200
|
+
? candidate
|
|
201
|
+
: mergePatch(existing.raw, candidate),
|
|
202
|
+
source,
|
|
203
|
+
});
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
servers.set(name, {
|
|
207
|
+
raw: mergePatch(existing?.raw ?? {}, candidate),
|
|
208
|
+
source,
|
|
209
|
+
});
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const applyProjectServer = (
|
|
213
|
+
servers: Map<string, EffectiveServer>,
|
|
214
|
+
source: McpConfigSource,
|
|
215
|
+
name: string,
|
|
216
|
+
candidate: unknown,
|
|
217
|
+
diagnostics: McpConfigDiagnostic[]
|
|
218
|
+
): void => {
|
|
219
|
+
if (
|
|
220
|
+
!(mcpServerNameSchema.safeParse(name).success && isPlainObject(candidate))
|
|
221
|
+
) {
|
|
222
|
+
addDiagnostic(
|
|
223
|
+
diagnostics,
|
|
224
|
+
source,
|
|
225
|
+
"invalid-server",
|
|
226
|
+
"Project MCP server entries require a valid name and object value.",
|
|
227
|
+
serverPath(name),
|
|
228
|
+
name
|
|
229
|
+
);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (isTransportDefinition(candidate)) {
|
|
233
|
+
servers.set(name, { raw: candidate, source });
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const unsupportedFields = Object.keys(candidate).filter(
|
|
237
|
+
(field) => !PROJECT_OVERLAY_FIELDS.has(field)
|
|
238
|
+
);
|
|
239
|
+
if (unsupportedFields.length > 0) {
|
|
240
|
+
addDiagnostic(
|
|
241
|
+
diagnostics,
|
|
242
|
+
source,
|
|
243
|
+
"invalid-server",
|
|
244
|
+
"Project overlays may set only enabled, exposure, and toolExposure.",
|
|
245
|
+
serverPath(name),
|
|
246
|
+
name
|
|
247
|
+
);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const previous = servers.get(name);
|
|
251
|
+
if (previous?.source.scope !== "global") {
|
|
252
|
+
addDiagnostic(
|
|
253
|
+
diagnostics,
|
|
254
|
+
source,
|
|
255
|
+
"invalid-server",
|
|
256
|
+
"A project override requires an existing user server definition.",
|
|
257
|
+
serverPath(name),
|
|
258
|
+
name
|
|
259
|
+
);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
servers.set(name, {
|
|
263
|
+
raw: mergePatch(previous.raw, candidate),
|
|
264
|
+
source: previous.source,
|
|
265
|
+
});
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const applyServerEntries = (
|
|
269
|
+
servers: Map<string, EffectiveServer>,
|
|
270
|
+
source: McpConfigSource,
|
|
271
|
+
diagnostics: McpConfigDiagnostic[],
|
|
272
|
+
applyEntry: (
|
|
273
|
+
servers: Map<string, EffectiveServer>,
|
|
274
|
+
source: McpConfigSource,
|
|
275
|
+
name: string,
|
|
276
|
+
candidate: unknown,
|
|
277
|
+
diagnostics: McpConfigDiagnostic[]
|
|
278
|
+
) => void
|
|
279
|
+
): void => {
|
|
280
|
+
const entries = readServerEntries(source, diagnostics);
|
|
281
|
+
if (entries === undefined) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
for (const [name, candidate] of Object.entries(entries)) {
|
|
285
|
+
applyEntry(servers, source, name, candidate, diagnostics);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const effectiveServers = (
|
|
290
|
+
snapshot: McpConfigSnapshot,
|
|
291
|
+
diagnostics: McpConfigDiagnostic[]
|
|
292
|
+
): ReadonlyMap<string, EffectiveServer> => {
|
|
293
|
+
const servers = new Map<string, EffectiveServer>();
|
|
294
|
+
for (const source of snapshot.sources) {
|
|
295
|
+
if (source.scope !== "project") {
|
|
296
|
+
applyServerEntries(servers, source, diagnostics, applyGlobalServer);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
for (const source of snapshot.sources) {
|
|
300
|
+
if (source.scope === "project") {
|
|
301
|
+
applyServerEntries(servers, source, diagnostics, applyProjectServer);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return servers;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const diagnosticCode = (field: readonly string[]): McpConfigDiagnosticCode => {
|
|
308
|
+
const rootField = field[0] ?? "";
|
|
309
|
+
if (rootField === "timeout") {
|
|
310
|
+
return "invalid-timeout";
|
|
311
|
+
}
|
|
312
|
+
if (rootField === "exposure" || rootField === "toolExposure") {
|
|
313
|
+
return "unsupported-exposure";
|
|
314
|
+
}
|
|
315
|
+
if (rootField === "type" || rootField === "command" || rootField === "url") {
|
|
316
|
+
return "unsupported-transport";
|
|
317
|
+
}
|
|
318
|
+
return "invalid-field";
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
const addSchemaDiagnostics = (
|
|
322
|
+
context: ResolutionContext,
|
|
323
|
+
error: ZodError
|
|
324
|
+
): void => {
|
|
325
|
+
for (const issue of error.issues) {
|
|
326
|
+
const fields =
|
|
327
|
+
issue.code === "unrecognized_keys"
|
|
328
|
+
? issue.keys.map((key) => [key])
|
|
329
|
+
: [issue.path.map(String)];
|
|
330
|
+
for (const field of fields) {
|
|
331
|
+
addDiagnostic(
|
|
332
|
+
context.diagnostics,
|
|
333
|
+
context.source,
|
|
334
|
+
diagnosticCode(field),
|
|
335
|
+
issue.message,
|
|
336
|
+
serverPath(context.name, field),
|
|
337
|
+
context.name
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const runConfigCommand = async (
|
|
344
|
+
command: string,
|
|
345
|
+
context: ResolutionContext,
|
|
346
|
+
field: readonly string[]
|
|
347
|
+
): Promise<string | undefined> => {
|
|
348
|
+
const cached = context.commandOutputs.get(command);
|
|
349
|
+
if (cached !== undefined) {
|
|
350
|
+
return cached;
|
|
351
|
+
}
|
|
352
|
+
try {
|
|
353
|
+
if (command.trim().length === 0) {
|
|
354
|
+
throw new Error("empty command");
|
|
355
|
+
}
|
|
356
|
+
const shellCommand =
|
|
357
|
+
process.platform === "win32"
|
|
358
|
+
? [process.env.COMSPEC ?? "cmd.exe", "/d", "/s", "/c", command]
|
|
359
|
+
: ["/bin/sh", "-c", command];
|
|
360
|
+
const commandEnvironment = Object.fromEntries(
|
|
361
|
+
Object.entries({ ...process.env, ...context.env }).filter(
|
|
362
|
+
(entry): entry is [string, string] => typeof entry[1] === "string"
|
|
363
|
+
)
|
|
364
|
+
);
|
|
365
|
+
const child = Bun.spawn(shellCommand, {
|
|
366
|
+
cwd: context.workspace,
|
|
367
|
+
env: commandEnvironment,
|
|
368
|
+
stdin: "ignore",
|
|
369
|
+
stdout: "pipe",
|
|
370
|
+
stderr: "ignore",
|
|
371
|
+
signal: AbortSignal.timeout(10_000),
|
|
372
|
+
});
|
|
373
|
+
try {
|
|
374
|
+
const reader = (child.stdout as ReadableStream<Uint8Array>).getReader();
|
|
375
|
+
const decoder = new TextDecoder();
|
|
376
|
+
let output = "";
|
|
377
|
+
let outputBytes = 0;
|
|
378
|
+
while (true) {
|
|
379
|
+
const { done, value } = await reader.read();
|
|
380
|
+
if (done) {
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
outputBytes += value.byteLength;
|
|
384
|
+
if (outputBytes > MAX_COMMAND_OUTPUT_BYTES) {
|
|
385
|
+
throw new Error("command output limit exceeded");
|
|
386
|
+
}
|
|
387
|
+
output += decoder.decode(value, { stream: true });
|
|
388
|
+
}
|
|
389
|
+
output += decoder.decode();
|
|
390
|
+
const exitCode = await child.exited;
|
|
391
|
+
if (exitCode !== 0) {
|
|
392
|
+
throw new Error("command exited unsuccessfully");
|
|
393
|
+
}
|
|
394
|
+
const trimmed = output.trim();
|
|
395
|
+
if (trimmed.length === 0) {
|
|
396
|
+
throw new Error("command returned no value");
|
|
397
|
+
}
|
|
398
|
+
context.commandOutputs.set(command, trimmed);
|
|
399
|
+
return trimmed;
|
|
400
|
+
} catch (error) {
|
|
401
|
+
child.kill();
|
|
402
|
+
await child.exited;
|
|
403
|
+
throw error;
|
|
404
|
+
}
|
|
405
|
+
} catch {
|
|
406
|
+
addDiagnostic(
|
|
407
|
+
context.diagnostics,
|
|
408
|
+
context.source,
|
|
409
|
+
"command-failed",
|
|
410
|
+
`Could not resolve command value for server ${context.name}.`,
|
|
411
|
+
serverPath(context.name, field),
|
|
412
|
+
context.name
|
|
413
|
+
);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
const resolveString = async (
|
|
419
|
+
value: string,
|
|
420
|
+
context: ResolutionContext,
|
|
421
|
+
field: readonly string[]
|
|
422
|
+
): Promise<string | undefined> => {
|
|
423
|
+
if ((field[0] === "env" || field[0] === "headers") && value.startsWith("!")) {
|
|
424
|
+
return runConfigCommand(value.slice(1), context, field);
|
|
425
|
+
}
|
|
426
|
+
const resolution = resolveConfigEnvironmentVariables(value, context.env);
|
|
427
|
+
if ("missingVariable" in resolution) {
|
|
428
|
+
addDiagnostic(
|
|
429
|
+
context.diagnostics,
|
|
430
|
+
context.source,
|
|
431
|
+
"missing-env",
|
|
432
|
+
`Missing environment variable ${resolution.missingVariable} for server ${context.name}`,
|
|
433
|
+
serverPath(context.name, field),
|
|
434
|
+
context.name
|
|
435
|
+
);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
return resolution.value;
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
const resolveStringRecord = async (
|
|
442
|
+
value: unknown,
|
|
443
|
+
context: ResolutionContext,
|
|
444
|
+
field: string
|
|
445
|
+
): Promise<Record<string, string> | undefined> => {
|
|
446
|
+
if (!isPlainObject(value)) {
|
|
447
|
+
return {};
|
|
448
|
+
}
|
|
449
|
+
const result: Record<string, string> = {};
|
|
450
|
+
for (const [key, candidate] of Object.entries(value)) {
|
|
451
|
+
if (typeof candidate !== "string") {
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
const commandValue = candidate.startsWith("!");
|
|
455
|
+
const resolved = await resolveString(candidate, context, [field, key]);
|
|
456
|
+
if (resolved === undefined) {
|
|
457
|
+
if (commandValue) {
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
result[key] = resolved;
|
|
463
|
+
}
|
|
464
|
+
return result;
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
const homeExpand = (value: string, homeRoot: string): string =>
|
|
468
|
+
value === "~" || value.startsWith(`~${path.sep}`)
|
|
469
|
+
? path.join(homeRoot, value.slice(2))
|
|
470
|
+
: value;
|
|
471
|
+
|
|
472
|
+
type ResolvedServerBase = Readonly<{
|
|
473
|
+
description: string | undefined;
|
|
474
|
+
disabled: boolean;
|
|
475
|
+
exposure: McpExposure;
|
|
476
|
+
name: string;
|
|
477
|
+
timeout: McpTimeouts;
|
|
478
|
+
toolExposure: Record<string, McpExposure> | undefined;
|
|
479
|
+
}>;
|
|
480
|
+
|
|
481
|
+
const resolvedServerBase = (
|
|
482
|
+
context: ResolutionContext,
|
|
483
|
+
raw: RawMcpServerConfig
|
|
484
|
+
): ResolvedServerBase => ({
|
|
485
|
+
description: raw.description,
|
|
486
|
+
disabled: raw.enabled === false,
|
|
487
|
+
exposure: raw.exposure ?? "direct",
|
|
488
|
+
name: context.name,
|
|
489
|
+
timeout: {
|
|
490
|
+
...DEFAULT_MCP_TIMEOUTS,
|
|
491
|
+
...(raw.timeout === undefined
|
|
492
|
+
? {}
|
|
493
|
+
: { execution: Math.round(raw.timeout * 1000) }),
|
|
494
|
+
},
|
|
495
|
+
toolExposure: raw.toolExposure,
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
const resolveLocalServer = async (
|
|
499
|
+
context: ResolutionContext,
|
|
500
|
+
raw: RawMcpServerConfig,
|
|
501
|
+
base: ResolvedServerBase
|
|
502
|
+
): Promise<ResolvedMcpServerConfig | undefined> => {
|
|
503
|
+
if (typeof raw.command !== "string") {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
const environment = await resolveStringRecord(raw.env, context, "env");
|
|
507
|
+
if (environment === undefined) {
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
const homeRoot = context.env.HOME ?? process.env.HOME ?? "";
|
|
511
|
+
const command = await resolveString(raw.command, context, ["command"]);
|
|
512
|
+
const args = await Promise.all(
|
|
513
|
+
(raw.args ?? []).map((arg, index) =>
|
|
514
|
+
resolveString(arg, context, ["args", String(index)])
|
|
515
|
+
)
|
|
516
|
+
);
|
|
517
|
+
let cwd: string | undefined;
|
|
518
|
+
if (raw.cwd !== undefined) {
|
|
519
|
+
const cwdValue = await resolveString(raw.cwd, context, ["cwd"]);
|
|
520
|
+
if (cwdValue === undefined) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
const expandedCwd = homeExpand(cwdValue, homeRoot);
|
|
524
|
+
cwd = path.isAbsolute(expandedCwd)
|
|
525
|
+
? expandedCwd
|
|
526
|
+
: path.resolve(context.workspace, expandedCwd);
|
|
527
|
+
}
|
|
528
|
+
if (
|
|
529
|
+
command === undefined ||
|
|
530
|
+
args.some((argument) => argument === undefined)
|
|
531
|
+
) {
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
const parsed = resolvedServerSchema.safeParse({
|
|
535
|
+
...base,
|
|
536
|
+
type: "local" as const,
|
|
537
|
+
command: [
|
|
538
|
+
homeExpand(command, homeRoot),
|
|
539
|
+
...args.map((arg) => homeExpand(arg ?? "", homeRoot)),
|
|
540
|
+
],
|
|
541
|
+
...(cwd === undefined ? {} : { cwd }),
|
|
542
|
+
...(Object.keys(environment).length === 0 ? {} : { environment }),
|
|
543
|
+
});
|
|
544
|
+
if (!parsed.success) {
|
|
545
|
+
addSchemaDiagnostics(context, parsed.error);
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
return parsed.data;
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
const parseHttpUrl = (value: string): URL | undefined => {
|
|
552
|
+
try {
|
|
553
|
+
const url = new URL(value);
|
|
554
|
+
if (url.hostname && ["http:", "https:"].includes(url.protocol)) {
|
|
555
|
+
return url;
|
|
556
|
+
}
|
|
557
|
+
} catch {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
return;
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
const hasOAuthClientSecret = (
|
|
564
|
+
context: ResolutionContext,
|
|
565
|
+
oauth: unknown
|
|
566
|
+
): boolean => {
|
|
567
|
+
if (!(isPlainObject(oauth) && Object.hasOwn(oauth, "clientSecret"))) {
|
|
568
|
+
return false;
|
|
569
|
+
}
|
|
570
|
+
addDiagnostic(
|
|
571
|
+
context.diagnostics,
|
|
572
|
+
context.source,
|
|
573
|
+
"unsupported-auth",
|
|
574
|
+
"MCP configuration cannot provide OAuth client secrets; use the host secret store.",
|
|
575
|
+
serverPath(context.name, ["oauth", "clientSecret"]),
|
|
576
|
+
context.name
|
|
577
|
+
);
|
|
578
|
+
return true;
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
const resolveRemoteServer = async (
|
|
582
|
+
context: ResolutionContext,
|
|
583
|
+
raw: RawMcpServerConfig,
|
|
584
|
+
base: ResolvedServerBase
|
|
585
|
+
): Promise<ResolvedMcpServerConfig | undefined> => {
|
|
586
|
+
if (typeof raw.url !== "string") {
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
const resolvedUrl = await resolveString(raw.url, context, ["url"]);
|
|
590
|
+
if (resolvedUrl === undefined) {
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
const url = parseHttpUrl(resolvedUrl);
|
|
594
|
+
if (url === undefined) {
|
|
595
|
+
addDiagnostic(
|
|
596
|
+
context.diagnostics,
|
|
597
|
+
context.source,
|
|
598
|
+
"invalid-url",
|
|
599
|
+
"URL must be absolute http or https URL",
|
|
600
|
+
serverPath(context.name, ["url"]),
|
|
601
|
+
context.name
|
|
602
|
+
);
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
if (hasOAuthClientSecret(context, raw.oauth)) {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
const headers = await resolveStringRecord(raw.headers, context, "headers");
|
|
609
|
+
if (headers === undefined) {
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
const oauth = raw.oauth;
|
|
613
|
+
const parsed = resolvedServerSchema.safeParse({
|
|
614
|
+
...base,
|
|
615
|
+
type: "remote" as const,
|
|
616
|
+
url: url.toString(),
|
|
617
|
+
...(Object.keys(headers).length === 0 ? {} : { headers }),
|
|
618
|
+
...omitUndefined({ auth: raw.auth, oauth }),
|
|
619
|
+
});
|
|
620
|
+
if (!parsed.success) {
|
|
621
|
+
addSchemaDiagnostics(context, parsed.error);
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
return parsed.data;
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
const resolveServer = async (
|
|
628
|
+
context: ResolutionContext,
|
|
629
|
+
raw: RawMcpServerConfig
|
|
630
|
+
): Promise<ResolvedMcpServerConfig | undefined> => {
|
|
631
|
+
const isLocal = typeof raw.command === "string";
|
|
632
|
+
const isRemote = typeof raw.url === "string";
|
|
633
|
+
if (isLocal === isRemote) {
|
|
634
|
+
addDiagnostic(
|
|
635
|
+
context.diagnostics,
|
|
636
|
+
context.source,
|
|
637
|
+
"invalid-server",
|
|
638
|
+
"A server must define exactly one of command or url.",
|
|
639
|
+
serverPath(context.name),
|
|
640
|
+
context.name
|
|
641
|
+
);
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
const typeMismatch =
|
|
645
|
+
(raw.type === "stdio" && !isLocal) ||
|
|
646
|
+
((raw.type === "http" || raw.type === "streamable-http") && !isRemote);
|
|
647
|
+
if (typeMismatch) {
|
|
648
|
+
addDiagnostic(
|
|
649
|
+
context.diagnostics,
|
|
650
|
+
context.source,
|
|
651
|
+
"unsupported-transport",
|
|
652
|
+
"Server type does not match its command or URL transport.",
|
|
653
|
+
serverPath(context.name, ["type"]),
|
|
654
|
+
context.name
|
|
655
|
+
);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
const base = resolvedServerBase(context, raw);
|
|
659
|
+
if (isLocal) {
|
|
660
|
+
return resolveLocalServer(context, raw, base);
|
|
661
|
+
}
|
|
662
|
+
return resolveRemoteServer(context, raw, base);
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
const malformedServerTransport = (
|
|
666
|
+
value: Readonly<Record<string, unknown>>
|
|
667
|
+
): "local" | "remote" =>
|
|
668
|
+
typeof value.command === "string" ? "local" : "remote";
|
|
669
|
+
|
|
670
|
+
export const resolveServers = async ({
|
|
671
|
+
env,
|
|
672
|
+
snapshot,
|
|
673
|
+
workspace,
|
|
674
|
+
}: {
|
|
675
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
676
|
+
snapshot: McpConfigSnapshot;
|
|
677
|
+
workspace: string;
|
|
678
|
+
}): Promise<McpConfigResult> => {
|
|
679
|
+
const diagnostics = snapshot.diagnostics.map((diagnostic) => ({
|
|
680
|
+
...diagnostic,
|
|
681
|
+
}));
|
|
682
|
+
const effective = effectiveServers(snapshot, diagnostics);
|
|
683
|
+
const servers: Record<string, ResolvedMcpServerConfig> = {};
|
|
684
|
+
const invalidServers: Record<string, InvalidMcpServerConfig> = {};
|
|
685
|
+
const serverSources: Record<string, McpConfigOrigin> = {};
|
|
686
|
+
for (const [name, { raw, source }] of effective) {
|
|
687
|
+
const context: ResolutionContext = {
|
|
688
|
+
commandOutputs: new Map(),
|
|
689
|
+
diagnostics,
|
|
690
|
+
env,
|
|
691
|
+
name,
|
|
692
|
+
source,
|
|
693
|
+
workspace,
|
|
694
|
+
};
|
|
695
|
+
serverSources[name] = { path: source.path, scope: source.scope };
|
|
696
|
+
const validated = rawServerSchema.safeParse(raw);
|
|
697
|
+
if (!validated.success) {
|
|
698
|
+
addSchemaDiagnostics(context, validated.error);
|
|
699
|
+
invalidServers[name] = {
|
|
700
|
+
error:
|
|
701
|
+
diagnostics.find((diagnostic) => diagnostic.serverName === name)
|
|
702
|
+
?.message ?? "Invalid MCP server configuration",
|
|
703
|
+
name,
|
|
704
|
+
transport: malformedServerTransport(raw),
|
|
705
|
+
};
|
|
706
|
+
continue;
|
|
707
|
+
}
|
|
708
|
+
const resolved = await resolveServer(context, validated.data);
|
|
709
|
+
if (resolved === undefined) {
|
|
710
|
+
invalidServers[name] = {
|
|
711
|
+
error:
|
|
712
|
+
diagnostics.find((diagnostic) => diagnostic.serverName === name)
|
|
713
|
+
?.message ?? "Invalid MCP server configuration",
|
|
714
|
+
name,
|
|
715
|
+
transport: malformedServerTransport(raw),
|
|
716
|
+
};
|
|
717
|
+
} else {
|
|
718
|
+
servers[name] = resolved;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return { diagnostics, invalidServers, serverSources, servers };
|
|
722
|
+
};
|