@ttoss/http-server-mcp-openapi 0.2.15 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -30,8 +30,8 @@ type JsonSchemaProperty = {
30
30
  /**
31
31
  * A REST-backed MCP tool derived from a single OpenAPI operation.
32
32
  *
33
- * The `path` / `query` / `body` builders turn the camelCase arguments an MCP
34
- * client sends into the pieces of an HTTP request against the original REST
33
+ * The `path` / `query` / `body` builders turn the arguments an MCP client
34
+ * sends (named per {@link OpenApiToToolsOptions.argumentNames}) into the pieces of an HTTP request against the original REST
35
35
  * API. They intentionally hold no transport concerns (base URL, auth); the
36
36
  * caller wires those in when it performs the request.
37
37
  */
@@ -40,7 +40,7 @@ interface ToolDefinition {
40
40
  name: string;
41
41
  /** Sanitised operation description (quotes escaped, newlines flattened). */
42
42
  description: string;
43
- /** Plain JSON Schema describing the tool's camelCase input object. */
43
+ /** Plain JSON Schema describing the tool's input object. */
44
44
  inputSchema: JsonObjectSchema;
45
45
  /** Uppercase HTTP method (`GET`, `POST`, `PUT`, `PATCH`, `DELETE`). */
46
46
  method: string;
@@ -52,10 +52,13 @@ interface ToolDefinition {
52
52
  path: (args: Record<string, unknown>) => string;
53
53
  /** Builds the query string (including leading `?`), or `undefined` if none. */
54
54
  query?: (args: Record<string, unknown>) => string;
55
- /** Builds the snake_case request body, or `undefined` if the op has no body. */
55
+ /**
56
+ * Builds the request body keyed by the spec's property names, or `undefined`
57
+ * if the op has no body.
58
+ */
56
59
  body?: (args: Record<string, unknown>) => Record<string, unknown>;
57
60
  /**
58
- * snake_case names of every top-level request-body property the operation's
61
+ * Spec names of every top-level request-body property the operation's
59
62
  * schema declares, including server-managed ones hidden from `inputSchema`.
60
63
  */
61
64
  acceptedBodyFields: string[];
@@ -65,6 +68,27 @@ interface ToolDefinition {
65
68
  * without this package needing to know about it.
66
69
  */
67
70
  extensions: Record<string, unknown>;
71
+ /**
72
+ * Path and query parameters flagged with the server-managed extension. They
73
+ * are absent from `inputSchema`, but `path` / `query` still read them from
74
+ * the args under `argName`, so the consumer fills them before building the
75
+ * request — `registerOpenApiTools` does it through `serverParameters`.
76
+ */
77
+ serverManagedParameters: ServerManagedParameter[];
78
+ }
79
+ /** A path or query parameter the server sets, never offered to the model. */
80
+ interface ServerManagedParameter {
81
+ /** The parameter's name in the spec. */
82
+ name: string;
83
+ /** Where the parameter goes. */
84
+ in: 'path' | 'query';
85
+ /** The args key `path` / `query` read the value from. */
86
+ argName: string;
87
+ /**
88
+ * The value the spec pins, when the extension is a string. `path` / `query`
89
+ * always send it, whatever the args or `serverParameters` carry.
90
+ */
91
+ value?: string;
68
92
  }
69
93
  /** Minimal shape of an OpenAPI document consumed by the generator. */
70
94
  interface OpenApiSpec {
@@ -74,6 +98,12 @@ interface OpenApiSpec {
74
98
  parameters?: Record<string, unknown>;
75
99
  };
76
100
  }
101
+ /**
102
+ * Documents a `$ref` may point into, keyed by the file part of the ref exactly
103
+ * as the spec writes it (`./tags.yaml` in `./tags.yaml#/components/schemas/Tag`).
104
+ * A leading `./` is optional on either side.
105
+ */
106
+ type OpenApiDocuments = Record<string, OpenApiSpec>;
77
107
  type RequestBodySpec = {
78
108
  required?: boolean;
79
109
  content?: {
@@ -119,16 +149,89 @@ interface OpenApiToToolsOptions {
119
149
  */
120
150
  excludeExtension?: string;
121
151
  /**
122
- * Body-property extension flag that, when truthy, hides the property from the
123
- * generated `inputSchema` while keeping it in `acceptedBodyFields` (for
124
- * server-managed fields the API sets itself).
152
+ * Extension flag that, when truthy, hides a value the server sets from the
153
+ * generated `inputSchema`.
154
+ *
155
+ * - On a **request-body property**, the property stays in
156
+ * `acceptedBodyFields` and is never sent (the API sets it itself).
157
+ * - On a **path or query parameter**, the parameter is listed in
158
+ * `serverManagedParameters` and the consumer supplies its value. A string
159
+ * extension value (`'true'`) pins it: the builders always send that value.
160
+ *
161
+ * Several names may be given; a node flagged by any of them is managed.
162
+ *
125
163
  * @default 'x-mcp-server-managed'
126
164
  */
127
- serverManagedExtension?: string;
165
+ serverManagedExtension?: string | string[];
166
+ /**
167
+ * How tool argument names are derived from the spec's parameter and
168
+ * body-property names.
169
+ *
170
+ * - `'camelCase'` folds `_` / `-` separators (`agent_id` → `agentId`) and maps
171
+ * them back when building the request.
172
+ * - `'verbatim'` uses the spec's names unchanged, so the MCP contract matches
173
+ * the REST contract exactly.
174
+ *
175
+ * @default 'camelCase'
176
+ */
177
+ argumentNames?: 'camelCase' | 'verbatim';
178
+ /**
179
+ * Documents that `$ref`s with a file part resolve against, keyed by that
180
+ * file part as the spec writes it — `./tags.yaml` for
181
+ * `./tags.yaml#/components/schemas/Tag`. Refs inside a document resolve
182
+ * relative to that document. A ref to a file absent from this map resolves
183
+ * to an empty schema, which accepts any value.
184
+ */
185
+ documents?: OpenApiDocuments;
128
186
  }
187
+ /** {@link OpenApiToToolsOptions} with every default applied. */
188
+ type ResolvedToolOptions = Required<Omit<OpenApiToToolsOptions, 'documents'>> & Pick<OpenApiToToolsOptions, 'documents'>;
129
189
  declare const DEFAULT_EXCLUDE_EXTENSION = "x-mcp-exclude";
130
190
  declare const DEFAULT_SERVER_MANAGED_EXTENSION = "x-mcp-server-managed";
131
191
  //#endregion
192
+ //#region src/serverManaged.d.ts
193
+ /** One extension name, or several that all mean "the server sets this". */
194
+ type ServerManagedExtension = string | string[];
195
+ //#endregion
196
+ //#region src/parameters.d.ts
197
+ /**
198
+ * Folds `_` and `-` separators into camelCase. OpenAPI operation and parameter
199
+ * names may be snake_case (`agent_id`) or kebab-case (`list-tools`); with the
200
+ * default `argumentNames: 'camelCase'` both are folded into tool arguments.
201
+ */
202
+ declare const snakeToCamel: (str: string) => string;
203
+ /** Maps a spec parameter or property name to a tool argument name. */
204
+ type ToArgName = (name: string) => string;
205
+ /** Arguments shared by the parameter extractors. */
206
+ type ExtractParamsArgs = {
207
+ parameters?: Array<{
208
+ name?: string;
209
+ in?: string;
210
+ [key: string]: unknown;
211
+ }>;
212
+ spec: OpenApiSpec; /** Sibling documents cross-file `$ref`s resolve against. */
213
+ documents?: OpenApiDocuments; /** Maps a spec name to its tool argument name. @default snakeToCamel */
214
+ toArgName?: ToArgName; /** @default DEFAULT_SERVER_MANAGED_EXTENSION */
215
+ serverManagedExtension?: ServerManagedExtension;
216
+ };
217
+ declare const extractPathParams: (args: ExtractParamsArgs) => Array<{
218
+ name: string;
219
+ argName: string;
220
+ serverManaged: boolean;
221
+ pinnedValue?: string;
222
+ }>;
223
+ declare const extractQueryParams: (args: ExtractParamsArgs) => Array<{
224
+ name: string;
225
+ argName: string;
226
+ description: string;
227
+ required: boolean;
228
+ type: string;
229
+ style?: string;
230
+ explode?: boolean;
231
+ serverManaged: boolean;
232
+ pinnedValue?: string;
233
+ }>;
234
+ //#endregion
132
235
  //#region src/registerOpenApiTools.d.ts
133
236
  /** The resolved HTTP request a tool call maps to, before transport concerns. */
134
237
  interface ResolvedRequest {
@@ -136,10 +239,16 @@ interface ResolvedRequest {
136
239
  method: string;
137
240
  /** Request path including the query string, e.g. `/agents/agt_1?limit=10`. */
138
241
  url: string;
139
- /** snake_case request body, or `undefined` when the operation has none. */
242
+ /** Request body keyed by the spec's names, or `undefined` when the operation has none. */
140
243
  body?: Record<string, unknown>;
141
244
  /** The tool definition the call resolved to (for auth/metadata lookups). */
142
245
  tool: ToolDefinition;
246
+ /**
247
+ * The headers `createMcpRouter`'s `getApiHeaders` produced for the MCP
248
+ * request this call belongs to — typically the caller's credentials. `{}`
249
+ * when `getApiHeaders` is not configured.
250
+ */
251
+ headers: Record<string, string>;
143
252
  }
144
253
  interface RegisterOpenApiToolsArgs {
145
254
  /** The MCP server the generated tools are registered on. */
@@ -155,15 +264,35 @@ interface RegisterOpenApiToolsArgs {
155
264
  */
156
265
  callApi: (request: ResolvedRequest) => Promise<unknown> | unknown;
157
266
  /**
158
- * Serialises the raw API data into the MCP tool's text payload.
159
- * @default (data) => JSON.stringify(data, null, 2)
267
+ * Serialises the raw API data into the MCP tool's text payload. The default
268
+ * passes strings through, pretty-prints anything else as JSON, and answers
269
+ * {@link NO_CONTENT_TEXT} for an empty body (e.g. a `204`).
160
270
  */
161
271
  toText?: (data: unknown) => string;
272
+ /**
273
+ * Supplies the values of the tool's server-managed path and query
274
+ * parameters (`tool.serverManagedParameters`), keyed by their spec name.
275
+ * Runs on every call, after any value the model sent for those parameters
276
+ * has been discarded.
277
+ *
278
+ * @example
279
+ * ```typescript
280
+ * serverParameters: ({ headers }) => ({
281
+ * project_id: projectIdFromToken(headers.Authorization),
282
+ * }),
283
+ * ```
284
+ */
285
+ serverParameters?: (args: {
286
+ tool: ToolDefinition;
287
+ headers: Record<string, string>;
288
+ }) => Record<string, unknown> | Promise<Record<string, unknown>>;
162
289
  }
290
+ /** The text the default `toText` answers when the API returned no body. */
291
+ declare const NO_CONTENT_TEXT = "Succeeded. The operation returned no content.";
163
292
  /**
164
293
  * Derives MCP tools from OpenAPI document(s) and registers each on the given
165
- * MCP server. Every tool's handler resolves the incoming camelCase args into a
166
- * concrete HTTP request and delegates execution to `callApi`.
294
+ * MCP server. Every tool's handler resolves the incoming args into a concrete
295
+ * HTTP request and delegates execution to `callApi`.
167
296
  *
168
297
  * @returns The list of {@link ToolDefinition} that were registered.
169
298
  *
@@ -177,10 +306,10 @@ interface RegisterOpenApiToolsArgs {
177
306
  * registerOpenApiTools({
178
307
  * server,
179
308
  * spec: myOpenApiDocument,
180
- * callApi: async ({ method, url, body }) => {
309
+ * callApi: async ({ method, url, body, headers }) => {
181
310
  * const res = await fetch(`https://api.example.com${url}`, {
182
311
  * method,
183
- * headers: { 'Content-Type': 'application/json' },
312
+ * headers: { ...headers, 'Content-Type': 'application/json' },
184
313
  * body: body ? JSON.stringify(body) : undefined,
185
314
  * });
186
315
  * return res.json();
@@ -203,16 +332,18 @@ type ResolvedSchema = {
203
332
  * `properties`, `items`, `oneOf`, `anyOf`, etc.), producing a self-contained
204
333
  * schema safe to hand to an MCP client or LLM provider as a tool definition —
205
334
  * provider tool schemas have no `components` section to resolve refs against.
335
+ *
336
+ * Refs into other files resolve against `documents`; see {@link OpenApiDocuments}.
206
337
  */
207
- declare const dereferenceSchema: (schema: Record<string, unknown> | undefined, spec: OpenApiSpec) => Record<string, unknown> | undefined;
338
+ declare const dereferenceSchema: (schema: Record<string, unknown> | undefined, spec: OpenApiSpec, documents?: OpenApiDocuments) => Record<string, unknown> | undefined;
208
339
  /**
209
340
  * Resolves a schema down to a single object shape: follows a top-level `$ref`
210
341
  * and merges `oneOf` / `anyOf` alternatives (union of properties, intersection
211
342
  * of `required`) so the caller sees one flat property set.
212
343
  */
213
- declare const resolveSchema: (schema: Record<string, unknown> | undefined, spec: OpenApiSpec) => ResolvedSchema;
214
- /** Follows a parameter `$ref` into `components.parameters`, if present. */
215
- declare const resolveParameter: (param: Record<string, unknown> | undefined, spec: OpenApiSpec) => {
344
+ declare const resolveSchema: (schema: Record<string, unknown> | undefined, spec: OpenApiSpec, documents?: OpenApiDocuments) => ResolvedSchema;
345
+ /** A parameter object after its `$ref` (if any) is followed. */
346
+ type ResolvedParameter = {
216
347
  name?: string;
217
348
  in?: string;
218
349
  required?: boolean;
@@ -225,16 +356,22 @@ declare const resolveParameter: (param: Record<string, unknown> | undefined, spe
225
356
  type?: string;
226
357
  };
227
358
  };
359
+ [extension: string]: unknown;
228
360
  };
361
+ /**
362
+ * Follows a parameter `$ref` — into `components.parameters`, or into another
363
+ * file through `documents` — if present.
364
+ */
365
+ declare const resolveParameter: (param: Record<string, unknown> | undefined, spec: OpenApiSpec, documents?: OpenApiDocuments) => ResolvedParameter;
229
366
  /** Builds a function that substitutes path params into the path template. */
230
367
  declare const buildPathFn: (pathTemplate: string, pathParams: Array<{
231
368
  name: string;
232
- camelName: string;
369
+ argName: string;
233
370
  }>) => ((args: Record<string, unknown>) => string);
234
371
  /** Query parameter with the OpenAPI serialisation rules declared for it. */
235
372
  type QueryParamSerialization = {
236
373
  name: string;
237
- camelName: string; /** OpenAPI `style` (`form`, `deepObject`, `spaceDelimited`, `pipeDelimited`). */
374
+ argName: string; /** OpenAPI `style` (`form`, `deepObject`, `spaceDelimited`, `pipeDelimited`). */
238
375
  style?: string; /** OpenAPI `explode`. Defaults to `true` for `form`, `false` otherwise. */
239
376
  explode?: boolean;
240
377
  };
@@ -245,36 +382,32 @@ type QueryParamSerialization = {
245
382
  */
246
383
  declare const buildQueryFn: (queryParams: QueryParamSerialization[]) => ((args: Record<string, unknown>) => string) | undefined;
247
384
  /**
248
- * Builds a function that maps camelCase args back to a snake_case request
249
- * body, skipping `undefined` args. Returns `undefined` when the op has no body.
385
+ * Builds a function that maps tool args back to a request body keyed by the
386
+ * spec's property names, skipping `undefined` args. Returns `undefined` when the op has no body.
250
387
  */
251
388
  declare const buildBodyFn: (bodyProps: Array<{
252
389
  snakeName: string;
253
- camelName: string;
390
+ argName: string;
254
391
  }>) => ((args: Record<string, unknown>) => Record<string, unknown>) | undefined;
255
392
  //#endregion
256
393
  //#region src/toolDefinitions.d.ts
257
- /**
258
- * Folds `_` and `-` separators into camelCase. OpenAPI operation and parameter
259
- * names may be snake_case (`agent_id`) or kebab-case (`list-tools`), and MCP
260
- * tool inputs are camelCase by convention, so both are folded here.
261
- */
262
- declare const snakeToCamel: (str: string) => string;
263
394
  /** Converts a camelCase `operationId` to a kebab-case tool name. */
264
395
  declare const operationIdToToolName: (operationId: string) => string;
265
396
  declare const getJsonSchemaType: (schemaType: string | undefined) => JsonSchemaProperty["type"];
266
397
  declare const buildInputSchema: (pathParams: Array<{
267
398
  name: string;
268
- camelName: string;
399
+ argName: string;
400
+ serverManaged?: boolean;
269
401
  }>, queryParams: Array<{
270
402
  name: string;
271
- camelName: string;
403
+ argName: string;
272
404
  description: string;
273
405
  required: boolean;
274
406
  type: string;
407
+ serverManaged?: boolean;
275
408
  }>, bodyProps: Array<{
276
409
  snakeName: string;
277
- camelName: string;
410
+ argName: string;
278
411
  description: string;
279
412
  required: boolean;
280
413
  type?: string;
@@ -284,33 +417,6 @@ declare const buildInputSchema: (pathParams: Array<{
284
417
  anyOf?: unknown[];
285
418
  allOf?: unknown[];
286
419
  }>) => JsonObjectSchema;
287
- declare const extractPathParams: (args: {
288
- parameters?: Array<{
289
- name?: string;
290
- in?: string;
291
- [key: string]: unknown;
292
- }>;
293
- spec: OpenApiSpec;
294
- }) => Array<{
295
- name: string;
296
- camelName: string;
297
- }>;
298
- declare const extractQueryParams: (args: {
299
- parameters?: Array<{
300
- name?: string;
301
- in?: string;
302
- [key: string]: unknown;
303
- }>;
304
- spec: OpenApiSpec;
305
- }) => Array<{
306
- name: string;
307
- camelName: string;
308
- description: string;
309
- required: boolean;
310
- type: string;
311
- style?: string;
312
- explode?: boolean;
313
- }>;
314
420
  /**
315
421
  * snake_case names of every top-level property an operation's request schema
316
422
  * declares, including server-managed ones.
@@ -318,14 +424,17 @@ declare const extractQueryParams: (args: {
318
424
  declare const extractAcceptedBodyFields: (args: {
319
425
  requestBody?: RequestBodySpec;
320
426
  spec: OpenApiSpec;
427
+ documents?: OpenApiDocuments;
321
428
  }) => string[];
322
429
  declare const extractBodyProps: (args: {
323
430
  requestBody?: RequestBodySpec;
324
431
  spec: OpenApiSpec;
325
- serverManagedExtension: string;
432
+ serverManagedExtension: ServerManagedExtension;
433
+ documents?: OpenApiDocuments; /** Maps a spec name to its tool argument name. @default snakeToCamel */
434
+ toArgName?: ToArgName;
326
435
  }) => Array<{
327
436
  snakeName: string;
328
- camelName: string;
437
+ argName: string;
329
438
  description: string;
330
439
  required: boolean;
331
440
  type?: string;
@@ -340,7 +449,7 @@ declare const processOperation: (args: {
340
449
  method: string;
341
450
  operation: OperationSpec;
342
451
  spec: OpenApiSpec;
343
- options: Required<OpenApiToToolsOptions>;
452
+ options: ResolvedToolOptions;
344
453
  /**
345
454
  * Parameters declared at the path-item level (shared by every operation on
346
455
  * the path). Merged ahead of the operation's own parameters so operation-level
@@ -356,7 +465,7 @@ declare const processPath: (args: {
356
465
  pathTemplate: string;
357
466
  pathItem: Record<string, OperationSpec>;
358
467
  spec: OpenApiSpec;
359
- options: Required<OpenApiToToolsOptions>;
468
+ options: ResolvedToolOptions;
360
469
  }) => ToolDefinition[];
361
470
  /**
362
471
  * Translates one or more OpenAPI documents into REST-backed MCP tool
@@ -375,4 +484,4 @@ declare const openApiToToolDefinitions: (args: {
375
484
  options?: OpenApiToToolsOptions;
376
485
  }) => ToolDefinition[];
377
486
  //#endregion
378
- export { DEFAULT_EXCLUDE_EXTENSION, DEFAULT_SERVER_MANAGED_EXTENSION, type JsonSchemaProperty, type OpenApiSpec, type OpenApiToToolsOptions, type OperationSpec, type QueryParamSerialization, type RegisterOpenApiToolsArgs, type RequestBodySpec, type ResolvedRequest, type ToolDefinition, buildBodyFn, buildInputSchema, buildPathFn, buildQueryFn, dereferenceSchema, extractAcceptedBodyFields, extractBodyProps, extractPathParams, extractQueryParams, getJsonSchemaType, openApiToToolDefinitions, operationIdToToolName, processOperation, processPath, registerOpenApiTools, resolveParameter, resolveSchema, snakeToCamel };
487
+ export { DEFAULT_EXCLUDE_EXTENSION, DEFAULT_SERVER_MANAGED_EXTENSION, type JsonSchemaProperty, NO_CONTENT_TEXT, type OpenApiDocuments, type OpenApiSpec, type OpenApiToToolsOptions, type OperationSpec, type QueryParamSerialization, type RegisterOpenApiToolsArgs, type RequestBodySpec, type ResolvedParameter, type ResolvedRequest, type ResolvedToolOptions, type ServerManagedParameter, type ToolDefinition, buildBodyFn, buildInputSchema, buildPathFn, buildQueryFn, dereferenceSchema, extractAcceptedBodyFields, extractBodyProps, extractPathParams, extractQueryParams, getJsonSchemaType, openApiToToolDefinitions, operationIdToToolName, processOperation, processPath, registerOpenApiTools, resolveParameter, resolveSchema, snakeToCamel };