@mcpfn/core 0.0.1 → 0.0.2
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 +11 -0
- package/dist/index.cjs +2228 -2145
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +49 -23
- package/dist/index.d.ts +49 -23
- package/dist/index.js +2239 -2159
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
package/dist/index.cjs
CHANGED
|
@@ -36,6 +36,7 @@ __export(index_exports, {
|
|
|
36
36
|
McpFnOutputValidationError: () => McpFnOutputValidationError,
|
|
37
37
|
McpFnRegistry: () => McpFnRegistry,
|
|
38
38
|
McpFnServer: () => McpFnServer,
|
|
39
|
+
McpFnServerDeclaration: () => McpFnServerDeclaration,
|
|
39
40
|
McpFnValidationError: () => McpFnValidationError,
|
|
40
41
|
assertMcpAppContracts: () => assertMcpAppContracts,
|
|
41
42
|
assertPromptSchemaSupportsStringValues: () => assertPromptSchemaSupportsStringValues,
|
|
@@ -45,11 +46,13 @@ __export(index_exports, {
|
|
|
45
46
|
createManifest: () => createManifest,
|
|
46
47
|
createMcpAppResource: () => createMcpAppResource,
|
|
47
48
|
createMcpFnServer: () => createMcpFnServer,
|
|
49
|
+
defineMcpFnServer: () => defineMcpFnServer,
|
|
48
50
|
diffManifests: () => diffManifests,
|
|
49
51
|
errorResult: () => errorResult,
|
|
50
52
|
jsonSafe: () => jsonSafe,
|
|
51
53
|
mcpAppToolMetadata: () => mcpAppToolMetadata,
|
|
52
54
|
promptArguments: () => promptArguments,
|
|
55
|
+
resolveMcpFnServerCapabilities: () => resolveMcpFnServerCapabilities,
|
|
53
56
|
schemaPromptArguments: () => schemaPromptArguments,
|
|
54
57
|
sha256: () => sha256,
|
|
55
58
|
structuredResult: () => structuredResult,
|
|
@@ -253,2384 +256,2461 @@ function sha256(value) {
|
|
|
253
256
|
return (0, import_node_crypto.createHash)("sha256").update(canonicalJson(value)).digest("hex");
|
|
254
257
|
}
|
|
255
258
|
|
|
256
|
-
// src/
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
function
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
function schemaTypes(schema) {
|
|
267
|
-
if (Array.isArray(schema.type)) {
|
|
268
|
-
return new Set(schema.type.filter((value) => typeof value === "string"));
|
|
259
|
+
// src/manifest.ts
|
|
260
|
+
var import_ajv2 = __toESM(require("ajv"), 1);
|
|
261
|
+
var import_ajv_formats2 = __toESM(require("ajv-formats"), 1);
|
|
262
|
+
var import_uriTemplate2 = require("@modelcontextprotocol/sdk/shared/uriTemplate.js");
|
|
263
|
+
var import_types = require("@modelcontextprotocol/sdk/types.js");
|
|
264
|
+
|
|
265
|
+
// src/uri-template.ts
|
|
266
|
+
function unsupportedUriTemplateOperator(uriTemplate) {
|
|
267
|
+
for (const match of uriTemplate.matchAll(/\{([^{}]+)\}/g)) {
|
|
268
|
+
if (match[1]?.startsWith(";")) return ";";
|
|
269
269
|
}
|
|
270
|
-
return
|
|
270
|
+
return void 0;
|
|
271
271
|
}
|
|
272
|
-
function
|
|
273
|
-
return
|
|
272
|
+
function uriTemplateMatchShape(uriTemplate) {
|
|
273
|
+
return uriTemplate.replace(/\{([^{}]+)\}/g, (_match, expression) => {
|
|
274
|
+
const operator = /^[+#./?&]/.exec(expression)?.[0] ?? "";
|
|
275
|
+
if (operator === "?" || operator === "&") {
|
|
276
|
+
const names = expression.slice(1).split(",").map((name) => name.replace(/\*/g, "").trim()).join(",");
|
|
277
|
+
return `{query:${operator}:${names}}`;
|
|
278
|
+
}
|
|
279
|
+
const exploded = expression.includes("*");
|
|
280
|
+
if (operator === "+" || operator === "#") return "{reserved}";
|
|
281
|
+
if (operator === ".") return ".{simple}";
|
|
282
|
+
if (operator === "/") return exploded ? "/{simple-exploded}" : "/{simple}";
|
|
283
|
+
return exploded ? "{simple-exploded}" : "{simple}";
|
|
284
|
+
});
|
|
274
285
|
}
|
|
275
|
-
function
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
286
|
+
function uriTemplateAutomaton(uriTemplate) {
|
|
287
|
+
const states = [{ epsilon: [], transitions: [] }];
|
|
288
|
+
let state = 0;
|
|
289
|
+
const nextState = () => {
|
|
290
|
+
states.push({ epsilon: [], transitions: [] });
|
|
291
|
+
return states.length - 1;
|
|
292
|
+
};
|
|
293
|
+
const literal = (value) => {
|
|
294
|
+
for (const character of value) {
|
|
295
|
+
const next = nextState();
|
|
296
|
+
states[state].transitions.push({
|
|
297
|
+
predicate: { kind: "literal", value: character },
|
|
298
|
+
to: next
|
|
299
|
+
});
|
|
300
|
+
state = next;
|
|
281
301
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
302
|
+
};
|
|
303
|
+
const oneOrMore = (excluded) => {
|
|
304
|
+
const loop = nextState();
|
|
305
|
+
const next = nextState();
|
|
306
|
+
const predicate = { kind: "except", value: excluded };
|
|
307
|
+
states[state].transitions.push({ predicate, to: loop });
|
|
308
|
+
states[loop].transitions.push({ predicate, to: loop });
|
|
309
|
+
states[loop].epsilon.push(next);
|
|
310
|
+
state = next;
|
|
311
|
+
};
|
|
312
|
+
const commaSeparated = () => {
|
|
313
|
+
oneOrMore("/,");
|
|
314
|
+
const boundary = state;
|
|
315
|
+
const firstComponentCharacter = nextState();
|
|
316
|
+
const component = nextState();
|
|
317
|
+
states[boundary].transitions.push({
|
|
318
|
+
predicate: { kind: "literal", value: "," },
|
|
319
|
+
to: firstComponentCharacter
|
|
320
|
+
});
|
|
321
|
+
const predicate = { kind: "except", value: "/," };
|
|
322
|
+
states[firstComponentCharacter].transitions.push({ predicate, to: component });
|
|
323
|
+
states[component].transitions.push({ predicate, to: component });
|
|
324
|
+
states[component].epsilon.push(boundary);
|
|
325
|
+
};
|
|
326
|
+
const expression = (value) => {
|
|
327
|
+
const operator = /^[+#./?&]/.exec(value)?.[0] ?? "";
|
|
328
|
+
const exploded = value.includes("*");
|
|
329
|
+
const names = value.slice(operator.length).split(",").map((name) => name.replace(/\*/g, "").trim()).filter(Boolean);
|
|
330
|
+
if (operator === "?" || operator === "&") {
|
|
331
|
+
names.forEach((name, index) => {
|
|
332
|
+
literal(index === 0 ? operator : "&");
|
|
333
|
+
literal(`${name}=`);
|
|
334
|
+
oneOrMore("&");
|
|
335
|
+
});
|
|
336
|
+
return;
|
|
285
337
|
}
|
|
286
|
-
if (
|
|
338
|
+
if (operator === ".") literal(".");
|
|
339
|
+
if (operator === "/") literal("/");
|
|
340
|
+
if (operator === "+" || operator === "#") oneOrMore("");
|
|
341
|
+
else if (exploded && (operator === "" || operator === "/")) commaSeparated();
|
|
342
|
+
else oneOrMore("/,");
|
|
343
|
+
};
|
|
344
|
+
const matcher = /\{([^{}]+)\}/g;
|
|
345
|
+
let cursor = 0;
|
|
346
|
+
for (const match of uriTemplate.matchAll(matcher)) {
|
|
347
|
+
literal(uriTemplate.slice(cursor, match.index));
|
|
348
|
+
expression(match[1]);
|
|
349
|
+
cursor = match.index + match[0].length;
|
|
287
350
|
}
|
|
288
|
-
|
|
351
|
+
literal(uriTemplate.slice(cursor));
|
|
352
|
+
return { final: state, states };
|
|
289
353
|
}
|
|
290
|
-
function
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
index += 1;
|
|
300
|
-
continue;
|
|
354
|
+
function epsilonClosure(automaton, initial) {
|
|
355
|
+
const closure = /* @__PURE__ */ new Set([initial]);
|
|
356
|
+
const queue = [initial];
|
|
357
|
+
while (queue.length) {
|
|
358
|
+
const state = queue.shift();
|
|
359
|
+
for (const next of automaton.states[state].epsilon) {
|
|
360
|
+
if (closure.has(next)) continue;
|
|
361
|
+
closure.add(next);
|
|
362
|
+
queue.push(next);
|
|
301
363
|
}
|
|
302
|
-
if (".^$*+?()[]{}|".includes(character)) break;
|
|
303
|
-
prefix += character;
|
|
304
364
|
}
|
|
305
|
-
return
|
|
365
|
+
return closure;
|
|
306
366
|
}
|
|
307
|
-
function
|
|
308
|
-
if (
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
367
|
+
function predicatesOverlap(left, right) {
|
|
368
|
+
if (left.kind === "literal" && right.kind === "literal") return left.value === right.value;
|
|
369
|
+
if (left.kind === "literal") return !right.value.includes(left.value);
|
|
370
|
+
if (right.kind === "literal") return !left.value.includes(right.value);
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
function uriTemplatesOverlap(left, right) {
|
|
374
|
+
if (uriTemplateMatchShape(left) === uriTemplateMatchShape(right)) return true;
|
|
375
|
+
const leftAutomaton = uriTemplateAutomaton(left);
|
|
376
|
+
const rightAutomaton = uriTemplateAutomaton(right);
|
|
377
|
+
const queue = [[0, 0]];
|
|
378
|
+
const visited = /* @__PURE__ */ new Set();
|
|
379
|
+
while (queue.length) {
|
|
380
|
+
const [leftState, rightState] = queue.shift();
|
|
381
|
+
const key = `${leftState}:${rightState}`;
|
|
382
|
+
if (visited.has(key)) continue;
|
|
383
|
+
visited.add(key);
|
|
384
|
+
const leftClosure = epsilonClosure(leftAutomaton, leftState);
|
|
385
|
+
const rightClosure = epsilonClosure(rightAutomaton, rightState);
|
|
386
|
+
if (leftClosure.has(leftAutomaton.final) && rightClosure.has(rightAutomaton.final)) {
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
for (const leftCurrent of leftClosure) {
|
|
390
|
+
for (const rightCurrent of rightClosure) {
|
|
391
|
+
for (const leftTransition of leftAutomaton.states[leftCurrent].transitions) {
|
|
392
|
+
for (const rightTransition of rightAutomaton.states[rightCurrent].transitions) {
|
|
393
|
+
if (predicatesOverlap(leftTransition.predicate, rightTransition.predicate)) {
|
|
394
|
+
queue.push([leftTransition.to, rightTransition.to]);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
318
399
|
}
|
|
319
|
-
suffix = ".^$*+?()[]{}|".includes(character) ? "" : suffix + character;
|
|
320
400
|
}
|
|
321
|
-
return
|
|
322
|
-
}
|
|
323
|
-
function patternsMayOverlap(left, right) {
|
|
324
|
-
if (hasUnescapedAlternation(left) || hasUnescapedAlternation(right)) return true;
|
|
325
|
-
const leftPrefix = anchoredLiteralPrefix(left);
|
|
326
|
-
const rightPrefix = anchoredLiteralPrefix(right);
|
|
327
|
-
if (leftPrefix && rightPrefix && !leftPrefix.startsWith(rightPrefix) && !rightPrefix.startsWith(leftPrefix)) return false;
|
|
328
|
-
const leftSuffix = anchoredLiteralSuffix(left);
|
|
329
|
-
const rightSuffix = anchoredLiteralSuffix(right);
|
|
330
|
-
if (leftSuffix && rightSuffix && !leftSuffix.endsWith(rightSuffix) && !rightSuffix.endsWith(leftSuffix)) return false;
|
|
331
|
-
return true;
|
|
401
|
+
return false;
|
|
332
402
|
}
|
|
333
|
-
|
|
334
|
-
|
|
403
|
+
|
|
404
|
+
// src/registry.ts
|
|
405
|
+
var import_ajv = __toESM(require("ajv"), 1);
|
|
406
|
+
var import_ajv_formats = __toESM(require("ajv-formats"), 1);
|
|
407
|
+
var import_uriTemplate = require("@modelcontextprotocol/sdk/shared/uriTemplate.js");
|
|
408
|
+
function formatErrors(errors) {
|
|
409
|
+
return (errors ?? []).map((error) => ({
|
|
410
|
+
path: error.instancePath || "/",
|
|
411
|
+
message: error.message ?? "Schema validation failed",
|
|
412
|
+
keyword: error.keyword
|
|
413
|
+
}));
|
|
335
414
|
}
|
|
336
|
-
function
|
|
337
|
-
if (
|
|
338
|
-
|
|
339
|
-
|
|
415
|
+
function assertName(kind, name) {
|
|
416
|
+
if (!/^[A-Za-z0-9_.-]{1,128}$/.test(name)) {
|
|
417
|
+
throw new McpFnValidationError(
|
|
418
|
+
`Invalid MCP ${kind} name ${JSON.stringify(name)}`
|
|
419
|
+
);
|
|
340
420
|
}
|
|
341
|
-
|
|
342
|
-
|
|
421
|
+
}
|
|
422
|
+
function assertSubscriptionCallbacks(kind, name, subscribe, unsubscribe) {
|
|
423
|
+
if (subscribe === void 0 !== (unsubscribe === void 0) || subscribe !== void 0 && typeof subscribe !== "function" || unsubscribe !== void 0 && typeof unsubscribe !== "function") {
|
|
424
|
+
throw new McpFnValidationError(
|
|
425
|
+
`${kind} ${name} must define subscribe and unsubscribe together as functions`
|
|
426
|
+
);
|
|
343
427
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
return [{ severity: "breaking", path: childPath, before: before[name], after: void 0 }];
|
|
351
|
-
}
|
|
352
|
-
if (!hadBefore) {
|
|
353
|
-
return [{ severity: "additive", path: childPath, before: void 0, after: after[name] }];
|
|
354
|
-
}
|
|
355
|
-
return nestedCapabilityChanges(before[name], after[name], childPath);
|
|
356
|
-
});
|
|
428
|
+
}
|
|
429
|
+
function assertUri(kind, uri) {
|
|
430
|
+
try {
|
|
431
|
+
new URL(uri);
|
|
432
|
+
} catch {
|
|
433
|
+
throw new McpFnValidationError(`Invalid MCP ${kind} URI ${JSON.stringify(uri)}`);
|
|
357
434
|
}
|
|
358
|
-
|
|
359
|
-
|
|
435
|
+
}
|
|
436
|
+
function mergePromptSchemaInventory(target, source) {
|
|
437
|
+
for (const [name, description] of source.descriptions) {
|
|
438
|
+
if (!target.descriptions.has(name)) target.descriptions.set(name, description);
|
|
360
439
|
}
|
|
361
|
-
|
|
362
|
-
|
|
440
|
+
for (const [name, schemas] of source.propertySchemas) {
|
|
441
|
+
target.propertySchemas.set(name, [...target.propertySchemas.get(name) ?? [], ...schemas]);
|
|
363
442
|
}
|
|
364
|
-
|
|
443
|
+
for (const name of source.required) target.required.add(name);
|
|
365
444
|
}
|
|
366
|
-
function
|
|
367
|
-
|
|
368
|
-
if (!
|
|
369
|
-
|
|
445
|
+
function resolveLocalSchemaReference(root, reference) {
|
|
446
|
+
if (reference === "#") return root;
|
|
447
|
+
if (!reference.startsWith("#/")) return void 0;
|
|
448
|
+
return reference.slice(2).split("/").reduce((current, segment) => {
|
|
449
|
+
if (!current || typeof current !== "object" || Array.isArray(current)) return void 0;
|
|
450
|
+
const key = segment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
451
|
+
return current[key];
|
|
452
|
+
}, root);
|
|
453
|
+
}
|
|
454
|
+
function derivePromptSchemaInventory(schema, root, references = /* @__PURE__ */ new Set()) {
|
|
455
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema)) return void 0;
|
|
456
|
+
const value = schema;
|
|
457
|
+
const nonFlatKeywords = [
|
|
458
|
+
"anyOf",
|
|
459
|
+
"oneOf",
|
|
460
|
+
"not",
|
|
461
|
+
"if",
|
|
462
|
+
"then",
|
|
463
|
+
"else",
|
|
464
|
+
"patternProperties",
|
|
465
|
+
"dependentSchemas",
|
|
466
|
+
"propertyNames"
|
|
467
|
+
];
|
|
468
|
+
if (nonFlatKeywords.some((keyword) => Object.hasOwn(value, keyword))) return void 0;
|
|
469
|
+
const inventory = {
|
|
470
|
+
descriptions: /* @__PURE__ */ new Map(),
|
|
471
|
+
propertySchemas: /* @__PURE__ */ new Map(),
|
|
472
|
+
required: /* @__PURE__ */ new Set()
|
|
473
|
+
};
|
|
474
|
+
if (value.$ref !== void 0) {
|
|
475
|
+
if (typeof value.$ref !== "string" || references.has(value.$ref)) return void 0;
|
|
476
|
+
const referenced = resolveLocalSchemaReference(root, value.$ref);
|
|
477
|
+
if (!referenced) return void 0;
|
|
478
|
+
const nextReferences = new Set(references).add(value.$ref);
|
|
479
|
+
const referencedInventory = derivePromptSchemaInventory(referenced, root, nextReferences);
|
|
480
|
+
if (!referencedInventory) return void 0;
|
|
481
|
+
mergePromptSchemaInventory(inventory, referencedInventory);
|
|
370
482
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
return
|
|
483
|
+
if (value.allOf !== void 0) {
|
|
484
|
+
if (!Array.isArray(value.allOf)) return void 0;
|
|
485
|
+
for (const member of value.allOf) {
|
|
486
|
+
const memberInventory = derivePromptSchemaInventory(member, root, references);
|
|
487
|
+
if (!memberInventory) return void 0;
|
|
488
|
+
mergePromptSchemaInventory(inventory, memberInventory);
|
|
376
489
|
}
|
|
377
|
-
}).map(([, patternSchema]) => patternSchema);
|
|
378
|
-
}
|
|
379
|
-
function diffSchema(before, after, path, direction, changes) {
|
|
380
|
-
if (before && typeof before === "object" && !Object.keys(before).length) before = true;
|
|
381
|
-
if (after && typeof after === "object" && !Object.keys(after).length) after = true;
|
|
382
|
-
if (before === void 0 && after !== void 0) {
|
|
383
|
-
push(changes, {
|
|
384
|
-
severity: direction === "input" ? "breaking" : "additive",
|
|
385
|
-
code: "schema-added",
|
|
386
|
-
path,
|
|
387
|
-
message: `Schema added at ${path}`,
|
|
388
|
-
after
|
|
389
|
-
});
|
|
390
|
-
return;
|
|
391
490
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
491
|
+
const properties = value.properties;
|
|
492
|
+
if (properties !== void 0 && (typeof properties !== "object" || properties === null || Array.isArray(properties))) return void 0;
|
|
493
|
+
if (value.required !== void 0 && !Array.isArray(value.required)) return void 0;
|
|
494
|
+
const required = Array.isArray(value.required) ? value.required.filter((entry) => typeof entry === "string") : [];
|
|
495
|
+
for (const name of required) inventory.required.add(name);
|
|
496
|
+
const propertySchemas = properties ?? {};
|
|
497
|
+
for (const [name, propertySchema] of Object.entries(propertySchemas)) {
|
|
498
|
+
inventory.propertySchemas.set(name, [
|
|
499
|
+
...inventory.propertySchemas.get(name) ?? [],
|
|
500
|
+
propertySchema
|
|
501
|
+
]);
|
|
502
|
+
if (propertySchema && typeof propertySchema === "object" && !Array.isArray(propertySchema) && typeof propertySchema.description === "string") {
|
|
503
|
+
inventory.descriptions.set(
|
|
504
|
+
name,
|
|
505
|
+
propertySchema.description
|
|
506
|
+
);
|
|
507
|
+
}
|
|
401
508
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
509
|
+
return inventory;
|
|
510
|
+
}
|
|
511
|
+
function intersectStringCandidates(left, right) {
|
|
512
|
+
if (!left) return right;
|
|
513
|
+
if (!right) return left;
|
|
514
|
+
return new Set([...left].filter((candidate) => right.has(candidate)));
|
|
515
|
+
}
|
|
516
|
+
function finiteStringCandidates(schema, root, references = /* @__PURE__ */ new Set()) {
|
|
517
|
+
if (typeof schema === "boolean") return schema ? void 0 : /* @__PURE__ */ new Set();
|
|
518
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema)) return /* @__PURE__ */ new Set();
|
|
519
|
+
const value = schema;
|
|
520
|
+
let candidates;
|
|
521
|
+
if (typeof value.type === "string" && value.type !== "string") return /* @__PURE__ */ new Set();
|
|
522
|
+
if (Array.isArray(value.type) && !value.type.includes("string")) return /* @__PURE__ */ new Set();
|
|
523
|
+
if (Object.hasOwn(value, "const")) {
|
|
524
|
+
candidates = typeof value.const === "string" ? /* @__PURE__ */ new Set([value.const]) : /* @__PURE__ */ new Set();
|
|
416
525
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
const tightened = beforeTypes === void 0 ? afterTypes !== void 0 : afterTypes !== void 0 && removed.length > 0;
|
|
423
|
-
const relaxed = afterTypes === void 0 || added.length > 0;
|
|
424
|
-
const breaking = direction === "input" ? tightened : relaxed;
|
|
425
|
-
push(changes, {
|
|
426
|
-
severity: breaking ? "breaking" : "additive",
|
|
427
|
-
code: "schema-type-changed",
|
|
428
|
-
path,
|
|
429
|
-
message: `Type changed from ${canonicalJson(before.type ?? "unspecified")} to ${canonicalJson(after.type ?? "unspecified")}`,
|
|
430
|
-
before: before.type,
|
|
431
|
-
after: after.type
|
|
432
|
-
});
|
|
526
|
+
if (Array.isArray(value.enum)) {
|
|
527
|
+
candidates = intersectStringCandidates(
|
|
528
|
+
candidates,
|
|
529
|
+
new Set(value.enum.filter((entry) => typeof entry === "string"))
|
|
530
|
+
);
|
|
433
531
|
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
after: after.enum
|
|
443
|
-
});
|
|
444
|
-
} else if (beforeEnum && !afterEnum) {
|
|
445
|
-
push(changes, {
|
|
446
|
-
severity: direction === "input" ? "additive" : "breaking",
|
|
447
|
-
code: "enum-removed",
|
|
448
|
-
path,
|
|
449
|
-
message: `The enum constraint was removed at ${path}`,
|
|
450
|
-
before: before.enum
|
|
451
|
-
});
|
|
452
|
-
} else if (beforeEnum && afterEnum) {
|
|
453
|
-
const removed = [...beforeEnum].filter((entry) => !afterEnum.has(entry));
|
|
454
|
-
const added = [...afterEnum].filter((entry) => !beforeEnum.has(entry));
|
|
455
|
-
if (removed.length) {
|
|
456
|
-
push(changes, {
|
|
457
|
-
severity: direction === "input" ? "breaking" : "additive",
|
|
458
|
-
code: "enum-narrowed",
|
|
459
|
-
path,
|
|
460
|
-
message: `Allowed values were removed at ${path}`,
|
|
461
|
-
before: before.enum,
|
|
462
|
-
after: after.enum
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
if (added.length) {
|
|
466
|
-
push(changes, {
|
|
467
|
-
severity: direction === "input" ? "additive" : "breaking",
|
|
468
|
-
code: "enum-widened",
|
|
469
|
-
path,
|
|
470
|
-
message: `Allowed values were added at ${path}`,
|
|
471
|
-
before: before.enum,
|
|
472
|
-
after: after.enum
|
|
473
|
-
});
|
|
474
|
-
}
|
|
532
|
+
if (value.$ref !== void 0) {
|
|
533
|
+
if (typeof value.$ref !== "string" || references.has(value.$ref)) return /* @__PURE__ */ new Set();
|
|
534
|
+
const referenced = resolveLocalSchemaReference(root, value.$ref);
|
|
535
|
+
if (!referenced) return /* @__PURE__ */ new Set();
|
|
536
|
+
candidates = intersectStringCandidates(
|
|
537
|
+
candidates,
|
|
538
|
+
finiteStringCandidates(referenced, root, new Set(references).add(value.$ref))
|
|
539
|
+
);
|
|
475
540
|
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
["exclusiveMaximum", "lower"],
|
|
483
|
-
["maxLength", "lower"],
|
|
484
|
-
["maxItems", "lower"],
|
|
485
|
-
["minProperties", "higher"],
|
|
486
|
-
["maxProperties", "lower"]
|
|
487
|
-
];
|
|
488
|
-
for (const [keyword, tightDirection] of tightening) {
|
|
489
|
-
const oldValue = before[keyword];
|
|
490
|
-
const newValue = after[keyword];
|
|
491
|
-
if (equal(oldValue, newValue)) {
|
|
492
|
-
continue;
|
|
541
|
+
if (Array.isArray(value.allOf)) {
|
|
542
|
+
for (const member of value.allOf) {
|
|
543
|
+
candidates = intersectStringCandidates(
|
|
544
|
+
candidates,
|
|
545
|
+
finiteStringCandidates(member, root, references)
|
|
546
|
+
);
|
|
493
547
|
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
548
|
+
}
|
|
549
|
+
for (const keyword of ["anyOf", "oneOf"]) {
|
|
550
|
+
if (!Array.isArray(value[keyword])) continue;
|
|
551
|
+
let union = /* @__PURE__ */ new Set();
|
|
552
|
+
for (const member of value[keyword]) {
|
|
553
|
+
const memberCandidates = finiteStringCandidates(member, root, references);
|
|
554
|
+
if (!memberCandidates) {
|
|
555
|
+
union = void 0;
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
for (const candidate of memberCandidates) union.add(candidate);
|
|
504
559
|
}
|
|
505
|
-
|
|
506
|
-
const breaking = direction === "input" ? tightened : !tightened;
|
|
507
|
-
push(changes, {
|
|
508
|
-
severity: breaking ? "breaking" : "additive",
|
|
509
|
-
code: tightened ? "constraint-tightened" : "constraint-relaxed",
|
|
510
|
-
path: `${path}.${keyword}`,
|
|
511
|
-
message: `${keyword} changed from ${oldValue ?? "unspecified"} to ${newValue ?? "unspecified"}`,
|
|
512
|
-
before: oldValue,
|
|
513
|
-
after: newValue
|
|
514
|
-
});
|
|
560
|
+
candidates = intersectStringCandidates(candidates, union);
|
|
515
561
|
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
}
|
|
531
|
-
const beforeUniqueItems = before.uniqueItems ?? false;
|
|
532
|
-
const afterUniqueItems = after.uniqueItems ?? false;
|
|
533
|
-
if (!equal(beforeUniqueItems, afterUniqueItems)) {
|
|
534
|
-
if (typeof beforeUniqueItems !== "boolean" || typeof afterUniqueItems !== "boolean") {
|
|
535
|
-
push(changes, {
|
|
536
|
-
severity: "breaking",
|
|
537
|
-
code: "schema-keyword-changed",
|
|
538
|
-
path: `${path}.uniqueItems`,
|
|
539
|
-
message: `uniqueItems changed at ${path}`,
|
|
540
|
-
before: before.uniqueItems,
|
|
541
|
-
after: after.uniqueItems
|
|
542
|
-
});
|
|
543
|
-
} else {
|
|
544
|
-
const tightened = !beforeUniqueItems && afterUniqueItems;
|
|
545
|
-
const breaking = direction === "input" ? tightened : !tightened;
|
|
546
|
-
push(changes, {
|
|
547
|
-
severity: breaking ? "breaking" : "additive",
|
|
548
|
-
code: tightened ? "constraint-tightened" : "constraint-relaxed",
|
|
549
|
-
path: `${path}.uniqueItems`,
|
|
550
|
-
message: `uniqueItems changed from ${beforeUniqueItems} to ${afterUniqueItems}`,
|
|
551
|
-
before: before.uniqueItems,
|
|
552
|
-
after: after.uniqueItems
|
|
553
|
-
});
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
const normalizeAdditionalProperties = (value) => value && typeof value === "object" && !Array.isArray(value) && !Object.keys(value).length ? true : value;
|
|
557
|
-
const beforeAdditional = normalizeAdditionalProperties(before.additionalProperties ?? true);
|
|
558
|
-
const afterAdditional = normalizeAdditionalProperties(after.additionalProperties ?? true);
|
|
559
|
-
if (beforeAdditional !== false && afterAdditional === false) {
|
|
560
|
-
push(changes, {
|
|
561
|
-
severity: direction === "input" ? "breaking" : "additive",
|
|
562
|
-
code: "additional-properties-disabled",
|
|
563
|
-
path,
|
|
564
|
-
message: `Additional properties are no longer accepted at ${path}`
|
|
565
|
-
});
|
|
566
|
-
} else if (beforeAdditional === false && afterAdditional !== false) {
|
|
567
|
-
push(changes, {
|
|
568
|
-
severity: direction === "input" ? "additive" : "breaking",
|
|
569
|
-
code: "additional-properties-enabled",
|
|
570
|
-
path,
|
|
571
|
-
message: `Additional properties are now accepted at ${path}`
|
|
572
|
-
});
|
|
573
|
-
} else if (!equal(beforeAdditional, afterAdditional)) {
|
|
574
|
-
diffSchema(
|
|
575
|
-
beforeAdditional,
|
|
576
|
-
afterAdditional,
|
|
577
|
-
`${path}.additionalProperties`,
|
|
578
|
-
direction,
|
|
579
|
-
changes
|
|
580
|
-
);
|
|
562
|
+
return candidates;
|
|
563
|
+
}
|
|
564
|
+
function schemaMayAcceptString(schema, root, references = /* @__PURE__ */ new Set()) {
|
|
565
|
+
if (typeof schema === "boolean") return schema;
|
|
566
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema)) return false;
|
|
567
|
+
const value = schema;
|
|
568
|
+
const finiteCandidates = finiteStringCandidates(schema, root, references);
|
|
569
|
+
if (finiteCandidates?.size === 0) return false;
|
|
570
|
+
if (value.$ref !== void 0) {
|
|
571
|
+
if (typeof value.$ref !== "string" || references.has(value.$ref)) return false;
|
|
572
|
+
const referenced = resolveLocalSchemaReference(root, value.$ref);
|
|
573
|
+
if (!referenced) return false;
|
|
574
|
+
const nextReferences = new Set(references).add(value.$ref);
|
|
575
|
+
if (!schemaMayAcceptString(referenced, root, nextReferences)) return false;
|
|
581
576
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
const
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
...Array.isArray(before.required) ? before.required : [],
|
|
588
|
-
...Array.isArray(after.required) ? after.required : []
|
|
589
|
-
])].sort();
|
|
590
|
-
const beforeRequired = new Set(
|
|
591
|
-
Array.isArray(before.required) ? before.required : []
|
|
592
|
-
);
|
|
593
|
-
const afterRequired = new Set(
|
|
594
|
-
Array.isArray(after.required) ? after.required : []
|
|
595
|
-
);
|
|
596
|
-
for (const name of propertyNames) {
|
|
597
|
-
const propertyPath = `${path}.properties.${name}`;
|
|
598
|
-
const oldProperty = beforeProperties[name];
|
|
599
|
-
const newProperty = afterProperties[name];
|
|
600
|
-
const hadOldProperty = Object.prototype.hasOwnProperty.call(beforeProperties, name);
|
|
601
|
-
const hasNewProperty = Object.prototype.hasOwnProperty.call(afterProperties, name);
|
|
602
|
-
if (!hadOldProperty && hasNewProperty) {
|
|
603
|
-
const newlyRequired = afterRequired.has(name);
|
|
604
|
-
const additionalPropertyChanges = [];
|
|
605
|
-
if (direction === "output" && typeof beforeAdditional === "object") {
|
|
606
|
-
diffSchema(
|
|
607
|
-
beforeAdditional,
|
|
608
|
-
newProperty,
|
|
609
|
-
`${propertyPath}.additionalProperties`,
|
|
610
|
-
"output",
|
|
611
|
-
additionalPropertyChanges
|
|
612
|
-
);
|
|
613
|
-
}
|
|
614
|
-
const inputFallbackChanges = [];
|
|
615
|
-
if (direction === "input") {
|
|
616
|
-
const patternSchemas = matchingPatternSchemas(before, name);
|
|
617
|
-
const fallbackSchemas = patternSchemas.length ? patternSchemas : [beforeAdditional];
|
|
618
|
-
fallbackSchemas.forEach((fallbackSchema, index) => diffSchema(
|
|
619
|
-
fallbackSchema,
|
|
620
|
-
newProperty,
|
|
621
|
-
`${propertyPath}.${patternSchemas.length ? `patternProperties.${index}` : "additionalProperties"}`,
|
|
622
|
-
"input",
|
|
623
|
-
inputFallbackChanges
|
|
624
|
-
));
|
|
625
|
-
}
|
|
626
|
-
const breaksDeclaredOutput = direction === "output" && (beforeAdditional === false || additionalPropertyChanges.some((change) => change.severity === "breaking"));
|
|
627
|
-
const narrowsAcceptedInput = direction === "input" && inputFallbackChanges.some((change) => change.severity === "breaking");
|
|
628
|
-
push(changes, {
|
|
629
|
-
severity: direction === "input" && (newlyRequired || narrowsAcceptedInput) || breaksDeclaredOutput ? "breaking" : "additive",
|
|
630
|
-
code: direction === "input" && newlyRequired ? "required-input-added" : breaksDeclaredOutput ? "output-property-added" : "property-added",
|
|
631
|
-
path: propertyPath,
|
|
632
|
-
message: `${newlyRequired ? "Required" : "Optional"} property ${name} was added`,
|
|
633
|
-
after: newProperty
|
|
634
|
-
});
|
|
635
|
-
continue;
|
|
636
|
-
}
|
|
637
|
-
if (hadOldProperty && !hasNewProperty) {
|
|
638
|
-
const fallbackChanges = [];
|
|
639
|
-
const patternSchemas = matchingPatternSchemas(after, name);
|
|
640
|
-
const fallbackSchemas = patternSchemas.length ? patternSchemas : [afterAdditional];
|
|
641
|
-
fallbackSchemas.forEach((fallbackSchema, index) => diffSchema(
|
|
642
|
-
oldProperty,
|
|
643
|
-
fallbackSchema,
|
|
644
|
-
`${propertyPath}.${patternSchemas.length ? `patternProperties.${index}` : "additionalProperties"}`,
|
|
645
|
-
direction,
|
|
646
|
-
fallbackChanges
|
|
647
|
-
));
|
|
648
|
-
const breaking2 = direction === "output" && beforeRequired.has(name) || fallbackChanges.some((change) => change.severity === "breaking");
|
|
649
|
-
push(changes, {
|
|
650
|
-
severity: breaking2 ? "breaking" : "additive",
|
|
651
|
-
code: "property-removed",
|
|
652
|
-
path: propertyPath,
|
|
653
|
-
message: `Property ${name} was removed`,
|
|
654
|
-
before: oldProperty
|
|
655
|
-
});
|
|
656
|
-
continue;
|
|
657
|
-
}
|
|
658
|
-
diffSchema(oldProperty, newProperty, propertyPath, direction, changes);
|
|
659
|
-
const wasRequired = beforeRequired.has(name);
|
|
660
|
-
const isRequired = afterRequired.has(name);
|
|
661
|
-
if (wasRequired === isRequired) continue;
|
|
662
|
-
const breaking = direction === "input" ? !wasRequired && isRequired : wasRequired && !isRequired;
|
|
663
|
-
push(changes, {
|
|
664
|
-
severity: breaking ? "breaking" : "additive",
|
|
665
|
-
code: breaking ? "requiredness-broken" : "requiredness-compatible",
|
|
666
|
-
path: propertyPath,
|
|
667
|
-
message: `Property ${name} changed from ${wasRequired ? "required" : "optional"} to ${isRequired ? "required" : "optional"}`
|
|
668
|
-
});
|
|
577
|
+
if (typeof value.type === "string" && value.type !== "string") return false;
|
|
578
|
+
if (Array.isArray(value.type) && !value.type.includes("string")) return false;
|
|
579
|
+
if (Object.hasOwn(value, "const") && typeof value.const !== "string") return false;
|
|
580
|
+
if (Array.isArray(value.enum) && !value.enum.some((entry) => typeof entry === "string")) {
|
|
581
|
+
return false;
|
|
669
582
|
}
|
|
670
|
-
if (
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
583
|
+
if (Array.isArray(value.allOf) && !value.allOf.every((member) => schemaMayAcceptString(member, root, references))) return false;
|
|
584
|
+
if (Array.isArray(value.anyOf) && !value.anyOf.some((member) => schemaMayAcceptString(member, root, references))) return false;
|
|
585
|
+
if (Array.isArray(value.oneOf) && !value.oneOf.some((member) => schemaMayAcceptString(member, root, references))) return false;
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
function assertPromptSchemaSupportsStringValues(schema, label) {
|
|
589
|
+
const inventory = derivePromptSchemaInventory(schema, schema);
|
|
590
|
+
if (!inventory) {
|
|
591
|
+
throw new McpFnValidationError(
|
|
592
|
+
`${label} must use derivable properties, allOf, or local $ref declarations`
|
|
677
593
|
);
|
|
678
594
|
}
|
|
679
|
-
const
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
...Object.keys(afterPatterns)
|
|
684
|
-
])].sort();
|
|
685
|
-
for (const pattern of patternNames) {
|
|
686
|
-
const hadBeforePattern = Object.prototype.hasOwnProperty.call(beforePatterns, pattern);
|
|
687
|
-
const hasAfterPattern = Object.prototype.hasOwnProperty.call(afterPatterns, pattern);
|
|
688
|
-
diffSchema(
|
|
689
|
-
hadBeforePattern ? beforePatterns[pattern] : beforeAdditional,
|
|
690
|
-
hasAfterPattern ? afterPatterns[pattern] : afterAdditional,
|
|
691
|
-
`${path}.patternProperties.${pattern}`,
|
|
692
|
-
direction,
|
|
693
|
-
changes
|
|
595
|
+
for (const [name, schemas] of inventory.propertySchemas) {
|
|
596
|
+
const finiteCandidates = finiteStringCandidates({ allOf: schemas }, schema);
|
|
597
|
+
let hasStringWitness = schemas.every(
|
|
598
|
+
(propertySchema) => schemaMayAcceptString(propertySchema, schema)
|
|
694
599
|
);
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
600
|
+
if (hasStringWitness && finiteCandidates) {
|
|
601
|
+
const candidateSchema = {
|
|
602
|
+
...schema.$defs && typeof schema.$defs === "object" ? { $defs: schema.$defs } : {},
|
|
603
|
+
...schema.definitions && typeof schema.definitions === "object" ? { definitions: schema.definitions } : {},
|
|
604
|
+
allOf: schemas
|
|
605
|
+
};
|
|
606
|
+
let validate;
|
|
607
|
+
try {
|
|
608
|
+
validate = new import_ajv.default({ strict: false, allowUnionTypes: true }).compile(candidateSchema);
|
|
609
|
+
} catch {
|
|
610
|
+
hasStringWitness = false;
|
|
706
611
|
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
for (const [remainingPattern, remainingSchema] of Object.entries(afterPatterns)) {
|
|
710
|
-
if (!patternsMayOverlap(pattern, remainingPattern)) continue;
|
|
711
|
-
diffSchema(
|
|
712
|
-
beforePatterns[pattern],
|
|
713
|
-
remainingSchema,
|
|
714
|
-
`${path}.patternProperties.${pattern}.overlap.${remainingPattern}`,
|
|
715
|
-
"output",
|
|
716
|
-
overlapChanges
|
|
717
|
-
);
|
|
612
|
+
if (validate) {
|
|
613
|
+
hasStringWitness = [...finiteCandidates].some((candidate) => validate(candidate));
|
|
718
614
|
}
|
|
719
615
|
}
|
|
720
|
-
if (
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
path: `${path}.patternProperties.${pattern}`,
|
|
725
|
-
message: `${hadBeforePattern ? "Removing" : "Adding"} pattern ${pattern} can change constraints for properties matched by another pattern`,
|
|
726
|
-
before: hadBeforePattern ? beforePatterns[pattern] : void 0,
|
|
727
|
-
after: hasAfterPattern ? afterPatterns[pattern] : void 0
|
|
728
|
-
});
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
const handledKeywords = /* @__PURE__ */ new Set([
|
|
732
|
-
"type",
|
|
733
|
-
"enum",
|
|
734
|
-
"properties",
|
|
735
|
-
"required",
|
|
736
|
-
"items",
|
|
737
|
-
"additionalProperties",
|
|
738
|
-
"patternProperties",
|
|
739
|
-
"pattern",
|
|
740
|
-
"format",
|
|
741
|
-
"const",
|
|
742
|
-
"uniqueItems",
|
|
743
|
-
...tightening.map(([keyword]) => keyword)
|
|
744
|
-
]);
|
|
745
|
-
const behavioralKeywords = /* @__PURE__ */ new Set([
|
|
746
|
-
"$comment",
|
|
747
|
-
"$id",
|
|
748
|
-
"$schema",
|
|
749
|
-
"default",
|
|
750
|
-
"deprecated",
|
|
751
|
-
"description",
|
|
752
|
-
"examples",
|
|
753
|
-
"readOnly",
|
|
754
|
-
"title",
|
|
755
|
-
"writeOnly"
|
|
756
|
-
]);
|
|
757
|
-
const keywords = [.../* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)])].sort();
|
|
758
|
-
for (const keyword of keywords) {
|
|
759
|
-
if (handledKeywords.has(keyword) || equal(before[keyword], after[keyword])) continue;
|
|
760
|
-
const behavioral = behavioralKeywords.has(keyword) || /^x-/i.test(keyword);
|
|
761
|
-
push(changes, {
|
|
762
|
-
severity: behavioral ? "behavioral" : "breaking",
|
|
763
|
-
code: behavioral ? "schema-guidance-changed" : "schema-keyword-changed",
|
|
764
|
-
path: `${path}.${keyword}`,
|
|
765
|
-
message: `${keyword} changed at ${path}`,
|
|
766
|
-
before: before[keyword],
|
|
767
|
-
after: after[keyword]
|
|
768
|
-
});
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
function diffTool(before, after, changes) {
|
|
772
|
-
const root = `tools.${before.name}`;
|
|
773
|
-
if (before.title !== after.title) {
|
|
774
|
-
push(changes, {
|
|
775
|
-
severity: "behavioral",
|
|
776
|
-
code: "tool-title-changed",
|
|
777
|
-
path: `${root}.title`,
|
|
778
|
-
message: `Tool title changed for ${before.name}`,
|
|
779
|
-
before: before.title,
|
|
780
|
-
after: after.title
|
|
781
|
-
});
|
|
782
|
-
}
|
|
783
|
-
if (before.description !== after.description) {
|
|
784
|
-
push(changes, {
|
|
785
|
-
severity: "behavioral",
|
|
786
|
-
code: "tool-description-changed",
|
|
787
|
-
path: `${root}.description`,
|
|
788
|
-
message: `Tool description changed for ${before.name}`,
|
|
789
|
-
before: before.description,
|
|
790
|
-
after: after.description
|
|
791
|
-
});
|
|
792
|
-
}
|
|
793
|
-
if (!equal(before.annotations ?? {}, after.annotations ?? {})) {
|
|
794
|
-
push(changes, {
|
|
795
|
-
severity: "behavioral",
|
|
796
|
-
code: "tool-annotations-changed",
|
|
797
|
-
path: `${root}.annotations`,
|
|
798
|
-
message: `Tool annotations changed for ${before.name}`,
|
|
799
|
-
before: before.annotations,
|
|
800
|
-
after: after.annotations
|
|
801
|
-
});
|
|
802
|
-
}
|
|
803
|
-
if (!equal(before.execution ?? {}, after.execution ?? {})) {
|
|
804
|
-
const oldSupport = before.execution?.taskSupport ?? "forbidden";
|
|
805
|
-
const newSupport = after.execution?.taskSupport ?? "forbidden";
|
|
806
|
-
const breaking = newSupport === "required" || oldSupport !== "forbidden" && newSupport === "forbidden";
|
|
807
|
-
push(changes, {
|
|
808
|
-
severity: breaking ? "breaking" : "additive",
|
|
809
|
-
code: "tool-task-support-changed",
|
|
810
|
-
path: `${root}.execution.taskSupport`,
|
|
811
|
-
message: `Tool task support changed from ${oldSupport} to ${newSupport}`,
|
|
812
|
-
before: oldSupport,
|
|
813
|
-
after: newSupport
|
|
814
|
-
});
|
|
815
|
-
}
|
|
816
|
-
if (!equal(before.icons ?? [], after.icons ?? [])) {
|
|
817
|
-
push(changes, {
|
|
818
|
-
severity: "behavioral",
|
|
819
|
-
code: "tool-icons-changed",
|
|
820
|
-
path: `${root}.icons`,
|
|
821
|
-
message: `Tool icons changed for ${before.name}`,
|
|
822
|
-
before: before.icons,
|
|
823
|
-
after: after.icons
|
|
824
|
-
});
|
|
825
|
-
}
|
|
826
|
-
if (!equal(before.metadata ?? {}, after.metadata ?? {})) {
|
|
827
|
-
push(changes, {
|
|
828
|
-
severity: "behavioral",
|
|
829
|
-
code: "tool-metadata-changed",
|
|
830
|
-
path: `${root}.metadata`,
|
|
831
|
-
message: `Tool metadata changed for ${before.name}`,
|
|
832
|
-
before: before.metadata,
|
|
833
|
-
after: after.metadata
|
|
834
|
-
});
|
|
835
|
-
}
|
|
836
|
-
diffSchema(before.inputSchema, after.inputSchema, `${root}.inputSchema`, "input", changes);
|
|
837
|
-
diffSchema(before.outputSchema, after.outputSchema, `${root}.outputSchema`, "output", changes);
|
|
838
|
-
}
|
|
839
|
-
function diffResource(before, after, changes) {
|
|
840
|
-
const root = `resources.${before.uri}`;
|
|
841
|
-
if ((before.subscribable ?? false) !== (after.subscribable ?? false)) {
|
|
842
|
-
const enabled = after.subscribable === true;
|
|
843
|
-
push(changes, {
|
|
844
|
-
severity: enabled ? "additive" : "breaking",
|
|
845
|
-
code: enabled ? "resource-subscription-enabled" : "resource-subscription-removed",
|
|
846
|
-
path: `${root}.subscribable`,
|
|
847
|
-
message: `Resource subscription support was ${enabled ? "enabled" : "removed"} for ${before.uri}`,
|
|
848
|
-
before: before.subscribable,
|
|
849
|
-
after: after.subscribable
|
|
850
|
-
});
|
|
851
|
-
}
|
|
852
|
-
if (before.mimeType !== after.mimeType) {
|
|
853
|
-
push(changes, {
|
|
854
|
-
severity: "breaking",
|
|
855
|
-
code: "resource-mime-type-changed",
|
|
856
|
-
path: `${root}.mimeType`,
|
|
857
|
-
message: `Resource MIME type changed for ${before.uri}`,
|
|
858
|
-
before: before.mimeType,
|
|
859
|
-
after: after.mimeType
|
|
860
|
-
});
|
|
861
|
-
}
|
|
862
|
-
for (const key of ["name", "title", "description", "annotations", "icons", "metadata"]) {
|
|
863
|
-
if (!equal(before[key], after[key])) {
|
|
864
|
-
push(changes, {
|
|
865
|
-
severity: "behavioral",
|
|
866
|
-
code: `resource-${key}-changed`,
|
|
867
|
-
path: `${root}.${key}`,
|
|
868
|
-
message: `Resource ${key} changed for ${before.uri}`,
|
|
869
|
-
before: before[key],
|
|
870
|
-
after: after[key]
|
|
871
|
-
});
|
|
616
|
+
if (!hasStringWitness) {
|
|
617
|
+
throw new McpFnValidationError(
|
|
618
|
+
`${label} argument ${name} must accept string values`
|
|
619
|
+
);
|
|
872
620
|
}
|
|
873
621
|
}
|
|
874
622
|
}
|
|
875
|
-
function
|
|
876
|
-
const
|
|
877
|
-
if (
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
code: "resource-template-uri-changed",
|
|
892
|
-
path: `${root}.uriTemplate`,
|
|
893
|
-
message: `Resource template URI changed for ${before.name}`,
|
|
894
|
-
before: before.uriTemplate,
|
|
895
|
-
after: after.uriTemplate
|
|
896
|
-
});
|
|
897
|
-
}
|
|
898
|
-
if (before.mimeType !== after.mimeType) {
|
|
899
|
-
push(changes, {
|
|
900
|
-
severity: "breaking",
|
|
901
|
-
code: "resource-template-mime-type-changed",
|
|
902
|
-
path: `${root}.mimeType`,
|
|
903
|
-
message: `Resource template MIME type changed for ${before.name}`,
|
|
904
|
-
before: before.mimeType,
|
|
905
|
-
after: after.mimeType
|
|
906
|
-
});
|
|
907
|
-
}
|
|
908
|
-
for (const key of ["title", "description", "annotations", "icons", "metadata"]) {
|
|
909
|
-
if (!equal(before[key], after[key])) {
|
|
910
|
-
push(changes, {
|
|
911
|
-
severity: "behavioral",
|
|
912
|
-
code: `resource-template-${key}-changed`,
|
|
913
|
-
path: `${root}.${key}`,
|
|
914
|
-
message: `Resource template ${key} changed for ${before.name}`,
|
|
915
|
-
before: before[key],
|
|
916
|
-
after: after[key]
|
|
917
|
-
});
|
|
918
|
-
}
|
|
919
|
-
}
|
|
623
|
+
function schemaPromptArguments(definition) {
|
|
624
|
+
const schema = definition.argumentsSchema;
|
|
625
|
+
if (!schema) return void 0;
|
|
626
|
+
const inventory = derivePromptSchemaInventory(schema, schema);
|
|
627
|
+
if (!inventory) return void 0;
|
|
628
|
+
const names = [.../* @__PURE__ */ new Set([
|
|
629
|
+
...inventory.propertySchemas.keys(),
|
|
630
|
+
...inventory.required
|
|
631
|
+
])].sort(compareCodeUnits);
|
|
632
|
+
return names.map((name) => {
|
|
633
|
+
return {
|
|
634
|
+
name,
|
|
635
|
+
...inventory.descriptions.has(name) ? { description: inventory.descriptions.get(name) } : {},
|
|
636
|
+
...inventory.required.has(name) ? { required: true } : {}
|
|
637
|
+
};
|
|
638
|
+
});
|
|
920
639
|
}
|
|
921
|
-
function
|
|
922
|
-
if (
|
|
640
|
+
function promptSchema(definition) {
|
|
641
|
+
if (definition.argumentsSchema) return definition.argumentsSchema;
|
|
642
|
+
const properties = Object.fromEntries(
|
|
643
|
+
(definition.arguments ?? []).map((argument) => [
|
|
644
|
+
argument.name,
|
|
645
|
+
{ type: "string", ...argument.description ? { description: argument.description } : {} }
|
|
646
|
+
])
|
|
647
|
+
);
|
|
923
648
|
return {
|
|
924
649
|
type: "object",
|
|
925
|
-
properties
|
|
926
|
-
|
|
927
|
-
),
|
|
928
|
-
required: (prompt.arguments ?? []).filter((argument) => argument.required).map((argument) => argument.name),
|
|
650
|
+
properties,
|
|
651
|
+
required: (definition.arguments ?? []).filter((argument) => argument.required).map((argument) => argument.name),
|
|
929
652
|
additionalProperties: false
|
|
930
653
|
};
|
|
931
654
|
}
|
|
932
|
-
function
|
|
933
|
-
|
|
934
|
-
for (const key of ["title", "description", "icons", "metadata"]) {
|
|
935
|
-
if (!equal(before[key], after[key])) {
|
|
936
|
-
push(changes, {
|
|
937
|
-
severity: "behavioral",
|
|
938
|
-
code: `prompt-${key}-changed`,
|
|
939
|
-
path: `${root}.${key}`,
|
|
940
|
-
message: `Prompt ${key} changed for ${before.name}`,
|
|
941
|
-
before: before[key],
|
|
942
|
-
after: after[key]
|
|
943
|
-
});
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
diffSchema(
|
|
947
|
-
promptArgumentSchema(before),
|
|
948
|
-
promptArgumentSchema(after),
|
|
949
|
-
`${root}.arguments`,
|
|
950
|
-
"input",
|
|
951
|
-
changes
|
|
952
|
-
);
|
|
953
|
-
if (!equal(before.arguments ?? [], after.arguments ?? [])) {
|
|
954
|
-
push(changes, {
|
|
955
|
-
severity: "behavioral",
|
|
956
|
-
code: "prompt-arguments-changed",
|
|
957
|
-
path: `${root}.argumentsMetadata`,
|
|
958
|
-
message: `Prompt argument metadata changed for ${before.name}`,
|
|
959
|
-
before: before.arguments,
|
|
960
|
-
after: after.arguments
|
|
961
|
-
});
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
function requirementValues(value) {
|
|
965
|
-
if (value === true) return /* @__PURE__ */ new Set(["true"]);
|
|
966
|
-
if (Array.isArray(value)) return new Set(value.map((entry) => canonicalJson(entry)));
|
|
967
|
-
return /* @__PURE__ */ new Set();
|
|
968
|
-
}
|
|
969
|
-
function extensionRequired(value) {
|
|
970
|
-
return Boolean(value && typeof value === "object" && !Array.isArray(value) && value.required === true);
|
|
655
|
+
function promptArguments(definition) {
|
|
656
|
+
return Array.isArray(definition.arguments) ? definition.arguments : schemaPromptArguments(definition);
|
|
971
657
|
}
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
path: `${label}.${name}`,
|
|
982
|
-
message: `${label} ${name} was removed`,
|
|
983
|
-
before: beforeValues.get(name)
|
|
984
|
-
});
|
|
985
|
-
} else compare(beforeValues.get(name), next, changes);
|
|
658
|
+
var McpFnRegistry = class {
|
|
659
|
+
ajv;
|
|
660
|
+
tools = /* @__PURE__ */ new Map();
|
|
661
|
+
resources = /* @__PURE__ */ new Map();
|
|
662
|
+
resourceTemplates = /* @__PURE__ */ new Map();
|
|
663
|
+
prompts = /* @__PURE__ */ new Map();
|
|
664
|
+
constructor() {
|
|
665
|
+
this.ajv = new import_ajv.default({ allErrors: true, strict: false, allowUnionTypes: true });
|
|
666
|
+
(0, import_ajv_formats.default)(this.ajv);
|
|
986
667
|
}
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
code: `${label}-added`,
|
|
992
|
-
path: `${label}.${name}`,
|
|
993
|
-
message: `${label} ${name} was added`,
|
|
994
|
-
after: afterValues.get(name)
|
|
995
|
-
});
|
|
668
|
+
register(definition) {
|
|
669
|
+
assertName("tool", definition.name);
|
|
670
|
+
if (this.tools.has(definition.name)) {
|
|
671
|
+
throw new McpFnValidationError(`Duplicate MCP tool: ${definition.name}`);
|
|
996
672
|
}
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
673
|
+
if (!definition.description.trim()) {
|
|
674
|
+
throw new McpFnValidationError(
|
|
675
|
+
`Tool ${definition.name} requires a non-empty description`
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
if (typeof definition.handler !== "function") {
|
|
679
|
+
throw new McpFnValidationError(
|
|
680
|
+
`Tool ${definition.name} requires a handler function`
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
if (definition.inputSchema.type !== "object") {
|
|
684
|
+
throw new McpFnValidationError(
|
|
685
|
+
`Tool ${definition.name} inputSchema must be an object schema`
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
if (definition.outputSchema !== void 0 && (definition.outputSchema === null || typeof definition.outputSchema !== "object" || Array.isArray(definition.outputSchema) || definition.outputSchema.type !== "object")) {
|
|
689
|
+
throw new McpFnValidationError(
|
|
690
|
+
`Tool ${definition.name} outputSchema must be an object schema`
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
const taskSupport = definition.execution?.taskSupport ?? "forbidden";
|
|
694
|
+
if (!["forbidden", "optional", "required"].includes(taskSupport)) {
|
|
695
|
+
throw new McpFnValidationError(
|
|
696
|
+
`Tool ${definition.name} has invalid taskSupport=${String(taskSupport)}`
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
if ((taskSupport === "required" || taskSupport === "optional") && !definition.taskHandler) {
|
|
700
|
+
throw new McpFnValidationError(
|
|
701
|
+
`Tool ${definition.name} declares taskSupport=${taskSupport} but has no taskHandler`
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
if (taskSupport === "forbidden" && definition.taskHandler) {
|
|
705
|
+
throw new McpFnValidationError(
|
|
706
|
+
`Tool ${definition.name} has a taskHandler but forbids task execution`
|
|
707
|
+
);
|
|
708
|
+
}
|
|
709
|
+
let validateInput;
|
|
710
|
+
let validateOutput;
|
|
711
|
+
try {
|
|
712
|
+
validateInput = this.ajv.compile(definition.inputSchema);
|
|
713
|
+
validateOutput = definition.outputSchema !== void 0 ? this.ajv.compile(definition.outputSchema) : void 0;
|
|
714
|
+
} catch (error) {
|
|
715
|
+
throw new McpFnValidationError(
|
|
716
|
+
`Tool ${definition.name} contains an invalid JSON Schema`,
|
|
717
|
+
{ cause: error instanceof Error ? error.message : String(error) }
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
this.tools.set(definition.name, {
|
|
721
|
+
definition,
|
|
722
|
+
validateInput,
|
|
723
|
+
validateOutput
|
|
1009
724
|
});
|
|
725
|
+
return this;
|
|
1010
726
|
}
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
code: "server-instructions-changed",
|
|
1015
|
-
path: "server.instructions",
|
|
1016
|
-
message: "Server instructions changed",
|
|
1017
|
-
before: before.server.instructions,
|
|
1018
|
-
after: after.server.instructions
|
|
1019
|
-
});
|
|
727
|
+
registerAll(definitions) {
|
|
728
|
+
for (const definition of definitions) this.register(definition);
|
|
729
|
+
return this;
|
|
1020
730
|
}
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
severity: "breaking",
|
|
1029
|
-
code: "tool-removed",
|
|
1030
|
-
path: `tools.${name}`,
|
|
1031
|
-
message: `Tool ${name} was removed`,
|
|
1032
|
-
before: oldTool
|
|
1033
|
-
});
|
|
1034
|
-
} else {
|
|
1035
|
-
diffTool(oldTool, newTool, changes);
|
|
731
|
+
registerResource(definition) {
|
|
732
|
+
assertUri("resource", definition.uri);
|
|
733
|
+
assertName("resource", definition.name);
|
|
734
|
+
if (typeof definition.read !== "function") {
|
|
735
|
+
throw new McpFnValidationError(
|
|
736
|
+
`Resource ${definition.name} requires a read handler function`
|
|
737
|
+
);
|
|
1036
738
|
}
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
if (!beforeTools.has(name)) {
|
|
1040
|
-
push(changes, {
|
|
1041
|
-
severity: "additive",
|
|
1042
|
-
code: "tool-added",
|
|
1043
|
-
path: `tools.${name}`,
|
|
1044
|
-
message: `Tool ${name} was added`,
|
|
1045
|
-
after: afterTools.get(name)
|
|
1046
|
-
});
|
|
739
|
+
if (this.resources.has(definition.uri)) {
|
|
740
|
+
throw new McpFnValidationError(`Duplicate MCP resource: ${definition.uri}`);
|
|
1047
741
|
}
|
|
742
|
+
assertSubscriptionCallbacks(
|
|
743
|
+
"Resource",
|
|
744
|
+
definition.name,
|
|
745
|
+
definition.subscribe,
|
|
746
|
+
definition.unsubscribe
|
|
747
|
+
);
|
|
748
|
+
this.resources.set(definition.uri, definition);
|
|
749
|
+
return this;
|
|
1048
750
|
}
|
|
1049
|
-
|
|
1050
|
-
"
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
(
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
751
|
+
registerResourceTemplate(definition) {
|
|
752
|
+
assertName("resource template", definition.name);
|
|
753
|
+
if (typeof definition.read !== "function") {
|
|
754
|
+
throw new McpFnValidationError(
|
|
755
|
+
`Resource template ${definition.name} requires a read handler function`
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
if (this.resourceTemplates.has(definition.name)) {
|
|
759
|
+
throw new McpFnValidationError(
|
|
760
|
+
`Duplicate MCP resource template: ${definition.name}`
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
if (typeof definition.uriTemplate !== "string") {
|
|
764
|
+
throw new McpFnValidationError(
|
|
765
|
+
`Invalid resource URI template ${JSON.stringify(definition.uriTemplate)}`
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
const unsupportedOperator = unsupportedUriTemplateOperator(definition.uriTemplate);
|
|
769
|
+
if (unsupportedOperator) {
|
|
770
|
+
throw new McpFnValidationError(
|
|
771
|
+
`Resource template ${definition.name} uses unsupported URI template operator ${unsupportedOperator}`
|
|
772
|
+
);
|
|
773
|
+
}
|
|
774
|
+
let template;
|
|
775
|
+
try {
|
|
776
|
+
template = new import_uriTemplate.UriTemplate(definition.uriTemplate);
|
|
777
|
+
} catch (error) {
|
|
778
|
+
throw new McpFnValidationError(
|
|
779
|
+
`Invalid resource URI template ${JSON.stringify(definition.uriTemplate)}`,
|
|
780
|
+
{ cause: error instanceof Error ? error.message : String(error) }
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
if (!template.variableNames.length) {
|
|
784
|
+
throw new McpFnValidationError(
|
|
785
|
+
`Resource template ${definition.name} must contain at least one variable`
|
|
786
|
+
);
|
|
787
|
+
}
|
|
788
|
+
assertSubscriptionCallbacks(
|
|
789
|
+
"Resource template",
|
|
790
|
+
definition.name,
|
|
791
|
+
definition.subscribe,
|
|
792
|
+
definition.unsubscribe
|
|
793
|
+
);
|
|
794
|
+
if ([...this.resourceTemplates.values()].some(
|
|
795
|
+
({ definition: registered }) => uriTemplatesOverlap(registered.uriTemplate, definition.uriTemplate)
|
|
796
|
+
)) {
|
|
797
|
+
throw new McpFnValidationError(
|
|
798
|
+
`Ambiguous MCP resource URI template: ${definition.uriTemplate}`
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
for (const name of Object.keys(definition.complete ?? {})) {
|
|
802
|
+
if (!template.variableNames.includes(name)) {
|
|
803
|
+
throw new McpFnValidationError(
|
|
804
|
+
`Resource template ${definition.name} has a completer for unknown variable ${name}`
|
|
805
|
+
);
|
|
1102
806
|
}
|
|
1103
|
-
push(changes, {
|
|
1104
|
-
severity: requirementChanged && requirementAdded ? "breaking" : requirementChanged && requirementRemoved ? "additive" : key === "extensions" && !extensionWasRequired && extensionIsRequired ? "breaking" : key === "extensions" && extensionWasRequired && !extensionIsRequired ? "additive" : removed ? "breaking" : added ? "additive" : "behavioral",
|
|
1105
|
-
code: `${key}-${removed ? "removed" : added ? "added" : "changed"}`,
|
|
1106
|
-
path: `${key}.${name}`,
|
|
1107
|
-
message: `${key} ${name} was ${removed ? "removed" : added ? "added" : "changed"}`,
|
|
1108
|
-
before: oldRecord[name],
|
|
1109
|
-
after: newRecord[name]
|
|
1110
|
-
});
|
|
1111
807
|
}
|
|
808
|
+
this.resourceTemplates.set(definition.name, { definition, template });
|
|
809
|
+
return this;
|
|
1112
810
|
}
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
if (!newValues.has(value)) {
|
|
1118
|
-
push(changes, {
|
|
1119
|
-
severity: "breaking",
|
|
1120
|
-
code: `${key}-support-removed`,
|
|
1121
|
-
path: key,
|
|
1122
|
-
message: `${key} support removed: ${value}`,
|
|
1123
|
-
before: value
|
|
1124
|
-
});
|
|
1125
|
-
}
|
|
811
|
+
registerPrompt(definition) {
|
|
812
|
+
assertName("prompt", definition.name);
|
|
813
|
+
if (this.prompts.has(definition.name)) {
|
|
814
|
+
throw new McpFnValidationError(`Duplicate MCP prompt: ${definition.name}`);
|
|
1126
815
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
816
|
+
if (typeof definition.get !== "function") {
|
|
817
|
+
throw new McpFnValidationError(
|
|
818
|
+
`Prompt ${definition.name} requires a get handler function`
|
|
819
|
+
);
|
|
820
|
+
}
|
|
821
|
+
if (definition.arguments !== void 0 && (!Array.isArray(definition.arguments) || definition.arguments.some(
|
|
822
|
+
(argument) => !argument || typeof argument !== "object" || Array.isArray(argument) || typeof argument.name !== "string"
|
|
823
|
+
))) {
|
|
824
|
+
throw new McpFnValidationError(`Prompt ${definition.name} arguments must be an array`);
|
|
825
|
+
}
|
|
826
|
+
for (const argument of definition.arguments ?? []) {
|
|
827
|
+
assertName("prompt argument", argument.name);
|
|
828
|
+
if (argument.description !== void 0 && typeof argument.description !== "string") {
|
|
829
|
+
throw new McpFnValidationError(
|
|
830
|
+
`Prompt ${definition.name} argument ${argument.name} description must be a string`
|
|
831
|
+
);
|
|
832
|
+
}
|
|
833
|
+
if (argument.required !== void 0 && typeof argument.required !== "boolean") {
|
|
834
|
+
throw new McpFnValidationError(
|
|
835
|
+
`Prompt ${definition.name} argument ${argument.name} required must be a boolean`
|
|
836
|
+
);
|
|
1136
837
|
}
|
|
1137
838
|
}
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
const summary = {
|
|
1143
|
-
breaking: changes.filter((change) => change.severity === "breaking").length,
|
|
1144
|
-
additive: changes.filter((change) => change.severity === "additive").length,
|
|
1145
|
-
behavioral: changes.filter((change) => change.severity === "behavioral").length
|
|
1146
|
-
};
|
|
1147
|
-
return { compatible: summary.breaking === 0, changes, summary };
|
|
1148
|
-
}
|
|
1149
|
-
|
|
1150
|
-
// src/manifest.ts
|
|
1151
|
-
var import_ajv2 = __toESM(require("ajv"), 1);
|
|
1152
|
-
var import_ajv_formats2 = __toESM(require("ajv-formats"), 1);
|
|
1153
|
-
var import_uriTemplate2 = require("@modelcontextprotocol/sdk/shared/uriTemplate.js");
|
|
1154
|
-
var import_types = require("@modelcontextprotocol/sdk/types.js");
|
|
1155
|
-
|
|
1156
|
-
// src/uri-template.ts
|
|
1157
|
-
function unsupportedUriTemplateOperator(uriTemplate) {
|
|
1158
|
-
for (const match of uriTemplate.matchAll(/\{([^{}]+)\}/g)) {
|
|
1159
|
-
if (match[1]?.startsWith(";")) return ";";
|
|
1160
|
-
}
|
|
1161
|
-
return void 0;
|
|
1162
|
-
}
|
|
1163
|
-
function uriTemplateMatchShape(uriTemplate) {
|
|
1164
|
-
return uriTemplate.replace(/\{([^{}]+)\}/g, (_match, expression) => {
|
|
1165
|
-
const operator = /^[+#./?&]/.exec(expression)?.[0] ?? "";
|
|
1166
|
-
if (operator === "?" || operator === "&") {
|
|
1167
|
-
const names = expression.slice(1).split(",").map((name) => name.replace(/\*/g, "").trim()).join(",");
|
|
1168
|
-
return `{query:${operator}:${names}}`;
|
|
839
|
+
if (definition.argumentsSchema !== void 0 && (!definition.argumentsSchema || typeof definition.argumentsSchema !== "object" || Array.isArray(definition.argumentsSchema) || definition.argumentsSchema.type !== "object")) {
|
|
840
|
+
throw new McpFnValidationError(
|
|
841
|
+
`Prompt ${definition.name} argumentsSchema must be an object schema`
|
|
842
|
+
);
|
|
1169
843
|
}
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
const states = [{ epsilon: [], transitions: [] }];
|
|
1179
|
-
let state = 0;
|
|
1180
|
-
const nextState = () => {
|
|
1181
|
-
states.push({ epsilon: [], transitions: [] });
|
|
1182
|
-
return states.length - 1;
|
|
1183
|
-
};
|
|
1184
|
-
const literal = (value) => {
|
|
1185
|
-
for (const character of value) {
|
|
1186
|
-
const next = nextState();
|
|
1187
|
-
states[state].transitions.push({
|
|
1188
|
-
predicate: { kind: "literal", value: character },
|
|
1189
|
-
to: next
|
|
1190
|
-
});
|
|
1191
|
-
state = next;
|
|
844
|
+
let validateArguments;
|
|
845
|
+
try {
|
|
846
|
+
validateArguments = this.ajv.compile(promptSchema(definition));
|
|
847
|
+
} catch (error) {
|
|
848
|
+
throw new McpFnValidationError(
|
|
849
|
+
`Prompt ${definition.name} contains an invalid arguments JSON Schema`,
|
|
850
|
+
{ cause: error instanceof Error ? error.message : String(error) }
|
|
851
|
+
);
|
|
1192
852
|
}
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
states[state].transitions.push({ predicate, to: loop });
|
|
1199
|
-
states[loop].transitions.push({ predicate, to: loop });
|
|
1200
|
-
states[loop].epsilon.push(next);
|
|
1201
|
-
state = next;
|
|
1202
|
-
};
|
|
1203
|
-
const commaSeparated = () => {
|
|
1204
|
-
oneOrMore("/,");
|
|
1205
|
-
const boundary = state;
|
|
1206
|
-
const firstComponentCharacter = nextState();
|
|
1207
|
-
const component = nextState();
|
|
1208
|
-
states[boundary].transitions.push({
|
|
1209
|
-
predicate: { kind: "literal", value: "," },
|
|
1210
|
-
to: firstComponentCharacter
|
|
1211
|
-
});
|
|
1212
|
-
const predicate = { kind: "except", value: "/," };
|
|
1213
|
-
states[firstComponentCharacter].transitions.push({ predicate, to: component });
|
|
1214
|
-
states[component].transitions.push({ predicate, to: component });
|
|
1215
|
-
states[component].epsilon.push(boundary);
|
|
1216
|
-
};
|
|
1217
|
-
const expression = (value) => {
|
|
1218
|
-
const operator = /^[+#./?&]/.exec(value)?.[0] ?? "";
|
|
1219
|
-
const exploded = value.includes("*");
|
|
1220
|
-
const names = value.slice(operator.length).split(",").map((name) => name.replace(/\*/g, "").trim()).filter(Boolean);
|
|
1221
|
-
if (operator === "?" || operator === "&") {
|
|
1222
|
-
names.forEach((name, index) => {
|
|
1223
|
-
literal(index === 0 ? operator : "&");
|
|
1224
|
-
literal(`${name}=`);
|
|
1225
|
-
oneOrMore("&");
|
|
1226
|
-
});
|
|
1227
|
-
return;
|
|
853
|
+
if (definition.argumentsSchema) {
|
|
854
|
+
assertPromptSchemaSupportsStringValues(
|
|
855
|
+
definition.argumentsSchema,
|
|
856
|
+
`Prompt ${definition.name} argumentsSchema`
|
|
857
|
+
);
|
|
1228
858
|
}
|
|
1229
|
-
|
|
1230
|
-
if (
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
for (const next of automaton.states[state].epsilon) {
|
|
1251
|
-
if (closure.has(next)) continue;
|
|
1252
|
-
closure.add(next);
|
|
1253
|
-
queue.push(next);
|
|
859
|
+
const argumentNames = (promptArguments(definition) ?? []).map(({ name }) => name);
|
|
860
|
+
if (new Set(argumentNames).size !== argumentNames.length) {
|
|
861
|
+
throw new McpFnValidationError(`Prompt ${definition.name} has duplicate arguments`);
|
|
862
|
+
}
|
|
863
|
+
if (definition.arguments && definition.argumentsSchema) {
|
|
864
|
+
const declared = definition.arguments.map(({ name, required }) => ({ name, required: required === true })).sort((left, right) => compareCodeUnits(left.name, right.name));
|
|
865
|
+
const schemaDeclared = schemaPromptArguments(definition).map(
|
|
866
|
+
({ name, required }) => ({ name, required: required === true })
|
|
867
|
+
);
|
|
868
|
+
if (JSON.stringify(declared) !== JSON.stringify(schemaDeclared)) {
|
|
869
|
+
throw new McpFnValidationError(
|
|
870
|
+
`Prompt ${definition.name} arguments and argumentsSchema disagree`
|
|
871
|
+
);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
for (const name of Object.keys(definition.complete ?? {})) {
|
|
875
|
+
if (!argumentNames.includes(name)) {
|
|
876
|
+
throw new McpFnValidationError(
|
|
877
|
+
`Prompt ${definition.name} has a completer for unknown argument ${name}`
|
|
878
|
+
);
|
|
879
|
+
}
|
|
1254
880
|
}
|
|
881
|
+
this.prompts.set(definition.name, { definition, validateArguments });
|
|
882
|
+
return this;
|
|
1255
883
|
}
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
}
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
const
|
|
1276
|
-
|
|
1277
|
-
if (
|
|
1278
|
-
|
|
884
|
+
has(name) {
|
|
885
|
+
return this.tools.has(name);
|
|
886
|
+
}
|
|
887
|
+
hasOutputSchema(name) {
|
|
888
|
+
return Boolean(this.tools.get(name)?.validateOutput);
|
|
889
|
+
}
|
|
890
|
+
definitions() {
|
|
891
|
+
return [...this.tools.values()].map(({ definition }) => definition).sort((left, right) => compareCodeUnits(left.name, right.name));
|
|
892
|
+
}
|
|
893
|
+
resourceDefinitions() {
|
|
894
|
+
return [...this.resources.values()].sort((left, right) => compareCodeUnits(left.uri, right.uri));
|
|
895
|
+
}
|
|
896
|
+
resourceTemplateDefinitions() {
|
|
897
|
+
return [...this.resourceTemplates.values()].map(({ definition }) => definition).sort((left, right) => compareCodeUnits(left.name, right.name));
|
|
898
|
+
}
|
|
899
|
+
promptDefinitions() {
|
|
900
|
+
return [...this.prompts.values()].map(({ definition }) => definition).sort((left, right) => compareCodeUnits(left.name, right.name));
|
|
901
|
+
}
|
|
902
|
+
capabilities(options = {}) {
|
|
903
|
+
const capabilities = {};
|
|
904
|
+
if (this.tools.size) capabilities.tools = { listChanged: options.listChanged ?? true };
|
|
905
|
+
if (this.resources.size || this.resourceTemplates.size) {
|
|
906
|
+
capabilities.resources = {
|
|
907
|
+
listChanged: options.listChanged ?? true,
|
|
908
|
+
subscribe: [...this.resources.values()].some((value) => value.subscribe) || [...this.resourceTemplates.values()].some(({ definition }) => definition.subscribe)
|
|
909
|
+
};
|
|
1279
910
|
}
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
911
|
+
if (this.prompts.size) capabilities.prompts = { listChanged: options.listChanged ?? true };
|
|
912
|
+
if ([...this.resourceTemplates.values()].some(({ definition }) => definition.complete) || [...this.prompts.values()].some(({ definition }) => definition.complete)) capabilities.completions = {};
|
|
913
|
+
if (this.definitions().some((definition) => {
|
|
914
|
+
const support = definition.execution?.taskSupport;
|
|
915
|
+
return support === "optional" || support === "required";
|
|
916
|
+
})) capabilities.tasks = { requests: { tools: { call: {} } } };
|
|
917
|
+
return capabilities;
|
|
918
|
+
}
|
|
919
|
+
listTools() {
|
|
920
|
+
return this.definitions().map((definition) => ({
|
|
921
|
+
name: definition.name,
|
|
922
|
+
...definition.title ? { title: definition.title } : {},
|
|
923
|
+
description: definition.description,
|
|
924
|
+
inputSchema: definition.inputSchema,
|
|
925
|
+
...definition.outputSchema ? { outputSchema: definition.outputSchema } : {},
|
|
926
|
+
...definition.annotations ? { annotations: definition.annotations } : {},
|
|
927
|
+
...definition.execution ? { execution: definition.execution } : {},
|
|
928
|
+
...definition.icons ? { icons: definition.icons } : {},
|
|
929
|
+
...definition.metadata ? { _meta: definition.metadata } : {}
|
|
930
|
+
}));
|
|
931
|
+
}
|
|
932
|
+
async listResources(context, extra) {
|
|
933
|
+
const resources = this.resourceDefinitions().map((definition) => ({
|
|
934
|
+
uri: definition.uri,
|
|
935
|
+
name: definition.name,
|
|
936
|
+
...definition.title ? { title: definition.title } : {},
|
|
937
|
+
...definition.description ? { description: definition.description } : {},
|
|
938
|
+
...definition.mimeType ? { mimeType: definition.mimeType } : {},
|
|
939
|
+
...definition.annotations ? { annotations: definition.annotations } : {},
|
|
940
|
+
...definition.icons ? { icons: definition.icons } : {},
|
|
941
|
+
...definition.metadata ? { _meta: definition.metadata } : {}
|
|
942
|
+
}));
|
|
943
|
+
for (const { definition } of this.resourceTemplates.values()) {
|
|
944
|
+
if (definition.list) resources.push(...(await definition.list(context, extra)).resources);
|
|
1290
945
|
}
|
|
946
|
+
return resources.sort((left, right) => compareCodeUnits(left.uri, right.uri));
|
|
1291
947
|
}
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
keyword: error.keyword
|
|
1304
|
-
}));
|
|
1305
|
-
}
|
|
1306
|
-
function assertName(kind, name) {
|
|
1307
|
-
if (!/^[A-Za-z0-9_.-]{1,128}$/.test(name)) {
|
|
1308
|
-
throw new McpFnValidationError(
|
|
1309
|
-
`Invalid MCP ${kind} name ${JSON.stringify(name)}`
|
|
1310
|
-
);
|
|
948
|
+
listResourceTemplates() {
|
|
949
|
+
return this.resourceTemplateDefinitions().map((definition) => ({
|
|
950
|
+
uriTemplate: definition.uriTemplate,
|
|
951
|
+
name: definition.name,
|
|
952
|
+
...definition.title ? { title: definition.title } : {},
|
|
953
|
+
...definition.description ? { description: definition.description } : {},
|
|
954
|
+
...definition.mimeType ? { mimeType: definition.mimeType } : {},
|
|
955
|
+
...definition.annotations ? { annotations: definition.annotations } : {},
|
|
956
|
+
...definition.icons ? { icons: definition.icons } : {},
|
|
957
|
+
...definition.metadata ? { _meta: definition.metadata } : {}
|
|
958
|
+
}));
|
|
1311
959
|
}
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
960
|
+
listPrompts() {
|
|
961
|
+
return this.promptDefinitions().map((definition) => ({
|
|
962
|
+
name: definition.name,
|
|
963
|
+
...definition.title ? { title: definition.title } : {},
|
|
964
|
+
...definition.description ? { description: definition.description } : {},
|
|
965
|
+
...promptArguments(definition) ? { arguments: promptArguments(definition) } : {},
|
|
966
|
+
...definition.icons ? { icons: definition.icons } : {},
|
|
967
|
+
...definition.metadata ? { _meta: definition.metadata } : {}
|
|
968
|
+
}));
|
|
1318
969
|
}
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
970
|
+
normalizeAndValidateArgs(registered, args) {
|
|
971
|
+
const normalizedArgs = args ?? {};
|
|
972
|
+
if (!registered.validateInput(normalizedArgs)) {
|
|
973
|
+
return {
|
|
974
|
+
args: normalizedArgs && typeof normalizedArgs === "object" && !Array.isArray(normalizedArgs) ? normalizedArgs : {},
|
|
975
|
+
issues: formatErrors(registered.validateInput.errors)
|
|
976
|
+
};
|
|
977
|
+
}
|
|
978
|
+
return { args: normalizedArgs };
|
|
1325
979
|
}
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
980
|
+
async callTool(name, args, context, extra) {
|
|
981
|
+
const registered = this.tools.get(name);
|
|
982
|
+
if (!registered) throw new McpFnValidationError(`Unknown MCP tool: ${name}`, { name });
|
|
983
|
+
const normalized = this.normalizeAndValidateArgs(registered, args);
|
|
984
|
+
if (normalized.issues) {
|
|
985
|
+
if (registered.definition.handleInvalidArguments) {
|
|
986
|
+
return this.finalizeResult(
|
|
987
|
+
registered,
|
|
988
|
+
await registered.definition.handleInvalidArguments(
|
|
989
|
+
normalized.args,
|
|
990
|
+
normalized.issues,
|
|
991
|
+
context,
|
|
992
|
+
extra
|
|
993
|
+
)
|
|
994
|
+
);
|
|
995
|
+
}
|
|
996
|
+
throw new McpFnValidationError(`Invalid arguments for ${name}`, { issues: normalized.issues });
|
|
997
|
+
}
|
|
998
|
+
return this.finalizeResult(
|
|
999
|
+
registered,
|
|
1000
|
+
await registered.definition.handler(normalized.args, context, extra)
|
|
1001
|
+
);
|
|
1330
1002
|
}
|
|
1331
|
-
|
|
1332
|
-
|
|
1003
|
+
taskSupport(name) {
|
|
1004
|
+
return this.tools.get(name)?.definition.execution?.taskSupport ?? "forbidden";
|
|
1333
1005
|
}
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
propertySchemas: /* @__PURE__ */ new Map(),
|
|
1363
|
-
required: /* @__PURE__ */ new Set()
|
|
1364
|
-
};
|
|
1365
|
-
if (value.$ref !== void 0) {
|
|
1366
|
-
if (typeof value.$ref !== "string" || references.has(value.$ref)) return void 0;
|
|
1367
|
-
const referenced = resolveLocalSchemaReference(root, value.$ref);
|
|
1368
|
-
if (!referenced) return void 0;
|
|
1369
|
-
const nextReferences = new Set(references).add(value.$ref);
|
|
1370
|
-
const referencedInventory = derivePromptSchemaInventory(referenced, root, nextReferences);
|
|
1371
|
-
if (!referencedInventory) return void 0;
|
|
1372
|
-
mergePromptSchemaInventory(inventory, referencedInventory);
|
|
1006
|
+
async createToolTask(name, args, context, extra) {
|
|
1007
|
+
const registered = this.tools.get(name);
|
|
1008
|
+
if (!registered) throw new McpFnValidationError(`Unknown MCP tool: ${name}`, { name });
|
|
1009
|
+
const normalized = this.normalizeAndValidateArgs(registered, args);
|
|
1010
|
+
if (normalized.issues) {
|
|
1011
|
+
throw new McpFnValidationError(`Invalid arguments for ${name}`, { issues: normalized.issues });
|
|
1012
|
+
}
|
|
1013
|
+
if (!registered.definition.taskHandler) {
|
|
1014
|
+
throw new McpFnValidationError(`Tool ${name} does not support task execution`);
|
|
1015
|
+
}
|
|
1016
|
+
const taskStore = new Proxy(extra.taskStore, {
|
|
1017
|
+
get: (target, property, receiver) => {
|
|
1018
|
+
if (property === "storeTaskResult") {
|
|
1019
|
+
return async (taskId, status, result) => target.storeTaskResult(
|
|
1020
|
+
taskId,
|
|
1021
|
+
status,
|
|
1022
|
+
this.finalizeResult(registered, result)
|
|
1023
|
+
);
|
|
1024
|
+
}
|
|
1025
|
+
const value = Reflect.get(target, property, receiver);
|
|
1026
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
1027
|
+
}
|
|
1028
|
+
});
|
|
1029
|
+
return registered.definition.taskHandler.createTask(
|
|
1030
|
+
normalized.args,
|
|
1031
|
+
context,
|
|
1032
|
+
{ ...extra, taskStore }
|
|
1033
|
+
);
|
|
1373
1034
|
}
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1035
|
+
matchResource(uri) {
|
|
1036
|
+
const exact = this.resources.get(uri);
|
|
1037
|
+
if (exact) return { kind: "resource", definition: exact };
|
|
1038
|
+
for (const { definition, template } of this.resourceTemplates.values()) {
|
|
1039
|
+
const variables = template.match(uri);
|
|
1040
|
+
if (variables) return { kind: "template", definition, variables };
|
|
1380
1041
|
}
|
|
1042
|
+
return void 0;
|
|
1381
1043
|
}
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1044
|
+
async readResource(uri, context, extra) {
|
|
1045
|
+
const match = this.matchResource(uri);
|
|
1046
|
+
if (!match) throw new McpFnValidationError(`Unknown MCP resource: ${uri}`);
|
|
1047
|
+
const url = new URL(uri);
|
|
1048
|
+
return match.kind === "resource" ? match.definition.read(url, context, extra) : match.definition.read(url, match.variables, context, extra);
|
|
1049
|
+
}
|
|
1050
|
+
async changeSubscription(uri, subscribed, context, extra) {
|
|
1051
|
+
const match = this.matchResource(uri);
|
|
1052
|
+
if (!match) throw new McpFnValidationError(`Unknown MCP resource: ${uri}`);
|
|
1053
|
+
const url = new URL(uri);
|
|
1054
|
+
if (match.kind === "resource") {
|
|
1055
|
+
const callback2 = subscribed ? match.definition.subscribe : match.definition.unsubscribe;
|
|
1056
|
+
if (!callback2) {
|
|
1057
|
+
throw new McpFnValidationError(
|
|
1058
|
+
`Resource ${uri} does not support ${subscribed ? "subscriptions" : "unsubscription"}`
|
|
1059
|
+
);
|
|
1060
|
+
}
|
|
1061
|
+
await callback2(url, context, extra);
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
const callback = subscribed ? match.definition.subscribe : match.definition.unsubscribe;
|
|
1065
|
+
if (!callback) {
|
|
1066
|
+
throw new McpFnValidationError(
|
|
1067
|
+
`Resource ${uri} does not support ${subscribed ? "subscriptions" : "unsubscription"}`
|
|
1397
1068
|
);
|
|
1398
1069
|
}
|
|
1070
|
+
await callback(url, match.variables, context, extra);
|
|
1399
1071
|
}
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
const value = schema;
|
|
1411
|
-
let candidates;
|
|
1412
|
-
if (typeof value.type === "string" && value.type !== "string") return /* @__PURE__ */ new Set();
|
|
1413
|
-
if (Array.isArray(value.type) && !value.type.includes("string")) return /* @__PURE__ */ new Set();
|
|
1414
|
-
if (Object.hasOwn(value, "const")) {
|
|
1415
|
-
candidates = typeof value.const === "string" ? /* @__PURE__ */ new Set([value.const]) : /* @__PURE__ */ new Set();
|
|
1416
|
-
}
|
|
1417
|
-
if (Array.isArray(value.enum)) {
|
|
1418
|
-
candidates = intersectStringCandidates(
|
|
1419
|
-
candidates,
|
|
1420
|
-
new Set(value.enum.filter((entry) => typeof entry === "string"))
|
|
1421
|
-
);
|
|
1422
|
-
}
|
|
1423
|
-
if (value.$ref !== void 0) {
|
|
1424
|
-
if (typeof value.$ref !== "string" || references.has(value.$ref)) return /* @__PURE__ */ new Set();
|
|
1425
|
-
const referenced = resolveLocalSchemaReference(root, value.$ref);
|
|
1426
|
-
if (!referenced) return /* @__PURE__ */ new Set();
|
|
1427
|
-
candidates = intersectStringCandidates(
|
|
1428
|
-
candidates,
|
|
1429
|
-
finiteStringCandidates(referenced, root, new Set(references).add(value.$ref))
|
|
1430
|
-
);
|
|
1072
|
+
async getPrompt(name, args, context, extra) {
|
|
1073
|
+
const registered = this.prompts.get(name);
|
|
1074
|
+
if (!registered) throw new McpFnValidationError(`Unknown MCP prompt: ${name}`);
|
|
1075
|
+
const normalized = args ?? {};
|
|
1076
|
+
if (!registered.validateArguments(normalized)) {
|
|
1077
|
+
throw new McpFnValidationError(`Invalid arguments for prompt ${name}`, {
|
|
1078
|
+
issues: formatErrors(registered.validateArguments.errors)
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
return registered.definition.get(normalized, context, extra);
|
|
1431
1082
|
}
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1083
|
+
async complete(ref, argument, completionContext, context, extra) {
|
|
1084
|
+
const empty = { completion: { values: [], total: 0, hasMore: false } };
|
|
1085
|
+
if (ref.type === "ref/prompt") {
|
|
1086
|
+
const prompt = this.prompts.get(ref.name);
|
|
1087
|
+
if (!prompt) throw new McpFnValidationError(`Unknown MCP prompt: ${ref.name}`);
|
|
1088
|
+
return prompt.definition.complete?.[argument.name]?.(
|
|
1089
|
+
argument.value,
|
|
1090
|
+
completionContext,
|
|
1091
|
+
context,
|
|
1092
|
+
extra
|
|
1093
|
+
) ?? empty;
|
|
1094
|
+
}
|
|
1095
|
+
const template = [...this.resourceTemplates.values()].find(({ definition }) => definition.uriTemplate === ref.uri);
|
|
1096
|
+
if (!template) {
|
|
1097
|
+
if (this.resources.has(ref.uri)) return empty;
|
|
1098
|
+
throw new McpFnValidationError(`Unknown MCP resource template: ${ref.uri}`);
|
|
1438
1099
|
}
|
|
1100
|
+
return template.definition.complete?.[argument.name]?.(
|
|
1101
|
+
argument.value,
|
|
1102
|
+
completionContext,
|
|
1103
|
+
context,
|
|
1104
|
+
extra
|
|
1105
|
+
) ?? empty;
|
|
1439
1106
|
}
|
|
1440
|
-
|
|
1441
|
-
if (
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1107
|
+
finalizeResult(registered, result) {
|
|
1108
|
+
if (registered.validateOutput && result.isError) {
|
|
1109
|
+
const { structuredContent: _omitted, ...errorWithoutStructuredContent } = result;
|
|
1110
|
+
return errorWithoutStructuredContent;
|
|
1111
|
+
}
|
|
1112
|
+
if (registered.validateOutput) {
|
|
1113
|
+
if (!result.structuredContent) {
|
|
1114
|
+
throw new McpFnOutputValidationError(
|
|
1115
|
+
`Tool ${registered.definition.name} declares outputSchema but returned no structuredContent`
|
|
1116
|
+
);
|
|
1117
|
+
}
|
|
1118
|
+
if (!registered.validateOutput(result.structuredContent)) {
|
|
1119
|
+
throw new McpFnOutputValidationError(
|
|
1120
|
+
`Invalid output from ${registered.definition.name}`,
|
|
1121
|
+
{ issues: formatErrors(registered.validateOutput.errors) }
|
|
1122
|
+
);
|
|
1448
1123
|
}
|
|
1449
|
-
for (const candidate of memberCandidates) union.add(candidate);
|
|
1450
1124
|
}
|
|
1451
|
-
|
|
1125
|
+
return result;
|
|
1452
1126
|
}
|
|
1453
|
-
|
|
1127
|
+
};
|
|
1128
|
+
|
|
1129
|
+
// src/manifest.ts
|
|
1130
|
+
function assertSortedUnique(label, values) {
|
|
1131
|
+
const canonical = [...new Set(values)].sort(compareCodeUnits);
|
|
1132
|
+
if (canonical.length !== values.length || canonical.some((value, index) => value !== values[index])) throw new McpFnValidationError(`${label} must be sorted and unique`);
|
|
1454
1133
|
}
|
|
1455
|
-
function
|
|
1456
|
-
if (typeof
|
|
1457
|
-
|
|
1458
|
-
const value = schema;
|
|
1459
|
-
const finiteCandidates = finiteStringCandidates(schema, root, references);
|
|
1460
|
-
if (finiteCandidates?.size === 0) return false;
|
|
1461
|
-
if (value.$ref !== void 0) {
|
|
1462
|
-
if (typeof value.$ref !== "string" || references.has(value.$ref)) return false;
|
|
1463
|
-
const referenced = resolveLocalSchemaReference(root, value.$ref);
|
|
1464
|
-
if (!referenced) return false;
|
|
1465
|
-
const nextReferences = new Set(references).add(value.$ref);
|
|
1466
|
-
if (!schemaMayAcceptString(referenced, root, nextReferences)) return false;
|
|
1467
|
-
}
|
|
1468
|
-
if (typeof value.type === "string" && value.type !== "string") return false;
|
|
1469
|
-
if (Array.isArray(value.type) && !value.type.includes("string")) return false;
|
|
1470
|
-
if (Object.hasOwn(value, "const") && typeof value.const !== "string") return false;
|
|
1471
|
-
if (Array.isArray(value.enum) && !value.enum.some((entry) => typeof entry === "string")) {
|
|
1472
|
-
return false;
|
|
1134
|
+
function assertObject(label, value) {
|
|
1135
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
1136
|
+
throw new McpFnValidationError(`${label} must be an object`);
|
|
1473
1137
|
}
|
|
1474
|
-
if (Array.isArray(value.allOf) && !value.allOf.every((member) => schemaMayAcceptString(member, root, references))) return false;
|
|
1475
|
-
if (Array.isArray(value.anyOf) && !value.anyOf.some((member) => schemaMayAcceptString(member, root, references))) return false;
|
|
1476
|
-
if (Array.isArray(value.oneOf) && !value.oneOf.some((member) => schemaMayAcceptString(member, root, references))) return false;
|
|
1477
|
-
return true;
|
|
1478
1138
|
}
|
|
1479
|
-
function
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
throw new McpFnValidationError(
|
|
1483
|
-
`${label} must use derivable properties, allOf, or local $ref declarations`
|
|
1484
|
-
);
|
|
1139
|
+
function assertName2(label, name) {
|
|
1140
|
+
if (typeof name !== "string" || !/^[A-Za-z0-9_.-]{1,128}$/.test(name)) {
|
|
1141
|
+
throw new McpFnValidationError(`${label} requires a name (letters, digits, dot, dash, or underscore)`);
|
|
1485
1142
|
}
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1143
|
+
}
|
|
1144
|
+
function assertMetadata(label, value) {
|
|
1145
|
+
if (value !== void 0) assertObject(label, value);
|
|
1146
|
+
}
|
|
1147
|
+
function assertSortedBy(label, values, select) {
|
|
1148
|
+
assertSortedUnique(label, values.map(select));
|
|
1149
|
+
}
|
|
1150
|
+
function createManifest(info, registry, options = {}) {
|
|
1151
|
+
assertMcpAppContracts(registry);
|
|
1152
|
+
const tools = registry.definitions().map((tool) => ({
|
|
1153
|
+
name: tool.name,
|
|
1154
|
+
...tool.title ? { title: tool.title } : {},
|
|
1155
|
+
description: tool.description,
|
|
1156
|
+
inputSchema: tool.inputSchema,
|
|
1157
|
+
...tool.outputSchema ? { outputSchema: tool.outputSchema } : {},
|
|
1158
|
+
...tool.annotations ? { annotations: tool.annotations } : {},
|
|
1159
|
+
...tool.execution ? { execution: tool.execution } : {},
|
|
1160
|
+
...tool.icons ? { icons: tool.icons } : {},
|
|
1161
|
+
...tool.metadata ? { metadata: tool.metadata } : {}
|
|
1162
|
+
}));
|
|
1163
|
+
const resources = registry.resourceDefinitions().map((resource) => ({
|
|
1164
|
+
uri: resource.uri,
|
|
1165
|
+
name: resource.name,
|
|
1166
|
+
...resource.title ? { title: resource.title } : {},
|
|
1167
|
+
...resource.description ? { description: resource.description } : {},
|
|
1168
|
+
...resource.mimeType ? { mimeType: resource.mimeType } : {},
|
|
1169
|
+
...resource.annotations ? { annotations: resource.annotations } : {},
|
|
1170
|
+
...resource.icons ? { icons: resource.icons } : {},
|
|
1171
|
+
...resource.subscribe ? { subscribable: true } : {},
|
|
1172
|
+
...resource.metadata ? { metadata: resource.metadata } : {}
|
|
1173
|
+
}));
|
|
1174
|
+
const resourceTemplates = registry.resourceTemplateDefinitions().map((resource) => ({
|
|
1175
|
+
uriTemplate: resource.uriTemplate,
|
|
1176
|
+
name: resource.name,
|
|
1177
|
+
...resource.title ? { title: resource.title } : {},
|
|
1178
|
+
...resource.description ? { description: resource.description } : {},
|
|
1179
|
+
...resource.mimeType ? { mimeType: resource.mimeType } : {},
|
|
1180
|
+
...resource.annotations ? { annotations: resource.annotations } : {},
|
|
1181
|
+
...resource.icons ? { icons: resource.icons } : {},
|
|
1182
|
+
...resource.subscribe ? { subscribable: true } : {},
|
|
1183
|
+
...resource.metadata ? { metadata: resource.metadata } : {}
|
|
1184
|
+
}));
|
|
1185
|
+
const prompts = registry.promptDefinitions().map((prompt) => ({
|
|
1186
|
+
name: prompt.name,
|
|
1187
|
+
...prompt.title ? { title: prompt.title } : {},
|
|
1188
|
+
...prompt.description ? { description: prompt.description } : {},
|
|
1189
|
+
...promptArguments(prompt) ? { arguments: promptArguments(prompt) } : {},
|
|
1190
|
+
...prompt.argumentsSchema ? { argumentsSchema: prompt.argumentsSchema } : {},
|
|
1191
|
+
...prompt.icons ? { icons: prompt.icons } : {},
|
|
1192
|
+
...prompt.metadata ? { metadata: prompt.metadata } : {}
|
|
1193
|
+
}));
|
|
1194
|
+
const body = {
|
|
1195
|
+
formatVersion: 1,
|
|
1196
|
+
server: {
|
|
1197
|
+
name: info.name,
|
|
1198
|
+
version: info.version,
|
|
1199
|
+
...info.instructions ? { instructions: info.instructions } : {}
|
|
1200
|
+
},
|
|
1201
|
+
...options.protocolVersions ? { protocolVersions: [...new Set(options.protocolVersions)].sort() } : {},
|
|
1202
|
+
...options.transports ? { transports: [...new Set(options.transports)].sort() } : {},
|
|
1203
|
+
capabilities: options.capabilities ?? registry.capabilities(),
|
|
1204
|
+
...options.clientRequirements ? {
|
|
1205
|
+
clientRequirements: {
|
|
1206
|
+
...options.clientRequirements,
|
|
1207
|
+
...options.clientRequirements.elicitation ? { elicitation: [...new Set(options.clientRequirements.elicitation)].sort() } : {}
|
|
1505
1208
|
}
|
|
1506
|
-
}
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
}
|
|
1513
|
-
}
|
|
1514
|
-
function schemaPromptArguments(definition) {
|
|
1515
|
-
const schema = definition.argumentsSchema;
|
|
1516
|
-
if (!schema) return void 0;
|
|
1517
|
-
const inventory = derivePromptSchemaInventory(schema, schema);
|
|
1518
|
-
if (!inventory) return void 0;
|
|
1519
|
-
const names = [.../* @__PURE__ */ new Set([
|
|
1520
|
-
...inventory.propertySchemas.keys(),
|
|
1521
|
-
...inventory.required
|
|
1522
|
-
])].sort(compareCodeUnits);
|
|
1523
|
-
return names.map((name) => {
|
|
1524
|
-
return {
|
|
1525
|
-
name,
|
|
1526
|
-
...inventory.descriptions.has(name) ? { description: inventory.descriptions.get(name) } : {},
|
|
1527
|
-
...inventory.required.has(name) ? { required: true } : {}
|
|
1528
|
-
};
|
|
1529
|
-
});
|
|
1530
|
-
}
|
|
1531
|
-
function promptSchema(definition) {
|
|
1532
|
-
if (definition.argumentsSchema) return definition.argumentsSchema;
|
|
1533
|
-
const properties = Object.fromEntries(
|
|
1534
|
-
(definition.arguments ?? []).map((argument) => [
|
|
1535
|
-
argument.name,
|
|
1536
|
-
{ type: "string", ...argument.description ? { description: argument.description } : {} }
|
|
1537
|
-
])
|
|
1538
|
-
);
|
|
1539
|
-
return {
|
|
1540
|
-
type: "object",
|
|
1541
|
-
properties,
|
|
1542
|
-
required: (definition.arguments ?? []).filter((argument) => argument.required).map((argument) => argument.name),
|
|
1543
|
-
additionalProperties: false
|
|
1209
|
+
} : {},
|
|
1210
|
+
...options.extensions ? { extensions: options.extensions } : {},
|
|
1211
|
+
tools,
|
|
1212
|
+
resources,
|
|
1213
|
+
resourceTemplates,
|
|
1214
|
+
prompts
|
|
1544
1215
|
};
|
|
1216
|
+
return { ...body, hash: sha256(body) };
|
|
1545
1217
|
}
|
|
1546
|
-
function
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
tools = /* @__PURE__ */ new Map();
|
|
1552
|
-
resources = /* @__PURE__ */ new Map();
|
|
1553
|
-
resourceTemplates = /* @__PURE__ */ new Map();
|
|
1554
|
-
prompts = /* @__PURE__ */ new Map();
|
|
1555
|
-
constructor() {
|
|
1556
|
-
this.ajv = new import_ajv.default({ allErrors: true, strict: false, allowUnionTypes: true });
|
|
1557
|
-
(0, import_ajv_formats.default)(this.ajv);
|
|
1218
|
+
function validateManifest(value) {
|
|
1219
|
+
assertObject("McpFn manifest", value);
|
|
1220
|
+
const manifest = value;
|
|
1221
|
+
if (manifest.formatVersion !== 1) {
|
|
1222
|
+
throw new McpFnValidationError("Unsupported McpFn manifest formatVersion");
|
|
1558
1223
|
}
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
if (
|
|
1570
|
-
throw new McpFnValidationError(
|
|
1571
|
-
`Tool ${definition.name} requires a handler function`
|
|
1572
|
-
);
|
|
1573
|
-
}
|
|
1574
|
-
if (definition.inputSchema.type !== "object") {
|
|
1575
|
-
throw new McpFnValidationError(
|
|
1576
|
-
`Tool ${definition.name} inputSchema must be an object schema`
|
|
1577
|
-
);
|
|
1224
|
+
if (!manifest.server || typeof manifest.server.name !== "string" || !manifest.server.name.trim() || typeof manifest.server.version !== "string" || !manifest.server.version.trim()) throw new McpFnValidationError("Manifest server name and version are required");
|
|
1225
|
+
if (manifest.server.instructions !== void 0 && typeof manifest.server.instructions !== "string") throw new McpFnValidationError("Manifest server instructions must be a string");
|
|
1226
|
+
if (!Array.isArray(manifest.tools)) {
|
|
1227
|
+
throw new McpFnValidationError("Manifest tools must be an array");
|
|
1228
|
+
}
|
|
1229
|
+
for (const [label, entries] of [
|
|
1230
|
+
["resources", manifest.resources],
|
|
1231
|
+
["resourceTemplates", manifest.resourceTemplates],
|
|
1232
|
+
["prompts", manifest.prompts]
|
|
1233
|
+
]) {
|
|
1234
|
+
if (entries !== void 0 && !Array.isArray(entries)) {
|
|
1235
|
+
throw new McpFnValidationError(`Manifest ${label} must be an array`);
|
|
1578
1236
|
}
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1237
|
+
}
|
|
1238
|
+
if (manifest.protocolVersions !== void 0 && (!Array.isArray(manifest.protocolVersions) || manifest.protocolVersions.some((version) => typeof version !== "string" || !version))) throw new McpFnValidationError("Manifest protocolVersions must contain non-empty strings");
|
|
1239
|
+
if (manifest.protocolVersions) assertSortedUnique("Manifest protocolVersions", manifest.protocolVersions);
|
|
1240
|
+
if (manifest.transports !== void 0 && (!Array.isArray(manifest.transports) || manifest.transports.some((transport) => transport !== "stdio" && transport !== "streamable-http"))) throw new McpFnValidationError("Manifest contains an unsupported transport");
|
|
1241
|
+
if (manifest.transports) assertSortedUnique("Manifest transports", manifest.transports);
|
|
1242
|
+
if (manifest.capabilities !== void 0) {
|
|
1243
|
+
const parsed = import_types.ServerCapabilitiesSchema.safeParse(manifest.capabilities);
|
|
1244
|
+
if (!parsed.success) throw new McpFnValidationError("Manifest capabilities are invalid");
|
|
1245
|
+
}
|
|
1246
|
+
if (manifest.clientRequirements !== void 0) {
|
|
1247
|
+
assertObject("Manifest clientRequirements", manifest.clientRequirements);
|
|
1248
|
+
if (manifest.clientRequirements.sampling !== void 0 && typeof manifest.clientRequirements.sampling !== "boolean") throw new McpFnValidationError("Manifest sampling requirement must be a boolean");
|
|
1249
|
+
if (manifest.clientRequirements.roots !== void 0 && typeof manifest.clientRequirements.roots !== "boolean") throw new McpFnValidationError("Manifest roots requirement must be a boolean");
|
|
1250
|
+
if (manifest.clientRequirements.elicitation !== void 0 && (!Array.isArray(manifest.clientRequirements.elicitation) || manifest.clientRequirements.elicitation.some((mode) => mode !== "form" && mode !== "url"))) throw new McpFnValidationError("Manifest elicitation requirements are invalid");
|
|
1251
|
+
if (manifest.clientRequirements.elicitation) {
|
|
1252
|
+
assertSortedUnique(
|
|
1253
|
+
"Manifest elicitation requirements",
|
|
1254
|
+
manifest.clientRequirements.elicitation
|
|
1582
1255
|
);
|
|
1583
1256
|
}
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1257
|
+
}
|
|
1258
|
+
assertMetadata("Manifest extensions", manifest.extensions);
|
|
1259
|
+
const ajv = new import_ajv2.default({ allErrors: true, strict: false, allowUnionTypes: true });
|
|
1260
|
+
(0, import_ajv_formats2.default)(ajv);
|
|
1261
|
+
for (const tool of manifest.tools) {
|
|
1262
|
+
assertObject("Every manifest tool", tool);
|
|
1263
|
+
assertName2("Every manifest tool", tool.name);
|
|
1264
|
+
if (typeof tool.description !== "string" || !tool.description.trim()) {
|
|
1265
|
+
throw new McpFnValidationError(`Manifest tool ${tool.name} requires a description`);
|
|
1589
1266
|
}
|
|
1590
|
-
if (
|
|
1591
|
-
throw new McpFnValidationError(
|
|
1592
|
-
`Tool ${definition.name} declares taskSupport=${taskSupport} but has no taskHandler`
|
|
1593
|
-
);
|
|
1267
|
+
if (!tool.inputSchema || tool.inputSchema.type !== "object") {
|
|
1268
|
+
throw new McpFnValidationError(`Manifest tool ${tool.name} requires an object inputSchema`);
|
|
1594
1269
|
}
|
|
1595
|
-
if (
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1270
|
+
if (tool.outputSchema !== void 0) {
|
|
1271
|
+
assertObject(`Manifest tool ${tool.name} outputSchema`, tool.outputSchema);
|
|
1272
|
+
if (tool.outputSchema.type !== "object") {
|
|
1273
|
+
throw new McpFnValidationError(`Manifest tool ${tool.name} requires an object outputSchema`);
|
|
1274
|
+
}
|
|
1599
1275
|
}
|
|
1600
|
-
let validateInput;
|
|
1601
|
-
let validateOutput;
|
|
1602
1276
|
try {
|
|
1603
|
-
|
|
1604
|
-
|
|
1277
|
+
ajv.compile(tool.inputSchema);
|
|
1278
|
+
if (tool.outputSchema !== void 0) ajv.compile(tool.outputSchema);
|
|
1605
1279
|
} catch (error) {
|
|
1606
1280
|
throw new McpFnValidationError(
|
|
1607
|
-
`
|
|
1281
|
+
`Manifest tool ${tool.name} contains an invalid JSON Schema`,
|
|
1608
1282
|
{ cause: error instanceof Error ? error.message : String(error) }
|
|
1609
1283
|
);
|
|
1610
1284
|
}
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
});
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
registerAll(definitions) {
|
|
1619
|
-
for (const definition of definitions) this.register(definition);
|
|
1620
|
-
return this;
|
|
1621
|
-
}
|
|
1622
|
-
registerResource(definition) {
|
|
1623
|
-
assertUri("resource", definition.uri);
|
|
1624
|
-
assertName("resource", definition.name);
|
|
1625
|
-
if (typeof definition.read !== "function") {
|
|
1285
|
+
for (const [label, metadata] of [
|
|
1286
|
+
["annotations", tool.annotations],
|
|
1287
|
+
["execution", tool.execution],
|
|
1288
|
+
["metadata", tool.metadata]
|
|
1289
|
+
]) assertMetadata(`Manifest tool ${tool.name} ${label}`, metadata);
|
|
1290
|
+
const taskSupport = tool.execution?.taskSupport;
|
|
1291
|
+
if (taskSupport !== void 0 && taskSupport !== "forbidden" && taskSupport !== "optional" && taskSupport !== "required") {
|
|
1626
1292
|
throw new McpFnValidationError(
|
|
1627
|
-
`
|
|
1293
|
+
`Manifest tool ${tool.name} has invalid taskSupport=${String(taskSupport)}`
|
|
1628
1294
|
);
|
|
1629
1295
|
}
|
|
1630
|
-
if (this.resources.has(definition.uri)) {
|
|
1631
|
-
throw new McpFnValidationError(`Duplicate MCP resource: ${definition.uri}`);
|
|
1632
|
-
}
|
|
1633
|
-
assertSubscriptionCallbacks(
|
|
1634
|
-
"Resource",
|
|
1635
|
-
definition.name,
|
|
1636
|
-
definition.subscribe,
|
|
1637
|
-
definition.unsubscribe
|
|
1638
|
-
);
|
|
1639
|
-
this.resources.set(definition.uri, definition);
|
|
1640
|
-
return this;
|
|
1641
1296
|
}
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
);
|
|
1297
|
+
assertSortedBy("Manifest tool names", manifest.tools, (tool) => tool.name);
|
|
1298
|
+
for (const resource of manifest.resources ?? []) {
|
|
1299
|
+
assertObject("Every manifest resource", resource);
|
|
1300
|
+
assertName2("Every manifest resource", resource.name);
|
|
1301
|
+
try {
|
|
1302
|
+
new URL(resource.uri);
|
|
1303
|
+
} catch {
|
|
1304
|
+
throw new McpFnValidationError(`Manifest resource ${resource.name} has an invalid URI`);
|
|
1648
1305
|
}
|
|
1649
|
-
if (
|
|
1306
|
+
if (resource.subscribable !== void 0 && typeof resource.subscribable !== "boolean") {
|
|
1650
1307
|
throw new McpFnValidationError(
|
|
1651
|
-
`
|
|
1308
|
+
`Manifest resource ${resource.name} subscribable must be a boolean`
|
|
1652
1309
|
);
|
|
1653
1310
|
}
|
|
1654
|
-
|
|
1311
|
+
assertMetadata(`Manifest resource ${resource.name} metadata`, resource.metadata);
|
|
1312
|
+
}
|
|
1313
|
+
assertSortedBy("Manifest resource URIs", manifest.resources ?? [], (resource) => resource.uri);
|
|
1314
|
+
const resourceTemplateUris = [];
|
|
1315
|
+
for (const resource of manifest.resourceTemplates ?? []) {
|
|
1316
|
+
assertObject("Every manifest resource template", resource);
|
|
1317
|
+
assertName2("Every manifest resource template", resource.name);
|
|
1318
|
+
if (typeof resource.uriTemplate !== "string") {
|
|
1655
1319
|
throw new McpFnValidationError(
|
|
1656
|
-
`
|
|
1320
|
+
`Manifest resource template ${resource.name} has an invalid URI template`
|
|
1657
1321
|
);
|
|
1658
1322
|
}
|
|
1659
|
-
const unsupportedOperator = unsupportedUriTemplateOperator(
|
|
1323
|
+
const unsupportedOperator = unsupportedUriTemplateOperator(resource.uriTemplate);
|
|
1660
1324
|
if (unsupportedOperator) {
|
|
1661
1325
|
throw new McpFnValidationError(
|
|
1662
|
-
`
|
|
1326
|
+
`Manifest resource template ${resource.name} uses unsupported URI template operator ${unsupportedOperator}`
|
|
1663
1327
|
);
|
|
1664
1328
|
}
|
|
1665
1329
|
let template;
|
|
1666
1330
|
try {
|
|
1667
|
-
template = new
|
|
1668
|
-
} catch
|
|
1331
|
+
template = new import_uriTemplate2.UriTemplate(resource.uriTemplate);
|
|
1332
|
+
} catch {
|
|
1669
1333
|
throw new McpFnValidationError(
|
|
1670
|
-
`
|
|
1671
|
-
{ cause: error instanceof Error ? error.message : String(error) }
|
|
1334
|
+
`Manifest resource template ${resource.name} has an invalid URI template`
|
|
1672
1335
|
);
|
|
1673
1336
|
}
|
|
1674
1337
|
if (!template.variableNames.length) {
|
|
1675
1338
|
throw new McpFnValidationError(
|
|
1676
|
-
`
|
|
1339
|
+
`Manifest resource template ${resource.name} must contain at least one variable`
|
|
1677
1340
|
);
|
|
1678
1341
|
}
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
definition.name,
|
|
1682
|
-
definition.subscribe,
|
|
1683
|
-
definition.unsubscribe
|
|
1684
|
-
);
|
|
1685
|
-
if ([...this.resourceTemplates.values()].some(
|
|
1686
|
-
({ definition: registered }) => uriTemplatesOverlap(registered.uriTemplate, definition.uriTemplate)
|
|
1342
|
+
if (resourceTemplateUris.some(
|
|
1343
|
+
(uriTemplate) => uriTemplatesOverlap(uriTemplate, resource.uriTemplate)
|
|
1687
1344
|
)) {
|
|
1688
1345
|
throw new McpFnValidationError(
|
|
1689
|
-
`
|
|
1346
|
+
`Manifest contains an ambiguous resource URI template: ${resource.uriTemplate}`
|
|
1690
1347
|
);
|
|
1691
1348
|
}
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
throw new McpFnValidationError(
|
|
1695
|
-
`Resource template ${definition.name} has a completer for unknown variable ${name}`
|
|
1696
|
-
);
|
|
1697
|
-
}
|
|
1698
|
-
}
|
|
1699
|
-
this.resourceTemplates.set(definition.name, { definition, template });
|
|
1700
|
-
return this;
|
|
1701
|
-
}
|
|
1702
|
-
registerPrompt(definition) {
|
|
1703
|
-
assertName("prompt", definition.name);
|
|
1704
|
-
if (this.prompts.has(definition.name)) {
|
|
1705
|
-
throw new McpFnValidationError(`Duplicate MCP prompt: ${definition.name}`);
|
|
1706
|
-
}
|
|
1707
|
-
if (typeof definition.get !== "function") {
|
|
1349
|
+
resourceTemplateUris.push(resource.uriTemplate);
|
|
1350
|
+
if (resource.subscribable !== void 0 && typeof resource.subscribable !== "boolean") {
|
|
1708
1351
|
throw new McpFnValidationError(
|
|
1709
|
-
`
|
|
1352
|
+
`Manifest resource template ${resource.name} subscribable must be a boolean`
|
|
1710
1353
|
);
|
|
1711
1354
|
}
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1355
|
+
assertMetadata(`Manifest resource template ${resource.name} metadata`, resource.metadata);
|
|
1356
|
+
}
|
|
1357
|
+
assertSortedBy(
|
|
1358
|
+
"Manifest resource template names",
|
|
1359
|
+
manifest.resourceTemplates ?? [],
|
|
1360
|
+
(resource) => resource.name
|
|
1361
|
+
);
|
|
1362
|
+
for (const prompt of manifest.prompts ?? []) {
|
|
1363
|
+
assertObject("Every manifest prompt", prompt);
|
|
1364
|
+
assertName2("Every manifest prompt", prompt.name);
|
|
1365
|
+
assertMetadata(`Manifest prompt ${prompt.name} metadata`, prompt.metadata);
|
|
1366
|
+
if (prompt.arguments !== void 0 && !Array.isArray(prompt.arguments)) {
|
|
1367
|
+
throw new McpFnValidationError(`Manifest prompt ${prompt.name} arguments must be an array`);
|
|
1716
1368
|
}
|
|
1717
|
-
|
|
1718
|
-
|
|
1369
|
+
const argumentNames = [];
|
|
1370
|
+
for (const argument of prompt.arguments ?? []) {
|
|
1371
|
+
assertObject(`Manifest prompt ${prompt.name} argument`, argument);
|
|
1372
|
+
assertName2(`Manifest prompt ${prompt.name} argument`, argument.name);
|
|
1719
1373
|
if (argument.description !== void 0 && typeof argument.description !== "string") {
|
|
1720
1374
|
throw new McpFnValidationError(
|
|
1721
|
-
`
|
|
1375
|
+
`Manifest prompt ${prompt.name} argument ${argument.name} description must be a string`
|
|
1722
1376
|
);
|
|
1723
1377
|
}
|
|
1724
1378
|
if (argument.required !== void 0 && typeof argument.required !== "boolean") {
|
|
1725
1379
|
throw new McpFnValidationError(
|
|
1726
|
-
`
|
|
1380
|
+
`Manifest prompt ${prompt.name} argument ${argument.name} required must be a boolean`
|
|
1381
|
+
);
|
|
1382
|
+
}
|
|
1383
|
+
argumentNames.push(argument.name);
|
|
1384
|
+
}
|
|
1385
|
+
if (new Set(argumentNames).size !== argumentNames.length) {
|
|
1386
|
+
throw new McpFnValidationError(`Manifest prompt ${prompt.name} has duplicate arguments`);
|
|
1387
|
+
}
|
|
1388
|
+
if (prompt.argumentsSchema !== void 0) {
|
|
1389
|
+
assertObject(`Manifest prompt ${prompt.name} argumentsSchema`, prompt.argumentsSchema);
|
|
1390
|
+
if (prompt.argumentsSchema.type !== "object") {
|
|
1391
|
+
throw new McpFnValidationError(
|
|
1392
|
+
`Manifest prompt ${prompt.name} argumentsSchema must be an object schema`
|
|
1393
|
+
);
|
|
1394
|
+
}
|
|
1395
|
+
try {
|
|
1396
|
+
ajv.compile(prompt.argumentsSchema);
|
|
1397
|
+
} catch {
|
|
1398
|
+
throw new McpFnValidationError(
|
|
1399
|
+
`Manifest prompt ${prompt.name} has an invalid arguments JSON Schema`
|
|
1400
|
+
);
|
|
1401
|
+
}
|
|
1402
|
+
assertPromptSchemaSupportsStringValues(
|
|
1403
|
+
prompt.argumentsSchema,
|
|
1404
|
+
`Manifest prompt ${prompt.name} argumentsSchema`
|
|
1405
|
+
);
|
|
1406
|
+
if (prompt.arguments) {
|
|
1407
|
+
const declared = prompt.arguments.map(({ name, required }) => ({ name, required: required === true })).sort((left, right) => compareCodeUnits(left.name, right.name));
|
|
1408
|
+
const schemaDeclared = schemaPromptArguments(prompt).map(
|
|
1409
|
+
({ name, required }) => ({ name, required: required === true })
|
|
1410
|
+
);
|
|
1411
|
+
if (JSON.stringify(declared) !== JSON.stringify(schemaDeclared)) {
|
|
1412
|
+
throw new McpFnValidationError(
|
|
1413
|
+
`Manifest prompt ${prompt.name} arguments and argumentsSchema disagree`
|
|
1414
|
+
);
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
assertSortedBy("Manifest prompt names", manifest.prompts ?? [], (prompt) => prompt.name);
|
|
1420
|
+
if (typeof manifest.hash !== "string" || !/^[a-f0-9]{64}$/.test(manifest.hash)) {
|
|
1421
|
+
throw new McpFnValidationError("Manifest hash must be a lowercase SHA-256 digest");
|
|
1422
|
+
}
|
|
1423
|
+
const { hash: _ignored, ...body } = manifest;
|
|
1424
|
+
const expectedHash = sha256(body);
|
|
1425
|
+
if (manifest.hash !== expectedHash) {
|
|
1426
|
+
throw new McpFnValidationError("Manifest hash does not match its contents", {
|
|
1427
|
+
expectedHash,
|
|
1428
|
+
actualHash: manifest.hash
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1431
|
+
return manifest;
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
// src/server.ts
|
|
1435
|
+
var import_server = require("@modelcontextprotocol/sdk/server/index.js");
|
|
1436
|
+
var import_stdio = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
1437
|
+
var import_webStandardStreamableHttp = require("@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js");
|
|
1438
|
+
var import_types2 = require("@modelcontextprotocol/sdk/types.js");
|
|
1439
|
+
function mergeCapabilities(base, extra) {
|
|
1440
|
+
const merged = { ...base, ...extra };
|
|
1441
|
+
for (const key of ["prompts", "resources", "tools", "tasks"]) {
|
|
1442
|
+
if (base[key] || extra?.[key]) {
|
|
1443
|
+
merged[key] = {
|
|
1444
|
+
...base[key],
|
|
1445
|
+
...extra?.[key]
|
|
1446
|
+
};
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
return merged;
|
|
1450
|
+
}
|
|
1451
|
+
function resolveMcpFnServerCapabilities(registry, additionalCapabilities, includeTaskOperations) {
|
|
1452
|
+
const registryCapabilities = registry.capabilities();
|
|
1453
|
+
if (registryCapabilities.tasks && includeTaskOperations) {
|
|
1454
|
+
registryCapabilities.tasks = {
|
|
1455
|
+
...registryCapabilities.tasks,
|
|
1456
|
+
list: {},
|
|
1457
|
+
cancel: {}
|
|
1458
|
+
};
|
|
1459
|
+
}
|
|
1460
|
+
return mergeCapabilities(registryCapabilities, additionalCapabilities);
|
|
1461
|
+
}
|
|
1462
|
+
function page(values, cursor, pageSize) {
|
|
1463
|
+
let offset = 0;
|
|
1464
|
+
if (cursor !== void 0) {
|
|
1465
|
+
const match = /^mcpfn:(\d+)$/.exec(cursor);
|
|
1466
|
+
if (!match) throw new import_types2.McpError(import_types2.ErrorCode.InvalidParams, "Invalid McpFn pagination cursor");
|
|
1467
|
+
offset = Number(match[1]);
|
|
1468
|
+
if (!Number.isSafeInteger(offset) || offset > values.length) {
|
|
1469
|
+
throw new import_types2.McpError(import_types2.ErrorCode.InvalidParams, "Expired McpFn pagination cursor");
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
const selected = values.slice(offset, offset + pageSize);
|
|
1473
|
+
const nextOffset = offset + selected.length;
|
|
1474
|
+
return {
|
|
1475
|
+
values: selected,
|
|
1476
|
+
...nextOffset < values.length ? { nextCursor: `mcpfn:${nextOffset}` } : {}
|
|
1477
|
+
};
|
|
1478
|
+
}
|
|
1479
|
+
var McpFnServer = class {
|
|
1480
|
+
registry;
|
|
1481
|
+
info;
|
|
1482
|
+
protocol;
|
|
1483
|
+
capabilities;
|
|
1484
|
+
contextFactory;
|
|
1485
|
+
toolVisibility;
|
|
1486
|
+
manifestOptions;
|
|
1487
|
+
pageSize;
|
|
1488
|
+
connected = false;
|
|
1489
|
+
constructor(options) {
|
|
1490
|
+
this.info = options.info;
|
|
1491
|
+
this.registry = options.registry;
|
|
1492
|
+
assertMcpAppContracts(this.registry);
|
|
1493
|
+
this.contextFactory = options.context ?? (() => void 0);
|
|
1494
|
+
this.toolVisibility = options.toolVisibility;
|
|
1495
|
+
this.pageSize = options.pageSize ?? 100;
|
|
1496
|
+
if (!Number.isInteger(this.pageSize) || this.pageSize < 1) {
|
|
1497
|
+
throw new Error("McpFn pageSize must be a positive integer");
|
|
1498
|
+
}
|
|
1499
|
+
const hasTaskTools = Boolean(this.registry.capabilities().tasks);
|
|
1500
|
+
if (hasTaskTools && !options.taskStore) {
|
|
1501
|
+
throw new Error("Task-capable McpFn tools require a taskStore");
|
|
1502
|
+
}
|
|
1503
|
+
const declaredCapabilities = mergeCapabilities(
|
|
1504
|
+
options.capabilities ?? {},
|
|
1505
|
+
options.additionalCapabilities
|
|
1506
|
+
);
|
|
1507
|
+
this.capabilities = resolveMcpFnServerCapabilities(
|
|
1508
|
+
this.registry,
|
|
1509
|
+
declaredCapabilities,
|
|
1510
|
+
Boolean(options.taskStore)
|
|
1511
|
+
);
|
|
1512
|
+
this.manifestOptions = {
|
|
1513
|
+
protocolVersions: options.protocolVersions,
|
|
1514
|
+
transports: options.transports,
|
|
1515
|
+
extensions: options.extensions,
|
|
1516
|
+
capabilities: this.capabilities,
|
|
1517
|
+
clientRequirements: options.clientRequirements
|
|
1518
|
+
};
|
|
1519
|
+
const { instructions, ...implementation } = options.info;
|
|
1520
|
+
this.protocol = new import_server.Server(
|
|
1521
|
+
implementation,
|
|
1522
|
+
{
|
|
1523
|
+
capabilities: this.capabilities,
|
|
1524
|
+
instructions,
|
|
1525
|
+
taskStore: options.taskStore,
|
|
1526
|
+
taskMessageQueue: options.taskMessageQueue,
|
|
1527
|
+
defaultTaskPollInterval: options.defaultTaskPollInterval,
|
|
1528
|
+
maxTaskQueueSize: options.maxTaskQueueSize,
|
|
1529
|
+
enforceStrictCapabilities: options.enforceStrictCapabilities,
|
|
1530
|
+
debouncedNotificationMethods: [
|
|
1531
|
+
"notifications/tools/list_changed",
|
|
1532
|
+
"notifications/resources/list_changed",
|
|
1533
|
+
"notifications/prompts/list_changed"
|
|
1534
|
+
]
|
|
1535
|
+
}
|
|
1536
|
+
);
|
|
1537
|
+
this.installHandlers();
|
|
1538
|
+
}
|
|
1539
|
+
installHandlers() {
|
|
1540
|
+
if (this.capabilities.tools) {
|
|
1541
|
+
this.protocol.setRequestHandler(import_types2.ListToolsRequestSchema, async (request, extra) => {
|
|
1542
|
+
const tools = this.registry.listTools();
|
|
1543
|
+
const visibleTools = this.toolVisibility ? await this.filterVisibleTools(tools, await this.contextFactory(extra), extra) : tools;
|
|
1544
|
+
const result = page(visibleTools, request.params?.cursor, this.pageSize);
|
|
1545
|
+
return { tools: result.values, ...result.nextCursor ? { nextCursor: result.nextCursor } : {} };
|
|
1546
|
+
});
|
|
1547
|
+
this.protocol.setRequestHandler(import_types2.CallToolRequestSchema, async (request, extra) => {
|
|
1548
|
+
const context = await this.contextFactory(extra);
|
|
1549
|
+
const listedTool = this.registry.listTools().find((tool) => tool.name === request.params.name);
|
|
1550
|
+
if (!listedTool || this.toolVisibility && !await this.toolVisibility({ tool: listedTool, context, extra })) {
|
|
1551
|
+
throw new import_types2.McpError(import_types2.ErrorCode.MethodNotFound, `Tool ${request.params.name} not found`);
|
|
1552
|
+
}
|
|
1553
|
+
const taskSupport = this.registry.taskSupport(request.params.name);
|
|
1554
|
+
const isTaskRequest = Boolean(request.params.task);
|
|
1555
|
+
if (taskSupport === "required" && !isTaskRequest) {
|
|
1556
|
+
throw new import_types2.McpError(
|
|
1557
|
+
import_types2.ErrorCode.MethodNotFound,
|
|
1558
|
+
`Tool ${request.params.name} requires task augmentation`
|
|
1559
|
+
);
|
|
1560
|
+
}
|
|
1561
|
+
if (taskSupport === "forbidden" && isTaskRequest) {
|
|
1562
|
+
throw new import_types2.McpError(
|
|
1563
|
+
import_types2.ErrorCode.MethodNotFound,
|
|
1564
|
+
`Tool ${request.params.name} does not support task augmentation`
|
|
1565
|
+
);
|
|
1566
|
+
}
|
|
1567
|
+
try {
|
|
1568
|
+
if (isTaskRequest) {
|
|
1569
|
+
if (!extra.taskStore) throw new Error("No task store is available");
|
|
1570
|
+
return await this.registry.createToolTask(
|
|
1571
|
+
request.params.name,
|
|
1572
|
+
request.params.arguments,
|
|
1573
|
+
context,
|
|
1574
|
+
extra
|
|
1575
|
+
);
|
|
1576
|
+
}
|
|
1577
|
+
return await this.registry.callTool(
|
|
1578
|
+
request.params.name,
|
|
1579
|
+
request.params.arguments,
|
|
1580
|
+
context,
|
|
1581
|
+
extra
|
|
1582
|
+
);
|
|
1583
|
+
} catch (error) {
|
|
1584
|
+
if (isTaskRequest) throw error;
|
|
1585
|
+
return errorResult(error, {
|
|
1586
|
+
includeStructuredContent: !this.registry.hasOutputSchema(request.params.name)
|
|
1587
|
+
});
|
|
1588
|
+
}
|
|
1589
|
+
});
|
|
1590
|
+
}
|
|
1591
|
+
if (this.capabilities.resources) {
|
|
1592
|
+
this.protocol.setRequestHandler(import_types2.ListResourcesRequestSchema, async (request, extra) => {
|
|
1593
|
+
const context = await this.contextFactory(extra);
|
|
1594
|
+
const result = page(
|
|
1595
|
+
await this.registry.listResources(context, extra),
|
|
1596
|
+
request.params?.cursor,
|
|
1597
|
+
this.pageSize
|
|
1598
|
+
);
|
|
1599
|
+
return {
|
|
1600
|
+
resources: result.values,
|
|
1601
|
+
...result.nextCursor ? { nextCursor: result.nextCursor } : {}
|
|
1602
|
+
};
|
|
1603
|
+
});
|
|
1604
|
+
this.protocol.setRequestHandler(import_types2.ListResourceTemplatesRequestSchema, async (request) => {
|
|
1605
|
+
const result = page(
|
|
1606
|
+
this.registry.listResourceTemplates(),
|
|
1607
|
+
request.params?.cursor,
|
|
1608
|
+
this.pageSize
|
|
1727
1609
|
);
|
|
1610
|
+
return {
|
|
1611
|
+
resourceTemplates: result.values,
|
|
1612
|
+
...result.nextCursor ? { nextCursor: result.nextCursor } : {}
|
|
1613
|
+
};
|
|
1614
|
+
});
|
|
1615
|
+
this.protocol.setRequestHandler(import_types2.ReadResourceRequestSchema, async (request, extra) => {
|
|
1616
|
+
const context = await this.contextFactory(extra);
|
|
1617
|
+
return this.registry.readResource(request.params.uri, context, extra);
|
|
1618
|
+
});
|
|
1619
|
+
if (this.capabilities.resources.subscribe) {
|
|
1620
|
+
this.protocol.setRequestHandler(import_types2.SubscribeRequestSchema, async (request, extra) => {
|
|
1621
|
+
const context = await this.contextFactory(extra);
|
|
1622
|
+
await this.registry.changeSubscription(request.params.uri, true, context, extra);
|
|
1623
|
+
return {};
|
|
1624
|
+
});
|
|
1625
|
+
this.protocol.setRequestHandler(import_types2.UnsubscribeRequestSchema, async (request, extra) => {
|
|
1626
|
+
const context = await this.contextFactory(extra);
|
|
1627
|
+
await this.registry.changeSubscription(request.params.uri, false, context, extra);
|
|
1628
|
+
return {};
|
|
1629
|
+
});
|
|
1728
1630
|
}
|
|
1729
1631
|
}
|
|
1730
|
-
if (
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
assertPromptSchemaSupportsStringValues(
|
|
1746
|
-
definition.argumentsSchema,
|
|
1747
|
-
`Prompt ${definition.name} argumentsSchema`
|
|
1748
|
-
);
|
|
1749
|
-
}
|
|
1750
|
-
const argumentNames = (promptArguments(definition) ?? []).map(({ name }) => name);
|
|
1751
|
-
if (new Set(argumentNames).size !== argumentNames.length) {
|
|
1752
|
-
throw new McpFnValidationError(`Prompt ${definition.name} has duplicate arguments`);
|
|
1753
|
-
}
|
|
1754
|
-
if (definition.arguments && definition.argumentsSchema) {
|
|
1755
|
-
const declared = definition.arguments.map(({ name, required }) => ({ name, required: required === true })).sort((left, right) => compareCodeUnits(left.name, right.name));
|
|
1756
|
-
const schemaDeclared = schemaPromptArguments(definition).map(
|
|
1757
|
-
({ name, required }) => ({ name, required: required === true })
|
|
1758
|
-
);
|
|
1759
|
-
if (JSON.stringify(declared) !== JSON.stringify(schemaDeclared)) {
|
|
1760
|
-
throw new McpFnValidationError(
|
|
1761
|
-
`Prompt ${definition.name} arguments and argumentsSchema disagree`
|
|
1632
|
+
if (this.capabilities.prompts) {
|
|
1633
|
+
this.protocol.setRequestHandler(import_types2.ListPromptsRequestSchema, async (request) => {
|
|
1634
|
+
const result = page(this.registry.listPrompts(), request.params?.cursor, this.pageSize);
|
|
1635
|
+
return {
|
|
1636
|
+
prompts: result.values,
|
|
1637
|
+
...result.nextCursor ? { nextCursor: result.nextCursor } : {}
|
|
1638
|
+
};
|
|
1639
|
+
});
|
|
1640
|
+
this.protocol.setRequestHandler(import_types2.GetPromptRequestSchema, async (request, extra) => {
|
|
1641
|
+
const context = await this.contextFactory(extra);
|
|
1642
|
+
return this.registry.getPrompt(
|
|
1643
|
+
request.params.name,
|
|
1644
|
+
request.params.arguments,
|
|
1645
|
+
context,
|
|
1646
|
+
extra
|
|
1762
1647
|
);
|
|
1763
|
-
}
|
|
1648
|
+
});
|
|
1764
1649
|
}
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1650
|
+
if (this.capabilities.completions) {
|
|
1651
|
+
this.protocol.setRequestHandler(import_types2.CompleteRequestSchema, async (request, extra) => {
|
|
1652
|
+
const context = await this.contextFactory(extra);
|
|
1653
|
+
return this.registry.complete(
|
|
1654
|
+
request.params.ref,
|
|
1655
|
+
request.params.argument,
|
|
1656
|
+
request.params.context?.arguments,
|
|
1657
|
+
context,
|
|
1658
|
+
extra
|
|
1769
1659
|
);
|
|
1770
|
-
}
|
|
1660
|
+
});
|
|
1771
1661
|
}
|
|
1772
|
-
this.prompts.set(definition.name, { definition, validateArguments });
|
|
1773
|
-
return this;
|
|
1774
|
-
}
|
|
1775
|
-
has(name) {
|
|
1776
|
-
return this.tools.has(name);
|
|
1777
|
-
}
|
|
1778
|
-
hasOutputSchema(name) {
|
|
1779
|
-
return Boolean(this.tools.get(name)?.validateOutput);
|
|
1780
|
-
}
|
|
1781
|
-
definitions() {
|
|
1782
|
-
return [...this.tools.values()].map(({ definition }) => definition).sort((left, right) => compareCodeUnits(left.name, right.name));
|
|
1783
|
-
}
|
|
1784
|
-
resourceDefinitions() {
|
|
1785
|
-
return [...this.resources.values()].sort((left, right) => compareCodeUnits(left.uri, right.uri));
|
|
1786
|
-
}
|
|
1787
|
-
resourceTemplateDefinitions() {
|
|
1788
|
-
return [...this.resourceTemplates.values()].map(({ definition }) => definition).sort((left, right) => compareCodeUnits(left.name, right.name));
|
|
1789
1662
|
}
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
const capabilities = {};
|
|
1795
|
-
if (this.tools.size) capabilities.tools = { listChanged: options.listChanged ?? true };
|
|
1796
|
-
if (this.resources.size || this.resourceTemplates.size) {
|
|
1797
|
-
capabilities.resources = {
|
|
1798
|
-
listChanged: options.listChanged ?? true,
|
|
1799
|
-
subscribe: [...this.resources.values()].some((value) => value.subscribe) || [...this.resourceTemplates.values()].some(({ definition }) => definition.subscribe)
|
|
1800
|
-
};
|
|
1801
|
-
}
|
|
1802
|
-
if (this.prompts.size) capabilities.prompts = { listChanged: options.listChanged ?? true };
|
|
1803
|
-
if ([...this.resourceTemplates.values()].some(({ definition }) => definition.complete) || [...this.prompts.values()].some(({ definition }) => definition.complete)) capabilities.completions = {};
|
|
1804
|
-
if (this.definitions().some((definition) => {
|
|
1805
|
-
const support = definition.execution?.taskSupport;
|
|
1806
|
-
return support === "optional" || support === "required";
|
|
1807
|
-
})) capabilities.tasks = { requests: { tools: { call: {} } } };
|
|
1808
|
-
return capabilities;
|
|
1663
|
+
async filterVisibleTools(tools, context, extra) {
|
|
1664
|
+
if (!this.toolVisibility) return tools;
|
|
1665
|
+
const decisions = await Promise.all(tools.map((tool) => this.toolVisibility({ tool, context, extra })));
|
|
1666
|
+
return tools.filter((_, index) => decisions[index]);
|
|
1809
1667
|
}
|
|
1810
|
-
|
|
1811
|
-
return this.
|
|
1812
|
-
name: definition.name,
|
|
1813
|
-
...definition.title ? { title: definition.title } : {},
|
|
1814
|
-
description: definition.description,
|
|
1815
|
-
inputSchema: definition.inputSchema,
|
|
1816
|
-
...definition.outputSchema ? { outputSchema: definition.outputSchema } : {},
|
|
1817
|
-
...definition.annotations ? { annotations: definition.annotations } : {},
|
|
1818
|
-
...definition.execution ? { execution: definition.execution } : {},
|
|
1819
|
-
...definition.icons ? { icons: definition.icons } : {},
|
|
1820
|
-
...definition.metadata ? { _meta: definition.metadata } : {}
|
|
1821
|
-
}));
|
|
1668
|
+
manifest() {
|
|
1669
|
+
return createManifest(this.info, this.registry, this.manifestOptions);
|
|
1822
1670
|
}
|
|
1823
|
-
async
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
...definition.title ? { title: definition.title } : {},
|
|
1828
|
-
...definition.description ? { description: definition.description } : {},
|
|
1829
|
-
...definition.mimeType ? { mimeType: definition.mimeType } : {},
|
|
1830
|
-
...definition.annotations ? { annotations: definition.annotations } : {},
|
|
1831
|
-
...definition.icons ? { icons: definition.icons } : {},
|
|
1832
|
-
...definition.metadata ? { _meta: definition.metadata } : {}
|
|
1833
|
-
}));
|
|
1834
|
-
for (const { definition } of this.resourceTemplates.values()) {
|
|
1835
|
-
if (definition.list) resources.push(...(await definition.list(context, extra)).resources);
|
|
1836
|
-
}
|
|
1837
|
-
return resources.sort((left, right) => compareCodeUnits(left.uri, right.uri));
|
|
1671
|
+
async connect(transport) {
|
|
1672
|
+
if (this.connected) throw new Error("McpFnServer is already connected to a transport");
|
|
1673
|
+
await this.protocol.connect(transport);
|
|
1674
|
+
this.connected = true;
|
|
1838
1675
|
}
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
uriTemplate: definition.uriTemplate,
|
|
1842
|
-
name: definition.name,
|
|
1843
|
-
...definition.title ? { title: definition.title } : {},
|
|
1844
|
-
...definition.description ? { description: definition.description } : {},
|
|
1845
|
-
...definition.mimeType ? { mimeType: definition.mimeType } : {},
|
|
1846
|
-
...definition.annotations ? { annotations: definition.annotations } : {},
|
|
1847
|
-
...definition.icons ? { icons: definition.icons } : {},
|
|
1848
|
-
...definition.metadata ? { _meta: definition.metadata } : {}
|
|
1849
|
-
}));
|
|
1676
|
+
async serveStdio() {
|
|
1677
|
+
await this.connect(new import_stdio.StdioServerTransport());
|
|
1850
1678
|
}
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
...definition.description ? { description: definition.description } : {},
|
|
1856
|
-
...promptArguments(definition) ? { arguments: promptArguments(definition) } : {},
|
|
1857
|
-
...definition.icons ? { icons: definition.icons } : {},
|
|
1858
|
-
...definition.metadata ? { _meta: definition.metadata } : {}
|
|
1859
|
-
}));
|
|
1679
|
+
async createWebStandardHandler(options = {}) {
|
|
1680
|
+
const transport = new import_webStandardStreamableHttp.WebStandardStreamableHTTPServerTransport(options);
|
|
1681
|
+
await this.connect(transport);
|
|
1682
|
+
return (request, handleOptions) => transport.handleRequest(request, handleOptions);
|
|
1860
1683
|
}
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
if (!registered.validateInput(normalizedArgs)) {
|
|
1864
|
-
return {
|
|
1865
|
-
args: normalizedArgs && typeof normalizedArgs === "object" && !Array.isArray(normalizedArgs) ? normalizedArgs : {},
|
|
1866
|
-
issues: formatErrors(registered.validateInput.errors)
|
|
1867
|
-
};
|
|
1868
|
-
}
|
|
1869
|
-
return { args: normalizedArgs };
|
|
1684
|
+
sample(params, options) {
|
|
1685
|
+
return this.protocol.createMessage(params, options);
|
|
1870
1686
|
}
|
|
1871
|
-
|
|
1872
|
-
const
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
return this.finalizeResult(
|
|
1878
|
-
registered,
|
|
1879
|
-
await registered.definition.handleInvalidArguments(
|
|
1880
|
-
normalized.args,
|
|
1881
|
-
normalized.issues,
|
|
1882
|
-
context,
|
|
1883
|
-
extra
|
|
1884
|
-
)
|
|
1885
|
-
);
|
|
1886
|
-
}
|
|
1887
|
-
throw new McpFnValidationError(`Invalid arguments for ${name}`, { issues: normalized.issues });
|
|
1888
|
-
}
|
|
1889
|
-
return this.finalizeResult(
|
|
1890
|
-
registered,
|
|
1891
|
-
await registered.definition.handler(normalized.args, context, extra)
|
|
1687
|
+
sampleForRequest(extra, params, options) {
|
|
1688
|
+
const schema = params.tools?.length ? import_types2.CreateMessageResultWithToolsSchema : import_types2.CreateMessageResultSchema;
|
|
1689
|
+
return extra.sendRequest(
|
|
1690
|
+
{ method: "sampling/createMessage", params },
|
|
1691
|
+
schema,
|
|
1692
|
+
options
|
|
1892
1693
|
);
|
|
1893
1694
|
}
|
|
1894
|
-
|
|
1895
|
-
return this.
|
|
1695
|
+
elicit(params, options) {
|
|
1696
|
+
return this.protocol.elicitInput(params, options);
|
|
1896
1697
|
}
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
throw new McpFnValidationError(`Invalid arguments for ${name}`, { issues: normalized.issues });
|
|
1903
|
-
}
|
|
1904
|
-
if (!registered.definition.taskHandler) {
|
|
1905
|
-
throw new McpFnValidationError(`Tool ${name} does not support task execution`);
|
|
1906
|
-
}
|
|
1907
|
-
const taskStore = new Proxy(extra.taskStore, {
|
|
1908
|
-
get: (target, property, receiver) => {
|
|
1909
|
-
if (property === "storeTaskResult") {
|
|
1910
|
-
return async (taskId, status, result) => target.storeTaskResult(
|
|
1911
|
-
taskId,
|
|
1912
|
-
status,
|
|
1913
|
-
this.finalizeResult(registered, result)
|
|
1914
|
-
);
|
|
1915
|
-
}
|
|
1916
|
-
const value = Reflect.get(target, property, receiver);
|
|
1917
|
-
return typeof value === "function" ? value.bind(target) : value;
|
|
1918
|
-
}
|
|
1919
|
-
});
|
|
1920
|
-
return registered.definition.taskHandler.createTask(
|
|
1921
|
-
normalized.args,
|
|
1922
|
-
context,
|
|
1923
|
-
{ ...extra, taskStore }
|
|
1698
|
+
elicitForRequest(extra, params, options) {
|
|
1699
|
+
return extra.sendRequest(
|
|
1700
|
+
{ method: "elicitation/create", params },
|
|
1701
|
+
import_types2.ElicitResultSchema,
|
|
1702
|
+
options
|
|
1924
1703
|
);
|
|
1925
1704
|
}
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
if (exact) return { kind: "resource", definition: exact };
|
|
1929
|
-
for (const { definition, template } of this.resourceTemplates.values()) {
|
|
1930
|
-
const variables = template.match(uri);
|
|
1931
|
-
if (variables) return { kind: "template", definition, variables };
|
|
1932
|
-
}
|
|
1933
|
-
return void 0;
|
|
1705
|
+
listRoots(options) {
|
|
1706
|
+
return this.protocol.listRoots(void 0, options);
|
|
1934
1707
|
}
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1708
|
+
listRootsForRequest(extra, options) {
|
|
1709
|
+
return extra.sendRequest(
|
|
1710
|
+
{ method: "roots/list" },
|
|
1711
|
+
import_types2.ListRootsResultSchema,
|
|
1712
|
+
options
|
|
1713
|
+
);
|
|
1940
1714
|
}
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
if (!match) throw new McpFnValidationError(`Unknown MCP resource: ${uri}`);
|
|
1944
|
-
const url = new URL(uri);
|
|
1945
|
-
if (match.kind === "resource") {
|
|
1946
|
-
const callback2 = subscribed ? match.definition.subscribe : match.definition.unsubscribe;
|
|
1947
|
-
if (!callback2) {
|
|
1948
|
-
throw new McpFnValidationError(
|
|
1949
|
-
`Resource ${uri} does not support ${subscribed ? "subscriptions" : "unsubscription"}`
|
|
1950
|
-
);
|
|
1951
|
-
}
|
|
1952
|
-
await callback2(url, context, extra);
|
|
1953
|
-
return;
|
|
1954
|
-
}
|
|
1955
|
-
const callback = subscribed ? match.definition.subscribe : match.definition.unsubscribe;
|
|
1956
|
-
if (!callback) {
|
|
1957
|
-
throw new McpFnValidationError(
|
|
1958
|
-
`Resource ${uri} does not support ${subscribed ? "subscriptions" : "unsubscription"}`
|
|
1959
|
-
);
|
|
1960
|
-
}
|
|
1961
|
-
await callback(url, match.variables, context, extra);
|
|
1715
|
+
sendLoggingMessage(params, sessionId) {
|
|
1716
|
+
return this.protocol.sendLoggingMessage(params, sessionId);
|
|
1962
1717
|
}
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
if (!registered) throw new McpFnValidationError(`Unknown MCP prompt: ${name}`);
|
|
1966
|
-
const normalized = args ?? {};
|
|
1967
|
-
if (!registered.validateArguments(normalized)) {
|
|
1968
|
-
throw new McpFnValidationError(`Invalid arguments for prompt ${name}`, {
|
|
1969
|
-
issues: formatErrors(registered.validateArguments.errors)
|
|
1970
|
-
});
|
|
1971
|
-
}
|
|
1972
|
-
return registered.definition.get(normalized, context, extra);
|
|
1718
|
+
sendResourceUpdated(params) {
|
|
1719
|
+
return this.protocol.sendResourceUpdated(params);
|
|
1973
1720
|
}
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
if (ref.type === "ref/prompt") {
|
|
1977
|
-
const prompt = this.prompts.get(ref.name);
|
|
1978
|
-
if (!prompt) throw new McpFnValidationError(`Unknown MCP prompt: ${ref.name}`);
|
|
1979
|
-
return prompt.definition.complete?.[argument.name]?.(
|
|
1980
|
-
argument.value,
|
|
1981
|
-
completionContext,
|
|
1982
|
-
context,
|
|
1983
|
-
extra
|
|
1984
|
-
) ?? empty;
|
|
1985
|
-
}
|
|
1986
|
-
const template = [...this.resourceTemplates.values()].find(({ definition }) => definition.uriTemplate === ref.uri);
|
|
1987
|
-
if (!template) {
|
|
1988
|
-
if (this.resources.has(ref.uri)) return empty;
|
|
1989
|
-
throw new McpFnValidationError(`Unknown MCP resource template: ${ref.uri}`);
|
|
1990
|
-
}
|
|
1991
|
-
return template.definition.complete?.[argument.name]?.(
|
|
1992
|
-
argument.value,
|
|
1993
|
-
completionContext,
|
|
1994
|
-
context,
|
|
1995
|
-
extra
|
|
1996
|
-
) ?? empty;
|
|
1721
|
+
sendResourceListChanged() {
|
|
1722
|
+
return this.protocol.sendResourceListChanged();
|
|
1997
1723
|
}
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
);
|
|
2008
|
-
}
|
|
2009
|
-
if (!registered.validateOutput(result.structuredContent)) {
|
|
2010
|
-
throw new McpFnOutputValidationError(
|
|
2011
|
-
`Invalid output from ${registered.definition.name}`,
|
|
2012
|
-
{ issues: formatErrors(registered.validateOutput.errors) }
|
|
2013
|
-
);
|
|
2014
|
-
}
|
|
2015
|
-
}
|
|
2016
|
-
return result;
|
|
1724
|
+
sendToolListChanged() {
|
|
1725
|
+
return this.protocol.sendToolListChanged();
|
|
1726
|
+
}
|
|
1727
|
+
sendPromptListChanged() {
|
|
1728
|
+
return this.protocol.sendPromptListChanged();
|
|
1729
|
+
}
|
|
1730
|
+
async close() {
|
|
1731
|
+
await this.protocol.close();
|
|
1732
|
+
this.connected = false;
|
|
2017
1733
|
}
|
|
2018
1734
|
};
|
|
1735
|
+
function createMcpFnServer(options) {
|
|
1736
|
+
return new McpFnServer(options);
|
|
1737
|
+
}
|
|
2019
1738
|
|
|
2020
|
-
// src/
|
|
2021
|
-
function
|
|
2022
|
-
|
|
2023
|
-
|
|
1739
|
+
// src/declaration.ts
|
|
1740
|
+
function hasDefinitionAdditions(options) {
|
|
1741
|
+
return Boolean(
|
|
1742
|
+
options.tools?.length || options.resources?.length || options.resourceTemplates?.length || options.prompts?.length
|
|
1743
|
+
);
|
|
2024
1744
|
}
|
|
2025
|
-
function
|
|
2026
|
-
|
|
2027
|
-
|
|
1745
|
+
function cloneRegistry(source) {
|
|
1746
|
+
const clone = new McpFnRegistry();
|
|
1747
|
+
for (const tool of source.definitions()) clone.register(tool);
|
|
1748
|
+
for (const resource of source.resourceDefinitions()) clone.registerResource(resource);
|
|
1749
|
+
for (const template of source.resourceTemplateDefinitions()) {
|
|
1750
|
+
clone.registerResourceTemplate(template);
|
|
2028
1751
|
}
|
|
1752
|
+
for (const prompt of source.promptDefinitions()) clone.registerPrompt(prompt);
|
|
1753
|
+
return clone;
|
|
2029
1754
|
}
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
1755
|
+
var McpFnServerDeclaration = class {
|
|
1756
|
+
info;
|
|
1757
|
+
registry;
|
|
1758
|
+
manifestOptions;
|
|
1759
|
+
constructor(options) {
|
|
1760
|
+
this.info = options.info;
|
|
1761
|
+
this.registry = options.registry && hasDefinitionAdditions(options) ? cloneRegistry(options.registry) : options.registry ?? new McpFnRegistry();
|
|
1762
|
+
for (const tool of options.tools ?? []) this.registry.register(tool);
|
|
1763
|
+
for (const resource of options.resources ?? []) this.registry.registerResource(resource);
|
|
1764
|
+
for (const template of options.resourceTemplates ?? []) {
|
|
1765
|
+
this.registry.registerResourceTemplate(template);
|
|
1766
|
+
}
|
|
1767
|
+
for (const prompt of options.prompts ?? []) this.registry.registerPrompt(prompt);
|
|
1768
|
+
const capabilities = resolveMcpFnServerCapabilities(
|
|
1769
|
+
this.registry,
|
|
1770
|
+
options.capabilities,
|
|
1771
|
+
Boolean(this.registry.capabilities().tasks)
|
|
1772
|
+
);
|
|
1773
|
+
this.manifestOptions = {
|
|
1774
|
+
protocolVersions: options.protocolVersions,
|
|
1775
|
+
transports: options.transports,
|
|
1776
|
+
extensions: options.extensions,
|
|
1777
|
+
capabilities,
|
|
1778
|
+
clientRequirements: options.clientRequirements
|
|
1779
|
+
};
|
|
1780
|
+
}
|
|
1781
|
+
manifest() {
|
|
1782
|
+
return createManifest(this.info, this.registry, this.manifestOptions);
|
|
1783
|
+
}
|
|
1784
|
+
createServer(runtime = {}) {
|
|
1785
|
+
if (Object.hasOwn(runtime, "additionalCapabilities") || Object.hasOwn(runtime, "capabilities")) {
|
|
1786
|
+
throw new Error(
|
|
1787
|
+
"McpFn declaration capabilities must be configured on defineMcpFnServer(), not createServer()"
|
|
1788
|
+
);
|
|
1789
|
+
}
|
|
1790
|
+
return createMcpFnServer({
|
|
1791
|
+
...runtime,
|
|
1792
|
+
...this.manifestOptions,
|
|
1793
|
+
info: this.info,
|
|
1794
|
+
registry: this.registry,
|
|
1795
|
+
additionalCapabilities: this.manifestOptions.capabilities
|
|
1796
|
+
});
|
|
2033
1797
|
}
|
|
1798
|
+
};
|
|
1799
|
+
function defineMcpFnServer(options) {
|
|
1800
|
+
return new McpFnServerDeclaration(options);
|
|
2034
1801
|
}
|
|
2035
|
-
|
|
2036
|
-
|
|
1802
|
+
|
|
1803
|
+
// src/diff.ts
|
|
1804
|
+
function push(changes, change) {
|
|
1805
|
+
changes.push(change);
|
|
2037
1806
|
}
|
|
2038
|
-
function
|
|
2039
|
-
|
|
1807
|
+
function valueSet(value) {
|
|
1808
|
+
return Array.isArray(value) ? new Set(value.map((entry) => canonicalJson(entry))) : void 0;
|
|
2040
1809
|
}
|
|
2041
|
-
function
|
|
2042
|
-
|
|
2043
|
-
const tools = registry.definitions().map((tool) => ({
|
|
2044
|
-
name: tool.name,
|
|
2045
|
-
...tool.title ? { title: tool.title } : {},
|
|
2046
|
-
description: tool.description,
|
|
2047
|
-
inputSchema: tool.inputSchema,
|
|
2048
|
-
...tool.outputSchema ? { outputSchema: tool.outputSchema } : {},
|
|
2049
|
-
...tool.annotations ? { annotations: tool.annotations } : {},
|
|
2050
|
-
...tool.execution ? { execution: tool.execution } : {},
|
|
2051
|
-
...tool.icons ? { icons: tool.icons } : {},
|
|
2052
|
-
...tool.metadata ? { metadata: tool.metadata } : {}
|
|
2053
|
-
}));
|
|
2054
|
-
const resources = registry.resourceDefinitions().map((resource) => ({
|
|
2055
|
-
uri: resource.uri,
|
|
2056
|
-
name: resource.name,
|
|
2057
|
-
...resource.title ? { title: resource.title } : {},
|
|
2058
|
-
...resource.description ? { description: resource.description } : {},
|
|
2059
|
-
...resource.mimeType ? { mimeType: resource.mimeType } : {},
|
|
2060
|
-
...resource.annotations ? { annotations: resource.annotations } : {},
|
|
2061
|
-
...resource.icons ? { icons: resource.icons } : {},
|
|
2062
|
-
...resource.subscribe ? { subscribable: true } : {},
|
|
2063
|
-
...resource.metadata ? { metadata: resource.metadata } : {}
|
|
2064
|
-
}));
|
|
2065
|
-
const resourceTemplates = registry.resourceTemplateDefinitions().map((resource) => ({
|
|
2066
|
-
uriTemplate: resource.uriTemplate,
|
|
2067
|
-
name: resource.name,
|
|
2068
|
-
...resource.title ? { title: resource.title } : {},
|
|
2069
|
-
...resource.description ? { description: resource.description } : {},
|
|
2070
|
-
...resource.mimeType ? { mimeType: resource.mimeType } : {},
|
|
2071
|
-
...resource.annotations ? { annotations: resource.annotations } : {},
|
|
2072
|
-
...resource.icons ? { icons: resource.icons } : {},
|
|
2073
|
-
...resource.subscribe ? { subscribable: true } : {},
|
|
2074
|
-
...resource.metadata ? { metadata: resource.metadata } : {}
|
|
2075
|
-
}));
|
|
2076
|
-
const prompts = registry.promptDefinitions().map((prompt) => ({
|
|
2077
|
-
name: prompt.name,
|
|
2078
|
-
...prompt.title ? { title: prompt.title } : {},
|
|
2079
|
-
...prompt.description ? { description: prompt.description } : {},
|
|
2080
|
-
...promptArguments(prompt) ? { arguments: promptArguments(prompt) } : {},
|
|
2081
|
-
...prompt.argumentsSchema ? { argumentsSchema: prompt.argumentsSchema } : {},
|
|
2082
|
-
...prompt.icons ? { icons: prompt.icons } : {},
|
|
2083
|
-
...prompt.metadata ? { metadata: prompt.metadata } : {}
|
|
2084
|
-
}));
|
|
2085
|
-
const body = {
|
|
2086
|
-
formatVersion: 1,
|
|
2087
|
-
server: {
|
|
2088
|
-
name: info.name,
|
|
2089
|
-
version: info.version,
|
|
2090
|
-
...info.instructions ? { instructions: info.instructions } : {}
|
|
2091
|
-
},
|
|
2092
|
-
...options.protocolVersions ? { protocolVersions: [...new Set(options.protocolVersions)].sort() } : {},
|
|
2093
|
-
...options.transports ? { transports: [...new Set(options.transports)].sort() } : {},
|
|
2094
|
-
capabilities: options.capabilities ?? registry.capabilities(),
|
|
2095
|
-
...options.clientRequirements ? {
|
|
2096
|
-
clientRequirements: {
|
|
2097
|
-
...options.clientRequirements,
|
|
2098
|
-
...options.clientRequirements.elicitation ? { elicitation: [...new Set(options.clientRequirements.elicitation)].sort() } : {}
|
|
2099
|
-
}
|
|
2100
|
-
} : {},
|
|
2101
|
-
...options.extensions ? { extensions: options.extensions } : {},
|
|
2102
|
-
tools,
|
|
2103
|
-
resources,
|
|
2104
|
-
resourceTemplates,
|
|
2105
|
-
prompts
|
|
2106
|
-
};
|
|
2107
|
-
return { ...body, hash: sha256(body) };
|
|
1810
|
+
function equal(left, right) {
|
|
1811
|
+
return canonicalJson(left) === canonicalJson(right);
|
|
2108
1812
|
}
|
|
2109
|
-
function
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
if (manifest.formatVersion !== 1) {
|
|
2113
|
-
throw new McpFnValidationError("Unsupported McpFn manifest formatVersion");
|
|
2114
|
-
}
|
|
2115
|
-
if (!manifest.server || typeof manifest.server.name !== "string" || !manifest.server.name.trim() || typeof manifest.server.version !== "string" || !manifest.server.version.trim()) throw new McpFnValidationError("Manifest server name and version are required");
|
|
2116
|
-
if (manifest.server.instructions !== void 0 && typeof manifest.server.instructions !== "string") throw new McpFnValidationError("Manifest server instructions must be a string");
|
|
2117
|
-
if (!Array.isArray(manifest.tools)) {
|
|
2118
|
-
throw new McpFnValidationError("Manifest tools must be an array");
|
|
2119
|
-
}
|
|
2120
|
-
for (const [label, entries] of [
|
|
2121
|
-
["resources", manifest.resources],
|
|
2122
|
-
["resourceTemplates", manifest.resourceTemplates],
|
|
2123
|
-
["prompts", manifest.prompts]
|
|
2124
|
-
]) {
|
|
2125
|
-
if (entries !== void 0 && !Array.isArray(entries)) {
|
|
2126
|
-
throw new McpFnValidationError(`Manifest ${label} must be an array`);
|
|
2127
|
-
}
|
|
2128
|
-
}
|
|
2129
|
-
if (manifest.protocolVersions !== void 0 && (!Array.isArray(manifest.protocolVersions) || manifest.protocolVersions.some((version) => typeof version !== "string" || !version))) throw new McpFnValidationError("Manifest protocolVersions must contain non-empty strings");
|
|
2130
|
-
if (manifest.protocolVersions) assertSortedUnique("Manifest protocolVersions", manifest.protocolVersions);
|
|
2131
|
-
if (manifest.transports !== void 0 && (!Array.isArray(manifest.transports) || manifest.transports.some((transport) => transport !== "stdio" && transport !== "streamable-http"))) throw new McpFnValidationError("Manifest contains an unsupported transport");
|
|
2132
|
-
if (manifest.transports) assertSortedUnique("Manifest transports", manifest.transports);
|
|
2133
|
-
if (manifest.capabilities !== void 0) {
|
|
2134
|
-
const parsed = import_types.ServerCapabilitiesSchema.safeParse(manifest.capabilities);
|
|
2135
|
-
if (!parsed.success) throw new McpFnValidationError("Manifest capabilities are invalid");
|
|
1813
|
+
function schemaTypes(schema) {
|
|
1814
|
+
if (Array.isArray(schema.type)) {
|
|
1815
|
+
return new Set(schema.type.filter((value) => typeof value === "string"));
|
|
2136
1816
|
}
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
1817
|
+
return typeof schema.type === "string" ? /* @__PURE__ */ new Set([schema.type]) : void 0;
|
|
1818
|
+
}
|
|
1819
|
+
function equalSets(left, right) {
|
|
1820
|
+
return left.size === right.size && [...left].every((value) => right.has(value));
|
|
1821
|
+
}
|
|
1822
|
+
function hasUnescapedAlternation(pattern) {
|
|
1823
|
+
let escaped = false;
|
|
1824
|
+
for (const character of pattern) {
|
|
1825
|
+
if (escaped) {
|
|
1826
|
+
escaped = false;
|
|
1827
|
+
continue;
|
|
1828
|
+
}
|
|
1829
|
+
if (character === "\\") {
|
|
1830
|
+
escaped = true;
|
|
1831
|
+
continue;
|
|
2147
1832
|
}
|
|
1833
|
+
if (character === "|") return true;
|
|
2148
1834
|
}
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
1835
|
+
return false;
|
|
1836
|
+
}
|
|
1837
|
+
function anchoredLiteralPrefix(pattern) {
|
|
1838
|
+
if (!pattern.startsWith("^")) return void 0;
|
|
1839
|
+
let prefix = "";
|
|
1840
|
+
for (let index = 1; index < pattern.length; index += 1) {
|
|
1841
|
+
const character = pattern[index];
|
|
1842
|
+
if (character === "\\") {
|
|
1843
|
+
const escaped = pattern[index + 1];
|
|
1844
|
+
if (!escaped || /[A-Za-z0-9]/.test(escaped)) break;
|
|
1845
|
+
prefix += escaped;
|
|
1846
|
+
index += 1;
|
|
1847
|
+
continue;
|
|
2157
1848
|
}
|
|
2158
|
-
if (
|
|
2159
|
-
|
|
1849
|
+
if (".^$*+?()[]{}|".includes(character)) break;
|
|
1850
|
+
prefix += character;
|
|
1851
|
+
}
|
|
1852
|
+
return prefix || void 0;
|
|
1853
|
+
}
|
|
1854
|
+
function anchoredLiteralSuffix(pattern) {
|
|
1855
|
+
if (!pattern.endsWith("$") || pattern.endsWith("\\$")) return void 0;
|
|
1856
|
+
let suffix = "";
|
|
1857
|
+
for (let index = 0; index < pattern.length - 1; index += 1) {
|
|
1858
|
+
const character = pattern[index];
|
|
1859
|
+
if (character === "\\") {
|
|
1860
|
+
const escaped = pattern[index + 1];
|
|
1861
|
+
if (!escaped) break;
|
|
1862
|
+
suffix = /[A-Za-z0-9]/.test(escaped) ? "" : suffix + escaped;
|
|
1863
|
+
index += 1;
|
|
1864
|
+
continue;
|
|
2160
1865
|
}
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
1866
|
+
suffix = ".^$*+?()[]{}|".includes(character) ? "" : suffix + character;
|
|
1867
|
+
}
|
|
1868
|
+
return suffix || void 0;
|
|
1869
|
+
}
|
|
1870
|
+
function patternsMayOverlap(left, right) {
|
|
1871
|
+
if (hasUnescapedAlternation(left) || hasUnescapedAlternation(right)) return true;
|
|
1872
|
+
const leftPrefix = anchoredLiteralPrefix(left);
|
|
1873
|
+
const rightPrefix = anchoredLiteralPrefix(right);
|
|
1874
|
+
if (leftPrefix && rightPrefix && !leftPrefix.startsWith(rightPrefix) && !rightPrefix.startsWith(leftPrefix)) return false;
|
|
1875
|
+
const leftSuffix = anchoredLiteralSuffix(left);
|
|
1876
|
+
const rightSuffix = anchoredLiteralSuffix(right);
|
|
1877
|
+
if (leftSuffix && rightSuffix && !leftSuffix.endsWith(rightSuffix) && !rightSuffix.endsWith(leftSuffix)) return false;
|
|
1878
|
+
return true;
|
|
1879
|
+
}
|
|
1880
|
+
function capabilityRecord(value) {
|
|
1881
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
1882
|
+
}
|
|
1883
|
+
function nestedCapabilityChanges(before, after, path) {
|
|
1884
|
+
if (equal(before, after)) return [];
|
|
1885
|
+
if (before === true && after === false) {
|
|
1886
|
+
return [{ severity: "breaking", path, before, after }];
|
|
1887
|
+
}
|
|
1888
|
+
if (before === false && after === true) {
|
|
1889
|
+
return [{ severity: "additive", path, before, after }];
|
|
1890
|
+
}
|
|
1891
|
+
if (capabilityRecord(before) && capabilityRecord(after)) {
|
|
1892
|
+
return [.../* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)])].sort(compareCodeUnits).flatMap((name) => {
|
|
1893
|
+
const childPath = `${path}.${name}`;
|
|
1894
|
+
const hadBefore = Object.prototype.hasOwnProperty.call(before, name);
|
|
1895
|
+
const hasAfter = Object.prototype.hasOwnProperty.call(after, name);
|
|
1896
|
+
if (!hasAfter) {
|
|
1897
|
+
return [{ severity: "breaking", path: childPath, before: before[name], after: void 0 }];
|
|
2165
1898
|
}
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
}
|
|
2171
|
-
throw new McpFnValidationError(
|
|
2172
|
-
`Manifest tool ${tool.name} contains an invalid JSON Schema`,
|
|
2173
|
-
{ cause: error instanceof Error ? error.message : String(error) }
|
|
2174
|
-
);
|
|
2175
|
-
}
|
|
2176
|
-
for (const [label, metadata] of [
|
|
2177
|
-
["annotations", tool.annotations],
|
|
2178
|
-
["execution", tool.execution],
|
|
2179
|
-
["metadata", tool.metadata]
|
|
2180
|
-
]) assertMetadata(`Manifest tool ${tool.name} ${label}`, metadata);
|
|
2181
|
-
const taskSupport = tool.execution?.taskSupport;
|
|
2182
|
-
if (taskSupport !== void 0 && taskSupport !== "forbidden" && taskSupport !== "optional" && taskSupport !== "required") {
|
|
2183
|
-
throw new McpFnValidationError(
|
|
2184
|
-
`Manifest tool ${tool.name} has invalid taskSupport=${String(taskSupport)}`
|
|
2185
|
-
);
|
|
2186
|
-
}
|
|
1899
|
+
if (!hadBefore) {
|
|
1900
|
+
return [{ severity: "additive", path: childPath, before: void 0, after: after[name] }];
|
|
1901
|
+
}
|
|
1902
|
+
return nestedCapabilityChanges(before[name], after[name], childPath);
|
|
1903
|
+
});
|
|
2187
1904
|
}
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
1905
|
+
if (capabilityRecord(before) && after === false) {
|
|
1906
|
+
return [{ severity: "breaking", path, before, after }];
|
|
1907
|
+
}
|
|
1908
|
+
if (before === false && capabilityRecord(after)) {
|
|
1909
|
+
return [{ severity: "additive", path, before, after }];
|
|
1910
|
+
}
|
|
1911
|
+
return [{ severity: "behavioral", path, before, after }];
|
|
1912
|
+
}
|
|
1913
|
+
function matchingPatternSchemas(schema, propertyName) {
|
|
1914
|
+
const patterns = schema.patternProperties;
|
|
1915
|
+
if (!patterns || typeof patterns !== "object" || Array.isArray(patterns)) {
|
|
1916
|
+
return [];
|
|
1917
|
+
}
|
|
1918
|
+
return Object.entries(patterns).filter(([pattern]) => {
|
|
2192
1919
|
try {
|
|
2193
|
-
new
|
|
1920
|
+
return new RegExp(pattern).test(propertyName);
|
|
2194
1921
|
} catch {
|
|
2195
|
-
|
|
2196
|
-
}
|
|
2197
|
-
if (resource.subscribable !== void 0 && typeof resource.subscribable !== "boolean") {
|
|
2198
|
-
throw new McpFnValidationError(
|
|
2199
|
-
`Manifest resource ${resource.name} subscribable must be a boolean`
|
|
2200
|
-
);
|
|
1922
|
+
return false;
|
|
2201
1923
|
}
|
|
2202
|
-
|
|
1924
|
+
}).map(([, patternSchema]) => patternSchema);
|
|
1925
|
+
}
|
|
1926
|
+
function diffSchema(before, after, path, direction, changes) {
|
|
1927
|
+
if (before && typeof before === "object" && !Object.keys(before).length) before = true;
|
|
1928
|
+
if (after && typeof after === "object" && !Object.keys(after).length) after = true;
|
|
1929
|
+
if (before === void 0 && after !== void 0) {
|
|
1930
|
+
push(changes, {
|
|
1931
|
+
severity: direction === "input" ? "breaking" : "additive",
|
|
1932
|
+
code: "schema-added",
|
|
1933
|
+
path,
|
|
1934
|
+
message: `Schema added at ${path}`,
|
|
1935
|
+
after
|
|
1936
|
+
});
|
|
1937
|
+
return;
|
|
2203
1938
|
}
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
1939
|
+
if (before !== void 0 && after === void 0) {
|
|
1940
|
+
push(changes, {
|
|
1941
|
+
severity: direction === "input" ? "additive" : "breaking",
|
|
1942
|
+
code: "schema-removed",
|
|
1943
|
+
path,
|
|
1944
|
+
message: `Schema removed at ${path}`,
|
|
1945
|
+
before
|
|
1946
|
+
});
|
|
1947
|
+
return;
|
|
1948
|
+
}
|
|
1949
|
+
if (before === void 0 || after === void 0 || equal(before, after)) return;
|
|
1950
|
+
if (typeof before === "boolean" || typeof after === "boolean") {
|
|
1951
|
+
const tightened = before === true || after === false;
|
|
1952
|
+
const relaxed = before === false || after === true;
|
|
1953
|
+
const breaking = direction === "input" ? tightened : relaxed;
|
|
1954
|
+
push(changes, {
|
|
1955
|
+
severity: breaking ? "breaking" : "additive",
|
|
1956
|
+
code: "boolean-schema-changed",
|
|
1957
|
+
path,
|
|
1958
|
+
message: `Boolean schema changed at ${path}`,
|
|
1959
|
+
before,
|
|
1960
|
+
after
|
|
1961
|
+
});
|
|
1962
|
+
return;
|
|
1963
|
+
}
|
|
1964
|
+
const beforeTypes = schemaTypes(before);
|
|
1965
|
+
const afterTypes = schemaTypes(after);
|
|
1966
|
+
if (!equal(before.type, after.type) && !(beforeTypes && afterTypes && equalSets(beforeTypes, afterTypes))) {
|
|
1967
|
+
const removed = beforeTypes && afterTypes ? [...beforeTypes].filter((value) => !afterTypes?.has(value)) : [];
|
|
1968
|
+
const added = beforeTypes && afterTypes ? [...afterTypes].filter((value) => !beforeTypes?.has(value)) : [];
|
|
1969
|
+
const tightened = beforeTypes === void 0 ? afterTypes !== void 0 : afterTypes !== void 0 && removed.length > 0;
|
|
1970
|
+
const relaxed = afterTypes === void 0 || added.length > 0;
|
|
1971
|
+
const breaking = direction === "input" ? tightened : relaxed;
|
|
1972
|
+
push(changes, {
|
|
1973
|
+
severity: breaking ? "breaking" : "additive",
|
|
1974
|
+
code: "schema-type-changed",
|
|
1975
|
+
path,
|
|
1976
|
+
message: `Type changed from ${canonicalJson(before.type ?? "unspecified")} to ${canonicalJson(after.type ?? "unspecified")}`,
|
|
1977
|
+
before: before.type,
|
|
1978
|
+
after: after.type
|
|
1979
|
+
});
|
|
1980
|
+
}
|
|
1981
|
+
const beforeEnum = valueSet(before.enum);
|
|
1982
|
+
const afterEnum = valueSet(after.enum);
|
|
1983
|
+
if (!beforeEnum && afterEnum) {
|
|
1984
|
+
push(changes, {
|
|
1985
|
+
severity: direction === "input" ? "breaking" : "additive",
|
|
1986
|
+
code: "enum-introduced",
|
|
1987
|
+
path,
|
|
1988
|
+
message: `An enum constraint was introduced at ${path}`,
|
|
1989
|
+
after: after.enum
|
|
1990
|
+
});
|
|
1991
|
+
} else if (beforeEnum && !afterEnum) {
|
|
1992
|
+
push(changes, {
|
|
1993
|
+
severity: direction === "input" ? "additive" : "breaking",
|
|
1994
|
+
code: "enum-removed",
|
|
1995
|
+
path,
|
|
1996
|
+
message: `The enum constraint was removed at ${path}`,
|
|
1997
|
+
before: before.enum
|
|
1998
|
+
});
|
|
1999
|
+
} else if (beforeEnum && afterEnum) {
|
|
2000
|
+
const removed = [...beforeEnum].filter((entry) => !afterEnum.has(entry));
|
|
2001
|
+
const added = [...afterEnum].filter((entry) => !beforeEnum.has(entry));
|
|
2002
|
+
if (removed.length) {
|
|
2003
|
+
push(changes, {
|
|
2004
|
+
severity: direction === "input" ? "breaking" : "additive",
|
|
2005
|
+
code: "enum-narrowed",
|
|
2006
|
+
path,
|
|
2007
|
+
message: `Allowed values were removed at ${path}`,
|
|
2008
|
+
before: before.enum,
|
|
2009
|
+
after: after.enum
|
|
2010
|
+
});
|
|
2219
2011
|
}
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
`
|
|
2226
|
-
|
|
2012
|
+
if (added.length) {
|
|
2013
|
+
push(changes, {
|
|
2014
|
+
severity: direction === "input" ? "additive" : "breaking",
|
|
2015
|
+
code: "enum-widened",
|
|
2016
|
+
path,
|
|
2017
|
+
message: `Allowed values were added at ${path}`,
|
|
2018
|
+
before: before.enum,
|
|
2019
|
+
after: after.enum
|
|
2020
|
+
});
|
|
2227
2021
|
}
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2022
|
+
}
|
|
2023
|
+
const tightening = [
|
|
2024
|
+
["minimum", "higher"],
|
|
2025
|
+
["exclusiveMinimum", "higher"],
|
|
2026
|
+
["minLength", "higher"],
|
|
2027
|
+
["minItems", "higher"],
|
|
2028
|
+
["maximum", "lower"],
|
|
2029
|
+
["exclusiveMaximum", "lower"],
|
|
2030
|
+
["maxLength", "lower"],
|
|
2031
|
+
["maxItems", "lower"],
|
|
2032
|
+
["minProperties", "higher"],
|
|
2033
|
+
["maxProperties", "lower"]
|
|
2034
|
+
];
|
|
2035
|
+
for (const [keyword, tightDirection] of tightening) {
|
|
2036
|
+
const oldValue = before[keyword];
|
|
2037
|
+
const newValue = after[keyword];
|
|
2038
|
+
if (equal(oldValue, newValue)) {
|
|
2039
|
+
continue;
|
|
2232
2040
|
}
|
|
2233
|
-
if (
|
|
2234
|
-
(
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2041
|
+
if (oldValue !== void 0 && typeof oldValue !== "number" || newValue !== void 0 && typeof newValue !== "number") {
|
|
2042
|
+
push(changes, {
|
|
2043
|
+
severity: "breaking",
|
|
2044
|
+
code: "schema-keyword-changed",
|
|
2045
|
+
path: `${path}.${keyword}`,
|
|
2046
|
+
message: `${keyword} changed at ${path}`,
|
|
2047
|
+
before: oldValue,
|
|
2048
|
+
after: newValue
|
|
2049
|
+
});
|
|
2050
|
+
continue;
|
|
2239
2051
|
}
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2052
|
+
const tightened = oldValue === void 0 ? true : newValue === void 0 ? false : tightDirection === "higher" ? newValue > oldValue : newValue < oldValue;
|
|
2053
|
+
const breaking = direction === "input" ? tightened : !tightened;
|
|
2054
|
+
push(changes, {
|
|
2055
|
+
severity: breaking ? "breaking" : "additive",
|
|
2056
|
+
code: tightened ? "constraint-tightened" : "constraint-relaxed",
|
|
2057
|
+
path: `${path}.${keyword}`,
|
|
2058
|
+
message: `${keyword} changed from ${oldValue ?? "unspecified"} to ${newValue ?? "unspecified"}`,
|
|
2059
|
+
before: oldValue,
|
|
2060
|
+
after: newValue
|
|
2061
|
+
});
|
|
2062
|
+
}
|
|
2063
|
+
for (const keyword of ["pattern", "format", "const"]) {
|
|
2064
|
+
const oldValue = before[keyword];
|
|
2065
|
+
const newValue = after[keyword];
|
|
2066
|
+
if (equal(oldValue, newValue)) continue;
|
|
2067
|
+
const tightened = oldValue === void 0 && newValue !== void 0;
|
|
2068
|
+
const relaxed = oldValue !== void 0 && newValue === void 0;
|
|
2069
|
+
push(changes, {
|
|
2070
|
+
severity: tightened || relaxed ? (direction === "input" ? tightened : relaxed) ? "breaking" : "additive" : "breaking",
|
|
2071
|
+
code: tightened ? "constraint-tightened" : relaxed ? "constraint-relaxed" : "schema-keyword-changed",
|
|
2072
|
+
path: `${path}.${keyword}`,
|
|
2073
|
+
message: `${keyword} changed at ${path}`,
|
|
2074
|
+
before: oldValue,
|
|
2075
|
+
after: newValue
|
|
2076
|
+
});
|
|
2077
|
+
}
|
|
2078
|
+
const beforeUniqueItems = before.uniqueItems ?? false;
|
|
2079
|
+
const afterUniqueItems = after.uniqueItems ?? false;
|
|
2080
|
+
if (!equal(beforeUniqueItems, afterUniqueItems)) {
|
|
2081
|
+
if (typeof beforeUniqueItems !== "boolean" || typeof afterUniqueItems !== "boolean") {
|
|
2082
|
+
push(changes, {
|
|
2083
|
+
severity: "breaking",
|
|
2084
|
+
code: "schema-keyword-changed",
|
|
2085
|
+
path: `${path}.uniqueItems`,
|
|
2086
|
+
message: `uniqueItems changed at ${path}`,
|
|
2087
|
+
before: before.uniqueItems,
|
|
2088
|
+
after: after.uniqueItems
|
|
2089
|
+
});
|
|
2090
|
+
} else {
|
|
2091
|
+
const tightened = !beforeUniqueItems && afterUniqueItems;
|
|
2092
|
+
const breaking = direction === "input" ? tightened : !tightened;
|
|
2093
|
+
push(changes, {
|
|
2094
|
+
severity: breaking ? "breaking" : "additive",
|
|
2095
|
+
code: tightened ? "constraint-tightened" : "constraint-relaxed",
|
|
2096
|
+
path: `${path}.uniqueItems`,
|
|
2097
|
+
message: `uniqueItems changed from ${beforeUniqueItems} to ${afterUniqueItems}`,
|
|
2098
|
+
before: before.uniqueItems,
|
|
2099
|
+
after: after.uniqueItems
|
|
2100
|
+
});
|
|
2245
2101
|
}
|
|
2246
|
-
assertMetadata(`Manifest resource template ${resource.name} metadata`, resource.metadata);
|
|
2247
2102
|
}
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2103
|
+
const normalizeAdditionalProperties = (value) => value && typeof value === "object" && !Array.isArray(value) && !Object.keys(value).length ? true : value;
|
|
2104
|
+
const beforeAdditional = normalizeAdditionalProperties(before.additionalProperties ?? true);
|
|
2105
|
+
const afterAdditional = normalizeAdditionalProperties(after.additionalProperties ?? true);
|
|
2106
|
+
if (beforeAdditional !== false && afterAdditional === false) {
|
|
2107
|
+
push(changes, {
|
|
2108
|
+
severity: direction === "input" ? "breaking" : "additive",
|
|
2109
|
+
code: "additional-properties-disabled",
|
|
2110
|
+
path,
|
|
2111
|
+
message: `Additional properties are no longer accepted at ${path}`
|
|
2112
|
+
});
|
|
2113
|
+
} else if (beforeAdditional === false && afterAdditional !== false) {
|
|
2114
|
+
push(changes, {
|
|
2115
|
+
severity: direction === "input" ? "additive" : "breaking",
|
|
2116
|
+
code: "additional-properties-enabled",
|
|
2117
|
+
path,
|
|
2118
|
+
message: `Additional properties are now accepted at ${path}`
|
|
2119
|
+
});
|
|
2120
|
+
} else if (!equal(beforeAdditional, afterAdditional)) {
|
|
2121
|
+
diffSchema(
|
|
2122
|
+
beforeAdditional,
|
|
2123
|
+
afterAdditional,
|
|
2124
|
+
`${path}.additionalProperties`,
|
|
2125
|
+
direction,
|
|
2126
|
+
changes
|
|
2127
|
+
);
|
|
2128
|
+
}
|
|
2129
|
+
const beforeProperties = before.properties ?? {};
|
|
2130
|
+
const afterProperties = after.properties ?? {};
|
|
2131
|
+
const propertyNames = [.../* @__PURE__ */ new Set([
|
|
2132
|
+
...Object.keys(beforeProperties),
|
|
2133
|
+
...Object.keys(afterProperties),
|
|
2134
|
+
...Array.isArray(before.required) ? before.required : [],
|
|
2135
|
+
...Array.isArray(after.required) ? after.required : []
|
|
2136
|
+
])].sort();
|
|
2137
|
+
const beforeRequired = new Set(
|
|
2138
|
+
Array.isArray(before.required) ? before.required : []
|
|
2252
2139
|
);
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
const
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2140
|
+
const afterRequired = new Set(
|
|
2141
|
+
Array.isArray(after.required) ? after.required : []
|
|
2142
|
+
);
|
|
2143
|
+
for (const name of propertyNames) {
|
|
2144
|
+
const propertyPath = `${path}.properties.${name}`;
|
|
2145
|
+
const oldProperty = beforeProperties[name];
|
|
2146
|
+
const newProperty = afterProperties[name];
|
|
2147
|
+
const hadOldProperty = Object.prototype.hasOwnProperty.call(beforeProperties, name);
|
|
2148
|
+
const hasNewProperty = Object.prototype.hasOwnProperty.call(afterProperties, name);
|
|
2149
|
+
if (!hadOldProperty && hasNewProperty) {
|
|
2150
|
+
const newlyRequired = afterRequired.has(name);
|
|
2151
|
+
const additionalPropertyChanges = [];
|
|
2152
|
+
if (direction === "output" && typeof beforeAdditional === "object") {
|
|
2153
|
+
diffSchema(
|
|
2154
|
+
beforeAdditional,
|
|
2155
|
+
newProperty,
|
|
2156
|
+
`${propertyPath}.additionalProperties`,
|
|
2157
|
+
"output",
|
|
2158
|
+
additionalPropertyChanges
|
|
2267
2159
|
);
|
|
2268
2160
|
}
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2161
|
+
const inputFallbackChanges = [];
|
|
2162
|
+
if (direction === "input") {
|
|
2163
|
+
const patternSchemas = matchingPatternSchemas(before, name);
|
|
2164
|
+
const fallbackSchemas = patternSchemas.length ? patternSchemas : [beforeAdditional];
|
|
2165
|
+
fallbackSchemas.forEach((fallbackSchema, index) => diffSchema(
|
|
2166
|
+
fallbackSchema,
|
|
2167
|
+
newProperty,
|
|
2168
|
+
`${propertyPath}.${patternSchemas.length ? `patternProperties.${index}` : "additionalProperties"}`,
|
|
2169
|
+
"input",
|
|
2170
|
+
inputFallbackChanges
|
|
2171
|
+
));
|
|
2172
|
+
}
|
|
2173
|
+
const breaksDeclaredOutput = direction === "output" && (beforeAdditional === false || additionalPropertyChanges.some((change) => change.severity === "breaking"));
|
|
2174
|
+
const narrowsAcceptedInput = direction === "input" && inputFallbackChanges.some((change) => change.severity === "breaking");
|
|
2175
|
+
push(changes, {
|
|
2176
|
+
severity: direction === "input" && (newlyRequired || narrowsAcceptedInput) || breaksDeclaredOutput ? "breaking" : "additive",
|
|
2177
|
+
code: direction === "input" && newlyRequired ? "required-input-added" : breaksDeclaredOutput ? "output-property-added" : "property-added",
|
|
2178
|
+
path: propertyPath,
|
|
2179
|
+
message: `${newlyRequired ? "Required" : "Optional"} property ${name} was added`,
|
|
2180
|
+
after: newProperty
|
|
2181
|
+
});
|
|
2182
|
+
continue;
|
|
2183
|
+
}
|
|
2184
|
+
if (hadOldProperty && !hasNewProperty) {
|
|
2185
|
+
const fallbackChanges = [];
|
|
2186
|
+
const patternSchemas = matchingPatternSchemas(after, name);
|
|
2187
|
+
const fallbackSchemas = patternSchemas.length ? patternSchemas : [afterAdditional];
|
|
2188
|
+
fallbackSchemas.forEach((fallbackSchema, index) => diffSchema(
|
|
2189
|
+
oldProperty,
|
|
2190
|
+
fallbackSchema,
|
|
2191
|
+
`${propertyPath}.${patternSchemas.length ? `patternProperties.${index}` : "additionalProperties"}`,
|
|
2192
|
+
direction,
|
|
2193
|
+
fallbackChanges
|
|
2194
|
+
));
|
|
2195
|
+
const breaking2 = direction === "output" && beforeRequired.has(name) || fallbackChanges.some((change) => change.severity === "breaking");
|
|
2196
|
+
push(changes, {
|
|
2197
|
+
severity: breaking2 ? "breaking" : "additive",
|
|
2198
|
+
code: "property-removed",
|
|
2199
|
+
path: propertyPath,
|
|
2200
|
+
message: `Property ${name} was removed`,
|
|
2201
|
+
before: oldProperty
|
|
2202
|
+
});
|
|
2203
|
+
continue;
|
|
2204
|
+
}
|
|
2205
|
+
diffSchema(oldProperty, newProperty, propertyPath, direction, changes);
|
|
2206
|
+
const wasRequired = beforeRequired.has(name);
|
|
2207
|
+
const isRequired = afterRequired.has(name);
|
|
2208
|
+
if (wasRequired === isRequired) continue;
|
|
2209
|
+
const breaking = direction === "input" ? !wasRequired && isRequired : wasRequired && !isRequired;
|
|
2210
|
+
push(changes, {
|
|
2211
|
+
severity: breaking ? "breaking" : "additive",
|
|
2212
|
+
code: breaking ? "requiredness-broken" : "requiredness-compatible",
|
|
2213
|
+
path: propertyPath,
|
|
2214
|
+
message: `Property ${name} changed from ${wasRequired ? "required" : "optional"} to ${isRequired ? "required" : "optional"}`
|
|
2215
|
+
});
|
|
2216
|
+
}
|
|
2217
|
+
if (before.items !== void 0 || after.items !== void 0) {
|
|
2218
|
+
diffSchema(
|
|
2219
|
+
before.items,
|
|
2220
|
+
after.items,
|
|
2221
|
+
`${path}.items`,
|
|
2222
|
+
direction,
|
|
2223
|
+
changes
|
|
2224
|
+
);
|
|
2225
|
+
}
|
|
2226
|
+
const beforePatterns = before.patternProperties ?? {};
|
|
2227
|
+
const afterPatterns = after.patternProperties ?? {};
|
|
2228
|
+
const patternNames = [.../* @__PURE__ */ new Set([
|
|
2229
|
+
...Object.keys(beforePatterns),
|
|
2230
|
+
...Object.keys(afterPatterns)
|
|
2231
|
+
])].sort();
|
|
2232
|
+
for (const pattern of patternNames) {
|
|
2233
|
+
const hadBeforePattern = Object.prototype.hasOwnProperty.call(beforePatterns, pattern);
|
|
2234
|
+
const hasAfterPattern = Object.prototype.hasOwnProperty.call(afterPatterns, pattern);
|
|
2235
|
+
diffSchema(
|
|
2236
|
+
hadBeforePattern ? beforePatterns[pattern] : beforeAdditional,
|
|
2237
|
+
hasAfterPattern ? afterPatterns[pattern] : afterAdditional,
|
|
2238
|
+
`${path}.patternProperties.${pattern}`,
|
|
2239
|
+
direction,
|
|
2240
|
+
changes
|
|
2241
|
+
);
|
|
2242
|
+
const overlapChanges = [];
|
|
2243
|
+
if (direction === "input" && !hadBeforePattern && hasAfterPattern) {
|
|
2244
|
+
for (const [existingPattern, existingSchema] of Object.entries(beforePatterns)) {
|
|
2245
|
+
if (!patternsMayOverlap(existingPattern, pattern)) continue;
|
|
2246
|
+
diffSchema(
|
|
2247
|
+
existingSchema,
|
|
2248
|
+
afterPatterns[pattern],
|
|
2249
|
+
`${path}.patternProperties.${pattern}.overlap.${existingPattern}`,
|
|
2250
|
+
"input",
|
|
2251
|
+
overlapChanges
|
|
2272
2252
|
);
|
|
2273
2253
|
}
|
|
2274
|
-
argumentNames.push(argument.name);
|
|
2275
|
-
}
|
|
2276
|
-
if (new Set(argumentNames).size !== argumentNames.length) {
|
|
2277
|
-
throw new McpFnValidationError(`Manifest prompt ${prompt.name} has duplicate arguments`);
|
|
2278
2254
|
}
|
|
2279
|
-
if (
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
} catch {
|
|
2289
|
-
throw new McpFnValidationError(
|
|
2290
|
-
`Manifest prompt ${prompt.name} has an invalid arguments JSON Schema`
|
|
2291
|
-
);
|
|
2292
|
-
}
|
|
2293
|
-
assertPromptSchemaSupportsStringValues(
|
|
2294
|
-
prompt.argumentsSchema,
|
|
2295
|
-
`Manifest prompt ${prompt.name} argumentsSchema`
|
|
2296
|
-
);
|
|
2297
|
-
if (prompt.arguments) {
|
|
2298
|
-
const declared = prompt.arguments.map(({ name, required }) => ({ name, required: required === true })).sort((left, right) => compareCodeUnits(left.name, right.name));
|
|
2299
|
-
const schemaDeclared = schemaPromptArguments(prompt).map(
|
|
2300
|
-
({ name, required }) => ({ name, required: required === true })
|
|
2255
|
+
if (direction === "output" && hadBeforePattern && !hasAfterPattern) {
|
|
2256
|
+
for (const [remainingPattern, remainingSchema] of Object.entries(afterPatterns)) {
|
|
2257
|
+
if (!patternsMayOverlap(pattern, remainingPattern)) continue;
|
|
2258
|
+
diffSchema(
|
|
2259
|
+
beforePatterns[pattern],
|
|
2260
|
+
remainingSchema,
|
|
2261
|
+
`${path}.patternProperties.${pattern}.overlap.${remainingPattern}`,
|
|
2262
|
+
"output",
|
|
2263
|
+
overlapChanges
|
|
2301
2264
|
);
|
|
2302
|
-
if (JSON.stringify(declared) !== JSON.stringify(schemaDeclared)) {
|
|
2303
|
-
throw new McpFnValidationError(
|
|
2304
|
-
`Manifest prompt ${prompt.name} arguments and argumentsSchema disagree`
|
|
2305
|
-
);
|
|
2306
|
-
}
|
|
2307
2265
|
}
|
|
2308
2266
|
}
|
|
2267
|
+
if (overlapChanges.some((change) => change.severity === "breaking")) {
|
|
2268
|
+
push(changes, {
|
|
2269
|
+
severity: "breaking",
|
|
2270
|
+
code: "overlapping-pattern-constraint-changed",
|
|
2271
|
+
path: `${path}.patternProperties.${pattern}`,
|
|
2272
|
+
message: `${hadBeforePattern ? "Removing" : "Adding"} pattern ${pattern} can change constraints for properties matched by another pattern`,
|
|
2273
|
+
before: hadBeforePattern ? beforePatterns[pattern] : void 0,
|
|
2274
|
+
after: hasAfterPattern ? afterPatterns[pattern] : void 0
|
|
2275
|
+
});
|
|
2276
|
+
}
|
|
2309
2277
|
}
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2278
|
+
const handledKeywords = /* @__PURE__ */ new Set([
|
|
2279
|
+
"type",
|
|
2280
|
+
"enum",
|
|
2281
|
+
"properties",
|
|
2282
|
+
"required",
|
|
2283
|
+
"items",
|
|
2284
|
+
"additionalProperties",
|
|
2285
|
+
"patternProperties",
|
|
2286
|
+
"pattern",
|
|
2287
|
+
"format",
|
|
2288
|
+
"const",
|
|
2289
|
+
"uniqueItems",
|
|
2290
|
+
...tightening.map(([keyword]) => keyword)
|
|
2291
|
+
]);
|
|
2292
|
+
const behavioralKeywords = /* @__PURE__ */ new Set([
|
|
2293
|
+
"$comment",
|
|
2294
|
+
"$id",
|
|
2295
|
+
"$schema",
|
|
2296
|
+
"default",
|
|
2297
|
+
"deprecated",
|
|
2298
|
+
"description",
|
|
2299
|
+
"examples",
|
|
2300
|
+
"readOnly",
|
|
2301
|
+
"title",
|
|
2302
|
+
"writeOnly"
|
|
2303
|
+
]);
|
|
2304
|
+
const keywords = [.../* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)])].sort();
|
|
2305
|
+
for (const keyword of keywords) {
|
|
2306
|
+
if (handledKeywords.has(keyword) || equal(before[keyword], after[keyword])) continue;
|
|
2307
|
+
const behavioral = behavioralKeywords.has(keyword) || /^x-/i.test(keyword);
|
|
2308
|
+
push(changes, {
|
|
2309
|
+
severity: behavioral ? "behavioral" : "breaking",
|
|
2310
|
+
code: behavioral ? "schema-guidance-changed" : "schema-keyword-changed",
|
|
2311
|
+
path: `${path}.${keyword}`,
|
|
2312
|
+
message: `${keyword} changed at ${path}`,
|
|
2313
|
+
before: before[keyword],
|
|
2314
|
+
after: after[keyword]
|
|
2315
|
+
});
|
|
2313
2316
|
}
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2317
|
+
}
|
|
2318
|
+
function diffTool(before, after, changes) {
|
|
2319
|
+
const root = `tools.${before.name}`;
|
|
2320
|
+
if (before.title !== after.title) {
|
|
2321
|
+
push(changes, {
|
|
2322
|
+
severity: "behavioral",
|
|
2323
|
+
code: "tool-title-changed",
|
|
2324
|
+
path: `${root}.title`,
|
|
2325
|
+
message: `Tool title changed for ${before.name}`,
|
|
2326
|
+
before: before.title,
|
|
2327
|
+
after: after.title
|
|
2320
2328
|
});
|
|
2321
2329
|
}
|
|
2322
|
-
|
|
2330
|
+
if (before.description !== after.description) {
|
|
2331
|
+
push(changes, {
|
|
2332
|
+
severity: "behavioral",
|
|
2333
|
+
code: "tool-description-changed",
|
|
2334
|
+
path: `${root}.description`,
|
|
2335
|
+
message: `Tool description changed for ${before.name}`,
|
|
2336
|
+
before: before.description,
|
|
2337
|
+
after: after.description
|
|
2338
|
+
});
|
|
2339
|
+
}
|
|
2340
|
+
if (!equal(before.annotations ?? {}, after.annotations ?? {})) {
|
|
2341
|
+
push(changes, {
|
|
2342
|
+
severity: "behavioral",
|
|
2343
|
+
code: "tool-annotations-changed",
|
|
2344
|
+
path: `${root}.annotations`,
|
|
2345
|
+
message: `Tool annotations changed for ${before.name}`,
|
|
2346
|
+
before: before.annotations,
|
|
2347
|
+
after: after.annotations
|
|
2348
|
+
});
|
|
2349
|
+
}
|
|
2350
|
+
if (!equal(before.execution ?? {}, after.execution ?? {})) {
|
|
2351
|
+
const oldSupport = before.execution?.taskSupport ?? "forbidden";
|
|
2352
|
+
const newSupport = after.execution?.taskSupport ?? "forbidden";
|
|
2353
|
+
const breaking = newSupport === "required" || oldSupport !== "forbidden" && newSupport === "forbidden";
|
|
2354
|
+
push(changes, {
|
|
2355
|
+
severity: breaking ? "breaking" : "additive",
|
|
2356
|
+
code: "tool-task-support-changed",
|
|
2357
|
+
path: `${root}.execution.taskSupport`,
|
|
2358
|
+
message: `Tool task support changed from ${oldSupport} to ${newSupport}`,
|
|
2359
|
+
before: oldSupport,
|
|
2360
|
+
after: newSupport
|
|
2361
|
+
});
|
|
2362
|
+
}
|
|
2363
|
+
if (!equal(before.icons ?? [], after.icons ?? [])) {
|
|
2364
|
+
push(changes, {
|
|
2365
|
+
severity: "behavioral",
|
|
2366
|
+
code: "tool-icons-changed",
|
|
2367
|
+
path: `${root}.icons`,
|
|
2368
|
+
message: `Tool icons changed for ${before.name}`,
|
|
2369
|
+
before: before.icons,
|
|
2370
|
+
after: after.icons
|
|
2371
|
+
});
|
|
2372
|
+
}
|
|
2373
|
+
if (!equal(before.metadata ?? {}, after.metadata ?? {})) {
|
|
2374
|
+
push(changes, {
|
|
2375
|
+
severity: "behavioral",
|
|
2376
|
+
code: "tool-metadata-changed",
|
|
2377
|
+
path: `${root}.metadata`,
|
|
2378
|
+
message: `Tool metadata changed for ${before.name}`,
|
|
2379
|
+
before: before.metadata,
|
|
2380
|
+
after: after.metadata
|
|
2381
|
+
});
|
|
2382
|
+
}
|
|
2383
|
+
diffSchema(before.inputSchema, after.inputSchema, `${root}.inputSchema`, "input", changes);
|
|
2384
|
+
diffSchema(before.outputSchema, after.outputSchema, `${root}.outputSchema`, "output", changes);
|
|
2323
2385
|
}
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2386
|
+
function diffResource(before, after, changes) {
|
|
2387
|
+
const root = `resources.${before.uri}`;
|
|
2388
|
+
if ((before.subscribable ?? false) !== (after.subscribable ?? false)) {
|
|
2389
|
+
const enabled = after.subscribable === true;
|
|
2390
|
+
push(changes, {
|
|
2391
|
+
severity: enabled ? "additive" : "breaking",
|
|
2392
|
+
code: enabled ? "resource-subscription-enabled" : "resource-subscription-removed",
|
|
2393
|
+
path: `${root}.subscribable`,
|
|
2394
|
+
message: `Resource subscription support was ${enabled ? "enabled" : "removed"} for ${before.uri}`,
|
|
2395
|
+
before: before.subscribable,
|
|
2396
|
+
after: after.subscribable
|
|
2397
|
+
});
|
|
2398
|
+
}
|
|
2399
|
+
if (before.mimeType !== after.mimeType) {
|
|
2400
|
+
push(changes, {
|
|
2401
|
+
severity: "breaking",
|
|
2402
|
+
code: "resource-mime-type-changed",
|
|
2403
|
+
path: `${root}.mimeType`,
|
|
2404
|
+
message: `Resource MIME type changed for ${before.uri}`,
|
|
2405
|
+
before: before.mimeType,
|
|
2406
|
+
after: after.mimeType
|
|
2407
|
+
});
|
|
2408
|
+
}
|
|
2409
|
+
for (const key of ["name", "title", "description", "annotations", "icons", "metadata"]) {
|
|
2410
|
+
if (!equal(before[key], after[key])) {
|
|
2411
|
+
push(changes, {
|
|
2412
|
+
severity: "behavioral",
|
|
2413
|
+
code: `resource-${key}-changed`,
|
|
2414
|
+
path: `${root}.${key}`,
|
|
2415
|
+
message: `Resource ${key} changed for ${before.uri}`,
|
|
2416
|
+
before: before[key],
|
|
2417
|
+
after: after[key]
|
|
2418
|
+
});
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2328
2421
|
}
|
|
2329
|
-
function
|
|
2330
|
-
const
|
|
2331
|
-
if (
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2422
|
+
function diffResourceTemplate(before, after, changes) {
|
|
2423
|
+
const root = `resourceTemplates.${before.name}`;
|
|
2424
|
+
if ((before.subscribable ?? false) !== (after.subscribable ?? false)) {
|
|
2425
|
+
const enabled = after.subscribable === true;
|
|
2426
|
+
push(changes, {
|
|
2427
|
+
severity: enabled ? "additive" : "breaking",
|
|
2428
|
+
code: enabled ? "resource-template-subscription-enabled" : "resource-template-subscription-removed",
|
|
2429
|
+
path: `${root}.subscribable`,
|
|
2430
|
+
message: `Resource template subscription support was ${enabled ? "enabled" : "removed"} for ${before.name}`,
|
|
2431
|
+
before: before.subscribable,
|
|
2432
|
+
after: after.subscribable
|
|
2433
|
+
});
|
|
2434
|
+
}
|
|
2435
|
+
if (before.uriTemplate !== after.uriTemplate) {
|
|
2436
|
+
push(changes, {
|
|
2437
|
+
severity: "breaking",
|
|
2438
|
+
code: "resource-template-uri-changed",
|
|
2439
|
+
path: `${root}.uriTemplate`,
|
|
2440
|
+
message: `Resource template URI changed for ${before.name}`,
|
|
2441
|
+
before: before.uriTemplate,
|
|
2442
|
+
after: after.uriTemplate
|
|
2443
|
+
});
|
|
2444
|
+
}
|
|
2445
|
+
if (before.mimeType !== after.mimeType) {
|
|
2446
|
+
push(changes, {
|
|
2447
|
+
severity: "breaking",
|
|
2448
|
+
code: "resource-template-mime-type-changed",
|
|
2449
|
+
path: `${root}.mimeType`,
|
|
2450
|
+
message: `Resource template MIME type changed for ${before.name}`,
|
|
2451
|
+
before: before.mimeType,
|
|
2452
|
+
after: after.mimeType
|
|
2453
|
+
});
|
|
2335
2454
|
}
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
}
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
var import_stdio = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
2347
|
-
var import_webStandardStreamableHttp = require("@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js");
|
|
2348
|
-
var import_types2 = require("@modelcontextprotocol/sdk/types.js");
|
|
2349
|
-
function mergeCapabilities(base, extra) {
|
|
2350
|
-
const merged = { ...base, ...extra };
|
|
2351
|
-
for (const key of ["prompts", "resources", "tools", "tasks"]) {
|
|
2352
|
-
if (base[key] || extra?.[key]) {
|
|
2353
|
-
merged[key] = {
|
|
2354
|
-
...base[key],
|
|
2355
|
-
...extra?.[key]
|
|
2356
|
-
};
|
|
2455
|
+
for (const key of ["title", "description", "annotations", "icons", "metadata"]) {
|
|
2456
|
+
if (!equal(before[key], after[key])) {
|
|
2457
|
+
push(changes, {
|
|
2458
|
+
severity: "behavioral",
|
|
2459
|
+
code: `resource-template-${key}-changed`,
|
|
2460
|
+
path: `${root}.${key}`,
|
|
2461
|
+
message: `Resource template ${key} changed for ${before.name}`,
|
|
2462
|
+
before: before[key],
|
|
2463
|
+
after: after[key]
|
|
2464
|
+
});
|
|
2357
2465
|
}
|
|
2358
2466
|
}
|
|
2359
|
-
return merged;
|
|
2360
2467
|
}
|
|
2361
|
-
function
|
|
2362
|
-
|
|
2363
|
-
if (cursor !== void 0) {
|
|
2364
|
-
const match = /^mcpfn:(\d+)$/.exec(cursor);
|
|
2365
|
-
if (!match) throw new import_types2.McpError(import_types2.ErrorCode.InvalidParams, "Invalid McpFn pagination cursor");
|
|
2366
|
-
offset = Number(match[1]);
|
|
2367
|
-
if (!Number.isSafeInteger(offset) || offset > values.length) {
|
|
2368
|
-
throw new import_types2.McpError(import_types2.ErrorCode.InvalidParams, "Expired McpFn pagination cursor");
|
|
2369
|
-
}
|
|
2370
|
-
}
|
|
2371
|
-
const selected = values.slice(offset, offset + pageSize);
|
|
2372
|
-
const nextOffset = offset + selected.length;
|
|
2468
|
+
function promptArgumentSchema(prompt) {
|
|
2469
|
+
if (prompt.argumentsSchema) return prompt.argumentsSchema;
|
|
2373
2470
|
return {
|
|
2374
|
-
|
|
2375
|
-
|
|
2471
|
+
type: "object",
|
|
2472
|
+
properties: Object.fromEntries(
|
|
2473
|
+
(prompt.arguments ?? []).map((argument) => [argument.name, { type: "string" }])
|
|
2474
|
+
),
|
|
2475
|
+
required: (prompt.arguments ?? []).filter((argument) => argument.required).map((argument) => argument.name),
|
|
2476
|
+
additionalProperties: false
|
|
2376
2477
|
};
|
|
2377
2478
|
}
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
this.info = options.info;
|
|
2390
|
-
this.registry = options.registry;
|
|
2391
|
-
assertMcpAppContracts(this.registry);
|
|
2392
|
-
this.contextFactory = options.context ?? (() => void 0);
|
|
2393
|
-
this.toolVisibility = options.toolVisibility;
|
|
2394
|
-
this.pageSize = options.pageSize ?? 100;
|
|
2395
|
-
if (!Number.isInteger(this.pageSize) || this.pageSize < 1) {
|
|
2396
|
-
throw new Error("McpFn pageSize must be a positive integer");
|
|
2397
|
-
}
|
|
2398
|
-
const registryCapabilities = this.registry.capabilities();
|
|
2399
|
-
if (registryCapabilities.tasks && !options.taskStore) {
|
|
2400
|
-
throw new Error("Task-capable McpFn tools require a taskStore");
|
|
2401
|
-
}
|
|
2402
|
-
if (registryCapabilities.tasks && options.taskStore) {
|
|
2403
|
-
registryCapabilities.tasks = {
|
|
2404
|
-
...registryCapabilities.tasks,
|
|
2405
|
-
list: {},
|
|
2406
|
-
cancel: {}
|
|
2407
|
-
};
|
|
2408
|
-
}
|
|
2409
|
-
this.capabilities = mergeCapabilities(registryCapabilities, options.additionalCapabilities);
|
|
2410
|
-
this.manifestOptions = {
|
|
2411
|
-
protocolVersions: options.protocolVersions,
|
|
2412
|
-
transports: options.transports,
|
|
2413
|
-
extensions: options.extensions,
|
|
2414
|
-
capabilities: this.capabilities,
|
|
2415
|
-
clientRequirements: options.clientRequirements
|
|
2416
|
-
};
|
|
2417
|
-
this.protocol = new import_server.Server(
|
|
2418
|
-
{ name: options.info.name, version: options.info.version },
|
|
2419
|
-
{
|
|
2420
|
-
capabilities: this.capabilities,
|
|
2421
|
-
instructions: options.info.instructions,
|
|
2422
|
-
taskStore: options.taskStore,
|
|
2423
|
-
taskMessageQueue: options.taskMessageQueue,
|
|
2424
|
-
defaultTaskPollInterval: options.defaultTaskPollInterval,
|
|
2425
|
-
maxTaskQueueSize: options.maxTaskQueueSize,
|
|
2426
|
-
enforceStrictCapabilities: options.enforceStrictCapabilities,
|
|
2427
|
-
debouncedNotificationMethods: [
|
|
2428
|
-
"notifications/tools/list_changed",
|
|
2429
|
-
"notifications/resources/list_changed",
|
|
2430
|
-
"notifications/prompts/list_changed"
|
|
2431
|
-
]
|
|
2432
|
-
}
|
|
2433
|
-
);
|
|
2434
|
-
this.installHandlers();
|
|
2435
|
-
}
|
|
2436
|
-
installHandlers() {
|
|
2437
|
-
if (this.capabilities.tools) {
|
|
2438
|
-
this.protocol.setRequestHandler(import_types2.ListToolsRequestSchema, async (request, extra) => {
|
|
2439
|
-
const tools = this.registry.listTools();
|
|
2440
|
-
const visibleTools = this.toolVisibility ? await this.filterVisibleTools(tools, await this.contextFactory(extra), extra) : tools;
|
|
2441
|
-
const result = page(visibleTools, request.params?.cursor, this.pageSize);
|
|
2442
|
-
return { tools: result.values, ...result.nextCursor ? { nextCursor: result.nextCursor } : {} };
|
|
2443
|
-
});
|
|
2444
|
-
this.protocol.setRequestHandler(import_types2.CallToolRequestSchema, async (request, extra) => {
|
|
2445
|
-
const context = await this.contextFactory(extra);
|
|
2446
|
-
const listedTool = this.registry.listTools().find((tool) => tool.name === request.params.name);
|
|
2447
|
-
if (!listedTool || this.toolVisibility && !await this.toolVisibility({ tool: listedTool, context, extra })) {
|
|
2448
|
-
throw new import_types2.McpError(import_types2.ErrorCode.MethodNotFound, `Tool ${request.params.name} not found`);
|
|
2449
|
-
}
|
|
2450
|
-
const taskSupport = this.registry.taskSupport(request.params.name);
|
|
2451
|
-
const isTaskRequest = Boolean(request.params.task);
|
|
2452
|
-
if (taskSupport === "required" && !isTaskRequest) {
|
|
2453
|
-
throw new import_types2.McpError(
|
|
2454
|
-
import_types2.ErrorCode.MethodNotFound,
|
|
2455
|
-
`Tool ${request.params.name} requires task augmentation`
|
|
2456
|
-
);
|
|
2457
|
-
}
|
|
2458
|
-
if (taskSupport === "forbidden" && isTaskRequest) {
|
|
2459
|
-
throw new import_types2.McpError(
|
|
2460
|
-
import_types2.ErrorCode.MethodNotFound,
|
|
2461
|
-
`Tool ${request.params.name} does not support task augmentation`
|
|
2462
|
-
);
|
|
2463
|
-
}
|
|
2464
|
-
try {
|
|
2465
|
-
if (isTaskRequest) {
|
|
2466
|
-
if (!extra.taskStore) throw new Error("No task store is available");
|
|
2467
|
-
return await this.registry.createToolTask(
|
|
2468
|
-
request.params.name,
|
|
2469
|
-
request.params.arguments,
|
|
2470
|
-
context,
|
|
2471
|
-
extra
|
|
2472
|
-
);
|
|
2473
|
-
}
|
|
2474
|
-
return await this.registry.callTool(
|
|
2475
|
-
request.params.name,
|
|
2476
|
-
request.params.arguments,
|
|
2477
|
-
context,
|
|
2478
|
-
extra
|
|
2479
|
-
);
|
|
2480
|
-
} catch (error) {
|
|
2481
|
-
if (isTaskRequest) throw error;
|
|
2482
|
-
return errorResult(error, {
|
|
2483
|
-
includeStructuredContent: !this.registry.hasOutputSchema(request.params.name)
|
|
2484
|
-
});
|
|
2485
|
-
}
|
|
2486
|
-
});
|
|
2487
|
-
}
|
|
2488
|
-
if (this.capabilities.resources) {
|
|
2489
|
-
this.protocol.setRequestHandler(import_types2.ListResourcesRequestSchema, async (request, extra) => {
|
|
2490
|
-
const context = await this.contextFactory(extra);
|
|
2491
|
-
const result = page(
|
|
2492
|
-
await this.registry.listResources(context, extra),
|
|
2493
|
-
request.params?.cursor,
|
|
2494
|
-
this.pageSize
|
|
2495
|
-
);
|
|
2496
|
-
return {
|
|
2497
|
-
resources: result.values,
|
|
2498
|
-
...result.nextCursor ? { nextCursor: result.nextCursor } : {}
|
|
2499
|
-
};
|
|
2500
|
-
});
|
|
2501
|
-
this.protocol.setRequestHandler(import_types2.ListResourceTemplatesRequestSchema, async (request) => {
|
|
2502
|
-
const result = page(
|
|
2503
|
-
this.registry.listResourceTemplates(),
|
|
2504
|
-
request.params?.cursor,
|
|
2505
|
-
this.pageSize
|
|
2506
|
-
);
|
|
2507
|
-
return {
|
|
2508
|
-
resourceTemplates: result.values,
|
|
2509
|
-
...result.nextCursor ? { nextCursor: result.nextCursor } : {}
|
|
2510
|
-
};
|
|
2511
|
-
});
|
|
2512
|
-
this.protocol.setRequestHandler(import_types2.ReadResourceRequestSchema, async (request, extra) => {
|
|
2513
|
-
const context = await this.contextFactory(extra);
|
|
2514
|
-
return this.registry.readResource(request.params.uri, context, extra);
|
|
2479
|
+
function diffPrompt(before, after, changes) {
|
|
2480
|
+
const root = `prompts.${before.name}`;
|
|
2481
|
+
for (const key of ["title", "description", "icons", "metadata"]) {
|
|
2482
|
+
if (!equal(before[key], after[key])) {
|
|
2483
|
+
push(changes, {
|
|
2484
|
+
severity: "behavioral",
|
|
2485
|
+
code: `prompt-${key}-changed`,
|
|
2486
|
+
path: `${root}.${key}`,
|
|
2487
|
+
message: `Prompt ${key} changed for ${before.name}`,
|
|
2488
|
+
before: before[key],
|
|
2489
|
+
after: after[key]
|
|
2515
2490
|
});
|
|
2516
|
-
if (this.capabilities.resources.subscribe) {
|
|
2517
|
-
this.protocol.setRequestHandler(import_types2.SubscribeRequestSchema, async (request, extra) => {
|
|
2518
|
-
const context = await this.contextFactory(extra);
|
|
2519
|
-
await this.registry.changeSubscription(request.params.uri, true, context, extra);
|
|
2520
|
-
return {};
|
|
2521
|
-
});
|
|
2522
|
-
this.protocol.setRequestHandler(import_types2.UnsubscribeRequestSchema, async (request, extra) => {
|
|
2523
|
-
const context = await this.contextFactory(extra);
|
|
2524
|
-
await this.registry.changeSubscription(request.params.uri, false, context, extra);
|
|
2525
|
-
return {};
|
|
2526
|
-
});
|
|
2527
|
-
}
|
|
2528
2491
|
}
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2492
|
+
}
|
|
2493
|
+
diffSchema(
|
|
2494
|
+
promptArgumentSchema(before),
|
|
2495
|
+
promptArgumentSchema(after),
|
|
2496
|
+
`${root}.arguments`,
|
|
2497
|
+
"input",
|
|
2498
|
+
changes
|
|
2499
|
+
);
|
|
2500
|
+
if (!equal(before.arguments ?? [], after.arguments ?? [])) {
|
|
2501
|
+
push(changes, {
|
|
2502
|
+
severity: "behavioral",
|
|
2503
|
+
code: "prompt-arguments-changed",
|
|
2504
|
+
path: `${root}.argumentsMetadata`,
|
|
2505
|
+
message: `Prompt argument metadata changed for ${before.name}`,
|
|
2506
|
+
before: before.arguments,
|
|
2507
|
+
after: after.arguments
|
|
2508
|
+
});
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
function requirementValues(value) {
|
|
2512
|
+
if (value === true) return /* @__PURE__ */ new Set(["true"]);
|
|
2513
|
+
if (Array.isArray(value)) return new Set(value.map((entry) => canonicalJson(entry)));
|
|
2514
|
+
return /* @__PURE__ */ new Set();
|
|
2515
|
+
}
|
|
2516
|
+
function extensionRequired(value) {
|
|
2517
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value) && value.required === true);
|
|
2518
|
+
}
|
|
2519
|
+
function diffCollection(label, before, after, key, compare, changes) {
|
|
2520
|
+
const beforeValues = new Map(before.map((value) => [key(value), value]));
|
|
2521
|
+
const afterValues = new Map(after.map((value) => [key(value), value]));
|
|
2522
|
+
for (const name of [...beforeValues.keys()].sort()) {
|
|
2523
|
+
const next = afterValues.get(name);
|
|
2524
|
+
if (!next) {
|
|
2525
|
+
push(changes, {
|
|
2526
|
+
severity: "breaking",
|
|
2527
|
+
code: `${label}-removed`,
|
|
2528
|
+
path: `${label}.${name}`,
|
|
2529
|
+
message: `${label} ${name} was removed`,
|
|
2530
|
+
before: beforeValues.get(name)
|
|
2545
2531
|
});
|
|
2546
|
-
}
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
);
|
|
2532
|
+
} else compare(beforeValues.get(name), next, changes);
|
|
2533
|
+
}
|
|
2534
|
+
for (const name of [...afterValues.keys()].sort()) {
|
|
2535
|
+
if (!beforeValues.has(name)) {
|
|
2536
|
+
push(changes, {
|
|
2537
|
+
severity: "additive",
|
|
2538
|
+
code: `${label}-added`,
|
|
2539
|
+
path: `${label}.${name}`,
|
|
2540
|
+
message: `${label} ${name} was added`,
|
|
2541
|
+
after: afterValues.get(name)
|
|
2557
2542
|
});
|
|
2558
2543
|
}
|
|
2559
2544
|
}
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
}
|
|
2573
|
-
async serveStdio() {
|
|
2574
|
-
await this.connect(new import_stdio.StdioServerTransport());
|
|
2575
|
-
}
|
|
2576
|
-
async createWebStandardHandler(options = {}) {
|
|
2577
|
-
const transport = new import_webStandardStreamableHttp.WebStandardStreamableHTTPServerTransport(options);
|
|
2578
|
-
await this.connect(transport);
|
|
2579
|
-
return (request, handleOptions) => transport.handleRequest(request, handleOptions);
|
|
2545
|
+
}
|
|
2546
|
+
function diffManifests(before, after) {
|
|
2547
|
+
const changes = [];
|
|
2548
|
+
if (before.server.name !== after.server.name) {
|
|
2549
|
+
push(changes, {
|
|
2550
|
+
severity: "breaking",
|
|
2551
|
+
code: "server-name-changed",
|
|
2552
|
+
path: "server.name",
|
|
2553
|
+
message: `Server name changed from ${before.server.name} to ${after.server.name}`,
|
|
2554
|
+
before: before.server.name,
|
|
2555
|
+
after: after.server.name
|
|
2556
|
+
});
|
|
2580
2557
|
}
|
|
2581
|
-
|
|
2582
|
-
|
|
2558
|
+
if (before.server.instructions !== after.server.instructions) {
|
|
2559
|
+
push(changes, {
|
|
2560
|
+
severity: "behavioral",
|
|
2561
|
+
code: "server-instructions-changed",
|
|
2562
|
+
path: "server.instructions",
|
|
2563
|
+
message: "Server instructions changed",
|
|
2564
|
+
before: before.server.instructions,
|
|
2565
|
+
after: after.server.instructions
|
|
2566
|
+
});
|
|
2583
2567
|
}
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2568
|
+
const beforeTools = new Map(before.tools.map((tool) => [tool.name, tool]));
|
|
2569
|
+
const afterTools = new Map(after.tools.map((tool) => [tool.name, tool]));
|
|
2570
|
+
for (const name of [...beforeTools.keys()].sort()) {
|
|
2571
|
+
const oldTool = beforeTools.get(name);
|
|
2572
|
+
const newTool = afterTools.get(name);
|
|
2573
|
+
if (!newTool) {
|
|
2574
|
+
push(changes, {
|
|
2575
|
+
severity: "breaking",
|
|
2576
|
+
code: "tool-removed",
|
|
2577
|
+
path: `tools.${name}`,
|
|
2578
|
+
message: `Tool ${name} was removed`,
|
|
2579
|
+
before: oldTool
|
|
2580
|
+
});
|
|
2581
|
+
} else {
|
|
2582
|
+
diffTool(oldTool, newTool, changes);
|
|
2583
|
+
}
|
|
2591
2584
|
}
|
|
2592
|
-
|
|
2593
|
-
|
|
2585
|
+
for (const name of [...afterTools.keys()].sort()) {
|
|
2586
|
+
if (!beforeTools.has(name)) {
|
|
2587
|
+
push(changes, {
|
|
2588
|
+
severity: "additive",
|
|
2589
|
+
code: "tool-added",
|
|
2590
|
+
path: `tools.${name}`,
|
|
2591
|
+
message: `Tool ${name} was added`,
|
|
2592
|
+
after: afterTools.get(name)
|
|
2593
|
+
});
|
|
2594
|
+
}
|
|
2594
2595
|
}
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2596
|
+
diffCollection(
|
|
2597
|
+
"resources",
|
|
2598
|
+
before.resources ?? [],
|
|
2599
|
+
after.resources ?? [],
|
|
2600
|
+
(resource) => resource.uri,
|
|
2601
|
+
diffResource,
|
|
2602
|
+
changes
|
|
2603
|
+
);
|
|
2604
|
+
diffCollection(
|
|
2605
|
+
"resourceTemplates",
|
|
2606
|
+
before.resourceTemplates ?? [],
|
|
2607
|
+
after.resourceTemplates ?? [],
|
|
2608
|
+
(resource) => resource.name,
|
|
2609
|
+
diffResourceTemplate,
|
|
2610
|
+
changes
|
|
2611
|
+
);
|
|
2612
|
+
diffCollection(
|
|
2613
|
+
"prompts",
|
|
2614
|
+
before.prompts ?? [],
|
|
2615
|
+
after.prompts ?? [],
|
|
2616
|
+
(prompt) => prompt.name,
|
|
2617
|
+
diffPrompt,
|
|
2618
|
+
changes
|
|
2619
|
+
);
|
|
2620
|
+
for (const key of ["capabilities", "clientRequirements", "extensions"]) {
|
|
2621
|
+
const oldRecord = before[key] ?? {};
|
|
2622
|
+
const newRecord = after[key] ?? {};
|
|
2623
|
+
for (const name of [.../* @__PURE__ */ new Set([...Object.keys(oldRecord), ...Object.keys(newRecord)])].sort()) {
|
|
2624
|
+
if (equal(oldRecord[name], newRecord[name])) continue;
|
|
2625
|
+
const removed = oldRecord[name] !== void 0 && newRecord[name] === void 0;
|
|
2626
|
+
const added = oldRecord[name] === void 0 && newRecord[name] !== void 0;
|
|
2627
|
+
const requirementChanged = key === "clientRequirements";
|
|
2628
|
+
const oldRequiredValues = requirementValues(oldRecord[name]);
|
|
2629
|
+
const newRequiredValues = requirementValues(newRecord[name]);
|
|
2630
|
+
const requirementAdded = [...newRequiredValues].some((value) => !oldRequiredValues.has(value));
|
|
2631
|
+
const requirementRemoved = [...oldRequiredValues].some((value) => !newRequiredValues.has(value));
|
|
2632
|
+
if (requirementChanged && !requirementAdded && !requirementRemoved) continue;
|
|
2633
|
+
const extensionWasRequired = key === "extensions" && extensionRequired(oldRecord[name]);
|
|
2634
|
+
const extensionIsRequired = key === "extensions" && extensionRequired(newRecord[name]);
|
|
2635
|
+
const capabilityChanges = key === "capabilities" && !removed && !added ? nestedCapabilityChanges(oldRecord[name], newRecord[name], `${key}.${name}`) : [];
|
|
2636
|
+
if (capabilityChanges.length) {
|
|
2637
|
+
for (const capabilityChange of capabilityChanges) {
|
|
2638
|
+
const action = capabilityChange.severity === "breaking" ? "removed" : capabilityChange.severity === "additive" ? "added" : "changed";
|
|
2639
|
+
push(changes, {
|
|
2640
|
+
severity: capabilityChange.severity,
|
|
2641
|
+
code: `capability-support-${action}`,
|
|
2642
|
+
path: capabilityChange.path,
|
|
2643
|
+
message: `Capability support was ${action} at ${capabilityChange.path}`,
|
|
2644
|
+
before: capabilityChange.before,
|
|
2645
|
+
after: capabilityChange.after
|
|
2646
|
+
});
|
|
2647
|
+
}
|
|
2648
|
+
continue;
|
|
2649
|
+
}
|
|
2650
|
+
push(changes, {
|
|
2651
|
+
severity: requirementChanged && requirementAdded ? "breaking" : requirementChanged && requirementRemoved ? "additive" : key === "extensions" && !extensionWasRequired && extensionIsRequired ? "breaking" : key === "extensions" && extensionWasRequired && !extensionIsRequired ? "additive" : removed ? "breaking" : added ? "additive" : "behavioral",
|
|
2652
|
+
code: `${key}-${removed ? "removed" : added ? "added" : "changed"}`,
|
|
2653
|
+
path: `${key}.${name}`,
|
|
2654
|
+
message: `${key} ${name} was ${removed ? "removed" : added ? "added" : "changed"}`,
|
|
2655
|
+
before: oldRecord[name],
|
|
2656
|
+
after: newRecord[name]
|
|
2657
|
+
});
|
|
2658
|
+
}
|
|
2601
2659
|
}
|
|
2602
|
-
|
|
2603
|
-
|
|
2660
|
+
for (const key of ["protocolVersions", "transports"]) {
|
|
2661
|
+
const oldValues = new Set(before[key] ?? []);
|
|
2662
|
+
const newValues = new Set(after[key] ?? []);
|
|
2663
|
+
for (const value of [...oldValues].sort()) {
|
|
2664
|
+
if (!newValues.has(value)) {
|
|
2665
|
+
push(changes, {
|
|
2666
|
+
severity: "breaking",
|
|
2667
|
+
code: `${key}-support-removed`,
|
|
2668
|
+
path: key,
|
|
2669
|
+
message: `${key} support removed: ${value}`,
|
|
2670
|
+
before: value
|
|
2671
|
+
});
|
|
2672
|
+
}
|
|
2673
|
+
}
|
|
2674
|
+
for (const value of [...newValues].sort()) {
|
|
2675
|
+
if (!oldValues.has(value)) {
|
|
2676
|
+
push(changes, {
|
|
2677
|
+
severity: "additive",
|
|
2678
|
+
code: `${key}-support-added`,
|
|
2679
|
+
path: key,
|
|
2680
|
+
message: `${key} support added: ${value}`,
|
|
2681
|
+
after: value
|
|
2682
|
+
});
|
|
2683
|
+
}
|
|
2684
|
+
}
|
|
2604
2685
|
}
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2686
|
+
changes.sort(
|
|
2687
|
+
(left, right) => compareCodeUnits(left.path, right.path) || compareCodeUnits(left.code, right.code)
|
|
2688
|
+
);
|
|
2689
|
+
const summary = {
|
|
2690
|
+
breaking: changes.filter((change) => change.severity === "breaking").length,
|
|
2691
|
+
additive: changes.filter((change) => change.severity === "additive").length,
|
|
2692
|
+
behavioral: changes.filter((change) => change.severity === "behavioral").length
|
|
2693
|
+
};
|
|
2694
|
+
return { compatible: summary.breaking === 0, changes, summary };
|
|
2695
|
+
}
|
|
2696
|
+
|
|
2697
|
+
// src/results.ts
|
|
2698
|
+
function textResult(text) {
|
|
2699
|
+
return { content: [{ type: "text", text }] };
|
|
2700
|
+
}
|
|
2701
|
+
function structuredResult(structuredContent, content) {
|
|
2702
|
+
const normalized = jsonSafe(structuredContent);
|
|
2703
|
+
if (!normalized || typeof normalized !== "object" || Array.isArray(normalized)) {
|
|
2704
|
+
throw new McpFnOutputValidationError(
|
|
2705
|
+
"structuredContent must serialize to an object"
|
|
2610
2706
|
);
|
|
2611
2707
|
}
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
}
|
|
2618
|
-
sendResourceListChanged() {
|
|
2619
|
-
return this.protocol.sendResourceListChanged();
|
|
2620
|
-
}
|
|
2621
|
-
sendToolListChanged() {
|
|
2622
|
-
return this.protocol.sendToolListChanged();
|
|
2623
|
-
}
|
|
2624
|
-
sendPromptListChanged() {
|
|
2625
|
-
return this.protocol.sendPromptListChanged();
|
|
2626
|
-
}
|
|
2627
|
-
async close() {
|
|
2628
|
-
await this.protocol.close();
|
|
2629
|
-
this.connected = false;
|
|
2630
|
-
}
|
|
2631
|
-
};
|
|
2632
|
-
function createMcpFnServer(options) {
|
|
2633
|
-
return new McpFnServer(options);
|
|
2708
|
+
return {
|
|
2709
|
+
content: content ?? [
|
|
2710
|
+
{ type: "text", text: JSON.stringify(normalized, null, 2) }
|
|
2711
|
+
],
|
|
2712
|
+
structuredContent: normalized
|
|
2713
|
+
};
|
|
2634
2714
|
}
|
|
2635
2715
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2636
2716
|
0 && (module.exports = {
|
|
@@ -2640,6 +2720,7 @@ function createMcpFnServer(options) {
|
|
|
2640
2720
|
McpFnOutputValidationError,
|
|
2641
2721
|
McpFnRegistry,
|
|
2642
2722
|
McpFnServer,
|
|
2723
|
+
McpFnServerDeclaration,
|
|
2643
2724
|
McpFnValidationError,
|
|
2644
2725
|
assertMcpAppContracts,
|
|
2645
2726
|
assertPromptSchemaSupportsStringValues,
|
|
@@ -2649,11 +2730,13 @@ function createMcpFnServer(options) {
|
|
|
2649
2730
|
createManifest,
|
|
2650
2731
|
createMcpAppResource,
|
|
2651
2732
|
createMcpFnServer,
|
|
2733
|
+
defineMcpFnServer,
|
|
2652
2734
|
diffManifests,
|
|
2653
2735
|
errorResult,
|
|
2654
2736
|
jsonSafe,
|
|
2655
2737
|
mcpAppToolMetadata,
|
|
2656
2738
|
promptArguments,
|
|
2739
|
+
resolveMcpFnServerCapabilities,
|
|
2657
2740
|
schemaPromptArguments,
|
|
2658
2741
|
sha256,
|
|
2659
2742
|
structuredResult,
|