@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.
Files changed (50) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +102 -2
  3. package/dist/index.d.ts +8 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/messages/en.d.ts +8 -0
  7. package/dist/messages/en.d.ts.map +1 -1
  8. package/dist/messages/en.js +8 -0
  9. package/dist/messages/en.js.map +1 -1
  10. package/dist/messages/index.d.ts +8 -0
  11. package/dist/messages/index.d.ts.map +1 -1
  12. package/dist/server/index.d.ts +4 -0
  13. package/dist/server/index.d.ts.map +1 -1
  14. package/dist/server/index.js +4 -0
  15. package/dist/server/index.js.map +1 -1
  16. package/dist/server/server-factory.d.ts +31 -0
  17. package/dist/server/server-factory.d.ts.map +1 -0
  18. package/dist/server/server-factory.js +49 -0
  19. package/dist/server/server-factory.js.map +1 -0
  20. package/dist/server/server-info.d.ts +34 -0
  21. package/dist/server/server-info.d.ts.map +1 -0
  22. package/dist/server/server-info.js +30 -0
  23. package/dist/server/server-info.js.map +1 -0
  24. package/dist/server/strict-input.d.ts +17 -0
  25. package/dist/server/strict-input.d.ts.map +1 -0
  26. package/dist/server/strict-input.js +157 -0
  27. package/dist/server/strict-input.js.map +1 -0
  28. package/dist/server/tool-list.d.ts +35 -0
  29. package/dist/server/tool-list.d.ts.map +1 -0
  30. package/dist/server/tool-list.js +68 -0
  31. package/dist/server/tool-list.js.map +1 -0
  32. package/dist/testing/catalog.d.ts +8 -0
  33. package/dist/testing/catalog.d.ts.map +1 -1
  34. package/dist/testing/catalog.js +8 -19
  35. package/dist/testing/catalog.js.map +1 -1
  36. package/dist/testing/index.d.ts +1 -1
  37. package/dist/testing/index.d.ts.map +1 -1
  38. package/dist/testing/index.js +1 -1
  39. package/dist/testing/index.js.map +1 -1
  40. package/module.json +1 -1
  41. package/package.json +1 -1
  42. package/src/index.ts +1 -1
  43. package/src/messages/en.ts +9 -0
  44. package/src/server/index.ts +22 -0
  45. package/src/server/server-factory.ts +78 -0
  46. package/src/server/server-info.ts +76 -0
  47. package/src/server/strict-input.ts +191 -0
  48. package/src/server/tool-list.ts +105 -0
  49. package/src/testing/catalog.ts +14 -23
  50. package/src/testing/index.ts +7 -1
