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