mcp-from-openapi 0.0.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.
@@ -0,0 +1,296 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SchemaBuilder = void 0;
4
+ /**
5
+ * Helper class for building and manipulating JSON schemas
6
+ */
7
+ class SchemaBuilder {
8
+ /**
9
+ * Merge multiple schemas into one
10
+ */
11
+ static merge(schemas) {
12
+ if (schemas.length === 0) {
13
+ return { type: 'object' };
14
+ }
15
+ if (schemas.length === 1) {
16
+ return schemas[0];
17
+ }
18
+ const merged = {
19
+ type: 'object',
20
+ properties: {},
21
+ required: [],
22
+ };
23
+ const allRequired = new Set();
24
+ for (const schema of schemas) {
25
+ if (schema.properties) {
26
+ merged.properties = {
27
+ ...merged.properties,
28
+ ...schema.properties,
29
+ };
30
+ }
31
+ if (schema.required) {
32
+ schema.required.forEach((field) => allRequired.add(field));
33
+ }
34
+ }
35
+ if (allRequired.size > 0) {
36
+ merged.required = Array.from(allRequired);
37
+ }
38
+ return merged;
39
+ }
40
+ /**
41
+ * Create a union schema (oneOf)
42
+ */
43
+ static union(schemas) {
44
+ if (schemas.length === 0) {
45
+ return {};
46
+ }
47
+ if (schemas.length === 1) {
48
+ return schemas[0];
49
+ }
50
+ return {
51
+ oneOf: schemas,
52
+ };
53
+ }
54
+ /**
55
+ * Deep clone a schema
56
+ */
57
+ static clone(schema) {
58
+ return JSON.parse(JSON.stringify(schema));
59
+ }
60
+ /**
61
+ * Remove $ref from schema (assumes already dereferenced)
62
+ */
63
+ static removeRefs(schema) {
64
+ const cloned = this.clone(schema);
65
+ this.removeRefsRecursive(cloned);
66
+ return cloned;
67
+ }
68
+ static removeRefsRecursive(obj) {
69
+ if (!obj || typeof obj !== 'object')
70
+ return;
71
+ if (obj.$ref) {
72
+ delete obj.$ref;
73
+ }
74
+ for (const key in obj) {
75
+ if (obj.hasOwnProperty(key)) {
76
+ const value = obj[key];
77
+ if (value && typeof value === 'object') {
78
+ this.removeRefsRecursive(value);
79
+ }
80
+ }
81
+ }
82
+ }
83
+ /**
84
+ * Add description to schema
85
+ */
86
+ static withDescription(schema, description) {
87
+ return {
88
+ ...schema,
89
+ description,
90
+ };
91
+ }
92
+ /**
93
+ * Mark schema as required
94
+ */
95
+ static required(schema) {
96
+ return schema;
97
+ }
98
+ /**
99
+ * Mark schema as optional
100
+ */
101
+ static optional(schema) {
102
+ return schema;
103
+ }
104
+ /**
105
+ * Add example to schema
106
+ */
107
+ static withExample(schema, example) {
108
+ const existingExamples = Array.isArray(schema.examples) ? schema.examples : [];
109
+ return {
110
+ ...schema,
111
+ examples: [...existingExamples, example],
112
+ };
113
+ }
114
+ /**
115
+ * Add default value to schema
116
+ */
117
+ static withDefault(schema, defaultValue) {
118
+ return {
119
+ ...schema,
120
+ default: defaultValue,
121
+ };
122
+ }
123
+ /**
124
+ * Add format to schema
125
+ */
126
+ static withFormat(schema, format) {
127
+ return {
128
+ ...schema,
129
+ format,
130
+ };
131
+ }
132
+ /**
133
+ * Add pattern to schema
134
+ */
135
+ static withPattern(schema, pattern) {
136
+ return {
137
+ ...schema,
138
+ pattern,
139
+ };
140
+ }
141
+ /**
142
+ * Add enum to schema
143
+ */
144
+ static withEnum(schema, values) {
145
+ return {
146
+ ...schema,
147
+ enum: values,
148
+ };
149
+ }
150
+ /**
151
+ * Add minimum/maximum constraints
152
+ */
153
+ static withRange(schema, min, max, options = {}) {
154
+ const result = { ...schema };
155
+ if (min !== undefined) {
156
+ if (options.exclusive) {
157
+ result.exclusiveMinimum = min;
158
+ }
159
+ else {
160
+ result.minimum = min;
161
+ }
162
+ }
163
+ if (max !== undefined) {
164
+ if (options.exclusive) {
165
+ result.exclusiveMaximum = max;
166
+ }
167
+ else {
168
+ result.maximum = max;
169
+ }
170
+ }
171
+ return result;
172
+ }
173
+ /**
174
+ * Add minLength/maxLength constraints
175
+ */
176
+ static withLength(schema, minLength, maxLength) {
177
+ const result = { ...schema };
178
+ if (minLength !== undefined) {
179
+ result.minLength = minLength;
180
+ }
181
+ if (maxLength !== undefined) {
182
+ result.maxLength = maxLength;
183
+ }
184
+ return result;
185
+ }
186
+ /**
187
+ * Create object schema
188
+ */
189
+ static object(properties, required) {
190
+ return {
191
+ type: 'object',
192
+ properties,
193
+ ...(required && required.length > 0 && { required }),
194
+ additionalProperties: false,
195
+ };
196
+ }
197
+ /**
198
+ * Create array schema
199
+ */
200
+ static array(items, constraints) {
201
+ return {
202
+ type: 'array',
203
+ items,
204
+ ...constraints,
205
+ };
206
+ }
207
+ /**
208
+ * Create string schema
209
+ */
210
+ static string(constraints) {
211
+ return {
212
+ type: 'string',
213
+ ...constraints,
214
+ };
215
+ }
216
+ /**
217
+ * Create number schema
218
+ */
219
+ static number(constraints) {
220
+ return {
221
+ type: 'number',
222
+ ...constraints,
223
+ };
224
+ }
225
+ /**
226
+ * Create integer schema
227
+ */
228
+ static integer(constraints) {
229
+ return {
230
+ type: 'integer',
231
+ ...constraints,
232
+ };
233
+ }
234
+ /**
235
+ * Create boolean schema
236
+ */
237
+ static boolean() {
238
+ return {
239
+ type: 'boolean',
240
+ };
241
+ }
242
+ /**
243
+ * Create null schema
244
+ */
245
+ static null() {
246
+ return {
247
+ type: 'null',
248
+ };
249
+ }
250
+ /**
251
+ * Flatten nested oneOf/anyOf/allOf schemas
252
+ */
253
+ static flatten(schema, maxDepth = 10) {
254
+ if (maxDepth <= 0)
255
+ return schema;
256
+ const cloned = this.clone(schema);
257
+ if (cloned.oneOf) {
258
+ const flattened = cloned.oneOf.flatMap((s) => {
259
+ const sub = this.flatten(s, maxDepth - 1);
260
+ return sub.oneOf ? sub.oneOf : [sub];
261
+ });
262
+ cloned.oneOf = flattened;
263
+ }
264
+ if (cloned.anyOf) {
265
+ const flattened = cloned.anyOf.flatMap((s) => {
266
+ const sub = this.flatten(s, maxDepth - 1);
267
+ return sub.anyOf ? sub.anyOf : [sub];
268
+ });
269
+ cloned.anyOf = flattened;
270
+ }
271
+ return cloned;
272
+ }
273
+ /**
274
+ * Simplify schema by removing unnecessary fields
275
+ */
276
+ static simplify(schema) {
277
+ const cloned = this.clone(schema);
278
+ // Remove empty arrays/objects
279
+ if (Array.isArray(cloned.required) && cloned.required.length === 0) {
280
+ delete cloned.required;
281
+ }
282
+ if (cloned.properties && Object.keys(cloned.properties).length === 0) {
283
+ delete cloned.properties;
284
+ }
285
+ if (Array.isArray(cloned.examples) && cloned.examples.length === 0) {
286
+ delete cloned.examples;
287
+ }
288
+ // Remove title if it matches description
289
+ if (cloned.title && cloned.description && cloned.title === cloned.description) {
290
+ delete cloned.title;
291
+ }
292
+ return cloned;
293
+ }
294
+ }
295
+ exports.SchemaBuilder = SchemaBuilder;
296
+ //# sourceMappingURL=schema-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema-builder.js","sourceRoot":"","sources":["../../src/schema-builder.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACH,MAAa,aAAa;IACxB;;OAEG;IACH,MAAM,CAAC,KAAK,CAAC,OAAsB;QACjC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;QAC5B,CAAC;QAED,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC;QACpB,CAAC;QAED,MAAM,MAAM,GAAgB;YAC1B,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE,EAAE;YACd,QAAQ,EAAE,EAAE;SACb,CAAC;QAEF,MAAM,WAAW,GAAG,IAAI,GAAG,EAAU,CAAC;QAEtC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC7B,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;gBACtB,MAAM,CAAC,UAAU,GAAG;oBAClB,GAAG,MAAM,CAAC,UAAU;oBACpB,GAAG,MAAM,CAAC,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AK,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACnE,OAAO,MAAM,CAAC,QAAQ,CAAC;QACzB,CAAC;QAED,yCAAyC;QACzC,IAAI,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,WAAW,IAAI,MAAM,CAAC,KAAK,KAAK,MAAM,CAAC,WAAW,EAAE,CAAC;YAC9E,OAAO,MAAM,CAAC,KAAK,CAAC;QACtB,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AA1WD,sCA0WC","sourcesContent":["import type { JSONSchema7 } from 'json-schema';\n\n/**\n * Helper class for building and manipulating JSON schemas\n */\nexport class SchemaBuilder {\n /**\n * Merge multiple schemas into one\n */\n static merge(schemas: JSONSchema7[]): JSONSchema7 {\n if (schemas.length === 0) {\n return { type: 'object' };\n }\n\n if (schemas.length === 1) {\n return schemas[0];\n }\n\n const merged: JSONSchema7 = {\n type: 'object',\n properties: {},\n required: [],\n };\n\n const allRequired = new Set<string>();\n\n for (const schema of schemas) {\n if (schema.properties) {\n merged.properties = {\n ...merged.properties,\n ...schema.properties,\n };\n }\n\n if (schema.required) {\n schema.required.forEach((field) => allRequired.add(field));\n }\n }\n\n if (allRequired.size > 0) {\n merged.required = Array.from(allRequired);\n }\n\n return merged;\n }\n\n /**\n * Create a union schema (oneOf)\n */\n static union(schemas: JSONSchema7[]): JSONSchema7 {\n if (schemas.length === 0) {\n return {};\n }\n\n if (schemas.length === 1) {\n return schemas[0];\n }\n\n return {\n oneOf: schemas,\n };\n }\n\n /**\n * Deep clone a schema\n */\n static clone(schema: JSONSchema7): JSONSchema7 {\n return JSON.parse(JSON.stringify(schema));\n }\n\n /**\n * Remove $ref from schema (assumes already dereferenced)\n */\n static removeRefs(schema: JSONSchema7): JSONSchema7 {\n const cloned = this.clone(schema);\n this.removeRefsRecursive(cloned);\n return cloned;\n }\n\n private static removeRefsRecursive(obj: any): void {\n if (!obj || typeof obj !== 'object') return;\n\n if (obj.$ref) {\n delete obj.$ref;\n }\n\n for (const key in obj) {\n if (obj.hasOwnProperty(key)) {\n const value = obj[key];\n if (value && typeof value === 'object') {\n this.removeRefsRecursive(value);\n }\n }\n }\n }\n\n /**\n * Add description to schema\n */\n static withDescription(schema: JSONSchema7, description: string): JSONSchema7 {\n return {\n ...schema,\n description,\n };\n }\n\n /**\n * Mark schema as required\n */\n static required(schema: JSONSchema7): JSONSchema7 {\n return schema;\n }\n\n /**\n * Mark schema as optional\n */\n static optional(schema: JSONSchema7): JSONSchema7 {\n return schema;\n }\n\n /**\n * Add example to schema\n */\n static withExample(schema: JSONSchema7, example: any): JSONSchema7 {\n const existingExamples = Array.isArray(schema.examples) ? schema.examples : [];\n return {\n ...schema,\n examples: [...existingExamples, example],\n };\n }\n\n /**\n * Add default value to schema\n */\n static withDefault(schema: JSONSchema7, defaultValue: any): JSONSchema7 {\n return {\n ...schema,\n default: defaultValue,\n };\n }\n\n /**\n * Add format to schema\n */\n static withFormat(schema: JSONSchema7, format: string): JSONSchema7 {\n return {\n ...schema,\n format,\n };\n }\n\n /**\n * Add pattern to schema\n */\n static withPattern(schema: JSONSchema7, pattern: string): JSONSchema7 {\n return {\n ...schema,\n pattern,\n };\n }\n\n /**\n * Add enum to schema\n */\n static withEnum(schema: JSONSchema7, values: any[]): JSONSchema7 {\n return {\n ...schema,\n enum: values,\n };\n }\n\n /**\n * Add minimum/maximum constraints\n */\n static withRange(\n schema: JSONSchema7,\n min?: number,\n max?: number,\n options: { exclusive?: boolean } = {}\n ): JSONSchema7 {\n const result = { ...schema };\n\n if (min !== undefined) {\n if (options.exclusive) {\n result.exclusiveMinimum = min;\n } else {\n result.minimum = min;\n }\n }\n\n if (max !== undefined) {\n if (options.exclusive) {\n result.exclusiveMaximum = max;\n } else {\n result.maximum = max;\n }\n }\n\n return result;\n }\n\n /**\n * Add minLength/maxLength constraints\n */\n static withLength(schema: JSONSchema7, minLength?: number, maxLength?: number): JSONSchema7 {\n const result = { ...schema };\n\n if (minLength !== undefined) {\n result.minLength = minLength;\n }\n\n if (maxLength !== undefined) {\n result.maxLength = maxLength;\n }\n\n return result;\n }\n\n /**\n * Create object schema\n */\n static object(\n properties: Record<string, JSONSchema7>,\n required?: string[]\n ): JSONSchema7 {\n return {\n type: 'object',\n properties,\n ...(required && required.length > 0 && { required }),\n additionalProperties: false,\n };\n }\n\n /**\n * Create array schema\n */\n static array(items: JSONSchema7, constraints?: {\n minItems?: number;\n maxItems?: number;\n uniqueItems?: boolean;\n }): JSONSchema7 {\n return {\n type: 'array',\n items,\n ...constraints,\n };\n }\n\n /**\n * Create string schema\n */\n static string(constraints?: {\n minLength?: number;\n maxLength?: number;\n pattern?: string;\n format?: string;\n enum?: string[];\n }): JSONSchema7 {\n return {\n type: 'string',\n ...constraints,\n };\n }\n\n /**\n * Create number schema\n */\n static number(constraints?: {\n minimum?: number;\n maximum?: number;\n exclusiveMinimum?: number;\n exclusiveMaximum?: number;\n multipleOf?: number;\n }): JSONSchema7 {\n return {\n type: 'number',\n ...constraints,\n };\n }\n\n /**\n * Create integer schema\n */\n static integer(constraints?: {\n minimum?: number;\n maximum?: number;\n exclusiveMinimum?: number;\n exclusiveMaximum?: number;\n multipleOf?: number;\n }): JSONSchema7 {\n return {\n type: 'integer',\n ...constraints,\n };\n }\n\n /**\n * Create boolean schema\n */\n static boolean(): JSONSchema7 {\n return {\n type: 'boolean',\n };\n }\n\n /**\n * Create null schema\n */\n static null(): JSONSchema7 {\n return {\n type: 'null',\n };\n }\n\n /**\n * Flatten nested oneOf/anyOf/allOf schemas\n */\n static flatten(schema: JSONSchema7, maxDepth = 10): JSONSchema7 {\n if (maxDepth <= 0) return schema;\n\n const cloned = this.clone(schema);\n\n if (cloned.oneOf) {\n const flattened = cloned.oneOf.flatMap((s) => {\n const sub = this.flatten(s as JSONSchema7, maxDepth - 1);\n return sub.oneOf ? sub.oneOf : [sub];\n });\n cloned.oneOf = flattened as JSONSchema7[];\n }\n\n if (cloned.anyOf) {\n const flattened = cloned.anyOf.flatMap((s) => {\n const sub = this.flatten(s as JSONSchema7, maxDepth - 1);\n return sub.anyOf ? sub.anyOf : [sub];\n });\n cloned.anyOf = flattened as JSONSchema7[];\n }\n\n return cloned;\n }\n\n /**\n * Simplify schema by removing unnecessary fields\n */\n static simplify(schema: JSONSchema7): JSONSchema7 {\n const cloned = this.clone(schema);\n\n // Remove empty arrays/objects\n if (Array.isArray(cloned.required) && cloned.required.length === 0) {\n delete cloned.required;\n }\n\n if (cloned.properties && Object.keys(cloned.properties).length === 0) {\n delete cloned.properties;\n }\n\n if (Array.isArray(cloned.examples) && cloned.examples.length === 0) {\n delete cloned.examples;\n }\n\n // Remove title if it matches description\n if (cloned.title && cloned.description && cloned.title === cloned.description) {\n delete cloned.title;\n }\n\n return cloned;\n }\n}\n"]}
package/src/types.d.ts ADDED
@@ -0,0 +1,371 @@
1
+ import type { JSONSchema7 } from 'json-schema';
2
+ import type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types';
3
+ /**
4
+ * OpenAPI specification version 3.0.x or 3.1.x
5
+ */
6
+ export type OpenAPIVersion = '3.0.0' | '3.0.1' | '3.0.2' | '3.0.3' | '3.1.0';
7
+ /**
8
+ * Unified OpenAPI Document type (supports both 3.0 and 3.1)
9
+ */
10
+ export type OpenAPIDocument = OpenAPIV3.Document | OpenAPIV3_1.Document;
11
+ /**
12
+ * HTTP methods supported by OpenAPI
13
+ */
14
+ export type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head' | 'options' | 'trace';
15
+ /**
16
+ * Parameter location types
17
+ */
18
+ export type ParameterLocation = 'path' | 'query' | 'header' | 'cookie' | 'body';
19
+ /**
20
+ * Authentication types supported
21
+ */
22
+ export type AuthType = 'apiKey' | 'http' | 'oauth2' | 'openIdConnect';
23
+ export type OperationObject = OpenAPIV3.OperationObject | OpenAPIV3_1.OperationObject;
24
+ export type ParameterObject = OpenAPIV3.ParameterObject | OpenAPIV3_1.ParameterObject;
25
+ export type RequestBodyObject = OpenAPIV3.RequestBodyObject | OpenAPIV3_1.RequestBodyObject;
26
+ export type ResponseObject = OpenAPIV3.ResponseObject | OpenAPIV3_1.ResponseObject;
27
+ export type ResponsesObject = OpenAPIV3.ResponsesObject | OpenAPIV3_1.ResponsesObject;
28
+ export type MediaTypeObject = OpenAPIV3.MediaTypeObject | OpenAPIV3_1.MediaTypeObject;
29
+ export type HeaderObject = OpenAPIV3.HeaderObject | OpenAPIV3_1.HeaderObject;
30
+ export type ExampleObject = OpenAPIV3.ExampleObject | OpenAPIV3_1.ExampleObject;
31
+ export type PathItemObject = OpenAPIV3.PathItemObject | OpenAPIV3_1.PathItemObject;
32
+ export type PathsObject = OpenAPIV3.PathsObject | OpenAPIV3_1.PathsObject;
33
+ export type ServerObject = OpenAPIV3.ServerObject | OpenAPIV3_1.ServerObject;
34
+ export type SecuritySchemeObject = OpenAPIV3.SecuritySchemeObject | OpenAPIV3_1.SecuritySchemeObject;
35
+ export type ReferenceObject = OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject;
36
+ export type TagObject = OpenAPIV3.TagObject | OpenAPIV3_1.TagObject;
37
+ export type ExternalDocumentationObject = OpenAPIV3.ExternalDocumentationObject | OpenAPIV3_1.ExternalDocumentationObject;
38
+ export type ServerVariableObject = OpenAPIV3.ServerVariableObject | OpenAPIV3_1.ServerVariableObject;
39
+ export type EncodingObject = OpenAPIV3.EncodingObject | OpenAPIV3_1.EncodingObject;
40
+ export type SecurityRequirementObject = OpenAPIV3.SecurityRequirementObject | OpenAPIV3_1.SecurityRequirementObject;
41
+ export type SchemaObject = OpenAPIV3.SchemaObject | OpenAPIV3_1.SchemaObject;
42
+ /**
43
+ * Helper to check if an object is a ReferenceObject
44
+ */
45
+ export declare function isReferenceObject(obj: any): obj is ReferenceObject;
46
+ /**
47
+ * Convert OpenAPI schema to JSONSchema7
48
+ * Note: OpenAPI 3.0 uses a subset of JSON Schema Draft 4
49
+ * OpenAPI 3.1 uses JSON Schema Draft 2020-12
50
+ */
51
+ export declare function toJSONSchema7(schema: SchemaObject | ReferenceObject): JSONSchema7;
52
+ /**
53
+ * Main MCP Tool definition generated from OpenAPI
54
+ */
55
+ export interface McpOpenAPITool {
56
+ /**
57
+ * Unique tool name (from operationId or generated)
58
+ */
59
+ name: string;
60
+ /**
61
+ * Tool description (from operation summary/description)
62
+ */
63
+ description: string;
64
+ /**
65
+ * Combined input schema including all parameters
66
+ * (path, query, header, cookie, body)
67
+ */
68
+ inputSchema: JSONSchema7;
69
+ /**
70
+ * Output schema based on response definitions
71
+ * Can be a union of multiple status codes
72
+ */
73
+ outputSchema?: JSONSchema7;
74
+ /**
75
+ * Mapping from input schema properties to actual request parameters
76
+ */
77
+ mapper: ParameterMapper[];
78
+ /**
79
+ * Additional metadata about the tool
80
+ */
81
+ metadata: ToolMetadata;
82
+ }
83
+ /**
84
+ * Maps input schema properties to their actual request locations
85
+ */
86
+ export interface ParameterMapper {
87
+ /**
88
+ * Property name in the input schema
89
+ */
90
+ inputKey: string;
91
+ /**
92
+ * Where this parameter should be placed in the request
93
+ */
94
+ type: ParameterLocation;
95
+ /**
96
+ * Original parameter name (before conflict resolution)
97
+ */
98
+ key: string;
99
+ /**
100
+ * Whether this parameter is required
101
+ */
102
+ required?: boolean;
103
+ /**
104
+ * Parameter style (for path/query parameters)
105
+ */
106
+ style?: string;
107
+ /**
108
+ * Whether to explode arrays/objects
109
+ */
110
+ explode?: boolean;
111
+ /**
112
+ * Custom serialization info
113
+ */
114
+ serialization?: SerializationInfo;
115
+ }
116
+ /**
117
+ * Serialization information for complex parameters
118
+ */
119
+ export interface SerializationInfo {
120
+ /**
121
+ * Content type for body parameters
122
+ */
123
+ contentType?: string;
124
+ /**
125
+ * Encoding rules
126
+ */
127
+ encoding?: Record<string, EncodingObject>;
128
+ }
129
+ /**
130
+ * Additional metadata about the generated tool
131
+ */
132
+ export interface ToolMetadata {
133
+ /**
134
+ * Original OpenAPI path
135
+ */
136
+ path: string;
137
+ /**
138
+ * HTTP method
139
+ */
140
+ method: HTTPMethod;
141
+ /**
142
+ * Operation ID from OpenAPI
143
+ */
144
+ operationId?: string;
145
+ /**
146
+ * Tags from OpenAPI
147
+ */
148
+ tags?: string[];
149
+ /**
150
+ * Whether operation is deprecated
151
+ */
152
+ deprecated?: boolean;
153
+ /**
154
+ * Security requirements
155
+ */
156
+ security?: SecurityRequirement[];
157
+ /**
158
+ * Server information
159
+ */
160
+ servers?: ServerInfo[];
161
+ /**
162
+ * Response status codes included in output schema
163
+ */
164
+ responseStatusCodes?: number[];
165
+ /**
166
+ * External documentation
167
+ */
168
+ externalDocs?: ExternalDocumentationObject;
169
+ }
170
+ /**
171
+ * Security requirement definition
172
+ */
173
+ export interface SecurityRequirement {
174
+ /**
175
+ * Security scheme name
176
+ */
177
+ scheme: string;
178
+ /**
179
+ * Security type
180
+ */
181
+ type: AuthType;
182
+ /**
183
+ * Scopes required (for OAuth2/OpenID Connect)
184
+ */
185
+ scopes?: string[];
186
+ /**
187
+ * Parameter name (for API key)
188
+ */
189
+ name?: string;
190
+ /**
191
+ * Parameter location (for API key)
192
+ */
193
+ in?: 'query' | 'header' | 'cookie';
194
+ }
195
+ /**
196
+ * Server information
197
+ */
198
+ export interface ServerInfo {
199
+ /**
200
+ * Server URL
201
+ */
202
+ url: string;
203
+ /**
204
+ * Server description
205
+ */
206
+ description?: string;
207
+ /**
208
+ * Server variables
209
+ */
210
+ variables?: Record<string, ServerVariableObject>;
211
+ }
212
+ /**
213
+ * Options for loading OpenAPI specifications
214
+ */
215
+ export interface LoadOptions {
216
+ /**
217
+ * Whether to dereference $refs in schemas
218
+ * @default true
219
+ */
220
+ dereference?: boolean;
221
+ /**
222
+ * Base URL for API requests
223
+ * Overrides servers in OpenAPI spec
224
+ */
225
+ baseUrl?: string;
226
+ /**
227
+ * Custom HTTP headers for loading from URL
228
+ */
229
+ headers?: Record<string, string>;
230
+ /**
231
+ * Request timeout in milliseconds
232
+ * @default 30000
233
+ */
234
+ timeout?: number;
235
+ /**
236
+ * Whether to validate the OpenAPI document
237
+ * @default true
238
+ */
239
+ validate?: boolean;
240
+ /**
241
+ * Whether to follow HTTP redirects
242
+ * @default true
243
+ */
244
+ followRedirects?: boolean;
245
+ }
246
+ /**
247
+ * Operation object with additional context for filtering
248
+ */
249
+ export type OperationWithContext = OperationObject & {
250
+ path: string;
251
+ method: string;
252
+ };
253
+ /**
254
+ * Options for generating tools
255
+ */
256
+ export interface GenerateOptions {
257
+ /**
258
+ * Include only these operation IDs
259
+ */
260
+ includeOperations?: string[];
261
+ /**
262
+ * Exclude these operation IDs
263
+ */
264
+ excludeOperations?: string[];
265
+ /**
266
+ * Custom filter function
267
+ */
268
+ filterFn?: (operation: OperationWithContext) => boolean;
269
+ /**
270
+ * Naming strategy for resolving conflicts
271
+ */
272
+ namingStrategy?: NamingStrategy;
273
+ /**
274
+ * Preferred response status codes (in order of preference)
275
+ * @default [200, 201, 204, 202, 203, 206]
276
+ */
277
+ preferredStatusCodes?: number[];
278
+ /**
279
+ * Whether to include deprecated operations
280
+ * @default false
281
+ */
282
+ includeDeprecated?: boolean;
283
+ /**
284
+ * Whether to include all response codes in output schema
285
+ * If false, only preferred status code is used
286
+ * @default true
287
+ */
288
+ includeAllResponses?: boolean;
289
+ /**
290
+ * Maximum depth for dereferencing schemas
291
+ * @default 10
292
+ */
293
+ maxSchemaDepth?: number;
294
+ /**
295
+ * Whether to include examples in schemas
296
+ * @default false
297
+ */
298
+ includeExamples?: boolean;
299
+ }
300
+ /**
301
+ * Naming strategy for resolving parameter conflicts
302
+ */
303
+ export interface NamingStrategy {
304
+ /**
305
+ * Resolver function for parameter name conflicts
306
+ * @param paramName - Original parameter name
307
+ * @param location - Parameter location
308
+ * @param index - Index of conflicting parameter (0-based)
309
+ * @returns New parameter name
310
+ */
311
+ conflictResolver: (paramName: string, location: ParameterLocation, index: number) => string;
312
+ /**
313
+ * Function to generate tool names
314
+ * @param path - OpenAPI path
315
+ * @param method - HTTP method
316
+ * @param operationId - Operation ID if available
317
+ * @returns Tool name
318
+ */
319
+ toolNameGenerator?: (path: string, method: HTTPMethod, operationId?: string) => string;
320
+ }
321
+ /**
322
+ * Validation result
323
+ */
324
+ export interface ValidationResult {
325
+ /**
326
+ * Whether the document is valid
327
+ */
328
+ valid: boolean;
329
+ /**
330
+ * Validation errors
331
+ */
332
+ errors?: ValidationErrorDetail[];
333
+ /**
334
+ * Validation warnings
335
+ */
336
+ warnings?: ValidationWarning[];
337
+ }
338
+ /**
339
+ * Validation error detail
340
+ */
341
+ export interface ValidationErrorDetail {
342
+ /**
343
+ * Error message
344
+ */
345
+ message: string;
346
+ /**
347
+ * Error path (JSON pointer)
348
+ */
349
+ path?: string;
350
+ /**
351
+ * Error code
352
+ */
353
+ code?: string;
354
+ }
355
+ /**
356
+ * Validation warning
357
+ */
358
+ export interface ValidationWarning {
359
+ /**
360
+ * Warning message
361
+ */
362
+ message: string;
363
+ /**
364
+ * Warning path (JSON pointer)
365
+ */
366
+ path?: string;
367
+ /**
368
+ * Warning code
369
+ */
370
+ code?: string;
371
+ }