@@ -0,0 +1,76 @@
1
+ // The `get_server_info` tool (#885): an assistant checks a "done" against production in one call. It answers the
2
+ // public facts of the ops build info route (release, commit, build and start times, the optional changelog) and the
3
+ // name and version the server answered at `initialize`, which follows the tool list. It reads no user data.
4
+ //
5
+ // The app passes the build info reader, so `/server` never loads `@softure-ai/ops` (an optional peer):
6
+ //
7
+ // import { readBuildInfo } from "@softure-ai/ops/server";
8
+ // createMcpServerFactory({ name: "acme", registerTools, serverInfoTool: { buildInfo: () => readBuildInfo() } });
9
+ import type { McpServer } from "@modelcontextprotocol/server";
10
+ import { errorLogLabel } from "@softure-ai/core";
11
+ import { z } from "zod";
12
+ import { en } from "../messages/en.js";
13
+ import { toolResult } from "./tools.js";
14
+
15
+ export const SERVER_INFO_TOOL_NAME = "get_server_info";
16
+
17
+ /** The build info of `@softure-ai/ops` (`readBuildInfo()`), typed by shape so mcp-access does not import ops. */
18
+ export interface McpBuildInfo {
19
+ readonly release: string | null;
20
+ readonly commit: string | null;
21
+ readonly builtAt: string | null;
22
+ readonly startedAt: string;
23
+ }
24
+
25
+ export interface ServerInfoToolOptions {
26
+ /** The build info, e.g. `() => readBuildInfo()` from `@softure-ai/ops/server` with the route's options. */
27
+ readonly buildInfo: () => McpBuildInfo | Promise<McpBuildInfo>;
28
+ /** The changelog the build info route answers, when the app passes one there. Only what is public. */
29
+ readonly changelog?: () => unknown;
30
+ /** Default: `serverInfoTool.description` of the module dictionary. */
31
+ readonly description?: string;
32
+ }
33
+
34
+ /** What `initialize` answers as `serverInfo`. */
35
+ export interface ServedServerInfo {
36
+ readonly name: string;
37
+ readonly version: string;
38
+ }
39
+
40
+ export interface RegisterServerInfoToolOptions extends ServerInfoToolOptions {
41
+ /** The name and version the server answers at `initialize`; `createMcpServerFactory` passes them in. */
42
+ readonly server: ServedServerInfo;
43
+ }
44
+
45
+ /** The tool's answer: the served name and version, the build info and, with a provider, the changelog. */
46
+ export type ServerInfoAnswer = McpBuildInfo & { readonly server: ServedServerInfo; readonly changelog?: unknown };
47
+
48
+ /** Registers `get_server_info`: no input fields, read only. A failing `buildInfo` reaches the SDK, which answers a tool error. */
49
+ export function registerServerInfoTool(server: McpServer, options: RegisterServerInfoToolOptions): void {
50
+ server.registerTool(
51
+ SERVER_INFO_TOOL_NAME,
52
+ {
53
+ description: options.description ?? en.serverInfoTool.description,
54
+ // An empty object rather than no schema, so strict input refuses a field sent to it.
55
+ inputSchema: z.object({}),
56
+ annotations: { readOnlyHint: true, idempotentHint: true, openWorldHint: false },
57
+ },
58
+ async () => toolResult(await readServerInfo(options)),
59
+ );
60
+ }
61
+
62
+ async function readServerInfo(options: RegisterServerInfoToolOptions): Promise<ServerInfoAnswer> {
63
+ const { release, commit, builtAt, startedAt } = await options.buildInfo();
64
+ const answer: ServerInfoAnswer = { server: { name: options.server.name, version: options.server.version }, release, commit, builtAt, startedAt };
65
+ return options.changelog === undefined ? answer : { ...answer, changelog: await readChangelog(options.changelog) };
66
+ }
67
+
68
+ /** As the build info route: a failing provider answers `null` and logs the error class, so the build info still answers. */
69
+ async function readChangelog(provider: () => unknown): Promise<unknown> {
70
+ try {
71
+ return (await provider()) ?? null;
72
+ } catch (error) {
73
+ console.error(`get_server_info: the changelog provider failed: ${errorLogLabel(error)}`);
74
+ return null;
75
+ }
76
+ }
@@ -0,0 +1,191 @@
1
+ // Strict tool input. zod objects strip unknown keys, so a tool call with a misspelt field (`notes` for
2
+ // `note`) or a field of a newer tool list answers as a write that worked while the value was dropped.
3
+ // `withStrictToolInput` rewrites every input schema once, at registration: each object refuses a field
4
+ // it does not know and names the ones it accepts, a field sent under its old name is refused with the
5
+ // stable code `input.field_renamed`, and boolean and number fields also take their exact text (a client
6
+ // on an older tool list sends a field it does not know as text). The SDK validates before the handler
7
+ // runs, so a refused call saves nothing.
8
+ //
9
+ // const server = withStrictToolInput(new McpServer({ name: "acme", version }), { renamedFields: { notes: "note" } });
10
+ import type { McpServer } from "@modelcontextprotocol/server";
11
+ import { formatMessage } from "@softure-ai/core";
12
+ import { z } from "zod";
13
+ import type * as core from "zod/v4/core";
14
+ import { en } from "../messages/en.js";
15
+
16
+ /** The texts of a refused field: `toolInput` of the module dictionary (`getMcpAccessMessages(config).toolInput`). */
17
+ export type StrictToolInputMessages = typeof en.toolInput;
18
+
19
+ export interface StrictToolInputOptions {
20
+ /** Old field name → new field name, refused as `input.field_renamed` wherever an object accepts the new name. */
21
+ readonly renamedFields?: Readonly<Record<string, string>>;
22
+ /** Boolean fields also take `"true"`/`"false"` and number fields a number written as text. Default: `true`. */
23
+ readonly coerceTextScalars?: boolean;
24
+ /** Default: the module's English dictionary. */
25
+ readonly messages?: StrictToolInputMessages;
26
+ }
27
+
28
+ /** A JSON number literal: no spaces, no hex, no empty text. */
29
+ const NUMBER_TEXT = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
30
+
31
+ const strictServers = new WeakSet<McpServer>();
32
+
33
+ type ToolConfig = Readonly<Record<string, unknown>> & { readonly inputSchema?: unknown };
34
+ type RegisterTool = (name: string, config: ToolConfig, callback: unknown) => unknown;
35
+
36
+ /** Makes every tool registered on the server from now on strict; wrapping a server twice changes nothing. */
37
+ export function withStrictToolInput<Server extends McpServer>(server: Server, options: StrictToolInputOptions = {}): Server {
38
+ if (strictServers.has(server)) return server;
39
+ // The SDK's overloads only differ in how the schema is typed; at runtime it is one method.
40
+ const register = server.registerTool.bind(server) as unknown as RegisterTool;
41
+ const registerStrict: RegisterTool = (name, config, callback) =>
42
+ register(name, config.inputSchema === undefined ? config : { ...config, inputSchema: toStrictToolInput(config.inputSchema, options) }, callback);
43
+ Object.defineProperty(server, "registerTool", { value: registerStrict, configurable: true, writable: true });
44
+ strictServers.add(server);
45
+ return server;
46
+ }
47
+
48
+ /** The strict form of a tool's input schema: a zod 4 schema or raw shape is rewritten, anything else passes unchanged. */
49
+ export function toStrictToolInput(schema: unknown, options: StrictToolInputOptions = {}): unknown {
50
+ const zodSchema = isZodSchema(schema) ? schema : isRawShape(schema) ? z.object(schema) : null;
51
+ if (zodSchema === null) return schema;
52
+ const rewriter = new StrictRewriter({
53
+ renamedFields: options.renamedFields ?? {},
54
+ coerceTextScalars: options.coerceTextScalars ?? true,
55
+ messages: options.messages ?? en.toolInput,
56
+ });
57
+ return rewriter.rewrite(zodSchema);
58
+ }
59
+
60
+ function isZodSchema(value: unknown): value is core.$ZodType {
61
+ return typeof value === "object" && value !== null && "_zod" in value;
62
+ }
63
+
64
+ function isRawShape(value: unknown): value is Record<string, core.$ZodType> {
65
+ if (typeof value !== "object" || value === null || Object.getPrototypeOf(value) !== Object.prototype) return false;
66
+ return Object.values(value).every(isZodSchema);
67
+ }
68
+
69
+ type RewriteSettings = Required<StrictToolInputOptions>;
70
+
71
+ /** A schema node as the rewriter reads it: its kind and the parts it rewrites. */
72
+ type AnyDef = core.$ZodTypeDef & Record<string, unknown>;
73
+
74
+ class StrictRewriter {
75
+ // The same node (a shared field, a recursive `z.lazy`) is rewritten once.
76
+ private readonly rewritten = new Map<core.$ZodType, core.$ZodType>();
77
+
78
+ constructor(private readonly settings: RewriteSettings) {}
79
+
80
+ rewrite(schema: core.$ZodType): core.$ZodType {
81
+ const known = this.rewritten.get(schema);
82
+ if (known !== undefined) return known;
83
+ const result = this.rewriteNode(schema);
84
+ this.rewritten.set(schema, result);
85
+ return result;
86
+ }
87
+
88
+ private rewriteNode(schema: core.$ZodType): core.$ZodType {
89
+ const def = schema._zod.def as AnyDef;
90
+ switch (def.type) {
91
+ case "object":
92
+ return this.rewriteObject(schema as core.$ZodObject);
93
+ case "array":
94
+ return this.cloneWith(schema, { element: this.rewrite(def.element as core.$ZodType) });
95
+ case "optional":
96
+ case "nullable":
97
+ case "default":
98
+ case "prefault":
99
+ case "nonoptional":
100
+ case "readonly":
101
+ case "catch":
102
+ return this.cloneWith(schema, { innerType: this.rewrite(def.innerType as core.$ZodType) });
103
+ case "union":
104
+ return this.cloneWith(schema, { options: (def.options as core.$ZodType[]).map((option) => this.rewrite(option)) });
105
+ case "intersection":
106
+ return this.cloneWith(schema, { left: this.rewrite(def.left as core.$ZodType), right: this.rewrite(def.right as core.$ZodType) });
107
+ case "record":
108
+ return this.cloneWith(schema, { valueType: this.rewrite(def.valueType as core.$ZodType) });
109
+ case "tuple":
110
+ return this.cloneWith(schema, {
111
+ items: (def.items as core.$ZodType[]).map((item) => this.rewrite(item)),
112
+ rest: def.rest === null ? null : this.rewrite(def.rest as core.$ZodType),
113
+ });
114
+ case "pipe":
115
+ return this.rewritePipe(schema, def);
116
+ case "lazy": {
117
+ const getter = def.getter as () => core.$ZodType;
118
+ return this.cloneWith(schema, { getter: () => this.rewrite(getter()) });
119
+ }
120
+ case "boolean":
121
+ return this.settings.coerceTextScalars && def.coerce !== true ? z.preprocess(readBooleanText, schema) : schema;
122
+ case "number":
123
+ return this.settings.coerceTextScalars && def.coerce !== true ? z.preprocess(readNumberText, schema) : schema;
124
+ default:
125
+ return schema;
126
+ }
127
+ }
128
+
129
+ /** Only the input side: `in`, or `out` after a preprocess (whose `in` is the transform). */
130
+ private rewritePipe(schema: core.$ZodType, def: AnyDef): core.$ZodType {
131
+ const input = def.in as core.$ZodType;
132
+ if (input._zod.traits.has("$ZodTransform")) return this.cloneWith(schema, { out: this.rewrite(def.out as core.$ZodType) });
133
+ return this.cloneWith(schema, { in: this.rewrite(input) });
134
+ }
135
+
136
+ private rewriteObject(schema: core.$ZodObject): core.$ZodType {
137
+ const def = schema._zod.def;
138
+ const shape = Object.fromEntries(Object.entries(def.shape).map(([name, field]) => [name, this.rewrite(field)]));
139
+ // An object with its own catchall (`looseObject`, `.catchall(x)`) accepts extra fields on purpose.
140
+ if (def.catchall !== undefined && def.catchall._zod.def.type !== "never") return this.cloneWith(schema, { shape });
141
+ // The object's own map, read for any issue: its parameter type is narrowed to the issues an object raises.
142
+ const ownError = def.error as core.$ZodErrorMap | undefined;
143
+ const describe = (issue: core.$ZodRawIssue): string | undefined => describeUnknownFields(issue, Object.keys(shape), this.settings);
144
+ const error: core.$ZodErrorMap = (issue) => describe(issue) ?? ownError?.(issue);
145
+ return this.cloneWith(schema, { shape, catchall: z.never(), error });
146
+ }
147
+
148
+ /** A copy of the node with some parts replaced; its description and other metadata stay with it. */
149
+ private cloneWith(schema: core.$ZodType, parts: Record<string, unknown>): core.$ZodType {
150
+ const copy = (schema as unknown as z.ZodType).clone({ ...schema._zod.def, ...parts });
151
+ const meta = z.globalRegistry.get(schema);
152
+ if (meta !== undefined) {
153
+ // An id names one schema in the JSON Schema output; the copy replaces the original, so it may not carry it twice.
154
+ const rest = { ...meta };
155
+ delete rest.id;
156
+ z.globalRegistry.add(copy, rest);
157
+ }
158
+ return copy;
159
+ }
160
+ }
161
+
162
+ /** The text for an object's unknown fields: renamed ones first, then the rest with the fields it accepts. */
163
+ function describeUnknownFields(issue: core.$ZodRawIssue, accepted: readonly string[], settings: RewriteSettings): string | undefined {
164
+ if (issue.code !== "unrecognized_keys") return undefined;
165
+ const { messages, renamedFields } = settings;
166
+ const renamed: string[] = [];
167
+ const unknown: string[] = [];
168
+ for (const field of issue.keys) {
169
+ const newName = Object.hasOwn(renamedFields, field) ? renamedFields[field] : undefined;
170
+ if (newName !== undefined && accepted.includes(newName)) renamed.push(formatMessage(messages.fieldRenamed, { field, renamed: newName }));
171
+ else unknown.push(field);
172
+ }
173
+ if (unknown.length > 0) {
174
+ renamed.push(formatMessage(messages.unknownFields, { fields: quoteAll(unknown), accepted: accepted.length > 0 ? quoteAll(accepted) : messages.noFields }));
175
+ }
176
+ return renamed.join(" ");
177
+ }
178
+
179
+ function quoteAll(names: readonly string[]): string {
180
+ return names.map((name) => JSON.stringify(name)).join(", ");
181
+ }
182
+
183
+ function readBooleanText(value: unknown): unknown {
184
+ if (value === "true") return true;
185
+ if (value === "false") return false;
186
+ return value;
187
+ }
188
+
189
+ function readNumberText(value: unknown): unknown {
190
+ return typeof value === "string" && NUMBER_TEXT.test(value) ? Number(value) : value;
191
+ }
@@ -0,0 +1,105 @@
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, type McpServerOptions, type Server } from "@modelcontextprotocol/server";
8
+
9
+ /** The release part when none is given. */
10
+ export const DEFAULT_TOOL_LIST_RELEASE = "1.0.0";
11
+
12
+ /** Hex digits of the fingerprint in the version. */
13
+ const FINGERPRINT_LENGTH = 12;
14
+ const RELEASE_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
15
+
16
+ /** What a client reads from `tools/list`: one entry per tool, as JSON. */
17
+ export type ServedTool = Readonly<Record<string, unknown>> & { readonly name: string };
18
+
19
+ export interface ToolListMeasurement {
20
+ /** `<release>-tools-<12 hex>`, for `serverInfo.version`. */
21
+ readonly toolListVersion: string;
22
+ readonly toolCount: number;
23
+ }
24
+
25
+ /** Registers the app's tools on the server it is given, e.g. `(server) => registerNoteTools(server, identity)`. */
26
+ export type RegisterTools = (server: McpServer) => unknown;
27
+
28
+ export interface MeasureToolListOptions {
29
+ /** The `MAJOR.MINOR.PATCH` before `-tools-`. Default: `1.0.0`. */
30
+ readonly release?: string;
31
+ /** The served server's name, so the recording server is shaped like it. Default: `tool-list`. */
32
+ readonly name?: string;
33
+ /** The served server's options (capabilities, instructions), for the same reason. */
34
+ readonly serverOptions?: McpServerOptions;
35
+ }
36
+
37
+ /**
38
+ * Registers the tools on a recording server shaped like the served one, lists them the way the endpoint
39
+ * serves them and returns the version with the tool count. Any change of a tool's name, title,
40
+ * description, annotations or input or output schema changes the version; the order of registration does not.
41
+ */
42
+ export async function measureToolList(registerTools: RegisterTools, options: MeasureToolListOptions = {}): Promise<ToolListMeasurement> {
43
+ const release = options.release ?? DEFAULT_TOOL_LIST_RELEASE;
44
+ assertRelease(release);
45
+ const server = new McpServer({ name: options.name ?? "tool-list", version: release }, options.serverOptions);
46
+ await registerTools(server);
47
+ return describeToolList(await listServedTools(() => server), release);
48
+ }
49
+
50
+ /** The version and count for a listed tool set. */
51
+ export function describeToolList(tools: readonly ServedTool[], release: string = DEFAULT_TOOL_LIST_RELEASE): ToolListMeasurement {
52
+ assertRelease(release);
53
+ return { toolListVersion: `${release}-tools-${fingerprintToolList(tools)}`, toolCount: tools.length };
54
+ }
55
+
56
+ /** The first 12 hex digits of a sha256 over the tools in name order, each as canonical JSON (keys sorted at every level). */
57
+ export function fingerprintToolList(tools: readonly ServedTool[]): string {
58
+ const ordered = [...tools].sort((left, right) => (left.name < right.name ? -1 : left.name > right.name ? 1 : 0));
59
+ return createHash("sha256").update(toCanonicalJson(ordered)).digest("hex").slice(0, FINGERPRINT_LENGTH);
60
+ }
61
+
62
+ /** 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. */
63
+ export async function listServedTools(createServer: () => McpServer | Server | Promise<McpServer | Server>): Promise<ServedTool[]> {
64
+ const handler = createMcpHandler(createServer, { responseMode: "json" });
65
+ const response = await handler.fetch(
66
+ new Request("http://localhost/api/mcp", {
67
+ method: "POST",
68
+ headers: { "content-type": "application/json", accept: "application/json, text/event-stream" },
69
+ body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list", params: {} }),
70
+ }),
71
+ );
72
+ const answer = readAnswer(await response.text());
73
+ // A server that registered no tool declares no tools capability, so it does not know the method.
74
+ if (answer.error?.code === METHOD_NOT_FOUND) return [];
75
+ if (answer.result?.tools === undefined) {
76
+ 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)}`}`);
77
+ }
78
+ return answer.result.tools.filter((tool): tool is ServedTool => typeof tool.name === "string" && tool.name !== "");
79
+ }
80
+
81
+ interface ToolsListAnswer {
82
+ readonly result?: { readonly tools?: readonly (Readonly<Record<string, unknown>> & { readonly name?: unknown })[] };
83
+ readonly error?: { readonly code?: unknown; readonly message?: unknown };
84
+ }
85
+
86
+ /** The JSON-RPC answer, sent as JSON or as one SSE `message` event. */
87
+ function readAnswer(body: string): ToolsListAnswer {
88
+ const data = body.startsWith("event:") ? (body.split("\n").find((line) => line.startsWith("data: "))?.slice("data: ".length) ?? "null") : body;
89
+ return (JSON.parse(data) ?? {}) as ToolsListAnswer;
90
+ }
91
+
92
+ function toCanonicalJson(value: unknown): string {
93
+ return JSON.stringify(value, (_key, entry: unknown) =>
94
+ entry !== null && typeof entry === "object" && !Array.isArray(entry)
95
+ ? Object.fromEntries(Object.entries(entry).sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)))
96
+ : entry,
97
+ );
98
+ }
99
+
100
+ /** A release that is not `MAJOR.MINOR.PATCH` would make an invalid version: a setup bug, thrown. */
101
+ function assertRelease(release: string): void {
102
+ if (!RELEASE_PATTERN.test(release)) {
103
+ throw new Error(`@softure-ai/mcp-access: the tool list release must be MAJOR.MINOR.PATCH, got "${release}"`);
104
+ }
105
+ }
@@ -1,11 +1,11 @@
1
1
  // Catalog parity: the tools listed in softure.config.ts (the token page shows them, agent docs read
