@nanobpm/urban 0.86.1 → 0.87.1
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/dist/openapi/spec.d.ts +63 -5
- package/dist/openapi/spec.js +172 -3
- package/dist/runtime/core/modules/mcp.js +80 -25
- package/package.json +1 -1
- package/src/openapi/mcp-parity.fixture.yaml +73 -0
- package/src/openapi/mcp-tools.snapshot.json +81 -2
- package/src/openapi/spec.test.ts +221 -0
- package/src/openapi/spec.ts +204 -7
- package/src/runtime/core/modules/mcp.test.ts +153 -0
- package/src/runtime/core/modules/mcp.ts +90 -21
package/dist/openapi/spec.d.ts
CHANGED
|
@@ -53,6 +53,11 @@ export interface OpenApiParameter {
|
|
|
53
53
|
in: "path" | "query" | "header" | "cookie";
|
|
54
54
|
required?: boolean;
|
|
55
55
|
schema?: OpenApiSchema;
|
|
56
|
+
/** The parameter's schema with every local component `$ref` inlined (via {@link resolveSchemaDeep}),
|
|
57
|
+
* so a consumer that cannot resolve `#/components/...` — e.g. an MCP client reading the projected
|
|
58
|
+
* tool `inputSchema` — sees a self-contained schema. Absent when the parameter declares no schema.
|
|
59
|
+
* The raw {@link schema} is retained for the type deriver, which maps a `$ref` to its named type. */
|
|
60
|
+
resolvedSchema?: OpenApiSchema;
|
|
56
61
|
}
|
|
57
62
|
export interface OpenApiMediaType {
|
|
58
63
|
schema?: OpenApiSchema;
|
|
@@ -119,6 +124,16 @@ export interface OperationInfo {
|
|
|
119
124
|
path: string;
|
|
120
125
|
parameters: OpenApiParameter[];
|
|
121
126
|
requestBodySchema?: OpenApiSchema;
|
|
127
|
+
/** The request-body schema with every local component `$ref` deeply inlined (via
|
|
128
|
+
* {@link resolveSchemaDeep}), so it is self-contained: no `$ref`, and an explicit `type` where the
|
|
129
|
+
* referenced component declared one. This is what the MCP tool projection copies into a tool's
|
|
130
|
+
* `inputSchema.properties.body` — an MCP client cannot resolve `#/components/...` outside the
|
|
131
|
+
* original document, so the projected schema must carry the concrete shape. The raw
|
|
132
|
+
* {@link requestBodySchema} is retained alongside it for the type deriver (which maps a `$ref` to
|
|
133
|
+
* its named TypeScript type) and for `sharedRequestBodySchemas` (which fingerprints shared refs).
|
|
134
|
+
* Absent exactly when {@link requestBodySchema} is absent. Downstream slices (ADR 0067) read the
|
|
135
|
+
* resolved body `type` from here rather than re-resolving a possibly-`$ref` raw schema. */
|
|
136
|
+
requestBodySchemaResolved?: OpenApiSchema;
|
|
122
137
|
requestBodyRequired: boolean;
|
|
123
138
|
/** Every documented JSON response, keyed by status code ("200","400",…,"default"), in a stable
|
|
124
139
|
* order. The type layer unions these into the operation's response type; the runtime validates a
|
|
@@ -144,6 +159,20 @@ export declare function parseSpec(text: string): OpenApiDoc;
|
|
|
144
159
|
export declare function refName(ref: string): string | undefined;
|
|
145
160
|
/** Follow local component `$ref`s to the concrete schema (guarding cycles). Non-local refs → undefined. */
|
|
146
161
|
export declare function resolveSchema(doc: OpenApiDoc, schema: OpenApiSchema | undefined): OpenApiSchema | undefined;
|
|
162
|
+
/** Deeply inline every local component `$ref` in a schema, returning a self-contained schema with no
|
|
163
|
+
* `$ref` anywhere in its tree (top-level chain plus nested `properties`/`items`/
|
|
164
|
+
* `additionalProperties`/`allOf`/`oneOf`/`anyOf`). This is the resolver the MCP tool projection uses
|
|
165
|
+
* so a tool's `inputSchema` carries the concrete shape rather than an opaque `#/components/...`
|
|
166
|
+
* pointer an MCP client cannot follow (ADR 0067). It reuses the same local-ref semantics as
|
|
167
|
+
* {@link resolveSchema}, extended to recurse into subschemas.
|
|
168
|
+
*
|
|
169
|
+
* Cycles and unresolvable refs FAIL LOUDLY rather than leak a `$ref`:
|
|
170
|
+
* - a `$ref` that reappears on its own resolution path (an irreducible cycle — a schema that
|
|
171
|
+
* cannot be finitely inlined) throws;
|
|
172
|
+
* - a `$ref` that is non-local or dangles (no matching `#/components/schemas/*`) throws.
|
|
173
|
+
* `seenRefs` tracks refs on the CURRENT path only, so a component referenced twice in sibling
|
|
174
|
+
* branches (a diamond) inlines fine — only genuine self-reference is rejected. */
|
|
175
|
+
export declare function resolveSchemaDeep(doc: OpenApiDoc, schema: OpenApiSchema | undefined, seenRefs?: ReadonlySet<string>): OpenApiSchema | undefined;
|
|
147
176
|
/** Walk the document's paths and return every operation that carries an operationId, in a stable
|
|
148
177
|
* (path, method) order. Operations without an operationId are skipped here; the runtime mount logs
|
|
149
178
|
* them at `warn` and skips them too — this keeps the pure collector total. */
|
|
@@ -164,15 +193,44 @@ export declare function isMcpExcluded(ext: unknown): boolean;
|
|
|
164
193
|
/** Whether an operation's HTTP method mutates state — anything other than a safe read verb. Drives
|
|
165
194
|
* the MCP read/mutate split: read tools are unguarded on loopback, mutating tools are gated. */
|
|
166
195
|
export declare function isMutatingMethod(method: HttpMethodLower): boolean;
|
|
167
|
-
/**
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
196
|
+
/** A JSON Schema (OpenAPI object subset) describing a tool's input, derived from an operation's
|
|
197
|
+
* path/query parameters and its request-body schema. It is the SINGLE derivation both the runtime
|
|
198
|
+
* MCP surface (`tools/list`) and the CI parity guard read, so a tool's advertised input schema and
|
|
199
|
+
* the HTTP validation it dispatches to can never drift (ADR 0053/0067).
|
|
200
|
+
*
|
|
201
|
+
* Every property schema is taken from the operation's resolved (`$ref`-free) metadata — the
|
|
202
|
+
* parameters' `resolvedSchema` and the body's `requestBodySchemaResolved`, both produced by
|
|
203
|
+
* {@link resolveSchemaDeep} in {@link collectOperations} — so the emitted schema is self-contained:
|
|
204
|
+
* an MCP client, which cannot follow a `#/components/...` pointer outside the original document,
|
|
205
|
+
* sees the concrete shape (and an explicit `type` on the body). */
|
|
206
|
+
export declare function toolInputSchema(op: OperationInfo): Record<string, unknown>;
|
|
207
|
+
/** Walk a JSON value (a projected tool schema) and return a pointer for every `$ref` still present.
|
|
208
|
+
* Empty means the schema is self-contained — no `#/components/...` an MCP client cannot resolve. The
|
|
209
|
+
* path is a dotted trail to each leak (e.g. `properties.body.properties.foo`). Pure/total. */
|
|
210
|
+
export declare function findSchemaRefLeaks(value: unknown, path?: string): string[];
|
|
211
|
+
/** Self-containment violations in one projected tool's `inputSchema`: any surviving `$ref` (an
|
|
212
|
+
* MCP client cannot resolve `#/components/...`), and — when the tool has a `body` property — a
|
|
213
|
+
* `body` that carries no explicit `type` (so the client cannot tell it is an object/array and
|
|
214
|
+
* mis-encodes the argument). Empty means the schema is a usable, self-contained input contract.
|
|
215
|
+
* This is the pure assertion the `check:mcp` guard runs over every projected tool (ADR 0067). */
|
|
216
|
+
export declare function findToolSchemaViolations(operationId: string, inputSchema: Record<string, unknown>): string[];
|
|
217
|
+
/** Every self-containment violation across all projected (non-excluded) tools for a document — the
|
|
218
|
+
* aggregate the `check:mcp` guard fails on. Excluded operations are skipped: they are withheld from
|
|
219
|
+
* the live tool surface, so their schema is never advertised to a client. */
|
|
220
|
+
export declare function findAllToolSchemaViolations(doc: OpenApiDoc): string[];
|
|
221
|
+
/** One operation's MCP-tool projection facts: its id, HTTP method, whether it mutates, whether
|
|
222
|
+
* `x-mcp` excludes it from projection, and its self-contained input schema. This is the shared unit
|
|
223
|
+
* the runtime MCP surface AND the CI spec↔tool parity guard both read — a single derivation of
|
|
224
|
+
* "which operations become tools and what each accepts", never a second walker (ADR 0053). */
|
|
171
225
|
export interface McpToolProjection {
|
|
172
226
|
operationId: string;
|
|
173
227
|
method: HttpMethodLower;
|
|
174
228
|
mutating: boolean;
|
|
175
229
|
excluded: boolean;
|
|
230
|
+
/** The tool's `inputSchema` as advertised to an MCP client (absent for an excluded op, which is
|
|
231
|
+
* never advertised). Captured in the snapshot so a projection change — including a body `$ref`
|
|
232
|
+
* that stops being resolved — is caught by `check:mcp` drift, not discovered by a client. */
|
|
233
|
+
inputSchema?: Record<string, unknown>;
|
|
176
234
|
}
|
|
177
235
|
/**
|
|
178
236
|
* Project every operationId-bearing operation to its MCP-tool facts, in the stable
|
|
@@ -180,7 +238,7 @@ export interface McpToolProjection {
|
|
|
180
238
|
* `excluded: true`) rather than dropped, so the parity guard can positively assert that an
|
|
181
239
|
* `x-mcp`-excluded door is present-but-withheld — the runtime drops them when it builds the live
|
|
182
240
|
* tool list. Derived entirely from {@link collectOperations}, so it can never diverge from what
|
|
183
|
-
* `mountApi` routes.
|
|
241
|
+
* `mountApi` routes. A non-excluded op also carries its self-contained {@link toolInputSchema}.
|
|
184
242
|
*/
|
|
185
243
|
export declare function collectMcpToolProjection(doc: OpenApiDoc): McpToolProjection[];
|
|
186
244
|
/**
|
package/dist/openapi/spec.js
CHANGED
|
@@ -115,6 +115,54 @@ export function resolveSchema(doc, schema) {
|
|
|
115
115
|
}
|
|
116
116
|
return current;
|
|
117
117
|
}
|
|
118
|
+
/** Deeply inline every local component `$ref` in a schema, returning a self-contained schema with no
|
|
119
|
+
* `$ref` anywhere in its tree (top-level chain plus nested `properties`/`items`/
|
|
120
|
+
* `additionalProperties`/`allOf`/`oneOf`/`anyOf`). This is the resolver the MCP tool projection uses
|
|
121
|
+
* so a tool's `inputSchema` carries the concrete shape rather than an opaque `#/components/...`
|
|
122
|
+
* pointer an MCP client cannot follow (ADR 0067). It reuses the same local-ref semantics as
|
|
123
|
+
* {@link resolveSchema}, extended to recurse into subschemas.
|
|
124
|
+
*
|
|
125
|
+
* Cycles and unresolvable refs FAIL LOUDLY rather than leak a `$ref`:
|
|
126
|
+
* - a `$ref` that reappears on its own resolution path (an irreducible cycle — a schema that
|
|
127
|
+
* cannot be finitely inlined) throws;
|
|
128
|
+
* - a `$ref` that is non-local or dangles (no matching `#/components/schemas/*`) throws.
|
|
129
|
+
* `seenRefs` tracks refs on the CURRENT path only, so a component referenced twice in sibling
|
|
130
|
+
* branches (a diamond) inlines fine — only genuine self-reference is rejected. */
|
|
131
|
+
export function resolveSchemaDeep(doc, schema, seenRefs = new Set()) {
|
|
132
|
+
if (!schema)
|
|
133
|
+
return undefined;
|
|
134
|
+
let current = schema;
|
|
135
|
+
let seen = seenRefs;
|
|
136
|
+
while (current.$ref) {
|
|
137
|
+
const ref = current.$ref;
|
|
138
|
+
if (seen.has(ref)) {
|
|
139
|
+
throw new Error(`OpenAPI schema $ref cycle: ${[...seen, ref].join(" -> ")} cannot be inlined into a self-contained schema`);
|
|
140
|
+
}
|
|
141
|
+
const name = refName(ref);
|
|
142
|
+
const next = name ? doc.components?.schemas?.[name] : undefined;
|
|
143
|
+
if (!next) {
|
|
144
|
+
throw new Error(`OpenAPI schema $ref "${ref}" does not resolve to a local component — a self-contained schema cannot contain an unresolvable $ref`);
|
|
145
|
+
}
|
|
146
|
+
seen = new Set(seen).add(ref);
|
|
147
|
+
current = next;
|
|
148
|
+
}
|
|
149
|
+
const out = { ...current };
|
|
150
|
+
delete out.$ref;
|
|
151
|
+
if (current.properties) {
|
|
152
|
+
out.properties = Object.fromEntries(Object.entries(current.properties).map(([k, v]) => [k, resolveSchemaDeep(doc, v, seen) ?? {}]));
|
|
153
|
+
}
|
|
154
|
+
if (current.items)
|
|
155
|
+
out.items = resolveSchemaDeep(doc, current.items, seen);
|
|
156
|
+
if (current.additionalProperties && typeof current.additionalProperties === "object") {
|
|
157
|
+
out.additionalProperties = resolveSchemaDeep(doc, current.additionalProperties, seen);
|
|
158
|
+
}
|
|
159
|
+
for (const key of ["allOf", "oneOf", "anyOf"]) {
|
|
160
|
+
const arr = current[key];
|
|
161
|
+
if (arr)
|
|
162
|
+
out[key] = arr.map((s) => resolveSchemaDeep(doc, s, seen) ?? {});
|
|
163
|
+
}
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
118
166
|
function firstJsonSchema(content) {
|
|
119
167
|
if (!content)
|
|
120
168
|
return undefined;
|
|
@@ -151,10 +199,29 @@ export function collectOperations(doc) {
|
|
|
151
199
|
const operationId = typeof opRaw.operationId === "string" ? opRaw.operationId : undefined;
|
|
152
200
|
if (!operationId || !isSafeOperationId(operationId))
|
|
153
201
|
continue;
|
|
202
|
+
// Resolve exclusion BEFORE any deep `$ref` inlining. An `x-mcp`-excluded operation is never
|
|
203
|
+
// advertised as a tool, so its schema is never projected or self-containment-guarded; doing
|
|
204
|
+
// its (throwing) deep resolution eagerly would let a dangling/cyclic `$ref` on an EXCLUDED
|
|
205
|
+
// door crash projection/guards for the whole document — even though that door is withheld.
|
|
206
|
+
// Excluded ops therefore carry no `resolvedSchema`/`requestBodySchemaResolved` (unused).
|
|
207
|
+
const mcpExcluded = isMcpExcluded(opRaw["x-mcp"]);
|
|
154
208
|
const opParams = Array.isArray(opRaw.parameters) ? opRaw.parameters : [];
|
|
155
|
-
const parameters = normalizeParameters([...pathParams, ...opParams])
|
|
209
|
+
const parameters = normalizeParameters([...pathParams, ...opParams]).map((p) => ({
|
|
210
|
+
...p,
|
|
211
|
+
// Inline any local component `$ref` so the projected MCP tool schema is self-contained. The
|
|
212
|
+
// raw `p.schema` is kept for the type deriver; the tool projection reads `resolvedSchema`.
|
|
213
|
+
resolvedSchema: mcpExcluded ? undefined : resolveSchemaDeep(doc, p.schema),
|
|
214
|
+
}));
|
|
156
215
|
const requestBody = isRecord(opRaw.requestBody) ? opRaw.requestBody : undefined;
|
|
157
216
|
const requestBodySchema = firstJsonSchema(isRecord(requestBody?.content) ? requestBody.content : undefined);
|
|
217
|
+
// Deeply inline component `$ref`s so downstream consumers (the MCP tool projection, ADR 0067)
|
|
218
|
+
// get a self-contained, explicitly-typed body schema — never an opaque `#/components/...`
|
|
219
|
+
// pointer. Raw `requestBodySchema` is retained (deriver + shared-ref fingerprinting). A cyclic
|
|
220
|
+
// or unresolvable body ref fails loudly here rather than leaking a `$ref` — but only for a
|
|
221
|
+
// projected (non-excluded) op; an excluded op skips this throwing work (see above).
|
|
222
|
+
const requestBodySchemaResolved = mcpExcluded
|
|
223
|
+
? undefined
|
|
224
|
+
: resolveSchemaDeep(doc, requestBodySchema);
|
|
158
225
|
const responses = isRecord(opRaw.responses) ? opRaw.responses : {};
|
|
159
226
|
// Effective security: an operation's own `security` (even an explicit `[]`, which opts out of
|
|
160
227
|
// a document-level default) wins; otherwise inherit the document-level default; otherwise open.
|
|
@@ -168,11 +235,12 @@ export function collectOperations(doc) {
|
|
|
168
235
|
path,
|
|
169
236
|
parameters,
|
|
170
237
|
requestBodySchema,
|
|
238
|
+
requestBodySchemaResolved,
|
|
171
239
|
requestBodyRequired: requestBody?.required === true,
|
|
172
240
|
responseSchemas: collectResponseSchemas(responses),
|
|
173
241
|
security,
|
|
174
242
|
eject: opRaw["x-urban-eject"] === true,
|
|
175
|
-
mcpExcluded
|
|
243
|
+
mcpExcluded,
|
|
176
244
|
summary: typeof opRaw.summary === "string" ? opRaw.summary : undefined,
|
|
177
245
|
});
|
|
178
246
|
}
|
|
@@ -206,13 +274,107 @@ const READ_ONLY_METHODS = ["get", "head", "options"];
|
|
|
206
274
|
export function isMutatingMethod(method) {
|
|
207
275
|
return !READ_ONLY_METHODS.includes(method);
|
|
208
276
|
}
|
|
277
|
+
/** A JSON Schema (OpenAPI object subset) describing a tool's input, derived from an operation's
|
|
278
|
+
* path/query parameters and its request-body schema. It is the SINGLE derivation both the runtime
|
|
279
|
+
* MCP surface (`tools/list`) and the CI parity guard read, so a tool's advertised input schema and
|
|
280
|
+
* the HTTP validation it dispatches to can never drift (ADR 0053/0067).
|
|
281
|
+
*
|
|
282
|
+
* Every property schema is taken from the operation's resolved (`$ref`-free) metadata — the
|
|
283
|
+
* parameters' `resolvedSchema` and the body's `requestBodySchemaResolved`, both produced by
|
|
284
|
+
* {@link resolveSchemaDeep} in {@link collectOperations} — so the emitted schema is self-contained:
|
|
285
|
+
* an MCP client, which cannot follow a `#/components/...` pointer outside the original document,
|
|
286
|
+
* sees the concrete shape (and an explicit `type` on the body). */
|
|
287
|
+
export function toolInputSchema(op) {
|
|
288
|
+
const properties = {};
|
|
289
|
+
const required = [];
|
|
290
|
+
for (const p of op.parameters) {
|
|
291
|
+
if (p.in === "path" || p.in === "query") {
|
|
292
|
+
properties[p.name] = p.resolvedSchema ?? p.schema ?? {};
|
|
293
|
+
if (p.required)
|
|
294
|
+
required.push(p.name);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
const body = op.requestBodySchemaResolved;
|
|
298
|
+
if (body) {
|
|
299
|
+
properties.body = body;
|
|
300
|
+
if (op.requestBodyRequired)
|
|
301
|
+
required.push("body");
|
|
302
|
+
}
|
|
303
|
+
const schema = { type: "object", properties };
|
|
304
|
+
if (required.length > 0)
|
|
305
|
+
schema.required = required;
|
|
306
|
+
return schema;
|
|
307
|
+
}
|
|
308
|
+
/** Walk a JSON value (a projected tool schema) and return a pointer for every `$ref` still present.
|
|
309
|
+
* Empty means the schema is self-contained — no `#/components/...` an MCP client cannot resolve. The
|
|
310
|
+
* path is a dotted trail to each leak (e.g. `properties.body.properties.foo`). Pure/total. */
|
|
311
|
+
export function findSchemaRefLeaks(value, path = "") {
|
|
312
|
+
const leaks = [];
|
|
313
|
+
const visit = (v, p) => {
|
|
314
|
+
if (Array.isArray(v)) {
|
|
315
|
+
v.forEach((item, i) => visit(item, `${p}[${i}]`));
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
if (v && typeof v === "object") {
|
|
319
|
+
for (const [k, child] of Object.entries(v)) {
|
|
320
|
+
// A JSON-Schema `$ref` is a *string* pointer; only that form is an unresolvable leak. A
|
|
321
|
+
// property literally named `$ref` with a non-string value is an ordinary schema key, so
|
|
322
|
+
// recurse into it (real leaks can still hide nested beneath such a property).
|
|
323
|
+
if (k === "$ref" && typeof child === "string")
|
|
324
|
+
leaks.push(p || "<root>");
|
|
325
|
+
else
|
|
326
|
+
visit(child, p ? `${p}.${k}` : k);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
visit(value, path);
|
|
331
|
+
return leaks;
|
|
332
|
+
}
|
|
333
|
+
/** Whether a (record-typed) schema declares an explicit JSON-Schema `type` — a non-empty string or a
|
|
334
|
+
* non-empty array of types. Operates on a raw record (not a typed {@link OpenApiSchema}) so it can
|
|
335
|
+
* vet a projected tool schema's `body` property without an unchecked cast. */
|
|
336
|
+
function hasExplicitType(schema) {
|
|
337
|
+
const t = schema.type;
|
|
338
|
+
if (typeof t === "string")
|
|
339
|
+
return t.length > 0;
|
|
340
|
+
return Array.isArray(t) && t.length > 0;
|
|
341
|
+
}
|
|
342
|
+
/** Self-containment violations in one projected tool's `inputSchema`: any surviving `$ref` (an
|
|
343
|
+
* MCP client cannot resolve `#/components/...`), and — when the tool has a `body` property — a
|
|
344
|
+
* `body` that carries no explicit `type` (so the client cannot tell it is an object/array and
|
|
345
|
+
* mis-encodes the argument). Empty means the schema is a usable, self-contained input contract.
|
|
346
|
+
* This is the pure assertion the `check:mcp` guard runs over every projected tool (ADR 0067). */
|
|
347
|
+
export function findToolSchemaViolations(operationId, inputSchema) {
|
|
348
|
+
const violations = [];
|
|
349
|
+
for (const leak of findSchemaRefLeaks(inputSchema)) {
|
|
350
|
+
violations.push(`${operationId}: inputSchema leaks an unresolved $ref at ${leak}`);
|
|
351
|
+
}
|
|
352
|
+
const properties = isRecord(inputSchema.properties) ? inputSchema.properties : undefined;
|
|
353
|
+
const body = properties && isRecord(properties.body) ? properties.body : undefined;
|
|
354
|
+
if (body && !hasExplicitType(body)) {
|
|
355
|
+
violations.push(`${operationId}: inputSchema.properties.body has no explicit \`type\``);
|
|
356
|
+
}
|
|
357
|
+
return violations;
|
|
358
|
+
}
|
|
359
|
+
/** Every self-containment violation across all projected (non-excluded) tools for a document — the
|
|
360
|
+
* aggregate the `check:mcp` guard fails on. Excluded operations are skipped: they are withheld from
|
|
361
|
+
* the live tool surface, so their schema is never advertised to a client. */
|
|
362
|
+
export function findAllToolSchemaViolations(doc) {
|
|
363
|
+
const violations = [];
|
|
364
|
+
for (const op of collectOperations(doc)) {
|
|
365
|
+
if (op.mcpExcluded)
|
|
366
|
+
continue;
|
|
367
|
+
violations.push(...findToolSchemaViolations(op.operationId, toolInputSchema(op)));
|
|
368
|
+
}
|
|
369
|
+
return violations;
|
|
370
|
+
}
|
|
209
371
|
/**
|
|
210
372
|
* Project every operationId-bearing operation to its MCP-tool facts, in the stable
|
|
211
373
|
* `collectOperations` (path, method) order. Excluded operations are RETAINED here (flagged
|
|
212
374
|
* `excluded: true`) rather than dropped, so the parity guard can positively assert that an
|
|
213
375
|
* `x-mcp`-excluded door is present-but-withheld — the runtime drops them when it builds the live
|
|
214
376
|
* tool list. Derived entirely from {@link collectOperations}, so it can never diverge from what
|
|
215
|
-
* `mountApi` routes.
|
|
377
|
+
* `mountApi` routes. A non-excluded op also carries its self-contained {@link toolInputSchema}.
|
|
216
378
|
*/
|
|
217
379
|
export function collectMcpToolProjection(doc) {
|
|
218
380
|
return collectOperations(doc).map((op) => ({
|
|
@@ -220,6 +382,7 @@ export function collectMcpToolProjection(doc) {
|
|
|
220
382
|
method: op.method,
|
|
221
383
|
mutating: isMutatingMethod(op.method),
|
|
222
384
|
excluded: op.mcpExcluded,
|
|
385
|
+
...(op.mcpExcluded ? {} : { inputSchema: toolInputSchema(op) }),
|
|
223
386
|
}));
|
|
224
387
|
}
|
|
225
388
|
/**
|
|
@@ -247,6 +410,12 @@ export function diffMcpToolProjection(snapshot, current) {
|
|
|
247
410
|
`excluded:${snap.excluded}} vs spec {method:${cur.method}, mutating:${cur.mutating}, ` +
|
|
248
411
|
`excluded:${cur.excluded}}`);
|
|
249
412
|
}
|
|
413
|
+
if (JSON.stringify(snap.inputSchema ?? null) !== JSON.stringify(cur.inputSchema ?? null)) {
|
|
414
|
+
// Emit a concise, constant-size line rather than inlining both full `inputSchema` payloads:
|
|
415
|
+
// for a non-trivial schema that would explode CI logs and bury the signal. The committed
|
|
416
|
+
// snapshot's own diff carries the exact field-level detail; this line just names the drift.
|
|
417
|
+
drift.push(`~ ${id}: inputSchema changed (regenerate the snapshot to see the field-level diff)`);
|
|
418
|
+
}
|
|
250
419
|
}
|
|
251
420
|
for (const id of snapshotById.keys()) {
|
|
252
421
|
if (!currentById.has(id)) {
|
|
@@ -34,7 +34,7 @@ import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/
|
|
|
34
34
|
import { CallToolRequestSchema, ErrorCode, GetPromptRequestSchema, isInitializeRequest, ListPromptsRequestSchema, ListResourcesRequestSchema, ListToolsRequestSchema, McpError, ReadResourceRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
35
35
|
import { GENERATED_DIR } from "../../../toolkit/artifact.js";
|
|
36
36
|
import { SYSTEM_BRIEF_JSON } from "../../../toolkit/derivers/system-brief.js";
|
|
37
|
-
import { collectOperations, evaluateSecurity, isMutatingMethod, parseSpec, sharedSecretSchemeName, } from "../../../openapi/spec.js";
|
|
37
|
+
import { collectOperations, evaluateSecurity, isMutatingMethod, isObjectSchema, parseSpec, resolveSchema, schemaHasType, sharedSecretSchemeName, toolInputSchema, } from "../../../openapi/spec.js";
|
|
38
38
|
import { errorMessage } from "../guards.js";
|
|
39
39
|
import { presentFormIdentifier } from "../host.js";
|
|
40
40
|
import { resolveBindMode } from "../manifest.js";
|
|
@@ -173,29 +173,10 @@ export function readHeader(headers, name) {
|
|
|
173
173
|
function isMutatingOperation(op) {
|
|
174
174
|
return isMutatingMethod(op.method);
|
|
175
175
|
}
|
|
176
|
-
/** A JSON Schema (OpenAPI object subset) describing a tool's input
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
|
|
180
|
-
const properties = {};
|
|
181
|
-
const required = [];
|
|
182
|
-
for (const p of op.parameters) {
|
|
183
|
-
if (p.in === "path" || p.in === "query") {
|
|
184
|
-
properties[p.name] = p.schema ?? {};
|
|
185
|
-
if (p.required)
|
|
186
|
-
required.push(p.name);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
if (op.requestBodySchema) {
|
|
190
|
-
properties.body = op.requestBodySchema;
|
|
191
|
-
if (op.requestBodyRequired)
|
|
192
|
-
required.push("body");
|
|
193
|
-
}
|
|
194
|
-
const schema = { type: "object", properties };
|
|
195
|
-
if (required.length > 0)
|
|
196
|
-
schema.required = required;
|
|
197
|
-
return schema;
|
|
198
|
-
}
|
|
176
|
+
/** A JSON Schema (OpenAPI object subset) describing a tool's input is derived once, in
|
|
177
|
+
* `openapi/spec.ts`'s {@link toolInputSchema}, from an operation's resolved (`$ref`-free) parameter
|
|
178
|
+
* and request-body metadata — so the tool input schema the runtime advertises and the schema the CI
|
|
179
|
+
* parity guard pins are the SAME derivation (one source of truth, ADR 0053/0067). */
|
|
199
180
|
/** Substitute an operation's `{param}` path-template segments with the supplied arguments, building
|
|
200
181
|
* the request path under the shared `/app/api` base so dispatch hits the exact `mountApi` route. */
|
|
201
182
|
function fillPath(op, args) {
|
|
@@ -276,6 +257,10 @@ export function missingRequiredArgs(inputSchema, args) {
|
|
|
276
257
|
* read-only) app operation is then guarded exactly as its HTTP route is — the app operation tools
|
|
277
258
|
* inherit the OpenAPI `security` contract rather than a bespoke MCP gate (ADR 0067 §4). */
|
|
278
259
|
function toHttpRequest(op, args, forwardHeaders) {
|
|
260
|
+
// `args.body` is expected to already be the structured value the operation declares — a
|
|
261
|
+
// pre-encoded JSON-string body is normalized upstream by `normalizeBodyArg` (called in
|
|
262
|
+
// `dispatchOperationTool`) so `JSON.stringify` here serializes an object/array ONCE rather than
|
|
263
|
+
// double-encoding a quoted string the door would reject (ADR 0067).
|
|
279
264
|
const bodyText = op.requestBodySchema !== undefined && args.body !== undefined
|
|
280
265
|
? JSON.stringify(args.body)
|
|
281
266
|
: "";
|
|
@@ -296,6 +281,69 @@ function toHttpRequest(op, args, forwardHeaders) {
|
|
|
296
281
|
text: () => Promise.resolve(bodyText),
|
|
297
282
|
};
|
|
298
283
|
}
|
|
284
|
+
/** The operation's effective request-body schema for classifying a tool-call `body`: P0's resolved,
|
|
285
|
+
* `$ref`-free schema ({@link OperationInfo.requestBodySchemaResolved}, produced by
|
|
286
|
+
* `collectOperations`). As defence in depth, if that metadata is somehow still a bare `$ref` (it
|
|
287
|
+
* should never be after resolution), re-resolve it via {@link resolveSchema} against `doc` so the
|
|
288
|
+
* classification below never keys off an opaque `#/components/...` pointer. */
|
|
289
|
+
function effectiveRequestBodySchema(op, doc) {
|
|
290
|
+
const schema = op.requestBodySchemaResolved ?? op.requestBodySchema;
|
|
291
|
+
if (schema?.$ref)
|
|
292
|
+
return doc ? resolveSchema(doc, schema) : undefined;
|
|
293
|
+
return schema;
|
|
294
|
+
}
|
|
295
|
+
/** Whether the operation declares a STRUCTURED (object/array) request body — the shapes the app door
|
|
296
|
+
* expects as a JSON object/array rather than a bare scalar. Classified from the RESOLVED schema so a
|
|
297
|
+
* `$ref`-bodied op (e.g. `previewDeliveryGraph`, whose object `type` is only visible after P0's
|
|
298
|
+
* resolution) is recognized, not merely an inline-`type` object body. */
|
|
299
|
+
function structuredBodyKind(schema) {
|
|
300
|
+
if (!schema)
|
|
301
|
+
return undefined;
|
|
302
|
+
if (isObjectSchema(schema))
|
|
303
|
+
return "object";
|
|
304
|
+
if (schemaHasType(schema, "array"))
|
|
305
|
+
return "array";
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
/** Normalize a tool-call `body` argument for FAITHFUL transport (ADR 0067). A client that saw an
|
|
309
|
+
* opaque input schema — or that coerces non-primitive args — may send `body` as a pre-encoded JSON
|
|
310
|
+
* *string* instead of the object/array the operation declares. Left as-is, `toHttpRequest`'s
|
|
311
|
+
* `JSON.stringify` would DOUBLE-encode it into a quoted string and the door would reject it with
|
|
312
|
+
* "expected object, got string". So when the op declares an object/array body and `body` arrives as
|
|
313
|
+
* a string, parse it once so it round-trips as the structured value. A genuine object/array argument
|
|
314
|
+
* passes through untouched (still serialized by the transport). A string that does not parse to the
|
|
315
|
+
* declared shape is a CLEAR tool error naming the expected shape — never a silently double-encoded
|
|
316
|
+
* (and 4xx-rejected) request. */
|
|
317
|
+
function normalizeBodyArg(op, args, doc) {
|
|
318
|
+
const body = args.body;
|
|
319
|
+
const kind = structuredBodyKind(effectiveRequestBodySchema(op, doc));
|
|
320
|
+
if (body === undefined || kind === undefined || typeof body !== "string") {
|
|
321
|
+
return { ok: true, args };
|
|
322
|
+
}
|
|
323
|
+
let parsed;
|
|
324
|
+
try {
|
|
325
|
+
parsed = JSON.parse(body);
|
|
326
|
+
}
|
|
327
|
+
catch {
|
|
328
|
+
return {
|
|
329
|
+
ok: false,
|
|
330
|
+
error: `validation failed: the "body" argument was sent as a string but this operation expects a JSON ${kind}; the string is not valid JSON`,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
const parsedKind = Array.isArray(parsed)
|
|
334
|
+
? "array"
|
|
335
|
+
: parsed !== null && typeof parsed === "object"
|
|
336
|
+
? "object"
|
|
337
|
+
: undefined;
|
|
338
|
+
if (parsedKind !== kind) {
|
|
339
|
+
const got = parsedKind ?? (parsed === null ? "null" : typeof parsed);
|
|
340
|
+
return {
|
|
341
|
+
ok: false,
|
|
342
|
+
error: `validation failed: the "body" argument was sent as a string but this operation expects a JSON ${kind}; it parsed to a ${got}`,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
return { ok: true, args: { ...args, body: parsed } };
|
|
346
|
+
}
|
|
299
347
|
/** Wrap a value as an MCP tool text result. A JSON payload is stringified; `isError` flags a
|
|
300
348
|
* non-2xx dispatch so the client sees the failure (carrying the delegate's own error body). */
|
|
301
349
|
function textResult(value, isError = false) {
|
|
@@ -735,10 +783,17 @@ export function mountMcp(ctx, app, apiRoutes) {
|
|
|
735
783
|
// empty-string substitution — the calling agent sees exactly which required params it omitted.
|
|
736
784
|
return textResult({ error: `validation failed: missing required path parameter(s): ${missing.join(", ")}` }, true);
|
|
737
785
|
}
|
|
786
|
+
// Faithful body transport (ADR 0067): a client may send an object/array `body` pre-encoded as a
|
|
787
|
+
// JSON string. Normalize it to the structured value from the operation's RESOLVED body schema so
|
|
788
|
+
// it is not double-encoded by `toHttpRequest`; a string that cannot parse to the declared shape
|
|
789
|
+
// is a clear tool error rather than a silent 4xx from a double-encoded body.
|
|
790
|
+
const normalized = normalizeBodyArg(op, args, (await loadSpecDoc()).doc);
|
|
791
|
+
if (!normalized.ok)
|
|
792
|
+
return textResult({ error: normalized.error }, true);
|
|
738
793
|
// Forward the MCP connection's headers so the reused router enforces the operation's own
|
|
739
794
|
// OpenAPI `security` (a mutating — or secured read — app op is guarded exactly as its HTTP
|
|
740
795
|
// route). A `401`/`403`/`500` from that enforcement flows back as an error tool result.
|
|
741
|
-
const res = await apiRouter(toHttpRequest(op, args, headers));
|
|
796
|
+
const res = await apiRouter(toHttpRequest(op, normalized.args, headers));
|
|
742
797
|
const status = res.status ?? 200;
|
|
743
798
|
return textResult(res.body ?? "", status >= 400);
|
|
744
799
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nanobpm/urban",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.87.1",
|
|
4
4
|
"description": "Urban: build and run code-first apps on Nano — runtime, derivation toolkit, and CLI in one. Author durable processes with @nanobpm/workflow (defineFlow), a typed datasource, forms, triggers and surfaces from a nano.app.json manifest, on Node or Deno.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -10,6 +10,20 @@
|
|
|
10
10
|
# - an `x-mcp`-EXCLUDED operation → present in the doc but withheld from the
|
|
11
11
|
# agent-facing tool surface (the security-
|
|
12
12
|
# relevant opt-out, `x-mcp: false`)
|
|
13
|
+
# - a `$ref`-BODIED operation (POST) → its request body is a bare
|
|
14
|
+
# `{ $ref: #/components/schemas/... }` with
|
|
15
|
+
# no inline `type`, whose referenced
|
|
16
|
+
# component itself nests another `$ref`.
|
|
17
|
+
# The projection must inline both so the
|
|
18
|
+
# tool's `inputSchema.properties.body` is
|
|
19
|
+
# self-contained (no `$ref`, explicit
|
|
20
|
+
# `type`) — an MCP client cannot resolve
|
|
21
|
+
# `#/components/...` outside this document.
|
|
22
|
+
# - an INLINE object-body op (POST) → its request body declares `type: object`
|
|
23
|
+
# inline, exercising the faithful object-body
|
|
24
|
+
# transport path (object arg → door receives
|
|
25
|
+
# an object; a pre-encoded JSON string is
|
|
26
|
+
# parsed, not double-encoded).
|
|
13
27
|
#
|
|
14
28
|
# Edit this fixture ONLY together with a reviewed `npm run gen:mcp-tools`.
|
|
15
29
|
openapi: 3.1.0
|
|
@@ -23,6 +37,30 @@ components:
|
|
|
23
37
|
in: header
|
|
24
38
|
name: X-Nano-Key
|
|
25
39
|
x-nano-secret-env: NANO_WEBHOOK_KEY
|
|
40
|
+
schemas:
|
|
41
|
+
# A request-body schema declared as a bare `$ref` with NO inline `type` — the exact shape that
|
|
42
|
+
# leaked an unresolved `#/components/...` pointer into a tool's inputSchema before it was resolved
|
|
43
|
+
# on the projection path (the motivating real op `previewDeliveryGraph`). It also NESTS another
|
|
44
|
+
# `$ref` (`root`) so deep resolution is exercised, not just a single top-level hop.
|
|
45
|
+
DeliveryGraphSubmit:
|
|
46
|
+
type: object
|
|
47
|
+
required:
|
|
48
|
+
- root
|
|
49
|
+
properties:
|
|
50
|
+
root:
|
|
51
|
+
$ref: "#/components/schemas/DeliveryNode"
|
|
52
|
+
note:
|
|
53
|
+
type: string
|
|
54
|
+
DeliveryNode:
|
|
55
|
+
type: object
|
|
56
|
+
required:
|
|
57
|
+
- id
|
|
58
|
+
properties:
|
|
59
|
+
id:
|
|
60
|
+
type: string
|
|
61
|
+
weight:
|
|
62
|
+
type: integer
|
|
63
|
+
minimum: 0
|
|
26
64
|
paths:
|
|
27
65
|
/widgets:
|
|
28
66
|
get:
|
|
@@ -47,3 +85,38 @@ paths:
|
|
|
47
85
|
responses:
|
|
48
86
|
"202":
|
|
49
87
|
description: accepted
|
|
88
|
+
/delivery-graph/preview:
|
|
89
|
+
post:
|
|
90
|
+
operationId: previewDeliveryGraph
|
|
91
|
+
summary: Preview a delivery graph (mutating — `$ref`-bodied, body must resolve to a self-contained schema).
|
|
92
|
+
requestBody:
|
|
93
|
+
required: true
|
|
94
|
+
content:
|
|
95
|
+
application/json:
|
|
96
|
+
schema:
|
|
97
|
+
$ref: "#/components/schemas/DeliveryGraphSubmit"
|
|
98
|
+
responses:
|
|
99
|
+
"200":
|
|
100
|
+
description: ok
|
|
101
|
+
/widgets/bulk:
|
|
102
|
+
post:
|
|
103
|
+
operationId: bulkCreateWidgets
|
|
104
|
+
summary: "Bulk create widgets (mutating — inline `type: object` request body, object-arg round-trip case)."
|
|
105
|
+
requestBody:
|
|
106
|
+
required: true
|
|
107
|
+
content:
|
|
108
|
+
application/json:
|
|
109
|
+
schema:
|
|
110
|
+
type: object
|
|
111
|
+
required:
|
|
112
|
+
- items
|
|
113
|
+
properties:
|
|
114
|
+
items:
|
|
115
|
+
type: array
|
|
116
|
+
items:
|
|
117
|
+
type: string
|
|
118
|
+
dryRun:
|
|
119
|
+
type: boolean
|
|
120
|
+
responses:
|
|
121
|
+
"201":
|
|
122
|
+
description: created
|