@softure-ai/mcp-access 0.2.6 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/README.md +102 -2
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/messages/en.d.ts +8 -0
- package/dist/messages/en.d.ts.map +1 -1
- package/dist/messages/en.js +8 -0
- package/dist/messages/en.js.map +1 -1
- package/dist/messages/index.d.ts +8 -0
- package/dist/messages/index.d.ts.map +1 -1
- package/dist/server/index.d.ts +4 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/server-factory.d.ts +31 -0
- package/dist/server/server-factory.d.ts.map +1 -0
- package/dist/server/server-factory.js +49 -0
- package/dist/server/server-factory.js.map +1 -0
- package/dist/server/server-info.d.ts +34 -0
- package/dist/server/server-info.d.ts.map +1 -0
- package/dist/server/server-info.js +30 -0
- package/dist/server/server-info.js.map +1 -0
- package/dist/server/strict-input.d.ts +17 -0
- package/dist/server/strict-input.d.ts.map +1 -0
- package/dist/server/strict-input.js +157 -0
- package/dist/server/strict-input.js.map +1 -0
- package/dist/server/tool-list.d.ts +35 -0
- package/dist/server/tool-list.d.ts.map +1 -0
- package/dist/server/tool-list.js +68 -0
- package/dist/server/tool-list.js.map +1 -0
- package/dist/testing/catalog.d.ts +8 -0
- package/dist/testing/catalog.d.ts.map +1 -1
- package/dist/testing/catalog.js +8 -19
- package/dist/testing/catalog.js.map +1 -1
- package/dist/testing/index.d.ts +1 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +1 -1
- package/dist/testing/index.js.map +1 -1
- package/module.json +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/messages/en.ts +9 -0
- package/src/server/index.ts +22 -0
- package/src/server/server-factory.ts +78 -0
- package/src/server/server-info.ts +76 -0
- package/src/server/strict-input.ts +191 -0
- package/src/server/tool-list.ts +105 -0
- package/src/testing/catalog.ts +14 -23
- package/src/testing/index.ts +7 -1
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { formatMessage } from "@softure-ai/core";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { en } from "../messages/en.js";
|
|
4
|
+
/** A JSON number literal: no spaces, no hex, no empty text. */
|
|
5
|
+
const NUMBER_TEXT = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
|
|
6
|
+
const strictServers = new WeakSet();
|
|
7
|
+
/** Makes every tool registered on the server from now on strict; wrapping a server twice changes nothing. */
|
|
8
|
+
export function withStrictToolInput(server, options = {}) {
|
|
9
|
+
if (strictServers.has(server))
|
|
10
|
+
return server;
|
|
11
|
+
// The SDK's overloads only differ in how the schema is typed; at runtime it is one method.
|
|
12
|
+
const register = server.registerTool.bind(server);
|
|
13
|
+
const registerStrict = (name, config, callback) => register(name, config.inputSchema === undefined ? config : { ...config, inputSchema: toStrictToolInput(config.inputSchema, options) }, callback);
|
|
14
|
+
Object.defineProperty(server, "registerTool", { value: registerStrict, configurable: true, writable: true });
|
|
15
|
+
strictServers.add(server);
|
|
16
|
+
return server;
|
|
17
|
+
}
|
|
18
|
+
/** The strict form of a tool's input schema: a zod 4 schema or raw shape is rewritten, anything else passes unchanged. */
|
|
19
|
+
export function toStrictToolInput(schema, options = {}) {
|
|
20
|
+
const zodSchema = isZodSchema(schema) ? schema : isRawShape(schema) ? z.object(schema) : null;
|
|
21
|
+
if (zodSchema === null)
|
|
22
|
+
return schema;
|
|
23
|
+
const rewriter = new StrictRewriter({
|
|
24
|
+
renamedFields: options.renamedFields ?? {},
|
|
25
|
+
coerceTextScalars: options.coerceTextScalars ?? true,
|
|
26
|
+
messages: options.messages ?? en.toolInput,
|
|
27
|
+
});
|
|
28
|
+
return rewriter.rewrite(zodSchema);
|
|
29
|
+
}
|
|
30
|
+
function isZodSchema(value) {
|
|
31
|
+
return typeof value === "object" && value !== null && "_zod" in value;
|
|
32
|
+
}
|
|
33
|
+
function isRawShape(value) {
|
|
34
|
+
if (typeof value !== "object" || value === null || Object.getPrototypeOf(value) !== Object.prototype)
|
|
35
|
+
return false;
|
|
36
|
+
return Object.values(value).every(isZodSchema);
|
|
37
|
+
}
|
|
38
|
+
class StrictRewriter {
|
|
39
|
+
settings;
|
|
40
|
+
// The same node (a shared field, a recursive `z.lazy`) is rewritten once.
|
|
41
|
+
rewritten = new Map();
|
|
42
|
+
constructor(settings) {
|
|
43
|
+
this.settings = settings;
|
|
44
|
+
}
|
|
45
|
+
rewrite(schema) {
|
|
46
|
+
const known = this.rewritten.get(schema);
|
|
47
|
+
if (known !== undefined)
|
|
48
|
+
return known;
|
|
49
|
+
const result = this.rewriteNode(schema);
|
|
50
|
+
this.rewritten.set(schema, result);
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
rewriteNode(schema) {
|
|
54
|
+
const def = schema._zod.def;
|
|
55
|
+
switch (def.type) {
|
|
56
|
+
case "object":
|
|
57
|
+
return this.rewriteObject(schema);
|
|
58
|
+
case "array":
|
|
59
|
+
return this.cloneWith(schema, { element: this.rewrite(def.element) });
|
|
60
|
+
case "optional":
|
|
61
|
+
case "nullable":
|
|
62
|
+
case "default":
|
|
63
|
+
case "prefault":
|
|
64
|
+
case "nonoptional":
|
|
65
|
+
case "readonly":
|
|
66
|
+
case "catch":
|
|
67
|
+
return this.cloneWith(schema, { innerType: this.rewrite(def.innerType) });
|
|
68
|
+
case "union":
|
|
69
|
+
return this.cloneWith(schema, { options: def.options.map((option) => this.rewrite(option)) });
|
|
70
|
+
case "intersection":
|
|
71
|
+
return this.cloneWith(schema, { left: this.rewrite(def.left), right: this.rewrite(def.right) });
|
|
72
|
+
case "record":
|
|
73
|
+
return this.cloneWith(schema, { valueType: this.rewrite(def.valueType) });
|
|
74
|
+
case "tuple":
|
|
75
|
+
return this.cloneWith(schema, {
|
|
76
|
+
items: def.items.map((item) => this.rewrite(item)),
|
|
77
|
+
rest: def.rest === null ? null : this.rewrite(def.rest),
|
|
78
|
+
});
|
|
79
|
+
case "pipe":
|
|
80
|
+
return this.rewritePipe(schema, def);
|
|
81
|
+
case "lazy": {
|
|
82
|
+
const getter = def.getter;
|
|
83
|
+
return this.cloneWith(schema, { getter: () => this.rewrite(getter()) });
|
|
84
|
+
}
|
|
85
|
+
case "boolean":
|
|
86
|
+
return this.settings.coerceTextScalars && def.coerce !== true ? z.preprocess(readBooleanText, schema) : schema;
|
|
87
|
+
case "number":
|
|
88
|
+
return this.settings.coerceTextScalars && def.coerce !== true ? z.preprocess(readNumberText, schema) : schema;
|
|
89
|
+
default:
|
|
90
|
+
return schema;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** Only the input side: `in`, or `out` after a preprocess (whose `in` is the transform). */
|
|
94
|
+
rewritePipe(schema, def) {
|
|
95
|
+
const input = def.in;
|
|
96
|
+
if (input._zod.traits.has("$ZodTransform"))
|
|
97
|
+
return this.cloneWith(schema, { out: this.rewrite(def.out) });
|
|
98
|
+
return this.cloneWith(schema, { in: this.rewrite(input) });
|
|
99
|
+
}
|
|
100
|
+
rewriteObject(schema) {
|
|
101
|
+
const def = schema._zod.def;
|
|
102
|
+
const shape = Object.fromEntries(Object.entries(def.shape).map(([name, field]) => [name, this.rewrite(field)]));
|
|
103
|
+
// An object with its own catchall (`looseObject`, `.catchall(x)`) accepts extra fields on purpose.
|
|
104
|
+
if (def.catchall !== undefined && def.catchall._zod.def.type !== "never")
|
|
105
|
+
return this.cloneWith(schema, { shape });
|
|
106
|
+
// The object's own map, read for any issue: its parameter type is narrowed to the issues an object raises.
|
|
107
|
+
const ownError = def.error;
|
|
108
|
+
const describe = (issue) => describeUnknownFields(issue, Object.keys(shape), this.settings);
|
|
109
|
+
const error = (issue) => describe(issue) ?? ownError?.(issue);
|
|
110
|
+
return this.cloneWith(schema, { shape, catchall: z.never(), error });
|
|
111
|
+
}
|
|
112
|
+
/** A copy of the node with some parts replaced; its description and other metadata stay with it. */
|
|
113
|
+
cloneWith(schema, parts) {
|
|
114
|
+
const copy = schema.clone({ ...schema._zod.def, ...parts });
|
|
115
|
+
const meta = z.globalRegistry.get(schema);
|
|
116
|
+
if (meta !== undefined) {
|
|
117
|
+
// An id names one schema in the JSON Schema output; the copy replaces the original, so it may not carry it twice.
|
|
118
|
+
const rest = { ...meta };
|
|
119
|
+
delete rest.id;
|
|
120
|
+
z.globalRegistry.add(copy, rest);
|
|
121
|
+
}
|
|
122
|
+
return copy;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** The text for an object's unknown fields: renamed ones first, then the rest with the fields it accepts. */
|
|
126
|
+
function describeUnknownFields(issue, accepted, settings) {
|
|
127
|
+
if (issue.code !== "unrecognized_keys")
|
|
128
|
+
return undefined;
|
|
129
|
+
const { messages, renamedFields } = settings;
|
|
130
|
+
const renamed = [];
|
|
131
|
+
const unknown = [];
|
|
132
|
+
for (const field of issue.keys) {
|
|
133
|
+
const newName = Object.hasOwn(renamedFields, field) ? renamedFields[field] : undefined;
|
|
134
|
+
if (newName !== undefined && accepted.includes(newName))
|
|
135
|
+
renamed.push(formatMessage(messages.fieldRenamed, { field, renamed: newName }));
|
|
136
|
+
else
|
|
137
|
+
unknown.push(field);
|
|
138
|
+
}
|
|
139
|
+
if (unknown.length > 0) {
|
|
140
|
+
renamed.push(formatMessage(messages.unknownFields, { fields: quoteAll(unknown), accepted: accepted.length > 0 ? quoteAll(accepted) : messages.noFields }));
|
|
141
|
+
}
|
|
142
|
+
return renamed.join(" ");
|
|
143
|
+
}
|
|
144
|
+
function quoteAll(names) {
|
|
145
|
+
return names.map((name) => JSON.stringify(name)).join(", ");
|
|
146
|
+
}
|
|
147
|
+
function readBooleanText(value) {
|
|
148
|
+
if (value === "true")
|
|
149
|
+
return true;
|
|
150
|
+
if (value === "false")
|
|
151
|
+
return false;
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
function readNumberText(value) {
|
|
155
|
+
return typeof value === "string" && NUMBER_TEXT.test(value) ? Number(value) : value;
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=strict-input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strict-input.js","sourceRoot":"","sources":["../../src/server/strict-input.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAC;AAcvC,+DAA+D;AAC/D,MAAM,WAAW,GAAG,+CAA+C,CAAC;AAEpE,MAAM,aAAa,GAAG,IAAI,OAAO,EAAa,CAAC;AAK/C,6GAA6G;AAC7G,MAAM,UAAU,mBAAmB,CAA2B,MAAc,EAAE,UAAkC,EAAE;IAChH,IAAI,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC;QAAE,OAAO,MAAM,CAAC;IAC7C,2FAA2F;IAC3F,MAAM,QAAQ,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CAA4B,CAAC;IAC7E,MAAM,cAAc,GAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,CAC9D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,WAAW,EAAE,iBAAiB,CAAC,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;IACnJ,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7G,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC1B,OAAO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;IACpC,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,cAAc,CAAC,KAAc;IACpC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AACtF,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { McpServer, type McpServerOptions, type Server } from "@modelcontextprotocol/server";
|
|
2
|
+
/** The release part when none is given. */
|
|
3
|
+
export declare const DEFAULT_TOOL_LIST_RELEASE = "1.0.0";
|
|
4
|
+
/** What a client reads from `tools/list`: one entry per tool, as JSON. */
|
|
5
|
+
export type ServedTool = Readonly<Record<string, unknown>> & {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
};
|
|
8
|
+
export interface ToolListMeasurement {
|
|
9
|
+
/** `<release>-tools-<12 hex>`, for `serverInfo.version`. */
|
|
10
|
+
readonly toolListVersion: string;
|
|
11
|
+
readonly toolCount: number;
|
|
12
|
+
}
|
|
13
|
+
/** Registers the app's tools on the server it is given, e.g. `(server) => registerNoteTools(server, identity)`. */
|
|
14
|
+
export type RegisterTools = (server: McpServer) => unknown;
|
|
15
|
+
export interface MeasureToolListOptions {
|
|
16
|
+
/** The `MAJOR.MINOR.PATCH` before `-tools-`. Default: `1.0.0`. */
|
|
17
|
+
readonly release?: string;
|
|
18
|
+
/** The served server's name, so the recording server is shaped like it. Default: `tool-list`. */
|
|
19
|
+
readonly name?: string;
|
|
20
|
+
/** The served server's options (capabilities, instructions), for the same reason. */
|
|
21
|
+
readonly serverOptions?: McpServerOptions;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Registers the tools on a recording server shaped like the served one, lists them the way the endpoint
|
|
25
|
+
* serves them and returns the version with the tool count. Any change of a tool's name, title,
|
|
26
|
+
* description, annotations or input or output schema changes the version; the order of registration does not.
|
|
27
|
+
*/
|
|
28
|
+
export declare function measureToolList(registerTools: RegisterTools, options?: MeasureToolListOptions): Promise<ToolListMeasurement>;
|
|
29
|
+
/** The version and count for a listed tool set. */
|
|
30
|
+
export declare function describeToolList(tools: readonly ServedTool[], release?: string): ToolListMeasurement;
|
|
31
|
+
/** The first 12 hex digits of a sha256 over the tools in name order, each as canonical JSON (keys sorted at every level). */
|
|
32
|
+
export declare function fingerprintToolList(tools: readonly ServedTool[]): string;
|
|
33
|
+
/** Every tool a fresh server from the factory lists (none when it registered none), asked with one stateless `tools/list` as the endpoint serves it. */
|
|
34
|
+
export declare function listServedTools(createServer: () => McpServer | Server | Promise<McpServer | Server>): Promise<ServedTool[]>;
|
|
35
|
+
//# sourceMappingURL=tool-list.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-list.d.ts","sourceRoot":"","sources":["../../src/server/tool-list.ts"],"names":[],"mappings":"AAMA,OAAO,EAAoB,SAAS,EAAoB,KAAK,gBAAgB,EAAE,KAAK,MAAM,EAAE,MAAM,8BAA8B,CAAC;AAEjI,2CAA2C;AAC3C,eAAO,MAAM,yBAAyB,UAAU,CAAC;AAMjD,0EAA0E;AAC1E,MAAM,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvF,MAAM,WAAW,mBAAmB;IAClC,4DAA4D;IAC5D,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,mHAAmH;AACnH,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,OAAO,CAAC;AAE3D,MAAM,WAAW,sBAAsB;IACrC,kEAAkE;IAClE,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,iGAAiG;IACjG,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,qFAAqF;IACrF,QAAQ,CAAC,aAAa,CAAC,EAAE,gBAAgB,CAAC;CAC3C;AAED;;;;GAIG;AACH,wBAAsB,eAAe,CAAC,aAAa,EAAE,aAAa,EAAE,OAAO,GAAE,sBAA2B,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAMtI;AAED,mDAAmD;AACnD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,SAAS,UAAU,EAAE,EAAE,OAAO,GAAE,MAAkC,GAAG,mBAAmB,CAG/H;AAED,6HAA6H;AAC7H,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,SAAS,UAAU,EAAE,GAAG,MAAM,CAGxE;AAED,wJAAwJ;AACxJ,wBAAsB,eAAe,CAAC,YAAY,EAAE,MAAM,SAAS,GAAG,MAAM,GAAG,OAAO,CAAC,SAAS,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAgBjI"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The server version that follows the tool list. MCP clients keep a cached `tools/list` until the
|
|
2
|
+
// version they read at `initialize` changes, so a release that adds a tool or changes a schema under
|
|
3
|
+
// the same version leaves the assistant on the old schemas. `measureToolList` fingerprints the list
|
|
4
|
+
// as the client reads it, `<release>-tools-<12 hex>`: a semver prerelease, since comparisons ignore
|
|
5
|
+
// build metadata. The same registrations always give the same version.
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { createMcpHandler, McpServer, METHOD_NOT_FOUND } from "@modelcontextprotocol/server";
|
|
8
|
+
/** The release part when none is given. */
|
|
9
|
+
export const DEFAULT_TOOL_LIST_RELEASE = "1.0.0";
|
|
10
|
+
/** Hex digits of the fingerprint in the version. */
|
|
11
|
+
const FINGERPRINT_LENGTH = 12;
|
|
12
|
+
const RELEASE_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
|
|
13
|
+
/**
|
|
14
|
+
* Registers the tools on a recording server shaped like the served one, lists them the way the endpoint
|
|
15
|
+
* serves them and returns the version with the tool count. Any change of a tool's name, title,
|
|
16
|
+
* description, annotations or input or output schema changes the version; the order of registration does not.
|
|
17
|
+
*/
|
|
18
|
+
export async function measureToolList(registerTools, options = {}) {
|
|
19
|
+
const release = options.release ?? DEFAULT_TOOL_LIST_RELEASE;
|
|
20
|
+
assertRelease(release);
|
|
21
|
+
const server = new McpServer({ name: options.name ?? "tool-list", version: release }, options.serverOptions);
|
|
22
|
+
await registerTools(server);
|
|
23
|
+
return describeToolList(await listServedTools(() => server), release);
|
|
24
|
+
}
|
|
25
|
+
/** The version and count for a listed tool set. */
|
|
26
|
+
export function describeToolList(tools, release = DEFAULT_TOOL_LIST_RELEASE) {
|
|
27
|
+
assertRelease(release);
|
|
28
|
+
return { toolListVersion: `${release}-tools-${fingerprintToolList(tools)}`, toolCount: tools.length };
|
|
29
|
+
}
|
|
30
|
+
/** The first 12 hex digits of a sha256 over the tools in name order, each as canonical JSON (keys sorted at every level). */
|
|
31
|
+
export function fingerprintToolList(tools) {
|
|
32
|
+
const ordered = [...tools].sort((left, right) => (left.name < right.name ? -1 : left.name > right.name ? 1 : 0));
|
|
33
|
+
return createHash("sha256").update(toCanonicalJson(ordered)).digest("hex").slice(0, FINGERPRINT_LENGTH);
|
|
34
|
+
}
|
|
35
|
+
/** Every tool a fresh server from the factory lists (none when it registered none), asked with one stateless `tools/list` as the endpoint serves it. */
|
|
36
|
+
export async function listServedTools(createServer) {
|
|
37
|
+
const handler = createMcpHandler(createServer, { responseMode: "json" });
|
|
38
|
+
const response = await handler.fetch(new Request("http://localhost/api/mcp", {
|
|
39
|
+
method: "POST",
|
|
40
|
+
headers: { "content-type": "application/json", accept: "application/json, text/event-stream" },
|
|
41
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list", params: {} }),
|
|
42
|
+
}));
|
|
43
|
+
const answer = readAnswer(await response.text());
|
|
44
|
+
// A server that registered no tool declares no tools capability, so it does not know the method.
|
|
45
|
+
if (answer.error?.code === METHOD_NOT_FOUND)
|
|
46
|
+
return [];
|
|
47
|
+
if (answer.result?.tools === undefined) {
|
|
48
|
+
throw new Error(`@softure-ai/mcp-access: listing the server's tools failed: ${typeof answer.error?.message === "string" ? answer.error.message : `HTTP ${String(response.status)}`}`);
|
|
49
|
+
}
|
|
50
|
+
return answer.result.tools.filter((tool) => typeof tool.name === "string" && tool.name !== "");
|
|
51
|
+
}
|
|
52
|
+
/** The JSON-RPC answer, sent as JSON or as one SSE `message` event. */
|
|
53
|
+
function readAnswer(body) {
|
|
54
|
+
const data = body.startsWith("event:") ? (body.split("\n").find((line) => line.startsWith("data: "))?.slice("data: ".length) ?? "null") : body;
|
|
55
|
+
return (JSON.parse(data) ?? {});
|
|
56
|
+
}
|
|
57
|
+
function toCanonicalJson(value) {
|
|
58
|
+
return JSON.stringify(value, (_key, entry) => entry !== null && typeof entry === "object" && !Array.isArray(entry)
|
|
59
|
+
? Object.fromEntries(Object.entries(entry).sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)))
|
|
60
|
+
: entry);
|
|
61
|
+
}
|
|
62
|
+
/** A release that is not `MAJOR.MINOR.PATCH` would make an invalid version: a setup bug, thrown. */
|
|
63
|
+
function assertRelease(release) {
|
|
64
|
+
if (!RELEASE_PATTERN.test(release)) {
|
|
65
|
+
throw new Error(`@softure-ai/mcp-access: the tool list release must be MAJOR.MINOR.PATCH, got "${release}"`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=tool-list.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-list.js","sourceRoot":"","sources":["../../src/server/tool-list.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,qGAAqG;AACrG,oGAAoG;AACpG,oGAAoG;AACpG,uEAAuE;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,g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C;AAED,oGAAoG;AACpG,SAAS,aAAa,CAAC,OAAe;IACpC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CAAC,iFAAiF,OAAO,GAAG,CAAC,CAAC;IAC/G,CAAC;AACH,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { SoftureConfig } from "@softure-ai/core";
|
|
2
2
|
import type { McpServerFactory } from "../server/endpoint.js";
|
|
3
|
+
import { type ToolListMeasurement } from "../server/tool-list.js";
|
|
3
4
|
export interface ToolCatalogDifferences {
|
|
4
5
|
/** Configured tools the server does not register for the identity that should get them. */
|
|
5
6
|
readonly missing: readonly string[];
|
|
@@ -8,8 +9,15 @@ export interface ToolCatalogDifferences {
|
|
|
8
9
|
/** Configured write tools the server gives a read-only identity. */
|
|
9
10
|
readonly writeForReaders: readonly string[];
|
|
10
11
|
}
|
|
12
|
+
/** What `measureServedToolLists` reports: a read-only and a write identity. */
|
|
13
|
+
export interface ServedToolLists {
|
|
14
|
+
readonly read: ToolListMeasurement;
|
|
15
|
+
readonly write: ToolListMeasurement;
|
|
16
|
+
}
|
|
11
17
|
/** Every difference between the configured catalog and the server, each list in the catalog's order, then the server's. */
|
|
12
18
|
export declare function getToolCatalogDifferences(config: SoftureConfig, createServer: McpServerFactory): Promise<ToolCatalogDifferences>;
|
|
13
19
|
/** Throws a list of every difference, e.g. in a unit test next to the server. */
|
|
14
20
|
export declare function expectToolCatalogMatchesServer(config: SoftureConfig, createServer: McpServerFactory): Promise<void>;
|
|
21
|
+
/** The tool list's version and count for each access level, for a snapshot that changes whenever a tool's schema does. */
|
|
22
|
+
export declare function measureServedToolLists(createServer: McpServerFactory, release?: string): Promise<ServedToolLists>;
|
|
15
23
|
//# sourceMappingURL=catalog.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/testing/catalog.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/testing/catalog.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,OAAO,EAAqC,KAAK,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAErG,MAAM,WAAW,sBAAsB;IACrC,2FAA2F;IAC3F,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,iEAAiE;IACjE,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,oEAAoE;IACpE,QAAQ,CAAC,eAAe,EAAE,SAAS,MAAM,EAAE,CAAC;CAC7C;AAED,+EAA+E;AAC/E,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAC;CACrC;AAKD,2HAA2H;AAC3H,wBAAsB,yBAAyB,CAAC,MAAM,EAAE,aAAa,EAAE,YAAY,EAAE,gBAAgB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAUtI;AAED,iFAAiF;AACjF,wBAAsB,8BAA8B,CAAC,MAAM,EAAE,aAAa,EAAE,YAAY,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAUzH;AAOD,0HAA0H;AAC1H,wBAAsB,sBAAsB,CAAC,YAAY,EAAE,gBAAgB,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAKvH"}
|
package/dist/testing/catalog.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
// Catalog parity: the tools listed in softure.config.ts (the token page shows them, agent docs read
|
|
2
|
-
// them) against the tools the app's MCP server really registers, for a read-only and a write identity.
|
|
3
|
-
// Framework-free: `expectToolCatalogMatchesServer` throws, so it works in any test runner.
|
|
4
|
-
import { createMcpHandler } from "@modelcontextprotocol/server";
|
|
5
1
|
import { getMcpAccessOptions } from "../server/options.js";
|
|
2
|
+
import { describeToolList, listServedTools } from "../server/tool-list.js";
|
|
6
3
|
const READER = { userId: "00000000-0000-4000-8000-000000000001", canWrite: false, tokenId: "catalog-check" };
|
|
7
4
|
const WRITER = { ...READER, canWrite: true };
|
|
8
5
|
/** Every difference between the configured catalog and the server, each list in the catalog's order, then the server's. */
|
|
@@ -31,21 +28,13 @@ export async function expectToolCatalogMatchesServer(config, createServer) {
|
|
|
31
28
|
}
|
|
32
29
|
/** The tool names a fresh server lists for the identity, asked the way the endpoint serves it. */
|
|
33
30
|
async function listServerTools(createServer, identity) {
|
|
34
|
-
|
|
35
|
-
const response = await handler.fetch(new Request("http://localhost/api/mcp", {
|
|
36
|
-
method: "POST",
|
|
37
|
-
headers: { "content-type": "application/json", accept: "application/json, text/event-stream" },
|
|
38
|
-
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list", params: {} }),
|
|
39
|
-
}));
|
|
40
|
-
const answer = readAnswer(await response.text());
|
|
41
|
-
if (answer.result?.tools === undefined) {
|
|
42
|
-
throw new Error(`@softure-ai/mcp-access: listing the server's tools failed: ${typeof answer.error?.message === "string" ? answer.error.message : `HTTP ${String(response.status)}`}`);
|
|
43
|
-
}
|
|
44
|
-
return answer.result.tools.map((tool) => (typeof tool.name === "string" ? tool.name : "")).filter((name) => name !== "");
|
|
31
|
+
return (await listServedTools(() => createServer(identity))).map((tool) => tool.name);
|
|
45
32
|
}
|
|
46
|
-
/** The
|
|
47
|
-
function
|
|
48
|
-
|
|
49
|
-
|
|
33
|
+
/** The tool list's version and count for each access level, for a snapshot that changes whenever a tool's schema does. */
|
|
34
|
+
export async function measureServedToolLists(createServer, release) {
|
|
35
|
+
return {
|
|
36
|
+
read: describeToolList(await listServedTools(() => createServer(READER)), release),
|
|
37
|
+
write: describeToolList(await listServedTools(() => createServer(WRITER)), release),
|
|
38
|
+
};
|
|
50
39
|
}
|
|
51
40
|
//# sourceMappingURL=catalog.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"catalog.js","sourceRoot":"","sources":["../../src/testing/catalog.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"catalog.js","sourceRoot":"","sources":["../../src/testing/catalog.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAA4B,MAAM,wBAAwB,CAAC;AAiBrG,MAAM,MAAM,GAAsB,EAAE,MAAM,EAAE,sCAAsC,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,CAAC;AAChI,MAAM,MAAM,GAAsB,EAAE,GAAG,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;AAEhE,2HAA2H;AAC3H,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAAC,MAAqB,EAAE,YAA8B;IACnG,MAAM,OAAO,GAAG,mBAAmB,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC;IAClD,MAAM,UAAU,GAAG,MAAM,eAAe,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IAC/D,MAAM,UAAU,GAAG,MAAM,eAAe,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IAC/D,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACzD,OAAO;QACL,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,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C,EAAE,CAAC,CAAC,CAAC,IAAI;KAChI,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC;IACxC,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,gGAAgG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACzI,CAAC;AACH,CAAC;AAED,kGAAkG;AAClG,KAAK,UAAU,eAAe,CAAC,YAA8B,EAAE,QAA2B;IACxF,OAAO,CAAC,MAAM,eAAe,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxF,CAAC;AAED,0HAA0H;AAC1H,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAC,YAA8B,EAAE,OAAgB;IAC3F,OAAO;QACL,IAAI,EAAE,gBAAgB,CAAC,MAAM,eAAe,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC;QAClF,KAAK,EAAE,gBAAgB,CAAC,MAAM,eAAe,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC;KACpF,CAAC;AACJ,CAAC"}
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { expectToolCatalogMatchesServer, getToolCatalogDifferences, type ToolCatalogDifferences } from "./catalog.js";
|
|
1
|
+
export { expectToolCatalogMatchesServer, getToolCatalogDifferences, measureServedToolLists, type ServedToolLists, type ToolCatalogDifferences, } from "./catalog.js";
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,8BAA8B,EAC9B,yBAAyB,EACzB,sBAAsB,EACtB,KAAK,eAAe,EACpB,KAAK,sBAAsB,GAC5B,MAAM,cAAc,CAAC"}
|
package/dist/testing/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// `@softure-ai/mcp-access/testing`: checks for an app's own tests.
|
|
2
|
-
export { expectToolCatalogMatchesServer, getToolCatalogDifferences } from "./catalog.js";
|
|
2
|
+
export { expectToolCatalogMatchesServer, getToolCatalogDifferences, measureServedToolLists, } from "./catalog.js";
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,OAAO,EACL,8BAA8B,EAC9B,yBAAyB,EACzB,sBAAsB,GAGvB,MAAM,cAAc,CAAC"}
|
package/module.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "mcp-access",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"dependsOn": { "security": "^0.2.0", "auth": "^0.2.0", "jobs": "^0.1.1?", "ops": "^0.2.0?" },
|
|
5
5
|
"dbSchema": "mcp",
|
|
6
6
|
"tables": ["access_tokens", "oauth_clients", "oauth_authorization_codes", "oauth_grants", "oauth_registration_window"],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softure-ai/mcp-access",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"description": "SOFTURE AI MCP access module: hashed, scoped, expiring access tokens, a rate-limited Bearer endpoint around the app's MCP server factory, and a token page with client setup instructions for Next.js; tool result helpers with a safe error boundary, a stdio entry, a catalog parity check and the softure-mcp prune command.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
package/src/index.ts
CHANGED
|
@@ -34,7 +34,7 @@ export const MCP_RATE_LIMIT_BUCKETS = {
|
|
|
34
34
|
export const mcpAccess = defineModule({
|
|
35
35
|
manifest: {
|
|
36
36
|
id: MODULE_ID,
|
|
37
|
-
version: "0.2.
|
|
37
|
+
version: "0.2.8",
|
|
38
38
|
dependsOn: { security: "^0.2.0", auth: "^0.2.0", jobs: "^0.1.1?", ops: "^0.2.0?" },
|
|
39
39
|
dbSchema: "mcp",
|
|
40
40
|
tables: ["access_tokens", "oauth_clients", "oauth_authorization_codes", "oauth_grants", "oauth_registration_window"],
|
package/src/messages/en.ts
CHANGED
|
@@ -85,6 +85,15 @@ export const en = {
|
|
|
85
85
|
nothingGranted: "No access was granted.",
|
|
86
86
|
backToApp: "Back to the app ({target})",
|
|
87
87
|
},
|
|
88
|
+
toolInput: {
|
|
89
|
+
unknownFields: "Unknown fields: {fields}. Accepted fields: {accepted}.",
|
|
90
|
+
noFields: "none",
|
|
91
|
+
fieldRenamed: "input.field_renamed: the field \"{field}\" is now \"{renamed}\". Send it as \"{renamed}\".",
|
|
92
|
+
},
|
|
93
|
+
serverInfoTool: {
|
|
94
|
+
description:
|
|
95
|
+
"Tells which build of this server answers: its name and version (the version changes whenever a tool changes), the release, the commit, when it was built and started, and the changelog when the app publishes one. Use it to check that a change is live.",
|
|
96
|
+
},
|
|
88
97
|
errors: {
|
|
89
98
|
"mcp-access": {
|
|
90
99
|
name_required: "Give the token a name, so you can tell it from the others.",
|
package/src/server/index.ts
CHANGED
|
@@ -102,6 +102,28 @@ export {
|
|
|
102
102
|
type RevokeAccessTokenInput,
|
|
103
103
|
type VerifiedAccessToken,
|
|
104
104
|
} from "./tokens.js";
|
|
105
|
+
export { createMcpServerFactory, type McpServerFactoryOptions } from "./server-factory.js";
|
|
106
|
+
export {
|
|
107
|
+
registerServerInfoTool,
|
|
108
|
+
SERVER_INFO_TOOL_NAME,
|
|
109
|
+
type McpBuildInfo,
|
|
110
|
+
type RegisterServerInfoToolOptions,
|
|
111
|
+
type ServedServerInfo,
|
|
112
|
+
type ServerInfoAnswer,
|
|
113
|
+
type ServerInfoToolOptions,
|
|
114
|
+
} from "./server-info.js";
|
|
115
|
+
export { toStrictToolInput, withStrictToolInput, type StrictToolInputMessages, type StrictToolInputOptions } from "./strict-input.js";
|
|
116
|
+
export {
|
|
117
|
+
DEFAULT_TOOL_LIST_RELEASE,
|
|
118
|
+
describeToolList,
|
|
119
|
+
fingerprintToolList,
|
|
120
|
+
listServedTools,
|
|
121
|
+
measureToolList,
|
|
122
|
+
type MeasureToolListOptions,
|
|
123
|
+
type RegisterTools,
|
|
124
|
+
type ServedTool,
|
|
125
|
+
type ToolListMeasurement,
|
|
126
|
+
} from "./tool-list.js";
|
|
105
127
|
export {
|
|
106
128
|
toolError,
|
|
107
129
|
toolResult,
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// The app's MCP server factory with a version that follows its tool list. The tools are measured once
|
|
2
|
+
// per access level (a read-only token sees fewer tools) on a recording server, and each request's
|
|
3
|
+
// server answers `initialize` with that level's version, so a client drops its cached `tools/list`
|
|
4
|
+
// whenever a release changes a tool. The factory plugs into `createMcpEndpoint`, `serveMcpStdio` and
|
|
5
|
+
// the catalog check like a hand-written one.
|
|
6
|
+
//
|
|
7
|
+
// export const createServer = createMcpServerFactory({
|
|
8
|
+
// name: "acme",
|
|
9
|
+
// registerTools: (server, identity) => registerNoteTools(server, identity),
|
|
10
|
+
// });
|
|
11
|
+
import { McpServer, type McpServerOptions } from "@modelcontextprotocol/server";
|
|
12
|
+
import type { McpServerIdentity } from "../contract.js";
|
|
13
|
+
import type { McpServerFactory } from "./endpoint.js";
|
|
14
|
+
import { withStrictToolInput, type StrictToolInputOptions } from "./strict-input.js";
|
|
15
|
+
import { registerServerInfoTool, type ServerInfoToolOptions } from "./server-info.js";
|
|
16
|
+
import { measureToolList, type ToolListMeasurement } from "./tool-list.js";
|
|
17
|
+
|
|
18
|
+
export interface McpServerFactoryOptions {
|
|
19
|
+
/** `serverInfo.name`. */
|
|
20
|
+
readonly name: string;
|
|
21
|
+
/** `serverInfo.title`, the name a client shows. */
|
|
22
|
+
readonly title?: string;
|
|
23
|
+
/** The `MAJOR.MINOR.PATCH` before `-tools-`. Default: `1.0.0`. */
|
|
24
|
+
readonly release?: string;
|
|
25
|
+
/** A fixed `serverInfo.version` instead of the measured one. */
|
|
26
|
+
readonly version?: string;
|
|
27
|
+
readonly serverOptions?: McpServerOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Registers the tools for one request. The tool list may depend on `identity.canWrite` only: it is
|
|
30
|
+
* measured once per access level. A list that depends on the account needs `measureToolList` per call.
|
|
31
|
+
*/
|
|
32
|
+
readonly registerTools: (server: McpServer, identity: McpServerIdentity) => unknown;
|
|
33
|
+
/** Every tool's input is strict (`withStrictToolInput`), on the served and the measured server alike. */
|
|
34
|
+
readonly strictToolInput?: boolean | StrictToolInputOptions;
|
|
35
|
+
/**
|
|
36
|
+
* Adds the read-only `get_server_info` tool for every access level: the build info (pass `readBuildInfo` of
|
|
37
|
+
* `@softure-ai/ops/server`) with the served name and version. Off by default.
|
|
38
|
+
*/
|
|
39
|
+
readonly serverInfoTool?: ServerInfoToolOptions;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** An `McpServerFactory` whose servers carry the tool list's version for the identity's access level. */
|
|
43
|
+
export function createMcpServerFactory(options: McpServerFactoryOptions): McpServerFactory {
|
|
44
|
+
const strictOptions = options.strictToolInput === true ? {} : options.strictToolInput === false ? undefined : options.strictToolInput;
|
|
45
|
+
// The served version reaches `get_server_info` here; the recording server registers it with a placeholder, since
|
|
46
|
+
// its handler never runs there and the version is not part of the listed tool.
|
|
47
|
+
const registerTools = async (server: McpServer, identity: McpServerIdentity, version = ""): Promise<void> => {
|
|
48
|
+
const target = strictOptions === undefined ? server : withStrictToolInput(server, strictOptions);
|
|
49
|
+
await options.registerTools(target, identity);
|
|
50
|
+
if (options.serverInfoTool !== undefined) registerServerInfoTool(target, { ...options.serverInfoTool, server: { name: options.name, version } });
|
|
51
|
+
};
|
|
52
|
+
const measurements = new Map<boolean, Promise<ToolListMeasurement>>();
|
|
53
|
+
|
|
54
|
+
// The promise is cached, so parallel first requests measure once; a failed measurement is dropped so
|
|
55
|
+
// the next request tries again instead of failing for good.
|
|
56
|
+
const measure = (identity: McpServerIdentity): Promise<ToolListMeasurement> => {
|
|
57
|
+
const cached = measurements.get(identity.canWrite);
|
|
58
|
+
if (cached !== undefined) return cached;
|
|
59
|
+
const measurement = measureToolList((server) => registerTools(server, identity), {
|
|
60
|
+
...(options.release === undefined ? {} : { release: options.release }),
|
|
61
|
+
name: options.name,
|
|
62
|
+
...(options.serverOptions === undefined ? {} : { serverOptions: options.serverOptions }),
|
|
63
|
+
});
|
|
64
|
+
measurements.set(identity.canWrite, measurement);
|
|
65
|
+
void measurement.catch(() => measurements.delete(identity.canWrite));
|
|
66
|
+
return measurement;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
return async (identity) => {
|
|
70
|
+
const version = options.version ?? (await measure(identity)).toolListVersion;
|
|
71
|
+
const server = new McpServer(
|
|
72
|
+
{ name: options.name, version, ...(options.title === undefined ? {} : { title: options.title }) },
|
|
73
|
+
options.serverOptions,
|
|
74
|
+
);
|
|
75
|
+
await registerTools(server, identity, version);
|
|
76
|
+
return server;
|
|
77
|
+
};
|
|
78
|
+
}
|