2
2
  // them) against the tools the app's MCP server really registers, for a read-only and a write identity.
3
3
  // Framework-free: `expectToolCatalogMatchesServer` throws, so it works in any test runner.
4
- import { createMcpHandler } from "@modelcontextprotocol/server";
5
4
  import type { SoftureConfig } from "@softure-ai/core";
6
5
  import type { McpServerIdentity } from "../contract.js";
7
6
  import type { McpServerFactory } from "../server/endpoint.js";
8
7
  import { getMcpAccessOptions } from "../server/options.js";
8
+ import { describeToolList, listServedTools, type ToolListMeasurement } from "../server/tool-list.js";
9
9
 
10
10
  export interface ToolCatalogDifferences {
11
11
  /** Configured tools the server does not register for the identity that should get them. */
@@ -16,6 +16,12 @@ export interface ToolCatalogDifferences {
16
16
  readonly writeForReaders: readonly string[];
17
17
  }
18
18
 
19
+ /** What `measureServedToolLists` reports: a read-only and a write identity. */
20
+ export interface ServedToolLists {
21
+ readonly read: ToolListMeasurement;
22
+ readonly write: ToolListMeasurement;
23
+ }
24
+
19
25
  const READER: McpServerIdentity = { userId: "00000000-0000-4000-8000-000000000001", canWrite: false, tokenId: "catalog-check" };
20
26
  const WRITER: McpServerIdentity = { ...READER, canWrite: true };
21
27
 
@@ -47,28 +53,13 @@ export async function expectToolCatalogMatchesServer(config: SoftureConfig, crea
47
53
 
48
54
  /** The tool names a fresh server lists for the identity, asked the way the endpoint serves it. */
49
55
  async function listServerTools(createServer: McpServerFactory, identity: McpServerIdentity): Promise<string[]> {
50
- const handler = createMcpHandler(() => createServer(identity), { responseMode: "json" });
51
- const response = await handler.fetch(
52
- new Request("http://localhost/api/mcp", {
53
- method: "POST",
54
- headers: { "content-type": "application/json", accept: "application/json, text/event-stream" },
55
- body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list", params: {} }),
56
- }),
57
- );
58
- const answer = readAnswer(await response.text());
59
- if (answer.result?.tools === undefined) {
60
- 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)}`}`);
61
- }
62
- return answer.result.tools.map((tool) => (typeof tool.name === "string" ? tool.name : "")).filter((name) => name !== "");
56
+ return (await listServedTools(() => createServer(identity))).map((tool) => tool.name);
63
57
  }
