mcp-from-openapi 0.0.1 → 1.0.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/CHANGELOG.md +45 -0
- package/package.json +5 -2
- package/src/errors.js +4 -1
- package/src/errors.js.map +1 -1
- package/src/generator.js +34 -13
- package/src/generator.js.map +1 -1
- package/src/index.d.ts +3 -1
- package/src/index.js +4 -1
- package/src/index.js.map +1 -1
- package/src/parameter-resolver.d.ts +6 -2
- package/src/parameter-resolver.js +87 -2
- package/src/parameter-resolver.js.map +1 -1
- package/src/schema-builder.d.ts +0 -8
- package/src/schema-builder.js +8 -13
- package/src/schema-builder.js.map +1 -1
- package/src/security-resolver.d.ts +289 -0
- package/src/security-resolver.js +324 -0
- package/src/security-resolver.js.map +1 -0
- package/src/types.d.ts +64 -1
- package/src/types.js +72 -11
- package/src/types.js.map +1 -1
- package/src/validator.js +3 -1
- package/src/validator.js.map +1 -1
package/src/schema-builder.js
CHANGED
|
@@ -72,7 +72,7 @@ class SchemaBuilder {
|
|
|
72
72
|
delete obj.$ref;
|
|
73
73
|
}
|
|
74
74
|
for (const key in obj) {
|
|
75
|
-
if (obj
|
|
75
|
+
if (key in obj) {
|
|
76
76
|
const value = obj[key];
|
|
77
77
|
if (value && typeof value === 'object') {
|
|
78
78
|
this.removeRefsRecursive(value);
|
|
@@ -89,18 +89,6 @@ class SchemaBuilder {
|
|
|
89
89
|
description,
|
|
90
90
|
};
|
|
91
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
92
|
/**
|
|
105
93
|
* Add example to schema
|
|
106
94
|
*/
|
|
@@ -268,6 +256,13 @@ class SchemaBuilder {
|
|
|
268
256
|
});
|
|
269
257
|
cloned.anyOf = flattened;
|
|
270
258
|
}
|
|
259
|
+
if (cloned.allOf) {
|
|
260
|
+
const flattened = cloned.allOf.flatMap((s) => {
|
|
261
|
+
const sub = this.flatten(s, maxDepth - 1);
|
|
262
|
+
return sub.allOf ? sub.allOf : [sub];
|
|
263
|
+
});
|
|
264
|
+
cloned.allOf = flattened;
|
|
265
|
+
}
|
|
271
266
|
return cloned;
|
|
272
267
|
}
|
|
273
268
|
/**
|
|
@@ -1 +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,UAAU;iBACrB,CAAC;YACJ,CAAC;YAED,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;gBACpB,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK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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"]}
|
|
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,UAAU;iBACrB,CAAC;YACJ,CAAC;YAED,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;gBACpB,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE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C,EAAE,CAAC;YACrE,OAAO,MAAM,CAAC,UAAU,CAAC;QAC3B,CAAC;QAED,IAAI,KAAK,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;AApWD,sCAoWC","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 (key in obj) {\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 * 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 if (cloned.allOf) {\n const flattened = cloned.allOf.flatMap((s) => {\n const sub = this.flatten(s as JSONSchema7, maxDepth - 1);\n return sub.allOf ? sub.allOf : [sub];\n });\n cloned.allOf = 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"]}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import type { ParameterMapper, SecurityParameterInfo } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Context for resolving security parameters
|
|
4
|
+
* Frameworks should implement this interface based on their auth system
|
|
5
|
+
*/
|
|
6
|
+
export interface SecurityContext {
|
|
7
|
+
/**
|
|
8
|
+
* JWT/Bearer token
|
|
9
|
+
*/
|
|
10
|
+
jwt?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Basic auth credentials (base64 encoded "username:password")
|
|
13
|
+
*/
|
|
14
|
+
basic?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Digest auth credentials
|
|
17
|
+
* Object with username, password, realm, nonce, etc.
|
|
18
|
+
*/
|
|
19
|
+
digest?: DigestAuthCredentials;
|
|
20
|
+
/**
|
|
21
|
+
* API key (single key for backward compatibility)
|
|
22
|
+
*/
|
|
23
|
+
apiKey?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Multiple API keys by name
|
|
26
|
+
* Use this when API requires different keys for different purposes
|
|
27
|
+
* @example { 'X-API-Key': 'key1', 'X-Client-Id': 'client123' }
|
|
28
|
+
*/
|
|
29
|
+
apiKeys?: Record<string, string>;
|
|
30
|
+
/**
|
|
31
|
+
* OAuth2 access token
|
|
32
|
+
*/
|
|
33
|
+
oauth2Token?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Client certificate for mutual TLS (mTLS)
|
|
36
|
+
*/
|
|
37
|
+
clientCertificate?: ClientCertificate;
|
|
38
|
+
/**
|
|
39
|
+
* Private key for signature-based auth (HMAC, AWS Signature V4, etc.)
|
|
40
|
+
*/
|
|
41
|
+
privateKey?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Public key (if needed for verification)
|
|
44
|
+
*/
|
|
45
|
+
publicKey?: string;
|
|
46
|
+
/**
|
|
47
|
+
* HMAC secret for HMAC-based authentication
|
|
48
|
+
*/
|
|
49
|
+
hmacSecret?: string;
|
|
50
|
+
/**
|
|
51
|
+
* AWS credentials for AWS Signature V4
|
|
52
|
+
*/
|
|
53
|
+
awsCredentials?: AWSCredentials;
|
|
54
|
+
/**
|
|
55
|
+
* Custom headers for proprietary authentication
|
|
56
|
+
* @example { 'X-Custom-Auth': 'custom-value' }
|
|
57
|
+
*/
|
|
58
|
+
customHeaders?: Record<string, string>;
|
|
59
|
+
/**
|
|
60
|
+
* Session cookies
|
|
61
|
+
*/
|
|
62
|
+
cookies?: Record<string, string>;
|
|
63
|
+
/**
|
|
64
|
+
* Custom resolver for framework-specific auth
|
|
65
|
+
* @param security - Security parameter info from mapper
|
|
66
|
+
* @returns The auth value to use, or undefined if not available
|
|
67
|
+
*/
|
|
68
|
+
customResolver?: (security: SecurityParameterInfo) => string | Promise<string | undefined>;
|
|
69
|
+
/**
|
|
70
|
+
* Signature generator for signature-based auth
|
|
71
|
+
* @param data - Data to sign (typically the request)
|
|
72
|
+
* @param security - Security parameter info
|
|
73
|
+
* @returns The signature or signed value
|
|
74
|
+
*/
|
|
75
|
+
signatureGenerator?: (data: SignatureData, security: SecurityParameterInfo) => string | Promise<string>;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Digest authentication credentials
|
|
79
|
+
*/
|
|
80
|
+
export interface DigestAuthCredentials {
|
|
81
|
+
username: string;
|
|
82
|
+
password: string;
|
|
83
|
+
realm?: string;
|
|
84
|
+
nonce?: string;
|
|
85
|
+
uri?: string;
|
|
86
|
+
qop?: string;
|
|
87
|
+
nc?: string;
|
|
88
|
+
cnonce?: string;
|
|
89
|
+
response?: string;
|
|
90
|
+
opaque?: string;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Client certificate for mTLS
|
|
94
|
+
*/
|
|
95
|
+
export interface ClientCertificate {
|
|
96
|
+
/**
|
|
97
|
+
* Certificate in PEM format
|
|
98
|
+
*/
|
|
99
|
+
cert: string;
|
|
100
|
+
/**
|
|
101
|
+
* Private key in PEM format
|
|
102
|
+
*/
|
|
103
|
+
key: string;
|
|
104
|
+
/**
|
|
105
|
+
* Passphrase for encrypted key
|
|
106
|
+
*/
|
|
107
|
+
passphrase?: string;
|
|
108
|
+
/**
|
|
109
|
+
* CA certificates
|
|
110
|
+
*/
|
|
111
|
+
ca?: string | string[];
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* AWS credentials for Signature V4
|
|
115
|
+
*/
|
|
116
|
+
export interface AWSCredentials {
|
|
117
|
+
accessKeyId: string;
|
|
118
|
+
secretAccessKey: string;
|
|
119
|
+
sessionToken?: string;
|
|
120
|
+
region?: string;
|
|
121
|
+
service?: string;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Data to be signed for signature-based auth
|
|
125
|
+
*/
|
|
126
|
+
export interface SignatureData {
|
|
127
|
+
method: string;
|
|
128
|
+
url: string;
|
|
129
|
+
headers: Record<string, string>;
|
|
130
|
+
body?: string;
|
|
131
|
+
timestamp?: number;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Resolved security parameters ready to be added to HTTP request
|
|
135
|
+
*/
|
|
136
|
+
export interface ResolvedSecurity {
|
|
137
|
+
/**
|
|
138
|
+
* Headers to add to the request
|
|
139
|
+
*/
|
|
140
|
+
headers: Record<string, string>;
|
|
141
|
+
/**
|
|
142
|
+
* Query parameters to add to the request
|
|
143
|
+
*/
|
|
144
|
+
query: Record<string, string>;
|
|
145
|
+
/**
|
|
146
|
+
* Cookie values to add to the request
|
|
147
|
+
*/
|
|
148
|
+
cookies: Record<string, string>;
|
|
149
|
+
/**
|
|
150
|
+
* Client certificate for mTLS (if applicable)
|
|
151
|
+
*/
|
|
152
|
+
clientCertificate?: ClientCertificate;
|
|
153
|
+
/**
|
|
154
|
+
* Whether signature-based auth is required
|
|
155
|
+
* If true, you need to call signRequest() before sending
|
|
156
|
+
*/
|
|
157
|
+
requiresSignature?: boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Signature metadata (if signature-based auth is used)
|
|
160
|
+
*/
|
|
161
|
+
signatureInfo?: {
|
|
162
|
+
scheme: string;
|
|
163
|
+
algorithm?: string;
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Security resolver that maps OpenAPI security requirements to actual auth values
|
|
168
|
+
*
|
|
169
|
+
* This helper handles security parameters from any OpenAPI spec, regardless of
|
|
170
|
+
* custom naming (BearerAuth, JWT, Authorization, etc.). It uses the mapper's
|
|
171
|
+
* security metadata to determine the auth type and format.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* // In FrontMCP
|
|
176
|
+
* const resolver = new SecurityResolver();
|
|
177
|
+
* const resolved = resolver.resolve(tool.mapper, {
|
|
178
|
+
* jwt: context.authInfo.jwt,
|
|
179
|
+
* apiKey: process.env.API_KEY
|
|
180
|
+
* });
|
|
181
|
+
*
|
|
182
|
+
* // Use resolved.headers in HTTP request
|
|
183
|
+
* fetch(url, { headers: { ...resolved.headers, ...otherHeaders } });
|
|
184
|
+
* ```
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* // Custom resolver for framework-specific auth
|
|
189
|
+
* const resolved = resolver.resolve(tool.mapper, {
|
|
190
|
+
* customResolver: (security) => {
|
|
191
|
+
* if (security.type === 'http' && security.httpScheme === 'bearer') {
|
|
192
|
+
* return myFramework.getAuthToken();
|
|
193
|
+
* }
|
|
194
|
+
* return undefined;
|
|
195
|
+
* }
|
|
196
|
+
* });
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
export declare class SecurityResolver {
|
|
200
|
+
/**
|
|
201
|
+
* Resolve security parameters from mapper entries
|
|
202
|
+
*
|
|
203
|
+
* @param mappers - Parameter mappers from the tool definition
|
|
204
|
+
* @param context - Security context with auth values or custom resolver
|
|
205
|
+
* @returns Resolved headers, query params, and cookies with auth applied
|
|
206
|
+
*/
|
|
207
|
+
resolve(mappers: ParameterMapper[], context: SecurityContext): Promise<ResolvedSecurity>;
|
|
208
|
+
/**
|
|
209
|
+
* Check if security scheme requires request signing
|
|
210
|
+
*/
|
|
211
|
+
private isSignatureBasedAuth;
|
|
212
|
+
/**
|
|
213
|
+
* Resolve the actual auth value based on security type
|
|
214
|
+
*/
|
|
215
|
+
private resolveAuthValue;
|
|
216
|
+
/**
|
|
217
|
+
* Resolve HTTP authentication (bearer, basic, digest, etc.)
|
|
218
|
+
*/
|
|
219
|
+
private resolveHttpAuth;
|
|
220
|
+
/**
|
|
221
|
+
* Resolve Bearer token authentication
|
|
222
|
+
*/
|
|
223
|
+
private resolveBearerAuth;
|
|
224
|
+
/**
|
|
225
|
+
* Resolve Basic authentication
|
|
226
|
+
*/
|
|
227
|
+
private resolveBasicAuth;
|
|
228
|
+
/**
|
|
229
|
+
* Resolve Digest authentication
|
|
230
|
+
*/
|
|
231
|
+
private resolveDigestAuth;
|
|
232
|
+
/**
|
|
233
|
+
* Resolve custom HTTP authentication schemes
|
|
234
|
+
*/
|
|
235
|
+
private resolveCustomHttpScheme;
|
|
236
|
+
/**
|
|
237
|
+
* Resolve API key authentication
|
|
238
|
+
*/
|
|
239
|
+
private resolveApiKey;
|
|
240
|
+
/**
|
|
241
|
+
* Resolve OAuth2/OpenID Connect authentication
|
|
242
|
+
*/
|
|
243
|
+
private resolveOAuth2;
|
|
244
|
+
/**
|
|
245
|
+
* Check if any security requirements are missing from context
|
|
246
|
+
*
|
|
247
|
+
* @param mappers - Parameter mappers from the tool definition
|
|
248
|
+
* @param context - Security context with auth values
|
|
249
|
+
* @returns Array of missing security scheme names
|
|
250
|
+
*/
|
|
251
|
+
checkMissingSecurity(mappers: ParameterMapper[], context: SecurityContext): Promise<string[]>;
|
|
252
|
+
/**
|
|
253
|
+
* Sign a request for signature-based authentication
|
|
254
|
+
*
|
|
255
|
+
* Use this when resolved.requiresSignature is true.
|
|
256
|
+
* This method will call the signatureGenerator from context to sign the request.
|
|
257
|
+
*
|
|
258
|
+
* @param mappers - Parameter mappers from the tool definition
|
|
259
|
+
* @param signatureData - Request data to sign
|
|
260
|
+
* @param context - Security context with signature generator
|
|
261
|
+
* @returns Headers with signature added
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* const resolved = resolver.resolve(tool.mapper, context);
|
|
266
|
+
* if (resolved.requiresSignature) {
|
|
267
|
+
* const signedHeaders = await resolver.signRequest(
|
|
268
|
+
* tool.mapper,
|
|
269
|
+
* { method: 'GET', url: 'https://api.example.com/data', headers: resolved.headers },
|
|
270
|
+
* context
|
|
271
|
+
* );
|
|
272
|
+
* // Use signedHeaders in request
|
|
273
|
+
* }
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
signRequest(mappers: ParameterMapper[], signatureData: SignatureData, context: SecurityContext): Promise<Record<string, string>>;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Create a basic security context from common auth sources
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* const context = createSecurityContext({
|
|
284
|
+
* jwt: process.env.JWT_TOKEN,
|
|
285
|
+
* apiKey: process.env.API_KEY
|
|
286
|
+
* });
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
export declare function createSecurityContext(auth: Partial<SecurityContext>): SecurityContext;
|