@tsed/platform-mcp 8.26.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/lib/esm/constants/constants.js +11 -0
- package/lib/esm/decorators/prompt.js +29 -0
- package/lib/esm/decorators/resource.js +33 -0
- package/lib/esm/decorators/tool.js +32 -0
- package/lib/esm/fn/definePrompt.js +52 -0
- package/lib/esm/fn/defineResource.js +31 -0
- package/lib/esm/fn/defineTool.js +85 -0
- package/lib/esm/index.js +40 -0
- package/lib/esm/interfaces/PlatformMcpSettings.js +1 -0
- package/lib/esm/services/McpServerFactory.js +46 -0
- package/lib/esm/services/PlatformMcpModule.js +54 -0
- package/lib/esm/utils/json-schema-to-zod/Types.js +1 -0
- package/lib/esm/utils/json-schema-to-zod/cli.js +83 -0
- package/lib/esm/utils/json-schema-to-zod/index.js +25 -0
- package/lib/esm/utils/json-schema-to-zod/jsonSchemaToZod.js +52 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseAllOf.js +43 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseAnyOf.js +17 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseArray.js +22 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseBoolean.js +9 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseConst.js +9 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseDefault.js +9 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseEnum.js +21 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseIfThenElse.js +26 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseMultipleType.js +10 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNot.js +13 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNull.js +9 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNullable.js +11 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNumber.js +52 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseObject.js +177 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseOneOf.js +40 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseSchema.js +153 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseString.js +63 -0
- package/lib/esm/utils/json-schema-to-zod/utils/cliTools.js +126 -0
- package/lib/esm/utils/json-schema-to-zod/utils/half.js +9 -0
- package/lib/esm/utils/json-schema-to-zod/utils/jsdocs.js +24 -0
- package/lib/esm/utils/json-schema-to-zod/utils/omit.js +12 -0
- package/lib/esm/utils/json-schema-to-zod/utils/withMessage.js +28 -0
- package/lib/esm/utils/toZod.js +17 -0
- package/lib/tsconfig.esm.tsbuildinfo +1 -0
- package/lib/types/constants/constants.d.ts +11 -0
- package/lib/types/decorators/prompt.d.ts +26 -0
- package/lib/types/decorators/resource.d.ts +28 -0
- package/lib/types/decorators/tool.d.ts +22 -0
- package/lib/types/fn/definePrompt.d.ts +78 -0
- package/lib/types/fn/defineResource.d.ts +41 -0
- package/lib/types/fn/defineTool.d.ts +284 -0
- package/lib/types/index.d.ts +40 -0
- package/lib/types/interfaces/PlatformMcpSettings.d.ts +43 -0
- package/lib/types/services/McpServerFactory.d.ts +15 -0
- package/lib/types/services/PlatformMcpModule.d.ts +22 -0
- package/lib/types/utils/json-schema-to-zod/Types.d.ts +116 -0
- package/lib/types/utils/json-schema-to-zod/cli.d.ts +2 -0
- package/lib/types/utils/json-schema-to-zod/index.d.ts +25 -0
- package/lib/types/utils/json-schema-to-zod/jsonSchemaToZod.d.ts +11 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseAllOf.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseAnyOf.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseArray.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseBoolean.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseConst.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseDefault.d.ts +8 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseEnum.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseIfThenElse.d.ts +12 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseMultipleType.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseNot.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseNull.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseNullable.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseNumber.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseObject.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseOneOf.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseSchema.d.ts +61 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseString.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/utils/cliTools.d.ts +67 -0
- package/lib/types/utils/json-schema-to-zod/utils/half.d.ts +7 -0
- package/lib/types/utils/json-schema-to-zod/utils/jsdocs.d.ts +15 -0
- package/lib/types/utils/json-schema-to-zod/utils/omit.d.ts +7 -0
- package/lib/types/utils/json-schema-to-zod/utils/withMessage.d.ts +19 -0
- package/lib/types/utils/toZod.d.ts +10 -0
- package/package.json +69 -0
- package/readme.md +73 -0
- package/src/constants/constants.ts +11 -0
- package/src/decorators/prompt.spec.ts +30 -0
- package/src/decorators/prompt.ts +39 -0
- package/src/decorators/resource.spec.ts +33 -0
- package/src/decorators/resource.ts +44 -0
- package/src/decorators/tool.spec.ts +68 -0
- package/src/decorators/tool.ts +34 -0
- package/src/fn/definePrompt.ts +93 -0
- package/src/fn/defineResource.ts +81 -0
- package/src/fn/defineTool.spec.ts +29 -0
- package/src/fn/defineTool.ts +141 -0
- package/src/index.ts +40 -0
- package/src/interfaces/PlatformMcpSettings.ts +45 -0
- package/src/services/McpServerFactory.ts +69 -0
- package/src/services/PlatformMcpModule.ts +67 -0
- package/src/utils/json-schema-to-zod/Types.ts +123 -0
- package/src/utils/json-schema-to-zod/cli.ts +88 -0
- package/src/utils/json-schema-to-zod/index.ts +26 -0
- package/src/utils/json-schema-to-zod/jsonSchemaToZod.ts +60 -0
- package/src/utils/json-schema-to-zod/parsers/parseAllOf.ts +50 -0
- package/src/utils/json-schema-to-zod/parsers/parseAnyOf.ts +19 -0
- package/src/utils/json-schema-to-zod/parsers/parseArray.ts +28 -0
- package/src/utils/json-schema-to-zod/parsers/parseBoolean.ts +11 -0
- package/src/utils/json-schema-to-zod/parsers/parseConst.ts +11 -0
- package/src/utils/json-schema-to-zod/parsers/parseDefault.ts +11 -0
- package/src/utils/json-schema-to-zod/parsers/parseEnum.ts +20 -0
- package/src/utils/json-schema-to-zod/parsers/parseIfThenElse.ts +35 -0
- package/src/utils/json-schema-to-zod/parsers/parseMultipleType.ts +12 -0
- package/src/utils/json-schema-to-zod/parsers/parseNot.ts +15 -0
- package/src/utils/json-schema-to-zod/parsers/parseNull.ts +11 -0
- package/src/utils/json-schema-to-zod/parsers/parseNullable.ts +13 -0
- package/src/utils/json-schema-to-zod/parsers/parseNumber.ts +56 -0
- package/src/utils/json-schema-to-zod/parsers/parseObject.ts +220 -0
- package/src/utils/json-schema-to-zod/parsers/parseOneOf.ts +44 -0
- package/src/utils/json-schema-to-zod/parsers/parseSchema.ts +190 -0
- package/src/utils/json-schema-to-zod/parsers/parseString.ts +73 -0
- package/src/utils/json-schema-to-zod/utils/cliTools.ts +189 -0
- package/src/utils/json-schema-to-zod/utils/half.ts +9 -0
- package/src/utils/json-schema-to-zod/utils/jsdocs.ts +29 -0
- package/src/utils/json-schema-to-zod/utils/omit.ts +14 -0
- package/src/utils/json-schema-to-zod/utils/withMessage.ts +42 -0
- package/src/utils/toZod.spec.ts +15 -0
- package/src/utils/toZod.ts +20 -0
- package/tsconfig.esm.json +14 -0
- package/vitest.config.mts +36 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {JsonSchema, JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
+
import {parseSchema} from "./parseSchema.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parses conditional schemas by generating union refinements that evaluate `if/then/else` logic at runtime.
|
|
6
|
+
*
|
|
7
|
+
* @module platform/mcp
|
|
8
|
+
* @since 8.17.0
|
|
9
|
+
*/
|
|
10
|
+
export const parseIfThenElse = (
|
|
11
|
+
schema: JsonSchemaObject & {
|
|
12
|
+
if: JsonSchema;
|
|
13
|
+
then: JsonSchema;
|
|
14
|
+
else: JsonSchema;
|
|
15
|
+
},
|
|
16
|
+
refs: Refs
|
|
17
|
+
): string => {
|
|
18
|
+
const $if = parseSchema(schema.if, {...refs, path: [...refs.path, "if"]});
|
|
19
|
+
const $then = parseSchema(schema.then, {
|
|
20
|
+
...refs,
|
|
21
|
+
path: [...refs.path, "then"]
|
|
22
|
+
});
|
|
23
|
+
const $else = parseSchema(schema.else, {
|
|
24
|
+
...refs,
|
|
25
|
+
path: [...refs.path, "else"]
|
|
26
|
+
});
|
|
27
|
+
return `z.union([${$then}, ${$else}]).superRefine((value,ctx) => {
|
|
28
|
+
const result = ${$if}.safeParse(value).success
|
|
29
|
+
? ${$then}.safeParse(value)
|
|
30
|
+
: ${$else}.safeParse(value);
|
|
31
|
+
if (!result.success) {
|
|
32
|
+
result.error.errors.forEach((error) => ctx.addIssue(error))
|
|
33
|
+
}
|
|
34
|
+
})`;
|
|
35
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
+
import {parseSchema} from "./parseSchema.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parses schemas whose `type` keyword is an array, creating a union of each primitive variant.
|
|
6
|
+
*
|
|
7
|
+
* @module platform/mcp
|
|
8
|
+
* @since 8.17.0
|
|
9
|
+
*/
|
|
10
|
+
export const parseMultipleType = (schema: JsonSchemaObject & {type: string[]}, refs: Refs) => {
|
|
11
|
+
return `z.union([${schema.type.map((type) => parseSchema({...schema, type} as any, {...refs, withoutDefaults: true})).join(", ")}])`;
|
|
12
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {JsonSchema, JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
+
import {parseSchema} from "./parseSchema.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parses the `not` keyword into a refinement that fails when the nested schema succeeds.
|
|
6
|
+
*
|
|
7
|
+
* @module platform/mcp
|
|
8
|
+
* @since 8.17.0
|
|
9
|
+
*/
|
|
10
|
+
export const parseNot = (schema: JsonSchemaObject & {not: JsonSchema}, refs: Refs) => {
|
|
11
|
+
return `z.any().refine((value) => !${parseSchema(schema.not, {
|
|
12
|
+
...refs,
|
|
13
|
+
path: [...refs.path, "not"]
|
|
14
|
+
})}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`;
|
|
15
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
+
import {omit} from "../utils/omit.js";
|
|
3
|
+
import {parseSchema} from "./parseSchema.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handles OpenAPI-style `nullable` keywords by delegating to the underlying schema and marking it nullable.
|
|
7
|
+
*
|
|
8
|
+
* @module platform/mcp
|
|
9
|
+
* @since 8.17.0
|
|
10
|
+
*/
|
|
11
|
+
export const parseNullable = (schema: JsonSchemaObject & {nullable: true}, refs: Refs) => {
|
|
12
|
+
return `${parseSchema(omit(schema, "nullable"), refs, true)}.nullable()`;
|
|
13
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {JsonSchemaObject} from "../Types.js";
|
|
2
|
+
import {withMessage} from "../utils/withMessage.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parses numeric schemas, emitting integer formats and range constraints as chained Zod modifiers.
|
|
6
|
+
*
|
|
7
|
+
* @module platform/mcp
|
|
8
|
+
* @since 8.17.0
|
|
9
|
+
*/
|
|
10
|
+
export const parseNumber = (schema: JsonSchemaObject & {type: "number" | "integer"}) => {
|
|
11
|
+
let r = "z.number()";
|
|
12
|
+
|
|
13
|
+
if (schema.type === "integer") {
|
|
14
|
+
r += withMessage(schema, "type", () => [".int(", ")"]);
|
|
15
|
+
} else {
|
|
16
|
+
r += withMessage(schema, "format", ({value}) => {
|
|
17
|
+
if (value === "int64") {
|
|
18
|
+
return [".int(", ")"];
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
r += withMessage(schema, "multipleOf", ({value, json}) => {
|
|
24
|
+
if (value === 1) {
|
|
25
|
+
if (r.startsWith("z.number().int(")) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return [".int(", ")"];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return [`.multipleOf(${json}`, ", ", ")"];
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (typeof schema.minimum === "number") {
|
|
36
|
+
if (schema.exclusiveMinimum === true) {
|
|
37
|
+
r += withMessage(schema, "minimum", ({json}) => [`.gt(${json}`, ", ", ")"]);
|
|
38
|
+
} else {
|
|
39
|
+
r += withMessage(schema, "minimum", ({json}) => [`.gte(${json}`, ", ", ")"]);
|
|
40
|
+
}
|
|
41
|
+
} else if (typeof schema.exclusiveMinimum === "number") {
|
|
42
|
+
r += withMessage(schema, "exclusiveMinimum", ({json}) => [`.gt(${json}`, ", ", ")"]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (typeof schema.maximum === "number") {
|
|
46
|
+
if (schema.exclusiveMaximum === true) {
|
|
47
|
+
r += withMessage(schema, "maximum", ({json}) => [`.lt(${json}`, ", ", ")"]);
|
|
48
|
+
} else {
|
|
49
|
+
r += withMessage(schema, "maximum", ({json}) => [`.lte(${json}`, ", ", ")"]);
|
|
50
|
+
}
|
|
51
|
+
} else if (typeof schema.exclusiveMaximum === "number") {
|
|
52
|
+
r += withMessage(schema, "exclusiveMaximum", ({json}) => [`.lt(${json}`, ", ", ")"]);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return r;
|
|
56
|
+
};
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import {JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
+
import {addJsdocs} from "../utils/jsdocs.js";
|
|
3
|
+
import {parseAllOf} from "./parseAllOf.js";
|
|
4
|
+
import {parseAnyOf} from "./parseAnyOf.js";
|
|
5
|
+
import {parseOneOf} from "./parseOneOf.js";
|
|
6
|
+
import {its, parseSchema} from "./parseSchema.js";
|
|
7
|
+
|
|
8
|
+
// Helper for z.record() generation - Zod v4 requires explicit key type
|
|
9
|
+
function emitRecord(valueSchema: string, refs: Refs): string {
|
|
10
|
+
if (refs.zodVersion === 3) {
|
|
11
|
+
return `z.record(${valueSchema})`;
|
|
12
|
+
}
|
|
13
|
+
// Default to v4 syntax
|
|
14
|
+
return `z.record(z.string(), ${valueSchema})`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Helper for error path in superRefine - Zod v4 uses simplified path
|
|
18
|
+
function emitErrorPath(refs: Refs): string {
|
|
19
|
+
if (refs.zodVersion === 3) {
|
|
20
|
+
return `path: [...ctx.path, key]`;
|
|
21
|
+
}
|
|
22
|
+
// Default to v4 syntax
|
|
23
|
+
return `path: [key]`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Parses object schemas, wiring properties, patternProperties, and catchall behavior into the resulting Zod object.
|
|
28
|
+
*
|
|
29
|
+
* @module platform/mcp
|
|
30
|
+
* @since 8.17.0
|
|
31
|
+
*/
|
|
32
|
+
export function parseObject(objectSchema: JsonSchemaObject & {type: "object"}, refs: Refs): string {
|
|
33
|
+
let properties: string | undefined = undefined;
|
|
34
|
+
|
|
35
|
+
if (objectSchema.properties) {
|
|
36
|
+
if (!Object.keys(objectSchema.properties).length) {
|
|
37
|
+
properties = "z.object({})";
|
|
38
|
+
} else {
|
|
39
|
+
properties = "z.object({ ";
|
|
40
|
+
|
|
41
|
+
properties += Object.keys(objectSchema.properties)
|
|
42
|
+
.map((key) => {
|
|
43
|
+
const propSchema = objectSchema.properties![key];
|
|
44
|
+
|
|
45
|
+
let result = `${JSON.stringify(key)}: ${parseSchema(propSchema, {
|
|
46
|
+
...refs,
|
|
47
|
+
path: [...refs.path, "properties", key]
|
|
48
|
+
})}`;
|
|
49
|
+
|
|
50
|
+
if (refs.withJsdocs && typeof propSchema === "object") {
|
|
51
|
+
result = addJsdocs(propSchema, result);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const hasDefault = typeof propSchema === "object" && propSchema.default !== undefined;
|
|
55
|
+
|
|
56
|
+
const required = Array.isArray(objectSchema.required)
|
|
57
|
+
? objectSchema.required.includes(key)
|
|
58
|
+
: typeof propSchema === "object" && propSchema.required === true;
|
|
59
|
+
|
|
60
|
+
const optional = !hasDefault && !required;
|
|
61
|
+
|
|
62
|
+
return optional ? `${result}.optional()` : result;
|
|
63
|
+
})
|
|
64
|
+
.join(", ");
|
|
65
|
+
|
|
66
|
+
properties += " })";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const additionalProperties =
|
|
71
|
+
objectSchema.additionalProperties !== undefined
|
|
72
|
+
? parseSchema(objectSchema.additionalProperties, {
|
|
73
|
+
...refs,
|
|
74
|
+
path: [...refs.path, "additionalProperties"]
|
|
75
|
+
})
|
|
76
|
+
: undefined;
|
|
77
|
+
|
|
78
|
+
let patternProperties: string | undefined = undefined;
|
|
79
|
+
|
|
80
|
+
if (objectSchema.patternProperties) {
|
|
81
|
+
const parsedPatternProperties = Object.fromEntries(
|
|
82
|
+
Object.entries(objectSchema.patternProperties).map(([key, value]) => {
|
|
83
|
+
return [
|
|
84
|
+
key,
|
|
85
|
+
parseSchema(value, {
|
|
86
|
+
...refs,
|
|
87
|
+
path: [...refs.path, "patternProperties", key]
|
|
88
|
+
})
|
|
89
|
+
];
|
|
90
|
+
}, {})
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
patternProperties = "";
|
|
94
|
+
|
|
95
|
+
if (properties) {
|
|
96
|
+
if (additionalProperties) {
|
|
97
|
+
patternProperties += `.catchall(z.union([${[...Object.values(parsedPatternProperties), additionalProperties].join(", ")}]))`;
|
|
98
|
+
} else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
99
|
+
patternProperties += `.catchall(z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
|
|
100
|
+
} else {
|
|
101
|
+
patternProperties += `.catchall(${Object.values(parsedPatternProperties)})`;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
if (additionalProperties) {
|
|
105
|
+
patternProperties += emitRecord(`z.union([${[...Object.values(parsedPatternProperties), additionalProperties].join(", ")}])`, refs);
|
|
106
|
+
} else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
107
|
+
patternProperties += emitRecord(`z.union([${Object.values(parsedPatternProperties).join(", ")}])`, refs);
|
|
108
|
+
} else {
|
|
109
|
+
patternProperties += emitRecord(`${Object.values(parsedPatternProperties)}`, refs);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
patternProperties += ".superRefine((value, ctx) => {\n";
|
|
114
|
+
|
|
115
|
+
patternProperties += "for (const key in value) {\n";
|
|
116
|
+
|
|
117
|
+
if (additionalProperties) {
|
|
118
|
+
if (objectSchema.properties) {
|
|
119
|
+
patternProperties += `let evaluated = [${Object.keys(objectSchema.properties)
|
|
120
|
+
.map((key) => JSON.stringify(key))
|
|
121
|
+
.join(", ")}].includes(key)\n`;
|
|
122
|
+
} else {
|
|
123
|
+
patternProperties += `let evaluated = false\n`;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
for (const key in objectSchema.patternProperties) {
|
|
128
|
+
patternProperties += "if (key.match(new RegExp(" + JSON.stringify(key) + "))) {\n";
|
|
129
|
+
if (additionalProperties) {
|
|
130
|
+
patternProperties += "evaluated = true\n";
|
|
131
|
+
}
|
|
132
|
+
patternProperties += "const result = " + parsedPatternProperties[key] + ".safeParse(value[key])\n";
|
|
133
|
+
patternProperties += "if (!result.success) {\n";
|
|
134
|
+
|
|
135
|
+
patternProperties += `ctx.addIssue({
|
|
136
|
+
${emitErrorPath(refs)},
|
|
137
|
+
code: 'custom',
|
|
138
|
+
message: \`Invalid input: Key matching regex /\${key}/ must match schema\`,
|
|
139
|
+
params: {
|
|
140
|
+
issues: result.error.issues
|
|
141
|
+
}
|
|
142
|
+
})\n`;
|
|
143
|
+
|
|
144
|
+
patternProperties += "}\n";
|
|
145
|
+
patternProperties += "}\n";
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (additionalProperties) {
|
|
149
|
+
patternProperties += "if (!evaluated) {\n";
|
|
150
|
+
patternProperties += "const result = " + additionalProperties + ".safeParse(value[key])\n";
|
|
151
|
+
patternProperties += "if (!result.success) {\n";
|
|
152
|
+
|
|
153
|
+
patternProperties += `ctx.addIssue({
|
|
154
|
+
${emitErrorPath(refs)},
|
|
155
|
+
code: 'custom',
|
|
156
|
+
message: \`Invalid input: must match catchall schema\`,
|
|
157
|
+
params: {
|
|
158
|
+
issues: result.error.issues
|
|
159
|
+
}
|
|
160
|
+
})\n`;
|
|
161
|
+
|
|
162
|
+
patternProperties += "}\n";
|
|
163
|
+
patternProperties += "}\n";
|
|
164
|
+
}
|
|
165
|
+
patternProperties += "}\n";
|
|
166
|
+
patternProperties += "})";
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
let output = properties
|
|
170
|
+
? patternProperties
|
|
171
|
+
? properties + patternProperties
|
|
172
|
+
: additionalProperties
|
|
173
|
+
? additionalProperties === "z.never()"
|
|
174
|
+
? properties + ".strict()"
|
|
175
|
+
: properties + `.catchall(${additionalProperties})`
|
|
176
|
+
: properties
|
|
177
|
+
: patternProperties
|
|
178
|
+
? patternProperties
|
|
179
|
+
: additionalProperties
|
|
180
|
+
? emitRecord(additionalProperties, refs)
|
|
181
|
+
: emitRecord("z.any()", refs);
|
|
182
|
+
|
|
183
|
+
if (its.an.anyOf(objectSchema)) {
|
|
184
|
+
output += `.and(${parseAnyOf(
|
|
185
|
+
{
|
|
186
|
+
...objectSchema,
|
|
187
|
+
anyOf: objectSchema.anyOf.map((x) =>
|
|
188
|
+
typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? {...x, type: "object"} : x
|
|
189
|
+
) as any
|
|
190
|
+
},
|
|
191
|
+
refs
|
|
192
|
+
)})`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (its.a.oneOf(objectSchema)) {
|
|
196
|
+
output += `.and(${parseOneOf(
|
|
197
|
+
{
|
|
198
|
+
...objectSchema,
|
|
199
|
+
oneOf: objectSchema.oneOf.map((x) =>
|
|
200
|
+
typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? {...x, type: "object"} : x
|
|
201
|
+
) as any
|
|
202
|
+
},
|
|
203
|
+
refs
|
|
204
|
+
)})`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (its.an.allOf(objectSchema)) {
|
|
208
|
+
output += `.and(${parseAllOf(
|
|
209
|
+
{
|
|
210
|
+
...objectSchema,
|
|
211
|
+
allOf: objectSchema.allOf.map((x) =>
|
|
212
|
+
typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? {...x, type: "object"} : x
|
|
213
|
+
) as any
|
|
214
|
+
},
|
|
215
|
+
refs
|
|
216
|
+
)})`;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return output;
|
|
220
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {JsonSchema, JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
+
import {parseSchema} from "./parseSchema.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parses `oneOf` schemas by generating super refinements that ensure exactly one child schema matches.
|
|
6
|
+
*
|
|
7
|
+
* @module platform/mcp
|
|
8
|
+
* @since 8.17.0
|
|
9
|
+
*/
|
|
10
|
+
export const parseOneOf = (schema: JsonSchemaObject & {oneOf: JsonSchema[]}, refs: Refs) => {
|
|
11
|
+
return schema.oneOf.length
|
|
12
|
+
? schema.oneOf.length === 1
|
|
13
|
+
? parseSchema(schema.oneOf[0], {
|
|
14
|
+
...refs,
|
|
15
|
+
path: [...refs.path, "oneOf", 0]
|
|
16
|
+
})
|
|
17
|
+
: `z.any().superRefine((x, ctx) => {
|
|
18
|
+
const schemas = [${schema.oneOf
|
|
19
|
+
.map((schema, i) =>
|
|
20
|
+
parseSchema(schema, {
|
|
21
|
+
...refs,
|
|
22
|
+
path: [...refs.path, "oneOf", i]
|
|
23
|
+
})
|
|
24
|
+
)
|
|
25
|
+
.join(", ")}];
|
|
26
|
+
const errors = schemas.reduce<z.ZodError[]>(
|
|
27
|
+
(errors, schema) =>
|
|
28
|
+
((result) =>
|
|
29
|
+
result.error ? [...errors, result.error] : errors)(
|
|
30
|
+
schema.safeParse(x),
|
|
31
|
+
),
|
|
32
|
+
[],
|
|
33
|
+
);
|
|
34
|
+
if (schemas.length - errors.length !== 1) {
|
|
35
|
+
ctx.addIssue({
|
|
36
|
+
path: ctx.path,
|
|
37
|
+
code: "invalid_union",
|
|
38
|
+
unionErrors: errors,
|
|
39
|
+
message: "Invalid input: Should pass single schema",
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
})`
|
|
43
|
+
: "z.any()";
|
|
44
|
+
};
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import {JsonSchema, JsonSchemaObject, ParserSelector, Refs, Serializable} from "../Types.js";
|
|
2
|
+
import {parseAllOf} from "./parseAllOf.js";
|
|
3
|
+
import {parseAnyOf} from "./parseAnyOf.js";
|
|
4
|
+
import {parseArray} from "./parseArray.js";
|
|
5
|
+
import {parseBoolean} from "./parseBoolean.js";
|
|
6
|
+
import {parseConst} from "./parseConst.js";
|
|
7
|
+
import {parseDefault} from "./parseDefault.js";
|
|
8
|
+
import {parseEnum} from "./parseEnum.js";
|
|
9
|
+
import {parseIfThenElse} from "./parseIfThenElse.js";
|
|
10
|
+
import {parseMultipleType} from "./parseMultipleType.js";
|
|
11
|
+
import {parseNot} from "./parseNot.js";
|
|
12
|
+
import {parseNull} from "./parseNull.js";
|
|
13
|
+
import {parseNullable} from "./parseNullable.js";
|
|
14
|
+
import {parseNumber} from "./parseNumber.js";
|
|
15
|
+
import {parseObject} from "./parseObject.js";
|
|
16
|
+
import {parseOneOf} from "./parseOneOf.js";
|
|
17
|
+
import {parseString} from "./parseString.js";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Recursively parses a JSON Schema node into a Zod expression string.
|
|
21
|
+
*
|
|
22
|
+
* @param schema JSON Schema node to inspect.
|
|
23
|
+
* @param refs Shared parsing context and options.
|
|
24
|
+
* @param blockMeta When true, skips describe/default annotations for nested invocations.
|
|
25
|
+
* @module platform/mcp
|
|
26
|
+
* @since 8.17.0
|
|
27
|
+
*/
|
|
28
|
+
export const parseSchema = (schema: JsonSchema, refs: Refs = {seen: new Map(), path: []}, blockMeta?: boolean): string => {
|
|
29
|
+
if (typeof schema !== "object") return schema ? "z.any()" : "z.never()";
|
|
30
|
+
|
|
31
|
+
if (refs.parserOverride) {
|
|
32
|
+
const custom = refs.parserOverride(schema, refs);
|
|
33
|
+
|
|
34
|
+
if (typeof custom === "string") {
|
|
35
|
+
return custom;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let seen = refs.seen.get(schema);
|
|
40
|
+
|
|
41
|
+
if (seen) {
|
|
42
|
+
if (seen.r !== undefined) {
|
|
43
|
+
return seen.r;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (refs.depth === undefined || seen.n >= refs.depth) {
|
|
47
|
+
return "z.any()";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
seen.n += 1;
|
|
51
|
+
} else {
|
|
52
|
+
seen = {r: undefined, n: 0};
|
|
53
|
+
refs.seen.set(schema, seen);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let parsed = selectParser(schema, refs);
|
|
57
|
+
if (!blockMeta) {
|
|
58
|
+
if (!refs.withoutDescribes) {
|
|
59
|
+
parsed = addDescribes(schema, parsed);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!refs.withoutDefaults) {
|
|
63
|
+
parsed = addDefaults(schema, parsed);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
parsed = addAnnotations(schema, parsed);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
seen.r = parsed;
|
|
70
|
+
|
|
71
|
+
return parsed;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const addDescribes = (schema: JsonSchemaObject, parsed: string): string => {
|
|
75
|
+
if (schema.description) {
|
|
76
|
+
parsed += `.describe(${JSON.stringify(schema.description)})`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return parsed;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const addDefaults = (schema: JsonSchemaObject, parsed: string): string => {
|
|
83
|
+
if (schema.default !== undefined) {
|
|
84
|
+
parsed += `.default(${JSON.stringify(schema.default)})`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return parsed;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const addAnnotations = (schema: JsonSchemaObject, parsed: string): string => {
|
|
91
|
+
if (schema.readOnly) {
|
|
92
|
+
parsed += ".readonly()";
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return parsed;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const selectParser: ParserSelector = (schema, refs) => {
|
|
99
|
+
if (its.a.nullable(schema)) {
|
|
100
|
+
return parseNullable(schema, refs);
|
|
101
|
+
} else if (its.an.object(schema)) {
|
|
102
|
+
return parseObject(schema, refs);
|
|
103
|
+
} else if (its.an.array(schema)) {
|
|
104
|
+
return parseArray(schema, refs);
|
|
105
|
+
} else if (its.an.anyOf(schema)) {
|
|
106
|
+
return parseAnyOf(schema, refs);
|
|
107
|
+
} else if (its.an.allOf(schema)) {
|
|
108
|
+
return parseAllOf(schema, refs);
|
|
109
|
+
} else if (its.a.oneOf(schema)) {
|
|
110
|
+
return parseOneOf(schema, refs);
|
|
111
|
+
} else if (its.a.not(schema)) {
|
|
112
|
+
return parseNot(schema, refs);
|
|
113
|
+
} else if (its.an.enum(schema)) {
|
|
114
|
+
return parseEnum(schema); //<-- needs to come before primitives
|
|
115
|
+
} else if (its.a.const(schema)) {
|
|
116
|
+
return parseConst(schema);
|
|
117
|
+
} else if (its.a.multipleType(schema)) {
|
|
118
|
+
return parseMultipleType(schema, refs);
|
|
119
|
+
} else if (its.a.primitive(schema, "string")) {
|
|
120
|
+
return parseString(schema);
|
|
121
|
+
} else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) {
|
|
122
|
+
return parseNumber(schema);
|
|
123
|
+
} else if (its.a.primitive(schema, "boolean")) {
|
|
124
|
+
return parseBoolean(schema);
|
|
125
|
+
} else if (its.a.primitive(schema, "null")) {
|
|
126
|
+
return parseNull(schema);
|
|
127
|
+
} else if (its.a.conditional(schema)) {
|
|
128
|
+
return parseIfThenElse(schema, refs);
|
|
129
|
+
} else {
|
|
130
|
+
return parseDefault(schema);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Type guards used by the schema parser to detect structural patterns.
|
|
136
|
+
*
|
|
137
|
+
* @module platform/mcp
|
|
138
|
+
* @since 8.17.0
|
|
139
|
+
*/
|
|
140
|
+
export const its = {
|
|
141
|
+
an: {
|
|
142
|
+
object: (x: JsonSchemaObject): x is JsonSchemaObject & {type: "object"} => x.type === "object",
|
|
143
|
+
array: (x: JsonSchemaObject): x is JsonSchemaObject & {type: "array"} => x.type === "array",
|
|
144
|
+
anyOf: (
|
|
145
|
+
x: JsonSchemaObject
|
|
146
|
+
): x is JsonSchemaObject & {
|
|
147
|
+
anyOf: JsonSchema[];
|
|
148
|
+
} => x.anyOf !== undefined,
|
|
149
|
+
allOf: (
|
|
150
|
+
x: JsonSchemaObject
|
|
151
|
+
): x is JsonSchemaObject & {
|
|
152
|
+
allOf: JsonSchema[];
|
|
153
|
+
} => x.allOf !== undefined,
|
|
154
|
+
enum: (
|
|
155
|
+
x: JsonSchemaObject
|
|
156
|
+
): x is JsonSchemaObject & {
|
|
157
|
+
enum: Serializable | Serializable[];
|
|
158
|
+
} => x.enum !== undefined
|
|
159
|
+
},
|
|
160
|
+
a: {
|
|
161
|
+
nullable: (x: JsonSchemaObject): x is JsonSchemaObject & {nullable: true} => (x as any).nullable === true,
|
|
162
|
+
multipleType: (x: JsonSchemaObject): x is JsonSchemaObject & {type: string[]} => Array.isArray(x.type),
|
|
163
|
+
not: (
|
|
164
|
+
x: JsonSchemaObject
|
|
165
|
+
): x is JsonSchemaObject & {
|
|
166
|
+
not: JsonSchema;
|
|
167
|
+
} => x.not !== undefined,
|
|
168
|
+
const: (
|
|
169
|
+
x: JsonSchemaObject
|
|
170
|
+
): x is JsonSchemaObject & {
|
|
171
|
+
const: Serializable;
|
|
172
|
+
} => x.const !== undefined,
|
|
173
|
+
primitive: <T extends "string" | "number" | "integer" | "boolean" | "null">(
|
|
174
|
+
x: JsonSchemaObject,
|
|
175
|
+
p: T
|
|
176
|
+
): x is JsonSchemaObject & {type: T} => x.type === p,
|
|
177
|
+
conditional: (
|
|
178
|
+
x: JsonSchemaObject
|
|
179
|
+
): x is JsonSchemaObject & {
|
|
180
|
+
if: JsonSchema;
|
|
181
|
+
then: JsonSchema;
|
|
182
|
+
else: JsonSchema;
|
|
183
|
+
} => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
|
|
184
|
+
oneOf: (
|
|
185
|
+
x: JsonSchemaObject
|
|
186
|
+
): x is JsonSchemaObject & {
|
|
187
|
+
oneOf: JsonSchema[];
|
|
188
|
+
} => x.oneOf !== undefined
|
|
189
|
+
}
|
|
190
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {JsonSchemaObject} from "../Types.js";
|
|
2
|
+
import {withMessage} from "../utils/withMessage.js";
|
|
3
|
+
import {parseSchema} from "./parseSchema.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parses string schemas, supporting format-specific helpers, regex constraints, and content media transformations.
|
|
7
|
+
*
|
|
8
|
+
* @module platform/mcp
|
|
9
|
+
* @since 8.17.0
|
|
10
|
+
*/
|
|
11
|
+
export const parseString = (schema: JsonSchemaObject & {type: "string"}) => {
|
|
12
|
+
let r = "z.string()";
|
|
13
|
+
|
|
14
|
+
r += withMessage(schema, "format", ({value}) => {
|
|
15
|
+
switch (value) {
|
|
16
|
+
case "email":
|
|
17
|
+
return [".email(", ")"];
|
|
18
|
+
case "ip":
|
|
19
|
+
return [".ip(", ")"];
|
|
20
|
+
case "ipv4":
|
|
21
|
+
return ['.ip({ version: "v4"', ", message: ", " })"];
|
|
22
|
+
case "ipv6":
|
|
23
|
+
return ['.ip({ version: "v6"', ", message: ", " })"];
|
|
24
|
+
case "uri":
|
|
25
|
+
return [".url(", ")"];
|
|
26
|
+
case "uuid":
|
|
27
|
+
return [".uuid(", ")"];
|
|
28
|
+
case "date-time":
|
|
29
|
+
return [".datetime({ offset: true", ", message: ", " })"];
|
|
30
|
+
case "time":
|
|
31
|
+
return [".time(", ")"];
|
|
32
|
+
case "date":
|
|
33
|
+
return [".date(", ")"];
|
|
34
|
+
case "binary":
|
|
35
|
+
return [".base64(", ")"];
|
|
36
|
+
case "duration":
|
|
37
|
+
return [".duration(", ")"];
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
r += withMessage(schema, "pattern", ({json}) => [`.regex(new RegExp(${json})`, ", ", ")"]);
|
|
42
|
+
|
|
43
|
+
r += withMessage(schema, "minLength", ({json}) => [`.min(${json}`, ", ", ")"]);
|
|
44
|
+
|
|
45
|
+
r += withMessage(schema, "maxLength", ({json}) => [`.max(${json}`, ", ", ")"]);
|
|
46
|
+
|
|
47
|
+
r += withMessage(schema, "contentEncoding", ({value}) => {
|
|
48
|
+
if (value === "base64") {
|
|
49
|
+
return [".base64(", ")"];
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const contentMediaType = withMessage(schema, "contentMediaType", ({value}) => {
|
|
54
|
+
if (value === "application/json") {
|
|
55
|
+
return [
|
|
56
|
+
'.transform((str, ctx) => { try { return JSON.parse(str); } catch (err) { ctx.addIssue({ code: "custom", message: "Invalid JSON" }); }}',
|
|
57
|
+
", ",
|
|
58
|
+
")"
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
if (contentMediaType != "") {
|
|
64
|
+
r += contentMediaType;
|
|
65
|
+
r += withMessage(schema, "contentSchema", ({value}) => {
|
|
66
|
+
if (value && value instanceof Object) {
|
|
67
|
+
return [`.pipe(${parseSchema(value)}`, ", ", ")"];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return r;
|
|
73
|
+
};
|