64
58
 
65
- interface ToolsListAnswer {
66
- readonly result?: { readonly tools?: readonly { readonly name?: unknown }[] };
67
- readonly error?: { readonly message?: unknown };
68
- }
69
-
70
- /** The JSON-RPC answer, sent as JSON or as one SSE `message` event. */
71
- function readAnswer(body: string): ToolsListAnswer {
72
- const data = body.startsWith("event:") ? (body.split("\n").find((line) => line.startsWith("data: "))?.slice("data: ".length) ?? "null") : body;
73
- return (JSON.parse(data) ?? {}) as ToolsListAnswer;
59
+ /** The tool list's version and count for each access level, for a snapshot that changes whenever a tool's schema does. */
60
+ export async function measureServedToolLists(createServer: McpServerFactory, release?: string): Promise<ServedToolLists> {
61
+ return {
62
+ read: describeToolList(await listServedTools(() => createServer(READER)), release),
63
+ write: describeToolList(await listServedTools(() => createServer(WRITER)), release),
64
+ };
74
65
  }
@@ -1,2 +1,8 @@
1
1
  // `@softure-ai/mcp-access/testing`: checks for an app's own tests.
2
- export { expectToolCatalogMatchesServer, getToolCatalogDifferences, type ToolCatalogDifferences } from "./catalog.js";
2
+ export {
3
+ expectToolCatalogMatchesServer,
4
+ getToolCatalogDifferences,
5
+ measureServedToolLists,
6
+ type ServedToolLists,
7
+ type ToolCatalogDifferences,
8
+ } from "./catalog.js";