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