@mastra/mcp 0.4.2-alpha.1 → 0.4.2-alpha.4
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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +25 -0
- package/dist/_tsup-dts-rollup.d.cts +1 -0
- package/dist/_tsup-dts-rollup.d.ts +1 -0
- package/dist/index.cjs +1802 -1800
- package/dist/index.js +1803 -1801
- package/package.json +2 -2
- package/src/client.ts +13 -3
package/dist/index.cjs
CHANGED
|
@@ -47,924 +47,1561 @@ var path__namespace = /*#__PURE__*/_interopNamespace(path);
|
|
|
47
47
|
|
|
48
48
|
// src/client.ts
|
|
49
49
|
|
|
50
|
-
// ../../node_modules/.pnpm/zod@
|
|
51
|
-
var
|
|
52
|
-
(
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseAnyOf.js
|
|
51
|
+
var parseAnyOf = (schema, refs) => {
|
|
52
|
+
return schema.anyOf.length ? schema.anyOf.length === 1 ? parseSchema(schema.anyOf[0], {
|
|
53
|
+
...refs,
|
|
54
|
+
path: [...refs.path, "anyOf", 0]
|
|
55
|
+
}) : `z.union([${schema.anyOf.map((schema2, i) => parseSchema(schema2, { ...refs, path: [...refs.path, "anyOf", i] })).join(", ")}])` : `z.any()`;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseBoolean.js
|
|
59
|
+
var parseBoolean = (_schema) => {
|
|
60
|
+
return "z.boolean()";
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseDefault.js
|
|
64
|
+
var parseDefault = (_schema) => {
|
|
65
|
+
return "z.any()";
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseMultipleType.js
|
|
69
|
+
var parseMultipleType = (schema, refs) => {
|
|
70
|
+
return `z.union([${schema.type.map((type) => parseSchema({ ...schema, type }, refs)).join(", ")}])`;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseNot.js
|
|
74
|
+
var parseNot = (schema, refs) => {
|
|
75
|
+
return `z.any().refine((value) => !${parseSchema(schema.not, {
|
|
76
|
+
...refs,
|
|
77
|
+
path: [...refs.path, "not"]
|
|
78
|
+
})}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseNull.js
|
|
82
|
+
var parseNull = (_schema) => {
|
|
83
|
+
return "z.null()";
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/utils/half.js
|
|
87
|
+
var half = (arr) => {
|
|
88
|
+
return [arr.slice(0, arr.length / 2), arr.slice(arr.length / 2)];
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseAllOf.js
|
|
92
|
+
var originalIndex = Symbol("Original index");
|
|
93
|
+
var ensureOriginalIndex = (arr) => {
|
|
94
|
+
let newArr = [];
|
|
95
|
+
for (let i = 0; i < arr.length; i++) {
|
|
96
|
+
const item = arr[i];
|
|
97
|
+
if (typeof item === "boolean") {
|
|
98
|
+
newArr.push(item ? { [originalIndex]: i } : { [originalIndex]: i, not: {} });
|
|
99
|
+
} else if (originalIndex in item) {
|
|
100
|
+
return arr;
|
|
101
|
+
} else {
|
|
102
|
+
newArr.push({ ...item, [originalIndex]: i });
|
|
103
|
+
}
|
|
55
104
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
105
|
+
return newArr;
|
|
106
|
+
};
|
|
107
|
+
function parseAllOf(schema, refs) {
|
|
108
|
+
if (schema.allOf.length === 0) {
|
|
109
|
+
return "z.never()";
|
|
110
|
+
} else if (schema.allOf.length === 1) {
|
|
111
|
+
const item = schema.allOf[0];
|
|
112
|
+
return parseSchema(item, {
|
|
113
|
+
...refs,
|
|
114
|
+
path: [...refs.path, "allOf", item[originalIndex]]
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
const [left, right] = half(ensureOriginalIndex(schema.allOf));
|
|
118
|
+
return `z.intersection(${parseAllOf({ allOf: left }, refs)}, ${parseAllOf({
|
|
119
|
+
allOf: right
|
|
120
|
+
}, refs)})`;
|
|
59
121
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/utils/withMessage.js
|
|
125
|
+
function withMessage(schema, key, get) {
|
|
126
|
+
const value = schema[key];
|
|
127
|
+
let r = "";
|
|
128
|
+
if (value !== void 0) {
|
|
129
|
+
const got = get({ value, json: JSON.stringify(value) });
|
|
130
|
+
if (got) {
|
|
131
|
+
const opener = got[0];
|
|
132
|
+
const prefix = got.length === 3 ? got[1] : "";
|
|
133
|
+
const closer = got.length === 3 ? got[2] : got[1];
|
|
134
|
+
r += opener;
|
|
135
|
+
if (schema.errorMessage?.[key] !== void 0) {
|
|
136
|
+
r += prefix + JSON.stringify(schema.errorMessage[key]);
|
|
137
|
+
}
|
|
138
|
+
r += closer;
|
|
73
139
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
140
|
+
}
|
|
141
|
+
return r;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseArray.js
|
|
145
|
+
var parseArray = (schema, refs) => {
|
|
146
|
+
if (Array.isArray(schema.items)) {
|
|
147
|
+
return `z.tuple([${schema.items.map((v, i) => parseSchema(v, { ...refs, path: [...refs.path, "items", i] }))}])`;
|
|
148
|
+
}
|
|
149
|
+
let r = !schema.items ? "z.array(z.any())" : `z.array(${parseSchema(schema.items, {
|
|
150
|
+
...refs,
|
|
151
|
+
path: [...refs.path, "items"]
|
|
152
|
+
})})`;
|
|
153
|
+
r += withMessage(schema, "minItems", ({ json }) => [
|
|
154
|
+
`.min(${json}`,
|
|
155
|
+
", ",
|
|
156
|
+
")"
|
|
157
|
+
]);
|
|
158
|
+
r += withMessage(schema, "maxItems", ({ json }) => [
|
|
159
|
+
`.max(${json}`,
|
|
160
|
+
", ",
|
|
161
|
+
")"
|
|
162
|
+
]);
|
|
163
|
+
return r;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseConst.js
|
|
167
|
+
var parseConst = (schema) => {
|
|
168
|
+
return `z.literal(${JSON.stringify(schema.const)})`;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseEnum.js
|
|
172
|
+
var parseEnum = (schema) => {
|
|
173
|
+
if (schema.enum.length === 0) {
|
|
174
|
+
return "z.never()";
|
|
175
|
+
} else if (schema.enum.length === 1) {
|
|
176
|
+
return `z.literal(${JSON.stringify(schema.enum[0])})`;
|
|
177
|
+
} else if (schema.enum.every((x) => typeof x === "string")) {
|
|
178
|
+
return `z.enum([${schema.enum.map((x) => JSON.stringify(x))}])`;
|
|
179
|
+
} else {
|
|
180
|
+
return `z.union([${schema.enum.map((x) => `z.literal(${JSON.stringify(x)})`).join(", ")}])`;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseIfThenElse.js
|
|
185
|
+
var parseIfThenElse = (schema, refs) => {
|
|
186
|
+
const $if = parseSchema(schema.if, { ...refs, path: [...refs.path, "if"] });
|
|
187
|
+
const $then = parseSchema(schema.then, {
|
|
188
|
+
...refs,
|
|
189
|
+
path: [...refs.path, "then"]
|
|
190
|
+
});
|
|
191
|
+
const $else = parseSchema(schema.else, {
|
|
192
|
+
...refs,
|
|
193
|
+
path: [...refs.path, "else"]
|
|
194
|
+
});
|
|
195
|
+
return `z.union([${$then}, ${$else}]).superRefine((value,ctx) => {
|
|
196
|
+
const result = ${$if}.safeParse(value).success
|
|
197
|
+
? ${$then}.safeParse(value)
|
|
198
|
+
: ${$else}.safeParse(value);
|
|
199
|
+
if (!result.success) {
|
|
200
|
+
result.error.errors.forEach((error) => ctx.addIssue(error))
|
|
201
|
+
}
|
|
202
|
+
})`;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseNumber.js
|
|
206
|
+
var parseNumber = (schema) => {
|
|
207
|
+
let r = "z.number()";
|
|
208
|
+
if (schema.type === "integer") {
|
|
209
|
+
r += withMessage(schema, "type", () => [".int(", ")"]);
|
|
210
|
+
} else {
|
|
211
|
+
r += withMessage(schema, "format", ({ value }) => {
|
|
212
|
+
if (value === "int64") {
|
|
213
|
+
return [".int(", ")"];
|
|
214
|
+
}
|
|
79
215
|
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
keys.push(key);
|
|
216
|
+
}
|
|
217
|
+
r += withMessage(schema, "multipleOf", ({ value, json }) => {
|
|
218
|
+
if (value === 1) {
|
|
219
|
+
if (r.startsWith("z.number().int(")) {
|
|
220
|
+
return;
|
|
86
221
|
}
|
|
222
|
+
return [".int(", ")"];
|
|
87
223
|
}
|
|
88
|
-
return
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
224
|
+
return [`.multipleOf(${json}`, ", ", ")"];
|
|
225
|
+
});
|
|
226
|
+
if (typeof schema.minimum === "number") {
|
|
227
|
+
if (schema.exclusiveMinimum === true) {
|
|
228
|
+
r += withMessage(schema, "minimum", ({ json }) => [
|
|
229
|
+
`.gt(${json}`,
|
|
230
|
+
", ",
|
|
231
|
+
")"
|
|
232
|
+
]);
|
|
233
|
+
} else {
|
|
234
|
+
r += withMessage(schema, "minimum", ({ json }) => [
|
|
235
|
+
`.gte(${json}`,
|
|
236
|
+
", ",
|
|
237
|
+
")"
|
|
238
|
+
]);
|
|
94
239
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
240
|
+
} else if (typeof schema.exclusiveMinimum === "number") {
|
|
241
|
+
r += withMessage(schema, "exclusiveMinimum", ({ json }) => [
|
|
242
|
+
`.gt(${json}`,
|
|
243
|
+
", ",
|
|
244
|
+
")"
|
|
245
|
+
]);
|
|
100
246
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
247
|
+
if (typeof schema.maximum === "number") {
|
|
248
|
+
if (schema.exclusiveMaximum === true) {
|
|
249
|
+
r += withMessage(schema, "maximum", ({ json }) => [
|
|
250
|
+
`.lt(${json}`,
|
|
251
|
+
", ",
|
|
252
|
+
")"
|
|
253
|
+
]);
|
|
254
|
+
} else {
|
|
255
|
+
r += withMessage(schema, "maximum", ({ json }) => [
|
|
256
|
+
`.lte(${json}`,
|
|
257
|
+
", ",
|
|
258
|
+
")"
|
|
259
|
+
]);
|
|
105
260
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return {
|
|
113
|
-
...first,
|
|
114
|
-
...second
|
|
115
|
-
// second overwrites first
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
})(objectUtil || (objectUtil = {}));
|
|
119
|
-
var ZodParsedType = util.arrayToEnum([
|
|
120
|
-
"string",
|
|
121
|
-
"nan",
|
|
122
|
-
"number",
|
|
123
|
-
"integer",
|
|
124
|
-
"float",
|
|
125
|
-
"boolean",
|
|
126
|
-
"date",
|
|
127
|
-
"bigint",
|
|
128
|
-
"symbol",
|
|
129
|
-
"function",
|
|
130
|
-
"undefined",
|
|
131
|
-
"null",
|
|
132
|
-
"array",
|
|
133
|
-
"object",
|
|
134
|
-
"unknown",
|
|
135
|
-
"promise",
|
|
136
|
-
"void",
|
|
137
|
-
"never",
|
|
138
|
-
"map",
|
|
139
|
-
"set"
|
|
140
|
-
]);
|
|
141
|
-
var getParsedType = (data) => {
|
|
142
|
-
const t = typeof data;
|
|
143
|
-
switch (t) {
|
|
144
|
-
case "undefined":
|
|
145
|
-
return ZodParsedType.undefined;
|
|
146
|
-
case "string":
|
|
147
|
-
return ZodParsedType.string;
|
|
148
|
-
case "number":
|
|
149
|
-
return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
150
|
-
case "boolean":
|
|
151
|
-
return ZodParsedType.boolean;
|
|
152
|
-
case "function":
|
|
153
|
-
return ZodParsedType.function;
|
|
154
|
-
case "bigint":
|
|
155
|
-
return ZodParsedType.bigint;
|
|
156
|
-
case "symbol":
|
|
157
|
-
return ZodParsedType.symbol;
|
|
158
|
-
case "object":
|
|
159
|
-
if (Array.isArray(data)) {
|
|
160
|
-
return ZodParsedType.array;
|
|
161
|
-
}
|
|
162
|
-
if (data === null) {
|
|
163
|
-
return ZodParsedType.null;
|
|
164
|
-
}
|
|
165
|
-
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
|
|
166
|
-
return ZodParsedType.promise;
|
|
167
|
-
}
|
|
168
|
-
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
169
|
-
return ZodParsedType.map;
|
|
170
|
-
}
|
|
171
|
-
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
172
|
-
return ZodParsedType.set;
|
|
173
|
-
}
|
|
174
|
-
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
175
|
-
return ZodParsedType.date;
|
|
176
|
-
}
|
|
177
|
-
return ZodParsedType.object;
|
|
178
|
-
default:
|
|
179
|
-
return ZodParsedType.unknown;
|
|
261
|
+
} else if (typeof schema.exclusiveMaximum === "number") {
|
|
262
|
+
r += withMessage(schema, "exclusiveMaximum", ({ json }) => [
|
|
263
|
+
`.lt(${json}`,
|
|
264
|
+
", ",
|
|
265
|
+
")"
|
|
266
|
+
]);
|
|
180
267
|
}
|
|
268
|
+
return r;
|
|
181
269
|
};
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
"
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
]
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
270
|
+
|
|
271
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseOneOf.js
|
|
272
|
+
var parseOneOf = (schema, refs) => {
|
|
273
|
+
return schema.oneOf.length ? schema.oneOf.length === 1 ? parseSchema(schema.oneOf[0], {
|
|
274
|
+
...refs,
|
|
275
|
+
path: [...refs.path, "oneOf", 0]
|
|
276
|
+
}) : `z.any().superRefine((x, ctx) => {
|
|
277
|
+
const schemas = [${schema.oneOf.map((schema2, i) => parseSchema(schema2, {
|
|
278
|
+
...refs,
|
|
279
|
+
path: [...refs.path, "oneOf", i]
|
|
280
|
+
})).join(", ")}];
|
|
281
|
+
const errors = schemas.reduce<z.ZodError[]>(
|
|
282
|
+
(errors, schema) =>
|
|
283
|
+
((result) =>
|
|
284
|
+
result.error ? [...errors, result.error] : errors)(
|
|
285
|
+
schema.safeParse(x),
|
|
286
|
+
),
|
|
287
|
+
[],
|
|
288
|
+
);
|
|
289
|
+
if (schemas.length - errors.length !== 1) {
|
|
290
|
+
ctx.addIssue({
|
|
291
|
+
path: ctx.path,
|
|
292
|
+
code: "invalid_union",
|
|
293
|
+
unionErrors: errors,
|
|
294
|
+
message: "Invalid input: Should pass single schema",
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
})` : "z.any()";
|
|
203
298
|
};
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
299
|
+
|
|
300
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/utils/jsdocs.js
|
|
301
|
+
var expandJsdocs = (jsdocs) => {
|
|
302
|
+
const lines = jsdocs.split("\n");
|
|
303
|
+
const result = lines.length === 1 ? lines[0] : `
|
|
304
|
+
${lines.map((x) => `* ${x}`).join("\n")}
|
|
305
|
+
`;
|
|
306
|
+
return `/**${result}*/
|
|
307
|
+
`;
|
|
308
|
+
};
|
|
309
|
+
var addJsdocs = (schema, parsed) => {
|
|
310
|
+
const description = schema.description;
|
|
311
|
+
if (!description) {
|
|
312
|
+
return parsed;
|
|
207
313
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
if (Object.setPrototypeOf) {
|
|
219
|
-
Object.setPrototypeOf(this, actualProto);
|
|
314
|
+
return `
|
|
315
|
+
${expandJsdocs(description)}${parsed}`;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseObject.js
|
|
319
|
+
function parseObject(objectSchema, refs) {
|
|
320
|
+
let properties = void 0;
|
|
321
|
+
if (objectSchema.properties) {
|
|
322
|
+
if (!Object.keys(objectSchema.properties).length) {
|
|
323
|
+
properties = "z.object({})";
|
|
220
324
|
} else {
|
|
221
|
-
|
|
325
|
+
properties = "z.object({ ";
|
|
326
|
+
properties += Object.keys(objectSchema.properties).map((key) => {
|
|
327
|
+
const propSchema = objectSchema.properties[key];
|
|
328
|
+
let result = `${JSON.stringify(key)}: ${parseSchema(propSchema, {
|
|
329
|
+
...refs,
|
|
330
|
+
path: [...refs.path, "properties", key]
|
|
331
|
+
})}`;
|
|
332
|
+
if (refs.withJsdocs && typeof propSchema === "object") {
|
|
333
|
+
result = addJsdocs(propSchema, result);
|
|
334
|
+
}
|
|
335
|
+
const hasDefault = typeof propSchema === "object" && propSchema.default !== void 0;
|
|
336
|
+
const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : typeof propSchema === "object" && propSchema.required === true;
|
|
337
|
+
const optional = !hasDefault && !required;
|
|
338
|
+
return optional ? `${result}.optional()` : result;
|
|
339
|
+
}).join(", ");
|
|
340
|
+
properties += " })";
|
|
222
341
|
}
|
|
223
|
-
this.name = "ZodError";
|
|
224
|
-
this.issues = issues;
|
|
225
342
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
343
|
+
const additionalProperties = objectSchema.additionalProperties !== void 0 ? parseSchema(objectSchema.additionalProperties, {
|
|
344
|
+
...refs,
|
|
345
|
+
path: [...refs.path, "additionalProperties"]
|
|
346
|
+
}) : void 0;
|
|
347
|
+
let patternProperties = void 0;
|
|
348
|
+
if (objectSchema.patternProperties) {
|
|
349
|
+
const parsedPatternProperties = Object.fromEntries(Object.entries(objectSchema.patternProperties).map(([key, value]) => {
|
|
350
|
+
return [
|
|
351
|
+
key,
|
|
352
|
+
parseSchema(value, {
|
|
353
|
+
...refs,
|
|
354
|
+
path: [...refs.path, "patternProperties", key]
|
|
355
|
+
})
|
|
356
|
+
];
|
|
357
|
+
}, {}));
|
|
358
|
+
patternProperties = "";
|
|
359
|
+
if (properties) {
|
|
360
|
+
if (additionalProperties) {
|
|
361
|
+
patternProperties += `.catchall(z.union([${[
|
|
362
|
+
...Object.values(parsedPatternProperties),
|
|
363
|
+
additionalProperties
|
|
364
|
+
].join(", ")}]))`;
|
|
365
|
+
} else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
366
|
+
patternProperties += `.catchall(z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
|
|
367
|
+
} else {
|
|
368
|
+
patternProperties += `.catchall(${Object.values(parsedPatternProperties)})`;
|
|
369
|
+
}
|
|
370
|
+
} else {
|
|
371
|
+
if (additionalProperties) {
|
|
372
|
+
patternProperties += `z.record(z.union([${[
|
|
373
|
+
...Object.values(parsedPatternProperties),
|
|
374
|
+
additionalProperties
|
|
375
|
+
].join(", ")}]))`;
|
|
376
|
+
} else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
377
|
+
patternProperties += `z.record(z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
|
|
378
|
+
} else {
|
|
379
|
+
patternProperties += `z.record(${Object.values(parsedPatternProperties)})`;
|
|
257
380
|
}
|
|
258
|
-
};
|
|
259
|
-
processError(this);
|
|
260
|
-
return fieldErrors;
|
|
261
|
-
}
|
|
262
|
-
static assert(value) {
|
|
263
|
-
if (!(value instanceof _ZodError)) {
|
|
264
|
-
throw new Error(`Not a ZodError: ${value}`);
|
|
265
381
|
}
|
|
382
|
+
patternProperties += ".superRefine((value, ctx) => {\n";
|
|
383
|
+
patternProperties += "for (const key in value) {\n";
|
|
384
|
+
if (additionalProperties) {
|
|
385
|
+
if (objectSchema.properties) {
|
|
386
|
+
patternProperties += `let evaluated = [${Object.keys(objectSchema.properties).map((key) => JSON.stringify(key)).join(", ")}].includes(key)
|
|
387
|
+
`;
|
|
388
|
+
} else {
|
|
389
|
+
patternProperties += `let evaluated = false
|
|
390
|
+
`;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
for (const key in objectSchema.patternProperties) {
|
|
394
|
+
patternProperties += "if (key.match(new RegExp(" + JSON.stringify(key) + "))) {\n";
|
|
395
|
+
if (additionalProperties) {
|
|
396
|
+
patternProperties += "evaluated = true\n";
|
|
397
|
+
}
|
|
398
|
+
patternProperties += "const result = " + parsedPatternProperties[key] + ".safeParse(value[key])\n";
|
|
399
|
+
patternProperties += "if (!result.success) {\n";
|
|
400
|
+
patternProperties += `ctx.addIssue({
|
|
401
|
+
path: [...ctx.path, key],
|
|
402
|
+
code: 'custom',
|
|
403
|
+
message: \`Invalid input: Key matching regex /\${key}/ must match schema\`,
|
|
404
|
+
params: {
|
|
405
|
+
issues: result.error.issues
|
|
406
|
+
}
|
|
407
|
+
})
|
|
408
|
+
`;
|
|
409
|
+
patternProperties += "}\n";
|
|
410
|
+
patternProperties += "}\n";
|
|
411
|
+
}
|
|
412
|
+
if (additionalProperties) {
|
|
413
|
+
patternProperties += "if (!evaluated) {\n";
|
|
414
|
+
patternProperties += "const result = " + additionalProperties + ".safeParse(value[key])\n";
|
|
415
|
+
patternProperties += "if (!result.success) {\n";
|
|
416
|
+
patternProperties += `ctx.addIssue({
|
|
417
|
+
path: [...ctx.path, key],
|
|
418
|
+
code: 'custom',
|
|
419
|
+
message: \`Invalid input: must match catchall schema\`,
|
|
420
|
+
params: {
|
|
421
|
+
issues: result.error.issues
|
|
422
|
+
}
|
|
423
|
+
})
|
|
424
|
+
`;
|
|
425
|
+
patternProperties += "}\n";
|
|
426
|
+
patternProperties += "}\n";
|
|
427
|
+
}
|
|
428
|
+
patternProperties += "}\n";
|
|
429
|
+
patternProperties += "})";
|
|
266
430
|
}
|
|
267
|
-
|
|
268
|
-
|
|
431
|
+
let output = properties ? patternProperties ? properties + patternProperties : additionalProperties ? additionalProperties === "z.never()" ? properties + ".strict()" : properties + `.catchall(${additionalProperties})` : properties : patternProperties ? patternProperties : additionalProperties ? `z.record(${additionalProperties})` : "z.record(z.any())";
|
|
432
|
+
if (its.an.anyOf(objectSchema)) {
|
|
433
|
+
output += `.and(${parseAnyOf({
|
|
434
|
+
anyOf: objectSchema.anyOf.map((x) => typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? { ...x, type: "object" } : x)
|
|
435
|
+
}, refs)})`;
|
|
269
436
|
}
|
|
270
|
-
|
|
271
|
-
|
|
437
|
+
if (its.a.oneOf(objectSchema)) {
|
|
438
|
+
output += `.and(${parseOneOf({
|
|
439
|
+
oneOf: objectSchema.oneOf.map((x) => typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? { ...x, type: "object" } : x)
|
|
440
|
+
}, refs)})`;
|
|
272
441
|
}
|
|
273
|
-
|
|
274
|
-
|
|
442
|
+
if (its.an.allOf(objectSchema)) {
|
|
443
|
+
output += `.and(${parseAllOf({
|
|
444
|
+
allOf: objectSchema.allOf.map((x) => typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? { ...x, type: "object" } : x)
|
|
445
|
+
}, refs)})`;
|
|
275
446
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
447
|
+
return output;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseString.js
|
|
451
|
+
var parseString = (schema) => {
|
|
452
|
+
let r = "z.string()";
|
|
453
|
+
r += withMessage(schema, "format", ({ value }) => {
|
|
454
|
+
switch (value) {
|
|
455
|
+
case "email":
|
|
456
|
+
return [".email(", ")"];
|
|
457
|
+
case "ip":
|
|
458
|
+
return [".ip(", ")"];
|
|
459
|
+
case "ipv4":
|
|
460
|
+
return ['.ip({ version: "v4"', ", message: ", " })"];
|
|
461
|
+
case "ipv6":
|
|
462
|
+
return ['.ip({ version: "v6"', ", message: ", " })"];
|
|
463
|
+
case "uri":
|
|
464
|
+
return [".url(", ")"];
|
|
465
|
+
case "uuid":
|
|
466
|
+
return [".uuid(", ")"];
|
|
467
|
+
case "date-time":
|
|
468
|
+
return [".datetime({ offset: true", ", message: ", " })"];
|
|
469
|
+
case "time":
|
|
470
|
+
return [".time(", ")"];
|
|
471
|
+
case "date":
|
|
472
|
+
return [".date(", ")"];
|
|
473
|
+
case "binary":
|
|
474
|
+
return [".base64(", ")"];
|
|
475
|
+
case "duration":
|
|
476
|
+
return [".duration(", ")"];
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
r += withMessage(schema, "pattern", ({ json }) => [
|
|
480
|
+
`.regex(new RegExp(${json})`,
|
|
481
|
+
", ",
|
|
482
|
+
")"
|
|
483
|
+
]);
|
|
484
|
+
r += withMessage(schema, "minLength", ({ json }) => [
|
|
485
|
+
`.min(${json}`,
|
|
486
|
+
", ",
|
|
487
|
+
")"
|
|
488
|
+
]);
|
|
489
|
+
r += withMessage(schema, "maxLength", ({ json }) => [
|
|
490
|
+
`.max(${json}`,
|
|
491
|
+
", ",
|
|
492
|
+
")"
|
|
493
|
+
]);
|
|
494
|
+
r += withMessage(schema, "contentEncoding", ({ value }) => {
|
|
495
|
+
if (value === "base64") {
|
|
496
|
+
return [".base64(", ")"];
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
const contentMediaType = withMessage(schema, "contentMediaType", ({ value }) => {
|
|
500
|
+
if (value === "application/json") {
|
|
501
|
+
return [
|
|
502
|
+
'.transform((str, ctx) => { try { return JSON.parse(str); } catch (err) { ctx.addIssue({ code: "custom", message: "Invalid JSON" }); }}',
|
|
503
|
+
", ",
|
|
504
|
+
")"
|
|
505
|
+
];
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
if (contentMediaType != "") {
|
|
509
|
+
r += contentMediaType;
|
|
510
|
+
r += withMessage(schema, "contentSchema", ({ value }) => {
|
|
511
|
+
if (value && value instanceof Object) {
|
|
512
|
+
return [
|
|
513
|
+
`.pipe(${parseSchema(value)}`,
|
|
514
|
+
", ",
|
|
515
|
+
")"
|
|
516
|
+
];
|
|
285
517
|
}
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
return r;
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/utils/omit.js
|
|
524
|
+
var omit = (obj, ...keys) => Object.keys(obj).reduce((acc, key) => {
|
|
525
|
+
if (!keys.includes(key)) {
|
|
526
|
+
acc[key] = obj[key];
|
|
527
|
+
}
|
|
528
|
+
return acc;
|
|
529
|
+
}, {});
|
|
530
|
+
|
|
531
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseNullable.js
|
|
532
|
+
var parseNullable = (schema, refs) => {
|
|
533
|
+
return `${parseSchema(omit(schema, "nullable"), refs, true)}.nullable()`;
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseSchema.js
|
|
537
|
+
var parseSchema = (schema, refs = { seen: /* @__PURE__ */ new Map(), path: [] }, blockMeta) => {
|
|
538
|
+
if (typeof schema !== "object")
|
|
539
|
+
return schema ? "z.any()" : "z.never()";
|
|
540
|
+
if (refs.parserOverride) {
|
|
541
|
+
const custom2 = refs.parserOverride(schema, refs);
|
|
542
|
+
if (typeof custom2 === "string") {
|
|
543
|
+
return custom2;
|
|
286
544
|
}
|
|
287
|
-
return { formErrors, fieldErrors };
|
|
288
545
|
}
|
|
289
|
-
get
|
|
290
|
-
|
|
546
|
+
let seen = refs.seen.get(schema);
|
|
547
|
+
if (seen) {
|
|
548
|
+
if (seen.r !== void 0) {
|
|
549
|
+
return seen.r;
|
|
550
|
+
}
|
|
551
|
+
if (refs.depth === void 0 || seen.n >= refs.depth) {
|
|
552
|
+
return "z.any()";
|
|
553
|
+
}
|
|
554
|
+
seen.n += 1;
|
|
555
|
+
} else {
|
|
556
|
+
seen = { r: void 0, n: 0 };
|
|
557
|
+
refs.seen.set(schema, seen);
|
|
558
|
+
}
|
|
559
|
+
let parsed = selectParser(schema, refs);
|
|
560
|
+
if (!blockMeta) {
|
|
561
|
+
if (!refs.withoutDescribes) {
|
|
562
|
+
parsed = addDescribes(schema, parsed);
|
|
563
|
+
}
|
|
564
|
+
if (!refs.withoutDefaults) {
|
|
565
|
+
parsed = addDefaults(schema, parsed);
|
|
566
|
+
}
|
|
567
|
+
parsed = addAnnotations(schema, parsed);
|
|
291
568
|
}
|
|
569
|
+
seen.r = parsed;
|
|
570
|
+
return parsed;
|
|
292
571
|
};
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
572
|
+
var addDescribes = (schema, parsed) => {
|
|
573
|
+
if (schema.description) {
|
|
574
|
+
parsed += `.describe(${JSON.stringify(schema.description)})`;
|
|
575
|
+
}
|
|
576
|
+
return parsed;
|
|
296
577
|
};
|
|
297
|
-
var
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
case ZodIssueCode.invalid_type:
|
|
301
|
-
if (issue.received === ZodParsedType.undefined) {
|
|
302
|
-
message = "Required";
|
|
303
|
-
} else {
|
|
304
|
-
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
305
|
-
}
|
|
306
|
-
break;
|
|
307
|
-
case ZodIssueCode.invalid_literal:
|
|
308
|
-
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
309
|
-
break;
|
|
310
|
-
case ZodIssueCode.unrecognized_keys:
|
|
311
|
-
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
312
|
-
break;
|
|
313
|
-
case ZodIssueCode.invalid_union:
|
|
314
|
-
message = `Invalid input`;
|
|
315
|
-
break;
|
|
316
|
-
case ZodIssueCode.invalid_union_discriminator:
|
|
317
|
-
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
318
|
-
break;
|
|
319
|
-
case ZodIssueCode.invalid_enum_value:
|
|
320
|
-
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
321
|
-
break;
|
|
322
|
-
case ZodIssueCode.invalid_arguments:
|
|
323
|
-
message = `Invalid function arguments`;
|
|
324
|
-
break;
|
|
325
|
-
case ZodIssueCode.invalid_return_type:
|
|
326
|
-
message = `Invalid function return type`;
|
|
327
|
-
break;
|
|
328
|
-
case ZodIssueCode.invalid_date:
|
|
329
|
-
message = `Invalid date`;
|
|
330
|
-
break;
|
|
331
|
-
case ZodIssueCode.invalid_string:
|
|
332
|
-
if (typeof issue.validation === "object") {
|
|
333
|
-
if ("includes" in issue.validation) {
|
|
334
|
-
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
335
|
-
if (typeof issue.validation.position === "number") {
|
|
336
|
-
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
337
|
-
}
|
|
338
|
-
} else if ("startsWith" in issue.validation) {
|
|
339
|
-
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
340
|
-
} else if ("endsWith" in issue.validation) {
|
|
341
|
-
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
342
|
-
} else {
|
|
343
|
-
util.assertNever(issue.validation);
|
|
344
|
-
}
|
|
345
|
-
} else if (issue.validation !== "regex") {
|
|
346
|
-
message = `Invalid ${issue.validation}`;
|
|
347
|
-
} else {
|
|
348
|
-
message = "Invalid";
|
|
349
|
-
}
|
|
350
|
-
break;
|
|
351
|
-
case ZodIssueCode.too_small:
|
|
352
|
-
if (issue.type === "array")
|
|
353
|
-
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
354
|
-
else if (issue.type === "string")
|
|
355
|
-
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
356
|
-
else if (issue.type === "number")
|
|
357
|
-
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
358
|
-
else if (issue.type === "date")
|
|
359
|
-
message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
|
|
360
|
-
else
|
|
361
|
-
message = "Invalid input";
|
|
362
|
-
break;
|
|
363
|
-
case ZodIssueCode.too_big:
|
|
364
|
-
if (issue.type === "array")
|
|
365
|
-
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
366
|
-
else if (issue.type === "string")
|
|
367
|
-
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
368
|
-
else if (issue.type === "number")
|
|
369
|
-
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
370
|
-
else if (issue.type === "bigint")
|
|
371
|
-
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
372
|
-
else if (issue.type === "date")
|
|
373
|
-
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
374
|
-
else
|
|
375
|
-
message = "Invalid input";
|
|
376
|
-
break;
|
|
377
|
-
case ZodIssueCode.custom:
|
|
378
|
-
message = `Invalid input`;
|
|
379
|
-
break;
|
|
380
|
-
case ZodIssueCode.invalid_intersection_types:
|
|
381
|
-
message = `Intersection results could not be merged`;
|
|
382
|
-
break;
|
|
383
|
-
case ZodIssueCode.not_multiple_of:
|
|
384
|
-
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
385
|
-
break;
|
|
386
|
-
case ZodIssueCode.not_finite:
|
|
387
|
-
message = "Number must be finite";
|
|
388
|
-
break;
|
|
389
|
-
default:
|
|
390
|
-
message = _ctx.defaultError;
|
|
391
|
-
util.assertNever(issue);
|
|
578
|
+
var addDefaults = (schema, parsed) => {
|
|
579
|
+
if (schema.default !== void 0) {
|
|
580
|
+
parsed += `.default(${JSON.stringify(schema.default)})`;
|
|
392
581
|
}
|
|
393
|
-
return
|
|
582
|
+
return parsed;
|
|
394
583
|
};
|
|
395
|
-
var
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
399
|
-
function getErrorMap() {
|
|
400
|
-
return overrideErrorMap;
|
|
401
|
-
}
|
|
402
|
-
var makeIssue = (params) => {
|
|
403
|
-
const { data, path: path2, errorMaps, issueData } = params;
|
|
404
|
-
const fullPath = [...path2, ...issueData.path || []];
|
|
405
|
-
const fullIssue = {
|
|
406
|
-
...issueData,
|
|
407
|
-
path: fullPath
|
|
408
|
-
};
|
|
409
|
-
if (issueData.message !== void 0) {
|
|
410
|
-
return {
|
|
411
|
-
...issueData,
|
|
412
|
-
path: fullPath,
|
|
413
|
-
message: issueData.message
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
let errorMessage = "";
|
|
417
|
-
const maps = errorMaps.filter((m) => !!m).slice().reverse();
|
|
418
|
-
for (const map of maps) {
|
|
419
|
-
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
584
|
+
var addAnnotations = (schema, parsed) => {
|
|
585
|
+
if (schema.readOnly) {
|
|
586
|
+
parsed += ".readonly()";
|
|
420
587
|
}
|
|
421
|
-
return
|
|
422
|
-
...issueData,
|
|
423
|
-
path: fullPath,
|
|
424
|
-
message: errorMessage
|
|
425
|
-
};
|
|
588
|
+
return parsed;
|
|
426
589
|
};
|
|
427
|
-
var
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
})
|
|
445
|
-
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
590
|
+
var selectParser = (schema, refs) => {
|
|
591
|
+
if (its.a.nullable(schema)) {
|
|
592
|
+
return parseNullable(schema, refs);
|
|
593
|
+
} else if (its.an.object(schema)) {
|
|
594
|
+
return parseObject(schema, refs);
|
|
595
|
+
} else if (its.an.array(schema)) {
|
|
596
|
+
return parseArray(schema, refs);
|
|
597
|
+
} else if (its.an.anyOf(schema)) {
|
|
598
|
+
return parseAnyOf(schema, refs);
|
|
599
|
+
} else if (its.an.allOf(schema)) {
|
|
600
|
+
return parseAllOf(schema, refs);
|
|
601
|
+
} else if (its.a.oneOf(schema)) {
|
|
602
|
+
return parseOneOf(schema, refs);
|
|
603
|
+
} else if (its.a.not(schema)) {
|
|
604
|
+
return parseNot(schema, refs);
|
|
605
|
+
} else if (its.an.enum(schema)) {
|
|
606
|
+
return parseEnum(schema);
|
|
607
|
+
} else if (its.a.const(schema)) {
|
|
608
|
+
return parseConst(schema);
|
|
609
|
+
} else if (its.a.multipleType(schema)) {
|
|
610
|
+
return parseMultipleType(schema, refs);
|
|
611
|
+
} else if (its.a.primitive(schema, "string")) {
|
|
612
|
+
return parseString(schema);
|
|
613
|
+
} else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) {
|
|
614
|
+
return parseNumber(schema);
|
|
615
|
+
} else if (its.a.primitive(schema, "boolean")) {
|
|
616
|
+
return parseBoolean();
|
|
617
|
+
} else if (its.a.primitive(schema, "null")) {
|
|
618
|
+
return parseNull();
|
|
619
|
+
} else if (its.a.conditional(schema)) {
|
|
620
|
+
return parseIfThenElse(schema, refs);
|
|
621
|
+
} else {
|
|
622
|
+
return parseDefault();
|
|
450
623
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
624
|
+
};
|
|
625
|
+
var its = {
|
|
626
|
+
an: {
|
|
627
|
+
object: (x) => x.type === "object",
|
|
628
|
+
array: (x) => x.type === "array",
|
|
629
|
+
anyOf: (x) => x.anyOf !== void 0,
|
|
630
|
+
allOf: (x) => x.allOf !== void 0,
|
|
631
|
+
enum: (x) => x.enum !== void 0
|
|
632
|
+
},
|
|
633
|
+
a: {
|
|
634
|
+
nullable: (x) => x.nullable === true,
|
|
635
|
+
multipleType: (x) => Array.isArray(x.type),
|
|
636
|
+
not: (x) => x.not !== void 0,
|
|
637
|
+
const: (x) => x.const !== void 0,
|
|
638
|
+
primitive: (x, p) => x.type === p,
|
|
639
|
+
conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
|
|
640
|
+
oneOf: (x) => x.oneOf !== void 0
|
|
454
641
|
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/jsonSchemaToZod.js
|
|
645
|
+
var jsonSchemaToZod = (schema, { module, name, type, noImport, ...rest } = {}) => {
|
|
646
|
+
if (type && (!name || module !== "esm")) {
|
|
647
|
+
throw new Error("Option `type` requires `name` to be set and `module` to be `esm`");
|
|
458
648
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
649
|
+
let result = parseSchema(schema, {
|
|
650
|
+
module,
|
|
651
|
+
name,
|
|
652
|
+
path: [],
|
|
653
|
+
seen: /* @__PURE__ */ new Map(),
|
|
654
|
+
...rest
|
|
655
|
+
});
|
|
656
|
+
const jsdocs = rest.withJsdocs && typeof schema !== "boolean" && schema.description ? expandJsdocs(schema.description) : "";
|
|
657
|
+
if (module === "cjs") {
|
|
658
|
+
result = `${jsdocs}module.exports = ${name ? `{ ${JSON.stringify(name)}: ${result} }` : result}
|
|
659
|
+
`;
|
|
660
|
+
if (!noImport) {
|
|
661
|
+
result = `${jsdocs}const { z } = require("zod")
|
|
662
|
+
|
|
663
|
+
${result}`;
|
|
467
664
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
syncPairs.push({
|
|
476
|
-
key,
|
|
477
|
-
value
|
|
478
|
-
});
|
|
665
|
+
} else if (module === "esm") {
|
|
666
|
+
result = `${jsdocs}export ${name ? `const ${name} =` : `default`} ${result}
|
|
667
|
+
`;
|
|
668
|
+
if (!noImport) {
|
|
669
|
+
result = `import { z } from "zod"
|
|
670
|
+
|
|
671
|
+
${result}`;
|
|
479
672
|
}
|
|
480
|
-
|
|
673
|
+
} else if (name) {
|
|
674
|
+
result = `${jsdocs}const ${name} = ${result}`;
|
|
481
675
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
if (key.status === "aborted")
|
|
487
|
-
return INVALID;
|
|
488
|
-
if (value.status === "aborted")
|
|
489
|
-
return INVALID;
|
|
490
|
-
if (key.status === "dirty")
|
|
491
|
-
status.dirty();
|
|
492
|
-
if (value.status === "dirty")
|
|
493
|
-
status.dirty();
|
|
494
|
-
if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
|
|
495
|
-
finalObject[key.value] = value.value;
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
return { status: status.value, value: finalObject };
|
|
676
|
+
if (type && name) {
|
|
677
|
+
let typeName = typeof type === "string" ? type : `${name[0].toUpperCase()}${name.substring(1)}`;
|
|
678
|
+
result += `export type ${typeName} = z.infer<typeof ${name}>
|
|
679
|
+
`;
|
|
499
680
|
}
|
|
681
|
+
return result;
|
|
500
682
|
};
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
var
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
function __classPrivateFieldGet(receiver, state, kind, f) {
|
|
511
|
-
if (typeof state === "function" ? receiver !== state || true : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
512
|
-
return state.get(receiver);
|
|
513
|
-
}
|
|
514
|
-
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
515
|
-
if (typeof state === "function" ? receiver !== state || true : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
516
|
-
return state.set(receiver, value), value;
|
|
517
|
-
}
|
|
518
|
-
var errorUtil;
|
|
519
|
-
(function(errorUtil2) {
|
|
520
|
-
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
521
|
-
errorUtil2.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
522
|
-
})(errorUtil || (errorUtil = {}));
|
|
523
|
-
var _ZodEnum_cache;
|
|
524
|
-
var _ZodNativeEnum_cache;
|
|
525
|
-
var ParseInputLazyPath = class {
|
|
526
|
-
constructor(parent, value, path2, key) {
|
|
527
|
-
this._cachedPath = [];
|
|
528
|
-
this.parent = parent;
|
|
529
|
-
this.data = value;
|
|
530
|
-
this._path = path2;
|
|
531
|
-
this._key = key;
|
|
683
|
+
|
|
684
|
+
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/index.js
|
|
685
|
+
var esm_default = jsonSchemaToZod;
|
|
686
|
+
|
|
687
|
+
// ../../node_modules/.pnpm/zod@3.24.3/node_modules/zod/lib/index.mjs
|
|
688
|
+
var util;
|
|
689
|
+
(function(util2) {
|
|
690
|
+
util2.assertEqual = (val) => val;
|
|
691
|
+
function assertIs(_arg) {
|
|
532
692
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
693
|
+
util2.assertIs = assertIs;
|
|
694
|
+
function assertNever(_x) {
|
|
695
|
+
throw new Error();
|
|
696
|
+
}
|
|
697
|
+
util2.assertNever = assertNever;
|
|
698
|
+
util2.arrayToEnum = (items) => {
|
|
699
|
+
const obj = {};
|
|
700
|
+
for (const item of items) {
|
|
701
|
+
obj[item] = item;
|
|
702
|
+
}
|
|
703
|
+
return obj;
|
|
704
|
+
};
|
|
705
|
+
util2.getValidEnumValues = (obj) => {
|
|
706
|
+
const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
707
|
+
const filtered = {};
|
|
708
|
+
for (const k of validKeys) {
|
|
709
|
+
filtered[k] = obj[k];
|
|
710
|
+
}
|
|
711
|
+
return util2.objectValues(filtered);
|
|
712
|
+
};
|
|
713
|
+
util2.objectValues = (obj) => {
|
|
714
|
+
return util2.objectKeys(obj).map(function(e) {
|
|
715
|
+
return obj[e];
|
|
716
|
+
});
|
|
717
|
+
};
|
|
718
|
+
util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
|
|
719
|
+
const keys = [];
|
|
720
|
+
for (const key in object) {
|
|
721
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
722
|
+
keys.push(key);
|
|
539
723
|
}
|
|
540
724
|
}
|
|
541
|
-
return
|
|
725
|
+
return keys;
|
|
726
|
+
};
|
|
727
|
+
util2.find = (arr, checker) => {
|
|
728
|
+
for (const item of arr) {
|
|
729
|
+
if (checker(item))
|
|
730
|
+
return item;
|
|
731
|
+
}
|
|
732
|
+
return void 0;
|
|
733
|
+
};
|
|
734
|
+
util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
|
|
735
|
+
function joinValues(array, separator = " | ") {
|
|
736
|
+
return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
|
|
542
737
|
}
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
} else {
|
|
548
|
-
if (!ctx.common.issues.length) {
|
|
549
|
-
throw new Error("Validation failed but no issues detected.");
|
|
738
|
+
util2.joinValues = joinValues;
|
|
739
|
+
util2.jsonStringifyReplacer = (_, value) => {
|
|
740
|
+
if (typeof value === "bigint") {
|
|
741
|
+
return value.toString();
|
|
550
742
|
}
|
|
743
|
+
return value;
|
|
744
|
+
};
|
|
745
|
+
})(util || (util = {}));
|
|
746
|
+
var objectUtil;
|
|
747
|
+
(function(objectUtil2) {
|
|
748
|
+
objectUtil2.mergeShapes = (first, second) => {
|
|
551
749
|
return {
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
return this._error;
|
|
556
|
-
const error = new ZodError(ctx.common.issues);
|
|
557
|
-
this._error = error;
|
|
558
|
-
return this._error;
|
|
559
|
-
}
|
|
750
|
+
...first,
|
|
751
|
+
...second
|
|
752
|
+
// second overwrites first
|
|
560
753
|
};
|
|
561
|
-
}
|
|
562
|
-
};
|
|
563
|
-
function processCreateParams(params) {
|
|
564
|
-
if (!params)
|
|
565
|
-
return {};
|
|
566
|
-
const { errorMap: errorMap2, invalid_type_error, required_error, description } = params;
|
|
567
|
-
if (errorMap2 && (invalid_type_error || required_error)) {
|
|
568
|
-
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
569
|
-
}
|
|
570
|
-
if (errorMap2)
|
|
571
|
-
return { errorMap: errorMap2, description };
|
|
572
|
-
const customMap = (iss, ctx) => {
|
|
573
|
-
var _a, _b;
|
|
574
|
-
const { message } = params;
|
|
575
|
-
if (iss.code === "invalid_enum_value") {
|
|
576
|
-
return { message: message !== null && message !== void 0 ? message : ctx.defaultError };
|
|
577
|
-
}
|
|
578
|
-
if (typeof ctx.data === "undefined") {
|
|
579
|
-
return { message: (_a = message !== null && message !== void 0 ? message : required_error) !== null && _a !== void 0 ? _a : ctx.defaultError };
|
|
580
|
-
}
|
|
581
|
-
if (iss.code !== "invalid_type")
|
|
582
|
-
return { message: ctx.defaultError };
|
|
583
|
-
return { message: (_b = message !== null && message !== void 0 ? message : invalid_type_error) !== null && _b !== void 0 ? _b : ctx.defaultError };
|
|
584
754
|
};
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
755
|
+
})(objectUtil || (objectUtil = {}));
|
|
756
|
+
var ZodParsedType = util.arrayToEnum([
|
|
757
|
+
"string",
|
|
758
|
+
"nan",
|
|
759
|
+
"number",
|
|
760
|
+
"integer",
|
|
761
|
+
"float",
|
|
762
|
+
"boolean",
|
|
763
|
+
"date",
|
|
764
|
+
"bigint",
|
|
765
|
+
"symbol",
|
|
766
|
+
"function",
|
|
767
|
+
"undefined",
|
|
768
|
+
"null",
|
|
769
|
+
"array",
|
|
770
|
+
"object",
|
|
771
|
+
"unknown",
|
|
772
|
+
"promise",
|
|
773
|
+
"void",
|
|
774
|
+
"never",
|
|
775
|
+
"map",
|
|
776
|
+
"set"
|
|
777
|
+
]);
|
|
778
|
+
var getParsedType = (data) => {
|
|
779
|
+
const t = typeof data;
|
|
780
|
+
switch (t) {
|
|
781
|
+
case "undefined":
|
|
782
|
+
return ZodParsedType.undefined;
|
|
783
|
+
case "string":
|
|
784
|
+
return ZodParsedType.string;
|
|
785
|
+
case "number":
|
|
786
|
+
return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
787
|
+
case "boolean":
|
|
788
|
+
return ZodParsedType.boolean;
|
|
789
|
+
case "function":
|
|
790
|
+
return ZodParsedType.function;
|
|
791
|
+
case "bigint":
|
|
792
|
+
return ZodParsedType.bigint;
|
|
793
|
+
case "symbol":
|
|
794
|
+
return ZodParsedType.symbol;
|
|
795
|
+
case "object":
|
|
796
|
+
if (Array.isArray(data)) {
|
|
797
|
+
return ZodParsedType.array;
|
|
798
|
+
}
|
|
799
|
+
if (data === null) {
|
|
800
|
+
return ZodParsedType.null;
|
|
801
|
+
}
|
|
802
|
+
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
|
|
803
|
+
return ZodParsedType.promise;
|
|
804
|
+
}
|
|
805
|
+
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
806
|
+
return ZodParsedType.map;
|
|
807
|
+
}
|
|
808
|
+
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
809
|
+
return ZodParsedType.set;
|
|
810
|
+
}
|
|
811
|
+
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
812
|
+
return ZodParsedType.date;
|
|
813
|
+
}
|
|
814
|
+
return ZodParsedType.object;
|
|
815
|
+
default:
|
|
816
|
+
return ZodParsedType.unknown;
|
|
590
817
|
}
|
|
591
|
-
|
|
592
|
-
|
|
818
|
+
};
|
|
819
|
+
var ZodIssueCode = util.arrayToEnum([
|
|
820
|
+
"invalid_type",
|
|
821
|
+
"invalid_literal",
|
|
822
|
+
"custom",
|
|
823
|
+
"invalid_union",
|
|
824
|
+
"invalid_union_discriminator",
|
|
825
|
+
"invalid_enum_value",
|
|
826
|
+
"unrecognized_keys",
|
|
827
|
+
"invalid_arguments",
|
|
828
|
+
"invalid_return_type",
|
|
829
|
+
"invalid_date",
|
|
830
|
+
"invalid_string",
|
|
831
|
+
"too_small",
|
|
832
|
+
"too_big",
|
|
833
|
+
"invalid_intersection_types",
|
|
834
|
+
"not_multiple_of",
|
|
835
|
+
"not_finite"
|
|
836
|
+
]);
|
|
837
|
+
var quotelessJson = (obj) => {
|
|
838
|
+
const json = JSON.stringify(obj, null, 2);
|
|
839
|
+
return json.replace(/"([^"]+)":/g, "$1:");
|
|
840
|
+
};
|
|
841
|
+
var ZodError = class _ZodError extends Error {
|
|
842
|
+
get errors() {
|
|
843
|
+
return this.issues;
|
|
593
844
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
schemaErrorMap: this._def.errorMap,
|
|
600
|
-
path: input.path,
|
|
601
|
-
parent: input.parent
|
|
845
|
+
constructor(issues) {
|
|
846
|
+
super();
|
|
847
|
+
this.issues = [];
|
|
848
|
+
this.addIssue = (sub) => {
|
|
849
|
+
this.issues = [...this.issues, sub];
|
|
602
850
|
};
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
return {
|
|
606
|
-
status: new ParseStatus(),
|
|
607
|
-
ctx: {
|
|
608
|
-
common: input.parent.common,
|
|
609
|
-
data: input.data,
|
|
610
|
-
parsedType: getParsedType(input.data),
|
|
611
|
-
schemaErrorMap: this._def.errorMap,
|
|
612
|
-
path: input.path,
|
|
613
|
-
parent: input.parent
|
|
614
|
-
}
|
|
851
|
+
this.addIssues = (subs = []) => {
|
|
852
|
+
this.issues = [...this.issues, ...subs];
|
|
615
853
|
};
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
854
|
+
const actualProto = new.target.prototype;
|
|
855
|
+
if (Object.setPrototypeOf) {
|
|
856
|
+
Object.setPrototypeOf(this, actualProto);
|
|
857
|
+
} else {
|
|
858
|
+
this.__proto__ = actualProto;
|
|
621
859
|
}
|
|
622
|
-
|
|
860
|
+
this.name = "ZodError";
|
|
861
|
+
this.issues = issues;
|
|
623
862
|
}
|
|
624
|
-
|
|
625
|
-
const
|
|
626
|
-
|
|
627
|
-
}
|
|
628
|
-
parse(data, params) {
|
|
629
|
-
const result = this.safeParse(data, params);
|
|
630
|
-
if (result.success)
|
|
631
|
-
return result.data;
|
|
632
|
-
throw result.error;
|
|
633
|
-
}
|
|
634
|
-
safeParse(data, params) {
|
|
635
|
-
var _a;
|
|
636
|
-
const ctx = {
|
|
637
|
-
common: {
|
|
638
|
-
issues: [],
|
|
639
|
-
async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
|
|
640
|
-
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap
|
|
641
|
-
},
|
|
642
|
-
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
643
|
-
schemaErrorMap: this._def.errorMap,
|
|
644
|
-
parent: null,
|
|
645
|
-
data,
|
|
646
|
-
parsedType: getParsedType(data)
|
|
647
|
-
};
|
|
648
|
-
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
649
|
-
return handleResult(ctx, result);
|
|
650
|
-
}
|
|
651
|
-
"~validate"(data) {
|
|
652
|
-
var _a, _b;
|
|
653
|
-
const ctx = {
|
|
654
|
-
common: {
|
|
655
|
-
issues: [],
|
|
656
|
-
async: !!this["~standard"].async
|
|
657
|
-
},
|
|
658
|
-
path: [],
|
|
659
|
-
schemaErrorMap: this._def.errorMap,
|
|
660
|
-
parent: null,
|
|
661
|
-
data,
|
|
662
|
-
parsedType: getParsedType(data)
|
|
863
|
+
format(_mapper) {
|
|
864
|
+
const mapper = _mapper || function(issue) {
|
|
865
|
+
return issue.message;
|
|
663
866
|
};
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
867
|
+
const fieldErrors = { _errors: [] };
|
|
868
|
+
const processError = (error) => {
|
|
869
|
+
for (const issue of error.issues) {
|
|
870
|
+
if (issue.code === "invalid_union") {
|
|
871
|
+
issue.unionErrors.map(processError);
|
|
872
|
+
} else if (issue.code === "invalid_return_type") {
|
|
873
|
+
processError(issue.returnTypeError);
|
|
874
|
+
} else if (issue.code === "invalid_arguments") {
|
|
875
|
+
processError(issue.argumentsError);
|
|
876
|
+
} else if (issue.path.length === 0) {
|
|
877
|
+
fieldErrors._errors.push(mapper(issue));
|
|
878
|
+
} else {
|
|
879
|
+
let curr = fieldErrors;
|
|
880
|
+
let i = 0;
|
|
881
|
+
while (i < issue.path.length) {
|
|
882
|
+
const el = issue.path[i];
|
|
883
|
+
const terminal = i === issue.path.length - 1;
|
|
884
|
+
if (!terminal) {
|
|
885
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
886
|
+
} else {
|
|
887
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
888
|
+
curr[el]._errors.push(mapper(issue));
|
|
889
|
+
}
|
|
890
|
+
curr = curr[el];
|
|
891
|
+
i++;
|
|
892
|
+
}
|
|
675
893
|
}
|
|
676
|
-
ctx.common = {
|
|
677
|
-
issues: [],
|
|
678
|
-
async: true
|
|
679
|
-
};
|
|
680
894
|
}
|
|
895
|
+
};
|
|
896
|
+
processError(this);
|
|
897
|
+
return fieldErrors;
|
|
898
|
+
}
|
|
899
|
+
static assert(value) {
|
|
900
|
+
if (!(value instanceof _ZodError)) {
|
|
901
|
+
throw new Error(`Not a ZodError: ${value}`);
|
|
681
902
|
}
|
|
682
|
-
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result) ? {
|
|
683
|
-
value: result.value
|
|
684
|
-
} : {
|
|
685
|
-
issues: ctx.common.issues
|
|
686
|
-
});
|
|
687
903
|
}
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
if (result.success)
|
|
691
|
-
return result.data;
|
|
692
|
-
throw result.error;
|
|
904
|
+
toString() {
|
|
905
|
+
return this.message;
|
|
693
906
|
}
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
common: {
|
|
697
|
-
issues: [],
|
|
698
|
-
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
699
|
-
async: true
|
|
700
|
-
},
|
|
701
|
-
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
702
|
-
schemaErrorMap: this._def.errorMap,
|
|
703
|
-
parent: null,
|
|
704
|
-
data,
|
|
705
|
-
parsedType: getParsedType(data)
|
|
706
|
-
};
|
|
707
|
-
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
708
|
-
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
709
|
-
return handleResult(ctx, result);
|
|
907
|
+
get message() {
|
|
908
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
710
909
|
}
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
910
|
+
get isEmpty() {
|
|
911
|
+
return this.issues.length === 0;
|
|
912
|
+
}
|
|
913
|
+
flatten(mapper = (issue) => issue.message) {
|
|
914
|
+
const fieldErrors = {};
|
|
915
|
+
const formErrors = [];
|
|
916
|
+
for (const sub of this.issues) {
|
|
917
|
+
if (sub.path.length > 0) {
|
|
918
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
919
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
717
920
|
} else {
|
|
718
|
-
|
|
719
|
-
}
|
|
720
|
-
};
|
|
721
|
-
return this._refinement((val, ctx) => {
|
|
722
|
-
const result = check(val);
|
|
723
|
-
const setError = () => ctx.addIssue({
|
|
724
|
-
code: ZodIssueCode.custom,
|
|
725
|
-
...getIssueProperties(val)
|
|
726
|
-
});
|
|
727
|
-
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
728
|
-
return result.then((data) => {
|
|
729
|
-
if (!data) {
|
|
730
|
-
setError();
|
|
731
|
-
return false;
|
|
732
|
-
} else {
|
|
733
|
-
return true;
|
|
734
|
-
}
|
|
735
|
-
});
|
|
921
|
+
formErrors.push(mapper(sub));
|
|
736
922
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
923
|
+
}
|
|
924
|
+
return { formErrors, fieldErrors };
|
|
925
|
+
}
|
|
926
|
+
get formErrors() {
|
|
927
|
+
return this.flatten();
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
ZodError.create = (issues) => {
|
|
931
|
+
const error = new ZodError(issues);
|
|
932
|
+
return error;
|
|
933
|
+
};
|
|
934
|
+
var errorMap = (issue, _ctx) => {
|
|
935
|
+
let message;
|
|
936
|
+
switch (issue.code) {
|
|
937
|
+
case ZodIssueCode.invalid_type:
|
|
938
|
+
if (issue.received === ZodParsedType.undefined) {
|
|
939
|
+
message = "Required";
|
|
740
940
|
} else {
|
|
741
|
-
|
|
941
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
742
942
|
}
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
943
|
+
break;
|
|
944
|
+
case ZodIssueCode.invalid_literal:
|
|
945
|
+
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
946
|
+
break;
|
|
947
|
+
case ZodIssueCode.unrecognized_keys:
|
|
948
|
+
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
949
|
+
break;
|
|
950
|
+
case ZodIssueCode.invalid_union:
|
|
951
|
+
message = `Invalid input`;
|
|
952
|
+
break;
|
|
953
|
+
case ZodIssueCode.invalid_union_discriminator:
|
|
954
|
+
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
955
|
+
break;
|
|
956
|
+
case ZodIssueCode.invalid_enum_value:
|
|
957
|
+
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
958
|
+
break;
|
|
959
|
+
case ZodIssueCode.invalid_arguments:
|
|
960
|
+
message = `Invalid function arguments`;
|
|
961
|
+
break;
|
|
962
|
+
case ZodIssueCode.invalid_return_type:
|
|
963
|
+
message = `Invalid function return type`;
|
|
964
|
+
break;
|
|
965
|
+
case ZodIssueCode.invalid_date:
|
|
966
|
+
message = `Invalid date`;
|
|
967
|
+
break;
|
|
968
|
+
case ZodIssueCode.invalid_string:
|
|
969
|
+
if (typeof issue.validation === "object") {
|
|
970
|
+
if ("includes" in issue.validation) {
|
|
971
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
972
|
+
if (typeof issue.validation.position === "number") {
|
|
973
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
974
|
+
}
|
|
975
|
+
} else if ("startsWith" in issue.validation) {
|
|
976
|
+
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
977
|
+
} else if ("endsWith" in issue.validation) {
|
|
978
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
979
|
+
} else {
|
|
980
|
+
util.assertNever(issue.validation);
|
|
981
|
+
}
|
|
982
|
+
} else if (issue.validation !== "regex") {
|
|
983
|
+
message = `Invalid ${issue.validation}`;
|
|
750
984
|
} else {
|
|
751
|
-
|
|
985
|
+
message = "Invalid";
|
|
752
986
|
}
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
987
|
+
break;
|
|
988
|
+
case ZodIssueCode.too_small:
|
|
989
|
+
if (issue.type === "array")
|
|
990
|
+
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
991
|
+
else if (issue.type === "string")
|
|
992
|
+
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
993
|
+
else if (issue.type === "number")
|
|
994
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
995
|
+
else if (issue.type === "date")
|
|
996
|
+
message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
|
|
997
|
+
else
|
|
998
|
+
message = "Invalid input";
|
|
999
|
+
break;
|
|
1000
|
+
case ZodIssueCode.too_big:
|
|
1001
|
+
if (issue.type === "array")
|
|
1002
|
+
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
1003
|
+
else if (issue.type === "string")
|
|
1004
|
+
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
1005
|
+
else if (issue.type === "number")
|
|
1006
|
+
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
1007
|
+
else if (issue.type === "bigint")
|
|
1008
|
+
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
1009
|
+
else if (issue.type === "date")
|
|
1010
|
+
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
1011
|
+
else
|
|
1012
|
+
message = "Invalid input";
|
|
1013
|
+
break;
|
|
1014
|
+
case ZodIssueCode.custom:
|
|
1015
|
+
message = `Invalid input`;
|
|
1016
|
+
break;
|
|
1017
|
+
case ZodIssueCode.invalid_intersection_types:
|
|
1018
|
+
message = `Intersection results could not be merged`;
|
|
1019
|
+
break;
|
|
1020
|
+
case ZodIssueCode.not_multiple_of:
|
|
1021
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
1022
|
+
break;
|
|
1023
|
+
case ZodIssueCode.not_finite:
|
|
1024
|
+
message = "Number must be finite";
|
|
1025
|
+
break;
|
|
1026
|
+
default:
|
|
1027
|
+
message = _ctx.defaultError;
|
|
1028
|
+
util.assertNever(issue);
|
|
1029
|
+
}
|
|
1030
|
+
return { message };
|
|
1031
|
+
};
|
|
1032
|
+
var overrideErrorMap = errorMap;
|
|
1033
|
+
function setErrorMap(map) {
|
|
1034
|
+
overrideErrorMap = map;
|
|
1035
|
+
}
|
|
1036
|
+
function getErrorMap() {
|
|
1037
|
+
return overrideErrorMap;
|
|
1038
|
+
}
|
|
1039
|
+
var makeIssue = (params) => {
|
|
1040
|
+
const { data, path: path2, errorMaps, issueData } = params;
|
|
1041
|
+
const fullPath = [...path2, ...issueData.path || []];
|
|
1042
|
+
const fullIssue = {
|
|
1043
|
+
...issueData,
|
|
1044
|
+
path: fullPath
|
|
1045
|
+
};
|
|
1046
|
+
if (issueData.message !== void 0) {
|
|
1047
|
+
return {
|
|
1048
|
+
...issueData,
|
|
1049
|
+
path: fullPath,
|
|
1050
|
+
message: issueData.message
|
|
796
1051
|
};
|
|
797
1052
|
}
|
|
798
|
-
|
|
799
|
-
|
|
1053
|
+
let errorMessage = "";
|
|
1054
|
+
const maps = errorMaps.filter((m) => !!m).slice().reverse();
|
|
1055
|
+
for (const map of maps) {
|
|
1056
|
+
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
800
1057
|
}
|
|
801
|
-
|
|
802
|
-
|
|
1058
|
+
return {
|
|
1059
|
+
...issueData,
|
|
1060
|
+
path: fullPath,
|
|
1061
|
+
message: errorMessage
|
|
1062
|
+
};
|
|
1063
|
+
};
|
|
1064
|
+
var EMPTY_PATH = [];
|
|
1065
|
+
function addIssueToContext(ctx, issueData) {
|
|
1066
|
+
const overrideMap = getErrorMap();
|
|
1067
|
+
const issue = makeIssue({
|
|
1068
|
+
issueData,
|
|
1069
|
+
data: ctx.data,
|
|
1070
|
+
path: ctx.path,
|
|
1071
|
+
errorMaps: [
|
|
1072
|
+
ctx.common.contextualErrorMap,
|
|
1073
|
+
// contextual error map is first priority
|
|
1074
|
+
ctx.schemaErrorMap,
|
|
1075
|
+
// then schema-bound map if available
|
|
1076
|
+
overrideMap,
|
|
1077
|
+
// then global override map
|
|
1078
|
+
overrideMap === errorMap ? void 0 : errorMap
|
|
1079
|
+
// then global default map
|
|
1080
|
+
].filter((x) => !!x)
|
|
1081
|
+
});
|
|
1082
|
+
ctx.common.issues.push(issue);
|
|
1083
|
+
}
|
|
1084
|
+
var ParseStatus = class _ParseStatus {
|
|
1085
|
+
constructor() {
|
|
1086
|
+
this.value = "valid";
|
|
803
1087
|
}
|
|
804
|
-
|
|
805
|
-
|
|
1088
|
+
dirty() {
|
|
1089
|
+
if (this.value === "valid")
|
|
1090
|
+
this.value = "dirty";
|
|
806
1091
|
}
|
|
807
|
-
|
|
808
|
-
|
|
1092
|
+
abort() {
|
|
1093
|
+
if (this.value !== "aborted")
|
|
1094
|
+
this.value = "aborted";
|
|
809
1095
|
}
|
|
810
|
-
|
|
811
|
-
|
|
1096
|
+
static mergeArray(status, results) {
|
|
1097
|
+
const arrayValue = [];
|
|
1098
|
+
for (const s of results) {
|
|
1099
|
+
if (s.status === "aborted")
|
|
1100
|
+
return INVALID;
|
|
1101
|
+
if (s.status === "dirty")
|
|
1102
|
+
status.dirty();
|
|
1103
|
+
arrayValue.push(s.value);
|
|
1104
|
+
}
|
|
1105
|
+
return { status: status.value, value: arrayValue };
|
|
812
1106
|
}
|
|
813
|
-
|
|
814
|
-
|
|
1107
|
+
static async mergeObjectAsync(status, pairs) {
|
|
1108
|
+
const syncPairs = [];
|
|
1109
|
+
for (const pair of pairs) {
|
|
1110
|
+
const key = await pair.key;
|
|
1111
|
+
const value = await pair.value;
|
|
1112
|
+
syncPairs.push({
|
|
1113
|
+
key,
|
|
1114
|
+
value
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
return _ParseStatus.mergeObjectSync(status, syncPairs);
|
|
815
1118
|
}
|
|
816
|
-
|
|
817
|
-
|
|
1119
|
+
static mergeObjectSync(status, pairs) {
|
|
1120
|
+
const finalObject = {};
|
|
1121
|
+
for (const pair of pairs) {
|
|
1122
|
+
const { key, value } = pair;
|
|
1123
|
+
if (key.status === "aborted")
|
|
1124
|
+
return INVALID;
|
|
1125
|
+
if (value.status === "aborted")
|
|
1126
|
+
return INVALID;
|
|
1127
|
+
if (key.status === "dirty")
|
|
1128
|
+
status.dirty();
|
|
1129
|
+
if (value.status === "dirty")
|
|
1130
|
+
status.dirty();
|
|
1131
|
+
if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
|
|
1132
|
+
finalObject[key.value] = value.value;
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
return { status: status.value, value: finalObject };
|
|
818
1136
|
}
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
1137
|
+
};
|
|
1138
|
+
var INVALID = Object.freeze({
|
|
1139
|
+
status: "aborted"
|
|
1140
|
+
});
|
|
1141
|
+
var DIRTY = (value) => ({ status: "dirty", value });
|
|
1142
|
+
var OK = (value) => ({ status: "valid", value });
|
|
1143
|
+
var isAborted = (x) => x.status === "aborted";
|
|
1144
|
+
var isDirty = (x) => x.status === "dirty";
|
|
1145
|
+
var isValid = (x) => x.status === "valid";
|
|
1146
|
+
var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
1147
|
+
function __classPrivateFieldGet(receiver, state, kind, f) {
|
|
1148
|
+
if (typeof state === "function" ? receiver !== state || true : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
1149
|
+
return state.get(receiver);
|
|
1150
|
+
}
|
|
1151
|
+
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
1152
|
+
if (typeof state === "function" ? receiver !== state || true : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
1153
|
+
return state.set(receiver, value), value;
|
|
1154
|
+
}
|
|
1155
|
+
var errorUtil;
|
|
1156
|
+
(function(errorUtil2) {
|
|
1157
|
+
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
1158
|
+
errorUtil2.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
1159
|
+
})(errorUtil || (errorUtil = {}));
|
|
1160
|
+
var _ZodEnum_cache;
|
|
1161
|
+
var _ZodNativeEnum_cache;
|
|
1162
|
+
var ParseInputLazyPath = class {
|
|
1163
|
+
constructor(parent, value, path2, key) {
|
|
1164
|
+
this._cachedPath = [];
|
|
1165
|
+
this.parent = parent;
|
|
1166
|
+
this.data = value;
|
|
1167
|
+
this._path = path2;
|
|
1168
|
+
this._key = key;
|
|
826
1169
|
}
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
}
|
|
1170
|
+
get path() {
|
|
1171
|
+
if (!this._cachedPath.length) {
|
|
1172
|
+
if (this._key instanceof Array) {
|
|
1173
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
1174
|
+
} else {
|
|
1175
|
+
this._cachedPath.push(...this._path, this._key);
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
return this._cachedPath;
|
|
835
1179
|
}
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
1180
|
+
};
|
|
1181
|
+
var handleResult = (ctx, result) => {
|
|
1182
|
+
if (isValid(result)) {
|
|
1183
|
+
return { success: true, data: result.value };
|
|
1184
|
+
} else {
|
|
1185
|
+
if (!ctx.common.issues.length) {
|
|
1186
|
+
throw new Error("Validation failed but no issues detected.");
|
|
1187
|
+
}
|
|
1188
|
+
return {
|
|
1189
|
+
success: false,
|
|
1190
|
+
get error() {
|
|
1191
|
+
if (this._error)
|
|
1192
|
+
return this._error;
|
|
1193
|
+
const error = new ZodError(ctx.common.issues);
|
|
1194
|
+
this._error = error;
|
|
1195
|
+
return this._error;
|
|
1196
|
+
}
|
|
1197
|
+
};
|
|
842
1198
|
}
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
});
|
|
1199
|
+
};
|
|
1200
|
+
function processCreateParams(params) {
|
|
1201
|
+
if (!params)
|
|
1202
|
+
return {};
|
|
1203
|
+
const { errorMap: errorMap2, invalid_type_error, required_error, description } = params;
|
|
1204
|
+
if (errorMap2 && (invalid_type_error || required_error)) {
|
|
1205
|
+
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
851
1206
|
}
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
1207
|
+
if (errorMap2)
|
|
1208
|
+
return { errorMap: errorMap2, description };
|
|
1209
|
+
const customMap = (iss, ctx) => {
|
|
1210
|
+
var _a, _b;
|
|
1211
|
+
const { message } = params;
|
|
1212
|
+
if (iss.code === "invalid_enum_value") {
|
|
1213
|
+
return { message: message !== null && message !== void 0 ? message : ctx.defaultError };
|
|
1214
|
+
}
|
|
1215
|
+
if (typeof ctx.data === "undefined") {
|
|
1216
|
+
return { message: (_a = message !== null && message !== void 0 ? message : required_error) !== null && _a !== void 0 ? _a : ctx.defaultError };
|
|
1217
|
+
}
|
|
1218
|
+
if (iss.code !== "invalid_type")
|
|
1219
|
+
return { message: ctx.defaultError };
|
|
1220
|
+
return { message: (_b = message !== null && message !== void 0 ? message : invalid_type_error) !== null && _b !== void 0 ? _b : ctx.defaultError };
|
|
1221
|
+
};
|
|
1222
|
+
return { errorMap: customMap, description };
|
|
1223
|
+
}
|
|
1224
|
+
var ZodType = class {
|
|
1225
|
+
get description() {
|
|
1226
|
+
return this._def.description;
|
|
858
1227
|
}
|
|
859
|
-
|
|
860
|
-
return
|
|
1228
|
+
_getType(input) {
|
|
1229
|
+
return getParsedType(input.data);
|
|
861
1230
|
}
|
|
862
|
-
|
|
863
|
-
return
|
|
1231
|
+
_getOrReturnCtx(input, ctx) {
|
|
1232
|
+
return ctx || {
|
|
1233
|
+
common: input.parent.common,
|
|
1234
|
+
data: input.data,
|
|
1235
|
+
parsedType: getParsedType(input.data),
|
|
1236
|
+
schemaErrorMap: this._def.errorMap,
|
|
1237
|
+
path: input.path,
|
|
1238
|
+
parent: input.parent
|
|
1239
|
+
};
|
|
864
1240
|
}
|
|
865
|
-
|
|
866
|
-
return
|
|
1241
|
+
_processInputParams(input) {
|
|
1242
|
+
return {
|
|
1243
|
+
status: new ParseStatus(),
|
|
1244
|
+
ctx: {
|
|
1245
|
+
common: input.parent.common,
|
|
1246
|
+
data: input.data,
|
|
1247
|
+
parsedType: getParsedType(input.data),
|
|
1248
|
+
schemaErrorMap: this._def.errorMap,
|
|
1249
|
+
path: input.path,
|
|
1250
|
+
parent: input.parent
|
|
1251
|
+
}
|
|
1252
|
+
};
|
|
867
1253
|
}
|
|
868
|
-
|
|
869
|
-
|
|
1254
|
+
_parseSync(input) {
|
|
1255
|
+
const result = this._parse(input);
|
|
1256
|
+
if (isAsync(result)) {
|
|
1257
|
+
throw new Error("Synchronous parse encountered promise.");
|
|
1258
|
+
}
|
|
1259
|
+
return result;
|
|
870
1260
|
}
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
var ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
875
|
-
var uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
876
|
-
var nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
877
|
-
var jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
878
|
-
var durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
|
|
879
|
-
var emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
880
|
-
var _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
881
|
-
var emojiRegex;
|
|
882
|
-
var ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
883
|
-
var ipv4CidrRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/;
|
|
884
|
-
var ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
885
|
-
var ipv6CidrRegex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
|
|
886
|
-
var base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
887
|
-
var base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
888
|
-
var dateRegexSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
|
|
889
|
-
var dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
890
|
-
function timeRegexSource(args) {
|
|
891
|
-
let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
|
|
892
|
-
if (args.precision) {
|
|
893
|
-
regex = `${regex}\\.\\d{${args.precision}}`;
|
|
894
|
-
} else if (args.precision == null) {
|
|
895
|
-
regex = `${regex}(\\.\\d+)?`;
|
|
1261
|
+
_parseAsync(input) {
|
|
1262
|
+
const result = this._parse(input);
|
|
1263
|
+
return Promise.resolve(result);
|
|
896
1264
|
}
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
function datetimeRegex(args) {
|
|
903
|
-
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
904
|
-
const opts = [];
|
|
905
|
-
opts.push(args.local ? `Z?` : `Z`);
|
|
906
|
-
if (args.offset)
|
|
907
|
-
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
908
|
-
regex = `${regex}(${opts.join("|")})`;
|
|
909
|
-
return new RegExp(`^${regex}$`);
|
|
910
|
-
}
|
|
911
|
-
function isValidIP(ip, version) {
|
|
912
|
-
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
913
|
-
return true;
|
|
1265
|
+
parse(data, params) {
|
|
1266
|
+
const result = this.safeParse(data, params);
|
|
1267
|
+
if (result.success)
|
|
1268
|
+
return result.data;
|
|
1269
|
+
throw result.error;
|
|
914
1270
|
}
|
|
915
|
-
|
|
916
|
-
|
|
1271
|
+
safeParse(data, params) {
|
|
1272
|
+
var _a;
|
|
1273
|
+
const ctx = {
|
|
1274
|
+
common: {
|
|
1275
|
+
issues: [],
|
|
1276
|
+
async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
|
|
1277
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap
|
|
1278
|
+
},
|
|
1279
|
+
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
1280
|
+
schemaErrorMap: this._def.errorMap,
|
|
1281
|
+
parent: null,
|
|
1282
|
+
data,
|
|
1283
|
+
parsedType: getParsedType(data)
|
|
1284
|
+
};
|
|
1285
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
1286
|
+
return handleResult(ctx, result);
|
|
917
1287
|
}
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
if (
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
1288
|
+
"~validate"(data) {
|
|
1289
|
+
var _a, _b;
|
|
1290
|
+
const ctx = {
|
|
1291
|
+
common: {
|
|
1292
|
+
issues: [],
|
|
1293
|
+
async: !!this["~standard"].async
|
|
1294
|
+
},
|
|
1295
|
+
path: [],
|
|
1296
|
+
schemaErrorMap: this._def.errorMap,
|
|
1297
|
+
parent: null,
|
|
1298
|
+
data,
|
|
1299
|
+
parsedType: getParsedType(data)
|
|
1300
|
+
};
|
|
1301
|
+
if (!this["~standard"].async) {
|
|
1302
|
+
try {
|
|
1303
|
+
const result = this._parseSync({ data, path: [], parent: ctx });
|
|
1304
|
+
return isValid(result) ? {
|
|
1305
|
+
value: result.value
|
|
1306
|
+
} : {
|
|
1307
|
+
issues: ctx.common.issues
|
|
1308
|
+
};
|
|
1309
|
+
} catch (err) {
|
|
1310
|
+
if ((_b = (_a = err === null || err === void 0 ? void 0 : err.message) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.includes("encountered")) {
|
|
1311
|
+
this["~standard"].async = true;
|
|
1312
|
+
}
|
|
1313
|
+
ctx.common = {
|
|
1314
|
+
issues: [],
|
|
1315
|
+
async: true
|
|
1316
|
+
};
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result) ? {
|
|
1320
|
+
value: result.value
|
|
1321
|
+
} : {
|
|
1322
|
+
issues: ctx.common.issues
|
|
1323
|
+
});
|
|
936
1324
|
}
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
1325
|
+
async parseAsync(data, params) {
|
|
1326
|
+
const result = await this.safeParseAsync(data, params);
|
|
1327
|
+
if (result.success)
|
|
1328
|
+
return result.data;
|
|
1329
|
+
throw result.error;
|
|
941
1330
|
}
|
|
942
|
-
|
|
943
|
-
|
|
1331
|
+
async safeParseAsync(data, params) {
|
|
1332
|
+
const ctx = {
|
|
1333
|
+
common: {
|
|
1334
|
+
issues: [],
|
|
1335
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
1336
|
+
async: true
|
|
1337
|
+
},
|
|
1338
|
+
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
1339
|
+
schemaErrorMap: this._def.errorMap,
|
|
1340
|
+
parent: null,
|
|
1341
|
+
data,
|
|
1342
|
+
parsedType: getParsedType(data)
|
|
1343
|
+
};
|
|
1344
|
+
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
1345
|
+
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
1346
|
+
return handleResult(ctx, result);
|
|
944
1347
|
}
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
1348
|
+
refine(check, message) {
|
|
1349
|
+
const getIssueProperties = (val) => {
|
|
1350
|
+
if (typeof message === "string" || typeof message === "undefined") {
|
|
1351
|
+
return { message };
|
|
1352
|
+
} else if (typeof message === "function") {
|
|
1353
|
+
return message(val);
|
|
1354
|
+
} else {
|
|
1355
|
+
return message;
|
|
1356
|
+
}
|
|
1357
|
+
};
|
|
1358
|
+
return this._refinement((val, ctx) => {
|
|
1359
|
+
const result = check(val);
|
|
1360
|
+
const setError = () => ctx.addIssue({
|
|
1361
|
+
code: ZodIssueCode.custom,
|
|
1362
|
+
...getIssueProperties(val)
|
|
959
1363
|
});
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
1364
|
+
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
1365
|
+
return result.then((data) => {
|
|
1366
|
+
if (!data) {
|
|
1367
|
+
setError();
|
|
1368
|
+
return false;
|
|
1369
|
+
} else {
|
|
1370
|
+
return true;
|
|
1371
|
+
}
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
if (!result) {
|
|
1375
|
+
setError();
|
|
1376
|
+
return false;
|
|
1377
|
+
} else {
|
|
1378
|
+
return true;
|
|
1379
|
+
}
|
|
1380
|
+
});
|
|
1381
|
+
}
|
|
1382
|
+
refinement(check, refinementData) {
|
|
1383
|
+
return this._refinement((val, ctx) => {
|
|
1384
|
+
if (!check(val)) {
|
|
1385
|
+
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
1386
|
+
return false;
|
|
1387
|
+
} else {
|
|
1388
|
+
return true;
|
|
1389
|
+
}
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
1392
|
+
_refinement(refinement) {
|
|
1393
|
+
return new ZodEffects({
|
|
1394
|
+
schema: this,
|
|
1395
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
1396
|
+
effect: { type: "refinement", refinement }
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
superRefine(refinement) {
|
|
1400
|
+
return this._refinement(refinement);
|
|
1401
|
+
}
|
|
1402
|
+
constructor(def) {
|
|
1403
|
+
this.spa = this.safeParseAsync;
|
|
1404
|
+
this._def = def;
|
|
1405
|
+
this.parse = this.parse.bind(this);
|
|
1406
|
+
this.safeParse = this.safeParse.bind(this);
|
|
1407
|
+
this.parseAsync = this.parseAsync.bind(this);
|
|
1408
|
+
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
1409
|
+
this.spa = this.spa.bind(this);
|
|
1410
|
+
this.refine = this.refine.bind(this);
|
|
1411
|
+
this.refinement = this.refinement.bind(this);
|
|
1412
|
+
this.superRefine = this.superRefine.bind(this);
|
|
1413
|
+
this.optional = this.optional.bind(this);
|
|
1414
|
+
this.nullable = this.nullable.bind(this);
|
|
1415
|
+
this.nullish = this.nullish.bind(this);
|
|
1416
|
+
this.array = this.array.bind(this);
|
|
1417
|
+
this.promise = this.promise.bind(this);
|
|
1418
|
+
this.or = this.or.bind(this);
|
|
1419
|
+
this.and = this.and.bind(this);
|
|
1420
|
+
this.transform = this.transform.bind(this);
|
|
1421
|
+
this.brand = this.brand.bind(this);
|
|
1422
|
+
this.default = this.default.bind(this);
|
|
1423
|
+
this.catch = this.catch.bind(this);
|
|
1424
|
+
this.describe = this.describe.bind(this);
|
|
1425
|
+
this.pipe = this.pipe.bind(this);
|
|
1426
|
+
this.readonly = this.readonly.bind(this);
|
|
1427
|
+
this.isNullable = this.isNullable.bind(this);
|
|
1428
|
+
this.isOptional = this.isOptional.bind(this);
|
|
1429
|
+
this["~standard"] = {
|
|
1430
|
+
version: 1,
|
|
1431
|
+
vendor: "zod",
|
|
1432
|
+
validate: (data) => this["~validate"](data)
|
|
1433
|
+
};
|
|
1434
|
+
}
|
|
1435
|
+
optional() {
|
|
1436
|
+
return ZodOptional.create(this, this._def);
|
|
1437
|
+
}
|
|
1438
|
+
nullable() {
|
|
1439
|
+
return ZodNullable.create(this, this._def);
|
|
1440
|
+
}
|
|
1441
|
+
nullish() {
|
|
1442
|
+
return this.nullable().optional();
|
|
1443
|
+
}
|
|
1444
|
+
array() {
|
|
1445
|
+
return ZodArray.create(this);
|
|
1446
|
+
}
|
|
1447
|
+
promise() {
|
|
1448
|
+
return ZodPromise.create(this, this._def);
|
|
1449
|
+
}
|
|
1450
|
+
or(option) {
|
|
1451
|
+
return ZodUnion.create([this, option], this._def);
|
|
1452
|
+
}
|
|
1453
|
+
and(incoming) {
|
|
1454
|
+
return ZodIntersection.create(this, incoming, this._def);
|
|
1455
|
+
}
|
|
1456
|
+
transform(transform) {
|
|
1457
|
+
return new ZodEffects({
|
|
1458
|
+
...processCreateParams(this._def),
|
|
1459
|
+
schema: this,
|
|
1460
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
1461
|
+
effect: { type: "transform", transform }
|
|
1462
|
+
});
|
|
1463
|
+
}
|
|
1464
|
+
default(def) {
|
|
1465
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
1466
|
+
return new ZodDefault({
|
|
1467
|
+
...processCreateParams(this._def),
|
|
1468
|
+
innerType: this,
|
|
1469
|
+
defaultValue: defaultValueFunc,
|
|
1470
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault
|
|
1471
|
+
});
|
|
1472
|
+
}
|
|
1473
|
+
brand() {
|
|
1474
|
+
return new ZodBranded({
|
|
1475
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
1476
|
+
type: this,
|
|
1477
|
+
...processCreateParams(this._def)
|
|
1478
|
+
});
|
|
1479
|
+
}
|
|
1480
|
+
catch(def) {
|
|
1481
|
+
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
1482
|
+
return new ZodCatch({
|
|
1483
|
+
...processCreateParams(this._def),
|
|
1484
|
+
innerType: this,
|
|
1485
|
+
catchValue: catchValueFunc,
|
|
1486
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch
|
|
1487
|
+
});
|
|
1488
|
+
}
|
|
1489
|
+
describe(description) {
|
|
1490
|
+
const This = this.constructor;
|
|
1491
|
+
return new This({
|
|
1492
|
+
...this._def,
|
|
1493
|
+
description
|
|
1494
|
+
});
|
|
1495
|
+
}
|
|
1496
|
+
pipe(target) {
|
|
1497
|
+
return ZodPipeline.create(this, target);
|
|
1498
|
+
}
|
|
1499
|
+
readonly() {
|
|
1500
|
+
return ZodReadonly.create(this);
|
|
1501
|
+
}
|
|
1502
|
+
isOptional() {
|
|
1503
|
+
return this.safeParse(void 0).success;
|
|
1504
|
+
}
|
|
1505
|
+
isNullable() {
|
|
1506
|
+
return this.safeParse(null).success;
|
|
1507
|
+
}
|
|
1508
|
+
};
|
|
1509
|
+
var cuidRegex = /^c[^\s-]{8,}$/i;
|
|
1510
|
+
var cuid2Regex = /^[0-9a-z]+$/;
|
|
1511
|
+
var ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
1512
|
+
var uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
1513
|
+
var nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
1514
|
+
var jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
1515
|
+
var durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
|
|
1516
|
+
var emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
1517
|
+
var _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
1518
|
+
var emojiRegex;
|
|
1519
|
+
var ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
1520
|
+
var ipv4CidrRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/;
|
|
1521
|
+
var ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
1522
|
+
var ipv6CidrRegex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
|
|
1523
|
+
var base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
1524
|
+
var base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
1525
|
+
var dateRegexSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
|
|
1526
|
+
var dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
1527
|
+
function timeRegexSource(args) {
|
|
1528
|
+
let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
|
|
1529
|
+
if (args.precision) {
|
|
1530
|
+
regex = `${regex}\\.\\d{${args.precision}}`;
|
|
1531
|
+
} else if (args.precision == null) {
|
|
1532
|
+
regex = `${regex}(\\.\\d+)?`;
|
|
1533
|
+
}
|
|
1534
|
+
return regex;
|
|
1535
|
+
}
|
|
1536
|
+
function timeRegex(args) {
|
|
1537
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
1538
|
+
}
|
|
1539
|
+
function datetimeRegex(args) {
|
|
1540
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
1541
|
+
const opts = [];
|
|
1542
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
1543
|
+
if (args.offset)
|
|
1544
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
1545
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
1546
|
+
return new RegExp(`^${regex}$`);
|
|
1547
|
+
}
|
|
1548
|
+
function isValidIP(ip, version) {
|
|
1549
|
+
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
1550
|
+
return true;
|
|
1551
|
+
}
|
|
1552
|
+
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
1553
|
+
return true;
|
|
1554
|
+
}
|
|
1555
|
+
return false;
|
|
1556
|
+
}
|
|
1557
|
+
function isValidJWT(jwt, alg) {
|
|
1558
|
+
if (!jwtRegex.test(jwt))
|
|
1559
|
+
return false;
|
|
1560
|
+
try {
|
|
1561
|
+
const [header] = jwt.split(".");
|
|
1562
|
+
const base64 = header.replace(/-/g, "+").replace(/_/g, "/").padEnd(header.length + (4 - header.length % 4) % 4, "=");
|
|
1563
|
+
const decoded = JSON.parse(atob(base64));
|
|
1564
|
+
if (typeof decoded !== "object" || decoded === null)
|
|
1565
|
+
return false;
|
|
1566
|
+
if (!decoded.typ || !decoded.alg)
|
|
1567
|
+
return false;
|
|
1568
|
+
if (alg && decoded.alg !== alg)
|
|
1569
|
+
return false;
|
|
1570
|
+
return true;
|
|
1571
|
+
} catch (_a) {
|
|
1572
|
+
return false;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
function isValidCidr(ip, version) {
|
|
1576
|
+
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
|
|
1577
|
+
return true;
|
|
1578
|
+
}
|
|
1579
|
+
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
|
|
1580
|
+
return true;
|
|
1581
|
+
}
|
|
1582
|
+
return false;
|
|
1583
|
+
}
|
|
1584
|
+
var ZodString = class _ZodString extends ZodType {
|
|
1585
|
+
_parse(input) {
|
|
1586
|
+
if (this._def.coerce) {
|
|
1587
|
+
input.data = String(input.data);
|
|
1588
|
+
}
|
|
1589
|
+
const parsedType = this._getType(input);
|
|
1590
|
+
if (parsedType !== ZodParsedType.string) {
|
|
1591
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
1592
|
+
addIssueToContext(ctx2, {
|
|
1593
|
+
code: ZodIssueCode.invalid_type,
|
|
1594
|
+
expected: ZodParsedType.string,
|
|
1595
|
+
received: ctx2.parsedType
|
|
1596
|
+
});
|
|
1597
|
+
return INVALID;
|
|
1598
|
+
}
|
|
1599
|
+
const status = new ParseStatus();
|
|
1600
|
+
let ctx = void 0;
|
|
1601
|
+
for (const check of this._def.checks) {
|
|
1602
|
+
if (check.kind === "min") {
|
|
1603
|
+
if (input.data.length < check.value) {
|
|
1604
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
968
1605
|
addIssueToContext(ctx, {
|
|
969
1606
|
code: ZodIssueCode.too_small,
|
|
970
1607
|
minimum: check.value,
|
|
@@ -4138,1020 +4775,385 @@ var InternalMastraMCPClient = class extends base.MastraBase {
|
|
|
4138
4775
|
timeout = protocol_js.DEFAULT_REQUEST_TIMEOUT_MSEC
|
|
4139
4776
|
}) {
|
|
4140
4777
|
super({ name: "MastraMCPClient" });
|
|
4141
|
-
this.name = name;
|
|
4142
|
-
this.timeout = timeout;
|
|
4143
|
-
this.logHandler = server.logger;
|
|
4144
|
-
this.enableServerLogs = server.enableServerLogs ?? true;
|
|
4145
|
-
this.serverConfig = server;
|
|
4146
|
-
this.client = new index_js$1.Client(
|
|
4147
|
-
{
|
|
4148
|
-
name,
|
|
4149
|
-
version
|
|
4150
|
-
},
|
|
4151
|
-
{
|
|
4152
|
-
capabilities
|
|
4153
|
-
}
|
|
4154
|
-
);
|
|
4155
|
-
this.setupLogging();
|
|
4156
|
-
}
|
|
4157
|
-
/**
|
|
4158
|
-
* Log a message at the specified level
|
|
4159
|
-
* @param level Log level
|
|
4160
|
-
* @param message Log message
|
|
4161
|
-
* @param details Optional additional details
|
|
4162
|
-
*/
|
|
4163
|
-
log(level, message, details) {
|
|
4164
|
-
const loggerMethod = convertLogLevelToLoggerMethod(level);
|
|
4165
|
-
const msg = `[${this.name}] ${message}`;
|
|
4166
|
-
this.logger[loggerMethod](msg, details);
|
|
4167
|
-
if (this.logHandler) {
|
|
4168
|
-
this.logHandler({
|
|
4169
|
-
level,
|
|
4170
|
-
message: msg,
|
|
4171
|
-
timestamp: /* @__PURE__ */ new Date(),
|
|
4172
|
-
serverName: this.name,
|
|
4173
|
-
details
|
|
4174
|
-
});
|
|
4175
|
-
}
|
|
4176
|
-
}
|
|
4177
|
-
setupLogging() {
|
|
4178
|
-
if (this.enableServerLogs) {
|
|
4179
|
-
this.client.setNotificationHandler(
|
|
4180
|
-
z.object({
|
|
4181
|
-
method: z.literal("notifications/message"),
|
|
4182
|
-
params: z.object({
|
|
4183
|
-
level: z.string()
|
|
4184
|
-
}).passthrough()
|
|
4185
|
-
}),
|
|
4186
|
-
(notification) => {
|
|
4187
|
-
const { level, ...params } = notification.params;
|
|
4188
|
-
this.log(level, "[MCP SERVER LOG]", params);
|
|
4189
|
-
}
|
|
4190
|
-
);
|
|
4191
|
-
}
|
|
4192
|
-
}
|
|
4193
|
-
async connectStdio(command) {
|
|
4194
|
-
this.log("debug", `Using Stdio transport for command: ${command}`);
|
|
4195
|
-
try {
|
|
4196
|
-
this.transport = new stdio_js$1.StdioClientTransport({
|
|
4197
|
-
command,
|
|
4198
|
-
args: this.serverConfig.args,
|
|
4199
|
-
env: { ...stdio_js$1.getDefaultEnvironment(), ...this.serverConfig.env || {} }
|
|
4200
|
-
});
|
|
4201
|
-
await this.client.connect(this.transport, { timeout: this.serverConfig.timeout ?? this.timeout });
|
|
4202
|
-
this.log("debug", `Successfully connected to MCP server via Stdio`);
|
|
4203
|
-
} catch (e) {
|
|
4204
|
-
this.log("error", e instanceof Error ? e.stack || e.message : JSON.stringify(e));
|
|
4205
|
-
throw e;
|
|
4206
|
-
}
|
|
4207
|
-
}
|
|
4208
|
-
async connectHttp(url) {
|
|
4209
|
-
const { requestInit, eventSourceInit } = this.serverConfig;
|
|
4210
|
-
this.log("debug", `Attempting to connect to URL: ${url}`);
|
|
4211
|
-
let shouldTrySSE = url.pathname.endsWith(`/sse`);
|
|
4212
|
-
if (!shouldTrySSE) {
|
|
4213
|
-
try {
|
|
4214
|
-
this.log("debug", "Trying Streamable HTTP transport...");
|
|
4215
|
-
const streamableTransport = new streamableHttp_js.StreamableHTTPClientTransport(url, {
|
|
4216
|
-
requestInit,
|
|
4217
|
-
reconnectionOptions: this.serverConfig.reconnectionOptions,
|
|
4218
|
-
sessionId: this.serverConfig.sessionId
|
|
4219
|
-
});
|
|
4220
|
-
await this.client.connect(streamableTransport, {
|
|
4221
|
-
timeout: (
|
|
4222
|
-
// this is hardcoded to 3s because the long default timeout would be extremely slow for sse backwards compat (60s)
|
|
4223
|
-
3e3
|
|
4224
|
-
)
|
|
4225
|
-
});
|
|
4226
|
-
this.transport = streamableTransport;
|
|
4227
|
-
this.log("debug", "Successfully connected using Streamable HTTP transport.");
|
|
4228
|
-
} catch (error) {
|
|
4229
|
-
this.log("debug", `Streamable HTTP transport failed: ${error}`);
|
|
4230
|
-
shouldTrySSE = true;
|
|
4231
|
-
}
|
|
4232
|
-
}
|
|
4233
|
-
if (shouldTrySSE) {
|
|
4234
|
-
this.log("debug", "Falling back to deprecated HTTP+SSE transport...");
|
|
4235
|
-
try {
|
|
4236
|
-
const sseTransport = new sse_js$1.SSEClientTransport(url, { requestInit, eventSourceInit });
|
|
4237
|
-
await this.client.connect(sseTransport, { timeout: this.serverConfig.timeout ?? this.timeout });
|
|
4238
|
-
this.transport = sseTransport;
|
|
4239
|
-
this.log("debug", "Successfully connected using deprecated HTTP+SSE transport.");
|
|
4240
|
-
} catch (sseError) {
|
|
4241
|
-
this.log(
|
|
4242
|
-
"error",
|
|
4243
|
-
`Failed to connect with SSE transport after failing to connect to Streamable HTTP transport first. SSE error: ${sseError}`
|
|
4244
|
-
);
|
|
4245
|
-
throw new Error("Could not connect to server with any available HTTP transport");
|
|
4246
|
-
}
|
|
4247
|
-
}
|
|
4248
|
-
}
|
|
4249
|
-
isConnected = false;
|
|
4250
|
-
async connect() {
|
|
4251
|
-
if (this.isConnected) return;
|
|
4252
|
-
const { command, url } = this.serverConfig;
|
|
4253
|
-
if (command) {
|
|
4254
|
-
await this.connectStdio(command);
|
|
4255
|
-
} else if (url) {
|
|
4256
|
-
await this.connectHttp(url);
|
|
4257
|
-
} else {
|
|
4258
|
-
throw new Error("Server configuration must include either a command or a url.");
|
|
4259
|
-
}
|
|
4260
|
-
this.isConnected = true;
|
|
4261
|
-
const originalOnClose = this.client.onclose;
|
|
4262
|
-
this.client.onclose = () => {
|
|
4263
|
-
this.log("debug", `MCP server connection closed`);
|
|
4264
|
-
this.isConnected = false;
|
|
4265
|
-
if (typeof originalOnClose === `function`) {
|
|
4266
|
-
originalOnClose();
|
|
4267
|
-
}
|
|
4268
|
-
};
|
|
4269
|
-
exitHook.asyncExitHook(
|
|
4270
|
-
async () => {
|
|
4271
|
-
this.log("debug", `Disconnecting MCP server during exit`);
|
|
4272
|
-
await this.disconnect();
|
|
4273
|
-
},
|
|
4274
|
-
{ wait: 5e3 }
|
|
4275
|
-
);
|
|
4276
|
-
process.on("SIGTERM", () => exitHook.gracefulExit());
|
|
4277
|
-
this.log("debug", `Successfully connected to MCP server`);
|
|
4278
|
-
}
|
|
4279
|
-
/**
|
|
4280
|
-
* Get the current session ID if using the Streamable HTTP transport.
|
|
4281
|
-
* Returns undefined if not connected or not using Streamable HTTP.
|
|
4282
|
-
*/
|
|
4283
|
-
get sessionId() {
|
|
4284
|
-
if (this.transport instanceof streamableHttp_js.StreamableHTTPClientTransport) {
|
|
4285
|
-
return this.transport.sessionId;
|
|
4286
|
-
}
|
|
4287
|
-
return void 0;
|
|
4288
|
-
}
|
|
4289
|
-
async disconnect() {
|
|
4290
|
-
if (!this.transport) {
|
|
4291
|
-
this.log("debug", "Disconnect called but no transport was connected.");
|
|
4292
|
-
return;
|
|
4293
|
-
}
|
|
4294
|
-
this.log("debug", `Disconnecting from MCP server`);
|
|
4295
|
-
try {
|
|
4296
|
-
await this.transport.close();
|
|
4297
|
-
this.log("debug", "Successfully disconnected from MCP server");
|
|
4298
|
-
} catch (e) {
|
|
4299
|
-
this.log("error", "Error during MCP server disconnect", {
|
|
4300
|
-
error: e instanceof Error ? e.stack : JSON.stringify(e, null, 2)
|
|
4301
|
-
});
|
|
4302
|
-
throw e;
|
|
4303
|
-
} finally {
|
|
4304
|
-
this.transport = void 0;
|
|
4305
|
-
this.isConnected = false;
|
|
4306
|
-
}
|
|
4307
|
-
}
|
|
4308
|
-
// TODO: do the type magic to return the right method type. Right now we get infinitely deep infered type errors from Zod without using "any"
|
|
4309
|
-
async resources() {
|
|
4310
|
-
this.log("debug", `Requesting resources from MCP server`);
|
|
4311
|
-
return await this.client.request({ method: "resources/list" }, types_js.ListResourcesResultSchema, {
|
|
4312
|
-
timeout: this.timeout
|
|
4313
|
-
});
|
|
4314
|
-
}
|
|
4315
|
-
async tools() {
|
|
4316
|
-
this.log("debug", `Requesting tools from MCP server`);
|
|
4317
|
-
const { tools: tools$1 } = await this.client.listTools({ timeout: this.timeout });
|
|
4318
|
-
const toolsRes = {};
|
|
4319
|
-
tools$1.forEach((tool) => {
|
|
4320
|
-
this.log("debug", `Processing tool: ${tool.name}`);
|
|
4321
|
-
const s = utils.jsonSchemaToModel(tool.inputSchema);
|
|
4322
|
-
const mastraTool = tools.createTool({
|
|
4323
|
-
id: `${this.name}_${tool.name}`,
|
|
4324
|
-
description: tool.description || "",
|
|
4325
|
-
inputSchema: s,
|
|
4326
|
-
execute: async ({ context }) => {
|
|
4327
|
-
try {
|
|
4328
|
-
this.log("debug", `Executing tool: ${tool.name}`, { toolArgs: context });
|
|
4329
|
-
const res = await this.client.callTool(
|
|
4330
|
-
{
|
|
4331
|
-
name: tool.name,
|
|
4332
|
-
arguments: context
|
|
4333
|
-
},
|
|
4334
|
-
types_js.CallToolResultSchema,
|
|
4335
|
-
{
|
|
4336
|
-
timeout: this.timeout
|
|
4337
|
-
}
|
|
4338
|
-
);
|
|
4339
|
-
this.log("debug", `Tool executed successfully: ${tool.name}`);
|
|
4340
|
-
return res;
|
|
4341
|
-
} catch (e) {
|
|
4342
|
-
this.log("error", `Error calling tool: ${tool.name}`, {
|
|
4343
|
-
error: e instanceof Error ? e.stack : JSON.stringify(e, null, 2),
|
|
4344
|
-
toolArgs: context
|
|
4345
|
-
});
|
|
4346
|
-
throw e;
|
|
4347
|
-
}
|
|
4348
|
-
}
|
|
4349
|
-
});
|
|
4350
|
-
if (tool.name) {
|
|
4351
|
-
toolsRes[tool.name] = mastraTool;
|
|
4352
|
-
}
|
|
4353
|
-
});
|
|
4354
|
-
return toolsRes;
|
|
4355
|
-
}
|
|
4356
|
-
};
|
|
4357
|
-
var MastraMCPClient = class extends InternalMastraMCPClient {
|
|
4358
|
-
constructor(args) {
|
|
4359
|
-
super(args);
|
|
4360
|
-
this.logger.warn(
|
|
4361
|
-
"[DEPRECATION] MastraMCPClient is deprecated and will be removed in a future release. Please use MCPClient instead."
|
|
4362
|
-
);
|
|
4363
|
-
}
|
|
4364
|
-
};
|
|
4365
|
-
var mcpClientInstances = /* @__PURE__ */ new Map();
|
|
4366
|
-
var MCPClient = class extends base.MastraBase {
|
|
4367
|
-
serverConfigs = {};
|
|
4368
|
-
id;
|
|
4369
|
-
defaultTimeout;
|
|
4370
|
-
mcpClientsById = /* @__PURE__ */ new Map();
|
|
4371
|
-
disconnectPromise = null;
|
|
4372
|
-
constructor(args) {
|
|
4373
|
-
super({ name: "MCPClient" });
|
|
4374
|
-
this.defaultTimeout = args.timeout ?? protocol_js.DEFAULT_REQUEST_TIMEOUT_MSEC;
|
|
4375
|
-
this.serverConfigs = args.servers;
|
|
4376
|
-
this.id = args.id ?? this.makeId();
|
|
4377
|
-
if (args.id) {
|
|
4378
|
-
this.id = args.id;
|
|
4379
|
-
const cached = mcpClientInstances.get(this.id);
|
|
4380
|
-
if (cached && !equal__default.default(cached.serverConfigs, args.servers)) {
|
|
4381
|
-
const existingInstance2 = mcpClientInstances.get(this.id);
|
|
4382
|
-
if (existingInstance2) {
|
|
4383
|
-
void existingInstance2.disconnect();
|
|
4384
|
-
}
|
|
4385
|
-
}
|
|
4386
|
-
} else {
|
|
4387
|
-
this.id = this.makeId();
|
|
4388
|
-
}
|
|
4389
|
-
const existingInstance = mcpClientInstances.get(this.id);
|
|
4390
|
-
if (existingInstance) {
|
|
4391
|
-
if (!args.id) {
|
|
4392
|
-
throw new Error(`MCPClient was initialized multiple times with the same configuration options.
|
|
4393
|
-
|
|
4394
|
-
This error is intended to prevent memory leaks.
|
|
4395
|
-
|
|
4396
|
-
To fix this you have three different options:
|
|
4397
|
-
1. If you need multiple MCPClient class instances with identical server configurations, set an id when configuring: new MCPClient({ id: "my-unique-id" })
|
|
4398
|
-
2. Call "await client.disconnect()" after you're done using the client and before you recreate another instance with the same options. If the identical MCPClient instance is already closed at the time of re-creating it, you will not see this error.
|
|
4399
|
-
3. If you only need one instance of MCPClient in your app, refactor your code so it's only created one time (ex. move it out of a loop into a higher scope code block)
|
|
4400
|
-
`);
|
|
4401
|
-
}
|
|
4402
|
-
return existingInstance;
|
|
4403
|
-
}
|
|
4404
|
-
mcpClientInstances.set(this.id, this);
|
|
4405
|
-
this.addToInstanceCache();
|
|
4406
|
-
return this;
|
|
4407
|
-
}
|
|
4408
|
-
addToInstanceCache() {
|
|
4409
|
-
if (!mcpClientInstances.has(this.id)) {
|
|
4410
|
-
mcpClientInstances.set(this.id, this);
|
|
4411
|
-
}
|
|
4412
|
-
}
|
|
4413
|
-
makeId() {
|
|
4414
|
-
const text = JSON.stringify(this.serverConfigs).normalize("NFKC");
|
|
4415
|
-
const idNamespace = uuid.v5(`MCPClient`, uuid.v5.DNS);
|
|
4416
|
-
return uuid.v5(text, idNamespace);
|
|
4417
|
-
}
|
|
4418
|
-
async disconnect() {
|
|
4419
|
-
if (this.disconnectPromise) {
|
|
4420
|
-
return this.disconnectPromise;
|
|
4421
|
-
}
|
|
4422
|
-
this.disconnectPromise = (async () => {
|
|
4423
|
-
try {
|
|
4424
|
-
mcpClientInstances.delete(this.id);
|
|
4425
|
-
await Promise.all(Array.from(this.mcpClientsById.values()).map((client) => client.disconnect()));
|
|
4426
|
-
this.mcpClientsById.clear();
|
|
4427
|
-
} finally {
|
|
4428
|
-
this.disconnectPromise = null;
|
|
4429
|
-
}
|
|
4430
|
-
})();
|
|
4431
|
-
return this.disconnectPromise;
|
|
4432
|
-
}
|
|
4433
|
-
async getTools() {
|
|
4434
|
-
this.addToInstanceCache();
|
|
4435
|
-
const connectedTools = {};
|
|
4436
|
-
await this.eachClientTools(async ({ serverName, tools }) => {
|
|
4437
|
-
for (const [toolName, toolConfig] of Object.entries(tools)) {
|
|
4438
|
-
connectedTools[`${serverName}_${toolName}`] = toolConfig;
|
|
4439
|
-
}
|
|
4440
|
-
});
|
|
4441
|
-
return connectedTools;
|
|
4442
|
-
}
|
|
4443
|
-
async getToolsets() {
|
|
4444
|
-
this.addToInstanceCache();
|
|
4445
|
-
const connectedToolsets = {};
|
|
4446
|
-
await this.eachClientTools(async ({ serverName, tools }) => {
|
|
4447
|
-
if (tools) {
|
|
4448
|
-
connectedToolsets[serverName] = tools;
|
|
4449
|
-
}
|
|
4450
|
-
});
|
|
4451
|
-
return connectedToolsets;
|
|
4452
|
-
}
|
|
4453
|
-
/**
|
|
4454
|
-
* Get the current session IDs for all connected MCP clients using the Streamable HTTP transport.
|
|
4455
|
-
* Returns an object mapping server names to their session IDs.
|
|
4456
|
-
*/
|
|
4457
|
-
get sessionIds() {
|
|
4458
|
-
const sessionIds = {};
|
|
4459
|
-
for (const [serverName, client] of this.mcpClientsById.entries()) {
|
|
4460
|
-
if (client.sessionId) {
|
|
4461
|
-
sessionIds[serverName] = client.sessionId;
|
|
4462
|
-
}
|
|
4463
|
-
}
|
|
4464
|
-
return sessionIds;
|
|
4465
|
-
}
|
|
4466
|
-
async getConnectedClient(name, config) {
|
|
4467
|
-
if (this.disconnectPromise) {
|
|
4468
|
-
await this.disconnectPromise;
|
|
4469
|
-
}
|
|
4470
|
-
const exists = this.mcpClientsById.has(name);
|
|
4471
|
-
const existingClient = this.mcpClientsById.get(name);
|
|
4472
|
-
if (exists) {
|
|
4473
|
-
if (!existingClient) {
|
|
4474
|
-
throw new Error(`Client ${name} exists but is undefined`);
|
|
4475
|
-
}
|
|
4476
|
-
await existingClient.connect();
|
|
4477
|
-
return existingClient;
|
|
4478
|
-
}
|
|
4479
|
-
this.logger.debug(`Connecting to ${name} MCP server`);
|
|
4480
|
-
const mcpClient = new InternalMastraMCPClient({
|
|
4481
|
-
name,
|
|
4482
|
-
server: config,
|
|
4483
|
-
timeout: config.timeout ?? this.defaultTimeout
|
|
4484
|
-
});
|
|
4485
|
-
this.mcpClientsById.set(name, mcpClient);
|
|
4486
|
-
try {
|
|
4487
|
-
await mcpClient.connect();
|
|
4488
|
-
} catch (e) {
|
|
4489
|
-
this.mcpClientsById.delete(name);
|
|
4490
|
-
this.logger.error(`MCPClient errored connecting to MCP server ${name}`, {
|
|
4491
|
-
error: e instanceof Error ? e.message : String(e)
|
|
4492
|
-
});
|
|
4493
|
-
throw new Error(
|
|
4494
|
-
`Failed to connect to MCP server ${name}: ${e instanceof Error ? e.stack || e.message : String(e)}`
|
|
4495
|
-
);
|
|
4496
|
-
}
|
|
4497
|
-
this.logger.debug(`Connected to ${name} MCP server`);
|
|
4498
|
-
return mcpClient;
|
|
4499
|
-
}
|
|
4500
|
-
async eachClientTools(cb) {
|
|
4501
|
-
await Promise.all(
|
|
4502
|
-
Object.entries(this.serverConfigs).map(async ([serverName, serverConfig]) => {
|
|
4503
|
-
const client = await this.getConnectedClient(serverName, serverConfig);
|
|
4504
|
-
const tools = await client.tools();
|
|
4505
|
-
await cb({ serverName, tools, client });
|
|
4506
|
-
})
|
|
4507
|
-
);
|
|
4508
|
-
}
|
|
4509
|
-
};
|
|
4510
|
-
var MCPConfiguration = class extends MCPClient {
|
|
4511
|
-
constructor(args) {
|
|
4512
|
-
super(args);
|
|
4513
|
-
this.logger.warn(
|
|
4514
|
-
`MCPConfiguration has been renamed to MCPClient and MCPConfiguration is deprecated. The API is identical but the MCPConfiguration export will be removed in the future. Update your imports now to prevent future errors.`
|
|
4515
|
-
);
|
|
4516
|
-
}
|
|
4517
|
-
};
|
|
4518
|
-
|
|
4519
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseAnyOf.js
|
|
4520
|
-
var parseAnyOf = (schema, refs) => {
|
|
4521
|
-
return schema.anyOf.length ? schema.anyOf.length === 1 ? parseSchema(schema.anyOf[0], {
|
|
4522
|
-
...refs,
|
|
4523
|
-
path: [...refs.path, "anyOf", 0]
|
|
4524
|
-
}) : `z.union([${schema.anyOf.map((schema2, i) => parseSchema(schema2, { ...refs, path: [...refs.path, "anyOf", i] })).join(", ")}])` : `z.any()`;
|
|
4525
|
-
};
|
|
4526
|
-
|
|
4527
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseBoolean.js
|
|
4528
|
-
var parseBoolean = (_schema) => {
|
|
4529
|
-
return "z.boolean()";
|
|
4530
|
-
};
|
|
4531
|
-
|
|
4532
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseDefault.js
|
|
4533
|
-
var parseDefault = (_schema) => {
|
|
4534
|
-
return "z.any()";
|
|
4535
|
-
};
|
|
4536
|
-
|
|
4537
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseMultipleType.js
|
|
4538
|
-
var parseMultipleType = (schema, refs) => {
|
|
4539
|
-
return `z.union([${schema.type.map((type) => parseSchema({ ...schema, type }, refs)).join(", ")}])`;
|
|
4540
|
-
};
|
|
4541
|
-
|
|
4542
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseNot.js
|
|
4543
|
-
var parseNot = (schema, refs) => {
|
|
4544
|
-
return `z.any().refine((value) => !${parseSchema(schema.not, {
|
|
4545
|
-
...refs,
|
|
4546
|
-
path: [...refs.path, "not"]
|
|
4547
|
-
})}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`;
|
|
4548
|
-
};
|
|
4549
|
-
|
|
4550
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseNull.js
|
|
4551
|
-
var parseNull = (_schema) => {
|
|
4552
|
-
return "z.null()";
|
|
4553
|
-
};
|
|
4554
|
-
|
|
4555
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/utils/half.js
|
|
4556
|
-
var half = (arr) => {
|
|
4557
|
-
return [arr.slice(0, arr.length / 2), arr.slice(arr.length / 2)];
|
|
4558
|
-
};
|
|
4559
|
-
|
|
4560
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseAllOf.js
|
|
4561
|
-
var originalIndex = Symbol("Original index");
|
|
4562
|
-
var ensureOriginalIndex = (arr) => {
|
|
4563
|
-
let newArr = [];
|
|
4564
|
-
for (let i = 0; i < arr.length; i++) {
|
|
4565
|
-
const item = arr[i];
|
|
4566
|
-
if (typeof item === "boolean") {
|
|
4567
|
-
newArr.push(item ? { [originalIndex]: i } : { [originalIndex]: i, not: {} });
|
|
4568
|
-
} else if (originalIndex in item) {
|
|
4569
|
-
return arr;
|
|
4570
|
-
} else {
|
|
4571
|
-
newArr.push({ ...item, [originalIndex]: i });
|
|
4572
|
-
}
|
|
4573
|
-
}
|
|
4574
|
-
return newArr;
|
|
4575
|
-
};
|
|
4576
|
-
function parseAllOf(schema, refs) {
|
|
4577
|
-
if (schema.allOf.length === 0) {
|
|
4578
|
-
return "z.never()";
|
|
4579
|
-
} else if (schema.allOf.length === 1) {
|
|
4580
|
-
const item = schema.allOf[0];
|
|
4581
|
-
return parseSchema(item, {
|
|
4582
|
-
...refs,
|
|
4583
|
-
path: [...refs.path, "allOf", item[originalIndex]]
|
|
4584
|
-
});
|
|
4585
|
-
} else {
|
|
4586
|
-
const [left, right] = half(ensureOriginalIndex(schema.allOf));
|
|
4587
|
-
return `z.intersection(${parseAllOf({ allOf: left }, refs)}, ${parseAllOf({
|
|
4588
|
-
allOf: right
|
|
4589
|
-
}, refs)})`;
|
|
4590
|
-
}
|
|
4591
|
-
}
|
|
4592
|
-
|
|
4593
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/utils/withMessage.js
|
|
4594
|
-
function withMessage(schema, key, get) {
|
|
4595
|
-
const value = schema[key];
|
|
4596
|
-
let r = "";
|
|
4597
|
-
if (value !== void 0) {
|
|
4598
|
-
const got = get({ value, json: JSON.stringify(value) });
|
|
4599
|
-
if (got) {
|
|
4600
|
-
const opener = got[0];
|
|
4601
|
-
const prefix = got.length === 3 ? got[1] : "";
|
|
4602
|
-
const closer = got.length === 3 ? got[2] : got[1];
|
|
4603
|
-
r += opener;
|
|
4604
|
-
if (schema.errorMessage?.[key] !== void 0) {
|
|
4605
|
-
r += prefix + JSON.stringify(schema.errorMessage[key]);
|
|
4606
|
-
}
|
|
4607
|
-
r += closer;
|
|
4608
|
-
}
|
|
4609
|
-
}
|
|
4610
|
-
return r;
|
|
4611
|
-
}
|
|
4612
|
-
|
|
4613
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseArray.js
|
|
4614
|
-
var parseArray = (schema, refs) => {
|
|
4615
|
-
if (Array.isArray(schema.items)) {
|
|
4616
|
-
return `z.tuple([${schema.items.map((v, i) => parseSchema(v, { ...refs, path: [...refs.path, "items", i] }))}])`;
|
|
4617
|
-
}
|
|
4618
|
-
let r = !schema.items ? "z.array(z.any())" : `z.array(${parseSchema(schema.items, {
|
|
4619
|
-
...refs,
|
|
4620
|
-
path: [...refs.path, "items"]
|
|
4621
|
-
})})`;
|
|
4622
|
-
r += withMessage(schema, "minItems", ({ json }) => [
|
|
4623
|
-
`.min(${json}`,
|
|
4624
|
-
", ",
|
|
4625
|
-
")"
|
|
4626
|
-
]);
|
|
4627
|
-
r += withMessage(schema, "maxItems", ({ json }) => [
|
|
4628
|
-
`.max(${json}`,
|
|
4629
|
-
", ",
|
|
4630
|
-
")"
|
|
4631
|
-
]);
|
|
4632
|
-
return r;
|
|
4633
|
-
};
|
|
4634
|
-
|
|
4635
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseConst.js
|
|
4636
|
-
var parseConst = (schema) => {
|
|
4637
|
-
return `z.literal(${JSON.stringify(schema.const)})`;
|
|
4638
|
-
};
|
|
4639
|
-
|
|
4640
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseEnum.js
|
|
4641
|
-
var parseEnum = (schema) => {
|
|
4642
|
-
if (schema.enum.length === 0) {
|
|
4643
|
-
return "z.never()";
|
|
4644
|
-
} else if (schema.enum.length === 1) {
|
|
4645
|
-
return `z.literal(${JSON.stringify(schema.enum[0])})`;
|
|
4646
|
-
} else if (schema.enum.every((x) => typeof x === "string")) {
|
|
4647
|
-
return `z.enum([${schema.enum.map((x) => JSON.stringify(x))}])`;
|
|
4648
|
-
} else {
|
|
4649
|
-
return `z.union([${schema.enum.map((x) => `z.literal(${JSON.stringify(x)})`).join(", ")}])`;
|
|
4650
|
-
}
|
|
4651
|
-
};
|
|
4652
|
-
|
|
4653
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseIfThenElse.js
|
|
4654
|
-
var parseIfThenElse = (schema, refs) => {
|
|
4655
|
-
const $if = parseSchema(schema.if, { ...refs, path: [...refs.path, "if"] });
|
|
4656
|
-
const $then = parseSchema(schema.then, {
|
|
4657
|
-
...refs,
|
|
4658
|
-
path: [...refs.path, "then"]
|
|
4659
|
-
});
|
|
4660
|
-
const $else = parseSchema(schema.else, {
|
|
4661
|
-
...refs,
|
|
4662
|
-
path: [...refs.path, "else"]
|
|
4663
|
-
});
|
|
4664
|
-
return `z.union([${$then}, ${$else}]).superRefine((value,ctx) => {
|
|
4665
|
-
const result = ${$if}.safeParse(value).success
|
|
4666
|
-
? ${$then}.safeParse(value)
|
|
4667
|
-
: ${$else}.safeParse(value);
|
|
4668
|
-
if (!result.success) {
|
|
4669
|
-
result.error.errors.forEach((error) => ctx.addIssue(error))
|
|
4670
|
-
}
|
|
4671
|
-
})`;
|
|
4672
|
-
};
|
|
4673
|
-
|
|
4674
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseNumber.js
|
|
4675
|
-
var parseNumber = (schema) => {
|
|
4676
|
-
let r = "z.number()";
|
|
4677
|
-
if (schema.type === "integer") {
|
|
4678
|
-
r += withMessage(schema, "type", () => [".int(", ")"]);
|
|
4679
|
-
} else {
|
|
4680
|
-
r += withMessage(schema, "format", ({ value }) => {
|
|
4681
|
-
if (value === "int64") {
|
|
4682
|
-
return [".int(", ")"];
|
|
4778
|
+
this.name = name;
|
|
4779
|
+
this.timeout = timeout;
|
|
4780
|
+
this.logHandler = server.logger;
|
|
4781
|
+
this.enableServerLogs = server.enableServerLogs ?? true;
|
|
4782
|
+
this.serverConfig = server;
|
|
4783
|
+
this.client = new index_js$1.Client(
|
|
4784
|
+
{
|
|
4785
|
+
name,
|
|
4786
|
+
version
|
|
4787
|
+
},
|
|
4788
|
+
{
|
|
4789
|
+
capabilities
|
|
4683
4790
|
}
|
|
4684
|
-
|
|
4791
|
+
);
|
|
4792
|
+
this.setupLogging();
|
|
4685
4793
|
}
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
|
|
4692
|
-
|
|
4693
|
-
|
|
4694
|
-
|
|
4695
|
-
|
|
4696
|
-
if (
|
|
4697
|
-
|
|
4698
|
-
|
|
4699
|
-
|
|
4700
|
-
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
`.gte(${json}`,
|
|
4705
|
-
", ",
|
|
4706
|
-
")"
|
|
4707
|
-
]);
|
|
4794
|
+
/**
|
|
4795
|
+
* Log a message at the specified level
|
|
4796
|
+
* @param level Log level
|
|
4797
|
+
* @param message Log message
|
|
4798
|
+
* @param details Optional additional details
|
|
4799
|
+
*/
|
|
4800
|
+
log(level, message, details) {
|
|
4801
|
+
const loggerMethod = convertLogLevelToLoggerMethod(level);
|
|
4802
|
+
const msg = `[${this.name}] ${message}`;
|
|
4803
|
+
this.logger[loggerMethod](msg, details);
|
|
4804
|
+
if (this.logHandler) {
|
|
4805
|
+
this.logHandler({
|
|
4806
|
+
level,
|
|
4807
|
+
message: msg,
|
|
4808
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
4809
|
+
serverName: this.name,
|
|
4810
|
+
details
|
|
4811
|
+
});
|
|
4708
4812
|
}
|
|
4709
|
-
} else if (typeof schema.exclusiveMinimum === "number") {
|
|
4710
|
-
r += withMessage(schema, "exclusiveMinimum", ({ json }) => [
|
|
4711
|
-
`.gt(${json}`,
|
|
4712
|
-
", ",
|
|
4713
|
-
")"
|
|
4714
|
-
]);
|
|
4715
4813
|
}
|
|
4716
|
-
|
|
4717
|
-
if (
|
|
4718
|
-
|
|
4719
|
-
|
|
4720
|
-
|
|
4721
|
-
|
|
4722
|
-
|
|
4723
|
-
|
|
4724
|
-
|
|
4725
|
-
|
|
4726
|
-
|
|
4727
|
-
|
|
4728
|
-
|
|
4814
|
+
setupLogging() {
|
|
4815
|
+
if (this.enableServerLogs) {
|
|
4816
|
+
this.client.setNotificationHandler(
|
|
4817
|
+
z.object({
|
|
4818
|
+
method: z.literal("notifications/message"),
|
|
4819
|
+
params: z.object({
|
|
4820
|
+
level: z.string()
|
|
4821
|
+
}).passthrough()
|
|
4822
|
+
}),
|
|
4823
|
+
(notification) => {
|
|
4824
|
+
const { level, ...params } = notification.params;
|
|
4825
|
+
this.log(level, "[MCP SERVER LOG]", params);
|
|
4826
|
+
}
|
|
4827
|
+
);
|
|
4729
4828
|
}
|
|
4730
|
-
} else if (typeof schema.exclusiveMaximum === "number") {
|
|
4731
|
-
r += withMessage(schema, "exclusiveMaximum", ({ json }) => [
|
|
4732
|
-
`.lt(${json}`,
|
|
4733
|
-
", ",
|
|
4734
|
-
")"
|
|
4735
|
-
]);
|
|
4736
4829
|
}
|
|
4737
|
-
|
|
4738
|
-
};
|
|
4739
|
-
|
|
4740
|
-
|
|
4741
|
-
|
|
4742
|
-
|
|
4743
|
-
|
|
4744
|
-
path: [...refs.path, "oneOf", 0]
|
|
4745
|
-
}) : `z.any().superRefine((x, ctx) => {
|
|
4746
|
-
const schemas = [${schema.oneOf.map((schema2, i) => parseSchema(schema2, {
|
|
4747
|
-
...refs,
|
|
4748
|
-
path: [...refs.path, "oneOf", i]
|
|
4749
|
-
})).join(", ")}];
|
|
4750
|
-
const errors = schemas.reduce<z.ZodError[]>(
|
|
4751
|
-
(errors, schema) =>
|
|
4752
|
-
((result) =>
|
|
4753
|
-
result.error ? [...errors, result.error] : errors)(
|
|
4754
|
-
schema.safeParse(x),
|
|
4755
|
-
),
|
|
4756
|
-
[],
|
|
4757
|
-
);
|
|
4758
|
-
if (schemas.length - errors.length !== 1) {
|
|
4759
|
-
ctx.addIssue({
|
|
4760
|
-
path: ctx.path,
|
|
4761
|
-
code: "invalid_union",
|
|
4762
|
-
unionErrors: errors,
|
|
4763
|
-
message: "Invalid input: Should pass single schema",
|
|
4830
|
+
async connectStdio(command) {
|
|
4831
|
+
this.log("debug", `Using Stdio transport for command: ${command}`);
|
|
4832
|
+
try {
|
|
4833
|
+
this.transport = new stdio_js$1.StdioClientTransport({
|
|
4834
|
+
command,
|
|
4835
|
+
args: this.serverConfig.args,
|
|
4836
|
+
env: { ...stdio_js$1.getDefaultEnvironment(), ...this.serverConfig.env || {} }
|
|
4764
4837
|
});
|
|
4765
|
-
|
|
4766
|
-
|
|
4767
|
-
}
|
|
4768
|
-
|
|
4769
|
-
|
|
4770
|
-
var expandJsdocs = (jsdocs) => {
|
|
4771
|
-
const lines = jsdocs.split("\n");
|
|
4772
|
-
const result = lines.length === 1 ? lines[0] : `
|
|
4773
|
-
${lines.map((x) => `* ${x}`).join("\n")}
|
|
4774
|
-
`;
|
|
4775
|
-
return `/**${result}*/
|
|
4776
|
-
`;
|
|
4777
|
-
};
|
|
4778
|
-
var addJsdocs = (schema, parsed) => {
|
|
4779
|
-
const description = schema.description;
|
|
4780
|
-
if (!description) {
|
|
4781
|
-
return parsed;
|
|
4782
|
-
}
|
|
4783
|
-
return `
|
|
4784
|
-
${expandJsdocs(description)}${parsed}`;
|
|
4785
|
-
};
|
|
4786
|
-
|
|
4787
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/parsers/parseObject.js
|
|
4788
|
-
function parseObject(objectSchema, refs) {
|
|
4789
|
-
let properties = void 0;
|
|
4790
|
-
if (objectSchema.properties) {
|
|
4791
|
-
if (!Object.keys(objectSchema.properties).length) {
|
|
4792
|
-
properties = "z.object({})";
|
|
4793
|
-
} else {
|
|
4794
|
-
properties = "z.object({ ";
|
|
4795
|
-
properties += Object.keys(objectSchema.properties).map((key) => {
|
|
4796
|
-
const propSchema = objectSchema.properties[key];
|
|
4797
|
-
let result = `${JSON.stringify(key)}: ${parseSchema(propSchema, {
|
|
4798
|
-
...refs,
|
|
4799
|
-
path: [...refs.path, "properties", key]
|
|
4800
|
-
})}`;
|
|
4801
|
-
if (refs.withJsdocs && typeof propSchema === "object") {
|
|
4802
|
-
result = addJsdocs(propSchema, result);
|
|
4803
|
-
}
|
|
4804
|
-
const hasDefault = typeof propSchema === "object" && propSchema.default !== void 0;
|
|
4805
|
-
const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : typeof propSchema === "object" && propSchema.required === true;
|
|
4806
|
-
const optional = !hasDefault && !required;
|
|
4807
|
-
return optional ? `${result}.optional()` : result;
|
|
4808
|
-
}).join(", ");
|
|
4809
|
-
properties += " })";
|
|
4838
|
+
await this.client.connect(this.transport, { timeout: this.serverConfig.timeout ?? this.timeout });
|
|
4839
|
+
this.log("debug", `Successfully connected to MCP server via Stdio`);
|
|
4840
|
+
} catch (e) {
|
|
4841
|
+
this.log("error", e instanceof Error ? e.stack || e.message : JSON.stringify(e));
|
|
4842
|
+
throw e;
|
|
4810
4843
|
}
|
|
4811
4844
|
}
|
|
4812
|
-
|
|
4813
|
-
|
|
4814
|
-
|
|
4815
|
-
|
|
4816
|
-
|
|
4817
|
-
|
|
4818
|
-
|
|
4819
|
-
|
|
4820
|
-
|
|
4821
|
-
|
|
4822
|
-
|
|
4823
|
-
|
|
4824
|
-
|
|
4825
|
-
|
|
4826
|
-
|
|
4827
|
-
|
|
4828
|
-
|
|
4829
|
-
|
|
4830
|
-
|
|
4831
|
-
|
|
4832
|
-
|
|
4833
|
-
|
|
4834
|
-
|
|
4835
|
-
patternProperties += `.catchall(z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
|
|
4836
|
-
} else {
|
|
4837
|
-
patternProperties += `.catchall(${Object.values(parsedPatternProperties)})`;
|
|
4838
|
-
}
|
|
4839
|
-
} else {
|
|
4840
|
-
if (additionalProperties) {
|
|
4841
|
-
patternProperties += `z.record(z.union([${[
|
|
4842
|
-
...Object.values(parsedPatternProperties),
|
|
4843
|
-
additionalProperties
|
|
4844
|
-
].join(", ")}]))`;
|
|
4845
|
-
} else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
4846
|
-
patternProperties += `z.record(z.union([${Object.values(parsedPatternProperties).join(", ")}]))`;
|
|
4847
|
-
} else {
|
|
4848
|
-
patternProperties += `z.record(${Object.values(parsedPatternProperties)})`;
|
|
4845
|
+
async connectHttp(url) {
|
|
4846
|
+
const { requestInit, eventSourceInit } = this.serverConfig;
|
|
4847
|
+
this.log("debug", `Attempting to connect to URL: ${url}`);
|
|
4848
|
+
let shouldTrySSE = url.pathname.endsWith(`/sse`);
|
|
4849
|
+
if (!shouldTrySSE) {
|
|
4850
|
+
try {
|
|
4851
|
+
this.log("debug", "Trying Streamable HTTP transport...");
|
|
4852
|
+
const streamableTransport = new streamableHttp_js.StreamableHTTPClientTransport(url, {
|
|
4853
|
+
requestInit,
|
|
4854
|
+
reconnectionOptions: this.serverConfig.reconnectionOptions,
|
|
4855
|
+
sessionId: this.serverConfig.sessionId
|
|
4856
|
+
});
|
|
4857
|
+
await this.client.connect(streamableTransport, {
|
|
4858
|
+
timeout: (
|
|
4859
|
+
// this is hardcoded to 3s because the long default timeout would be extremely slow for sse backwards compat (60s)
|
|
4860
|
+
3e3
|
|
4861
|
+
)
|
|
4862
|
+
});
|
|
4863
|
+
this.transport = streamableTransport;
|
|
4864
|
+
this.log("debug", "Successfully connected using Streamable HTTP transport.");
|
|
4865
|
+
} catch (error) {
|
|
4866
|
+
this.log("debug", `Streamable HTTP transport failed: ${error}`);
|
|
4867
|
+
shouldTrySSE = true;
|
|
4849
4868
|
}
|
|
4850
4869
|
}
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4870
|
+
if (shouldTrySSE) {
|
|
4871
|
+
this.log("debug", "Falling back to deprecated HTTP+SSE transport...");
|
|
4872
|
+
try {
|
|
4873
|
+
const sseTransport = new sse_js$1.SSEClientTransport(url, { requestInit, eventSourceInit });
|
|
4874
|
+
await this.client.connect(sseTransport, { timeout: this.serverConfig.timeout ?? this.timeout });
|
|
4875
|
+
this.transport = sseTransport;
|
|
4876
|
+
this.log("debug", "Successfully connected using deprecated HTTP+SSE transport.");
|
|
4877
|
+
} catch (sseError) {
|
|
4878
|
+
this.log(
|
|
4879
|
+
"error",
|
|
4880
|
+
`Failed to connect with SSE transport after failing to connect to Streamable HTTP transport first. SSE error: ${sseError}`
|
|
4881
|
+
);
|
|
4882
|
+
throw new Error("Could not connect to server with any available HTTP transport");
|
|
4860
4883
|
}
|
|
4861
4884
|
}
|
|
4862
|
-
|
|
4863
|
-
|
|
4864
|
-
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4869
|
-
|
|
4870
|
-
|
|
4871
|
-
|
|
4872
|
-
|
|
4873
|
-
params: {
|
|
4874
|
-
issues: result.error.issues
|
|
4875
|
-
}
|
|
4876
|
-
})
|
|
4877
|
-
`;
|
|
4878
|
-
patternProperties += "}\n";
|
|
4879
|
-
patternProperties += "}\n";
|
|
4885
|
+
}
|
|
4886
|
+
isConnected = false;
|
|
4887
|
+
async connect() {
|
|
4888
|
+
if (this.isConnected) return;
|
|
4889
|
+
const { command, url } = this.serverConfig;
|
|
4890
|
+
if (command) {
|
|
4891
|
+
await this.connectStdio(command);
|
|
4892
|
+
} else if (url) {
|
|
4893
|
+
await this.connectHttp(url);
|
|
4894
|
+
} else {
|
|
4895
|
+
throw new Error("Server configuration must include either a command or a url.");
|
|
4880
4896
|
}
|
|
4881
|
-
|
|
4882
|
-
|
|
4883
|
-
|
|
4884
|
-
|
|
4885
|
-
|
|
4886
|
-
|
|
4887
|
-
|
|
4888
|
-
|
|
4889
|
-
|
|
4890
|
-
|
|
4891
|
-
|
|
4892
|
-
|
|
4893
|
-
|
|
4894
|
-
|
|
4895
|
-
|
|
4897
|
+
this.isConnected = true;
|
|
4898
|
+
const originalOnClose = this.client.onclose;
|
|
4899
|
+
this.client.onclose = () => {
|
|
4900
|
+
this.log("debug", `MCP server connection closed`);
|
|
4901
|
+
this.isConnected = false;
|
|
4902
|
+
if (typeof originalOnClose === `function`) {
|
|
4903
|
+
originalOnClose();
|
|
4904
|
+
}
|
|
4905
|
+
};
|
|
4906
|
+
exitHook.asyncExitHook(
|
|
4907
|
+
async () => {
|
|
4908
|
+
this.log("debug", `Disconnecting MCP server during exit`);
|
|
4909
|
+
await this.disconnect();
|
|
4910
|
+
},
|
|
4911
|
+
{ wait: 5e3 }
|
|
4912
|
+
);
|
|
4913
|
+
process.on("SIGTERM", () => exitHook.gracefulExit());
|
|
4914
|
+
this.log("debug", `Successfully connected to MCP server`);
|
|
4915
|
+
}
|
|
4916
|
+
/**
|
|
4917
|
+
* Get the current session ID if using the Streamable HTTP transport.
|
|
4918
|
+
* Returns undefined if not connected or not using Streamable HTTP.
|
|
4919
|
+
*/
|
|
4920
|
+
get sessionId() {
|
|
4921
|
+
if (this.transport instanceof streamableHttp_js.StreamableHTTPClientTransport) {
|
|
4922
|
+
return this.transport.sessionId;
|
|
4896
4923
|
}
|
|
4897
|
-
|
|
4898
|
-
patternProperties += "})";
|
|
4924
|
+
return void 0;
|
|
4899
4925
|
}
|
|
4900
|
-
|
|
4901
|
-
|
|
4902
|
-
|
|
4903
|
-
|
|
4904
|
-
}
|
|
4926
|
+
async disconnect() {
|
|
4927
|
+
if (!this.transport) {
|
|
4928
|
+
this.log("debug", "Disconnect called but no transport was connected.");
|
|
4929
|
+
return;
|
|
4930
|
+
}
|
|
4931
|
+
this.log("debug", `Disconnecting from MCP server`);
|
|
4932
|
+
try {
|
|
4933
|
+
await this.transport.close();
|
|
4934
|
+
this.log("debug", "Successfully disconnected from MCP server");
|
|
4935
|
+
} catch (e) {
|
|
4936
|
+
this.log("error", "Error during MCP server disconnect", {
|
|
4937
|
+
error: e instanceof Error ? e.stack : JSON.stringify(e, null, 2)
|
|
4938
|
+
});
|
|
4939
|
+
throw e;
|
|
4940
|
+
} finally {
|
|
4941
|
+
this.transport = void 0;
|
|
4942
|
+
this.isConnected = false;
|
|
4943
|
+
}
|
|
4905
4944
|
}
|
|
4906
|
-
|
|
4907
|
-
|
|
4908
|
-
|
|
4909
|
-
},
|
|
4945
|
+
// TODO: do the type magic to return the right method type. Right now we get infinitely deep infered type errors from Zod without using "any"
|
|
4946
|
+
async resources() {
|
|
4947
|
+
this.log("debug", `Requesting resources from MCP server`);
|
|
4948
|
+
return await this.client.request({ method: "resources/list" }, types_js.ListResourcesResultSchema, {
|
|
4949
|
+
timeout: this.timeout
|
|
4950
|
+
});
|
|
4910
4951
|
}
|
|
4911
|
-
|
|
4912
|
-
|
|
4913
|
-
allOf: objectSchema.allOf.map((x) => typeof x === "object" && !x.type && (x.properties || x.additionalProperties || x.patternProperties) ? { ...x, type: "object" } : x)
|
|
4914
|
-
}, refs)})`;
|
|
4952
|
+
convertInputSchema(inputSchema) {
|
|
4953
|
+
return utils.isZodType(inputSchema) ? inputSchema : utils.resolveSerializedZodOutput(esm_default(inputSchema));
|
|
4915
4954
|
}
|
|
4916
|
-
|
|
4917
|
-
|
|
4918
|
-
|
|
4919
|
-
|
|
4920
|
-
|
|
4921
|
-
|
|
4922
|
-
|
|
4923
|
-
|
|
4924
|
-
|
|
4925
|
-
|
|
4926
|
-
|
|
4927
|
-
|
|
4928
|
-
|
|
4929
|
-
|
|
4930
|
-
|
|
4931
|
-
|
|
4932
|
-
|
|
4933
|
-
|
|
4934
|
-
|
|
4935
|
-
|
|
4936
|
-
|
|
4937
|
-
|
|
4938
|
-
|
|
4939
|
-
|
|
4940
|
-
|
|
4941
|
-
|
|
4942
|
-
|
|
4943
|
-
|
|
4944
|
-
|
|
4945
|
-
|
|
4946
|
-
|
|
4947
|
-
|
|
4948
|
-
|
|
4949
|
-
|
|
4950
|
-
|
|
4951
|
-
|
|
4952
|
-
]);
|
|
4953
|
-
r += withMessage(schema, "minLength", ({ json }) => [
|
|
4954
|
-
`.min(${json}`,
|
|
4955
|
-
", ",
|
|
4956
|
-
")"
|
|
4957
|
-
]);
|
|
4958
|
-
r += withMessage(schema, "maxLength", ({ json }) => [
|
|
4959
|
-
`.max(${json}`,
|
|
4960
|
-
", ",
|
|
4961
|
-
")"
|
|
4962
|
-
]);
|
|
4963
|
-
r += withMessage(schema, "contentEncoding", ({ value }) => {
|
|
4964
|
-
if (value === "base64") {
|
|
4965
|
-
return [".base64(", ")"];
|
|
4966
|
-
}
|
|
4967
|
-
});
|
|
4968
|
-
const contentMediaType = withMessage(schema, "contentMediaType", ({ value }) => {
|
|
4969
|
-
if (value === "application/json") {
|
|
4970
|
-
return [
|
|
4971
|
-
'.transform((str, ctx) => { try { return JSON.parse(str); } catch (err) { ctx.addIssue({ code: "custom", message: "Invalid JSON" }); }}',
|
|
4972
|
-
", ",
|
|
4973
|
-
")"
|
|
4974
|
-
];
|
|
4975
|
-
}
|
|
4976
|
-
});
|
|
4977
|
-
if (contentMediaType != "") {
|
|
4978
|
-
r += contentMediaType;
|
|
4979
|
-
r += withMessage(schema, "contentSchema", ({ value }) => {
|
|
4980
|
-
if (value && value instanceof Object) {
|
|
4981
|
-
return [
|
|
4982
|
-
`.pipe(${parseSchema(value)}`,
|
|
4983
|
-
", ",
|
|
4984
|
-
")"
|
|
4985
|
-
];
|
|
4955
|
+
async tools() {
|
|
4956
|
+
this.log("debug", `Requesting tools from MCP server`);
|
|
4957
|
+
const { tools: tools$1 } = await this.client.listTools({ timeout: this.timeout });
|
|
4958
|
+
const toolsRes = {};
|
|
4959
|
+
tools$1.forEach((tool) => {
|
|
4960
|
+
this.log("debug", `Processing tool: ${tool.name}`);
|
|
4961
|
+
const mastraTool = tools.createTool({
|
|
4962
|
+
id: `${this.name}_${tool.name}`,
|
|
4963
|
+
description: tool.description || "",
|
|
4964
|
+
inputSchema: this.convertInputSchema(tool.inputSchema),
|
|
4965
|
+
execute: async ({ context }) => {
|
|
4966
|
+
try {
|
|
4967
|
+
this.log("debug", `Executing tool: ${tool.name}`, { toolArgs: context });
|
|
4968
|
+
const res = await this.client.callTool(
|
|
4969
|
+
{
|
|
4970
|
+
name: tool.name,
|
|
4971
|
+
arguments: context
|
|
4972
|
+
},
|
|
4973
|
+
types_js.CallToolResultSchema,
|
|
4974
|
+
{
|
|
4975
|
+
timeout: this.timeout
|
|
4976
|
+
}
|
|
4977
|
+
);
|
|
4978
|
+
this.log("debug", `Tool executed successfully: ${tool.name}`);
|
|
4979
|
+
return res;
|
|
4980
|
+
} catch (e) {
|
|
4981
|
+
this.log("error", `Error calling tool: ${tool.name}`, {
|
|
4982
|
+
error: e instanceof Error ? e.stack : JSON.stringify(e, null, 2),
|
|
4983
|
+
toolArgs: context
|
|
4984
|
+
});
|
|
4985
|
+
throw e;
|
|
4986
|
+
}
|
|
4987
|
+
}
|
|
4988
|
+
});
|
|
4989
|
+
if (tool.name) {
|
|
4990
|
+
toolsRes[tool.name] = mastraTool;
|
|
4986
4991
|
}
|
|
4987
4992
|
});
|
|
4993
|
+
return toolsRes;
|
|
4988
4994
|
}
|
|
4989
|
-
return r;
|
|
4990
4995
|
};
|
|
4991
|
-
|
|
4992
|
-
|
|
4993
|
-
|
|
4994
|
-
|
|
4995
|
-
|
|
4996
|
+
var MastraMCPClient = class extends InternalMastraMCPClient {
|
|
4997
|
+
constructor(args) {
|
|
4998
|
+
super(args);
|
|
4999
|
+
this.logger.warn(
|
|
5000
|
+
"[DEPRECATION] MastraMCPClient is deprecated and will be removed in a future release. Please use MCPClient instead."
|
|
5001
|
+
);
|
|
4996
5002
|
}
|
|
4997
|
-
|
|
4998
|
-
|
|
5003
|
+
};
|
|
5004
|
+
var mcpClientInstances = /* @__PURE__ */ new Map();
|
|
5005
|
+
var MCPClient = class extends base.MastraBase {
|
|
5006
|
+
serverConfigs = {};
|
|
5007
|
+
id;
|
|
5008
|
+
defaultTimeout;
|
|
5009
|
+
mcpClientsById = /* @__PURE__ */ new Map();
|
|
5010
|
+
disconnectPromise = null;
|
|
5011
|
+
constructor(args) {
|
|
5012
|
+
super({ name: "MCPClient" });
|
|
5013
|
+
this.defaultTimeout = args.timeout ?? protocol_js.DEFAULT_REQUEST_TIMEOUT_MSEC;
|
|
5014
|
+
this.serverConfigs = args.servers;
|
|
5015
|
+
this.id = args.id ?? this.makeId();
|
|
5016
|
+
if (args.id) {
|
|
5017
|
+
this.id = args.id;
|
|
5018
|
+
const cached = mcpClientInstances.get(this.id);
|
|
5019
|
+
if (cached && !equal__default.default(cached.serverConfigs, args.servers)) {
|
|
5020
|
+
const existingInstance2 = mcpClientInstances.get(this.id);
|
|
5021
|
+
if (existingInstance2) {
|
|
5022
|
+
void existingInstance2.disconnect();
|
|
5023
|
+
}
|
|
5024
|
+
}
|
|
5025
|
+
} else {
|
|
5026
|
+
this.id = this.makeId();
|
|
5027
|
+
}
|
|
5028
|
+
const existingInstance = mcpClientInstances.get(this.id);
|
|
5029
|
+
if (existingInstance) {
|
|
5030
|
+
if (!args.id) {
|
|
5031
|
+
throw new Error(`MCPClient was initialized multiple times with the same configuration options.
|
|
4999
5032
|
|
|
5000
|
-
|
|
5001
|
-
var parseNullable = (schema, refs) => {
|
|
5002
|
-
return `${parseSchema(omit(schema, "nullable"), refs, true)}.nullable()`;
|
|
5003
|
-
};
|
|
5033
|
+
This error is intended to prevent memory leaks.
|
|
5004
5034
|
|
|
5005
|
-
|
|
5006
|
-
|
|
5007
|
-
|
|
5008
|
-
|
|
5009
|
-
|
|
5010
|
-
|
|
5011
|
-
|
|
5012
|
-
return custom2;
|
|
5013
|
-
}
|
|
5014
|
-
}
|
|
5015
|
-
let seen = refs.seen.get(schema);
|
|
5016
|
-
if (seen) {
|
|
5017
|
-
if (seen.r !== void 0) {
|
|
5018
|
-
return seen.r;
|
|
5019
|
-
}
|
|
5020
|
-
if (refs.depth === void 0 || seen.n >= refs.depth) {
|
|
5021
|
-
return "z.any()";
|
|
5035
|
+
To fix this you have three different options:
|
|
5036
|
+
1. If you need multiple MCPClient class instances with identical server configurations, set an id when configuring: new MCPClient({ id: "my-unique-id" })
|
|
5037
|
+
2. Call "await client.disconnect()" after you're done using the client and before you recreate another instance with the same options. If the identical MCPClient instance is already closed at the time of re-creating it, you will not see this error.
|
|
5038
|
+
3. If you only need one instance of MCPClient in your app, refactor your code so it's only created one time (ex. move it out of a loop into a higher scope code block)
|
|
5039
|
+
`);
|
|
5040
|
+
}
|
|
5041
|
+
return existingInstance;
|
|
5022
5042
|
}
|
|
5023
|
-
|
|
5024
|
-
|
|
5025
|
-
|
|
5026
|
-
refs.seen.set(schema, seen);
|
|
5043
|
+
mcpClientInstances.set(this.id, this);
|
|
5044
|
+
this.addToInstanceCache();
|
|
5045
|
+
return this;
|
|
5027
5046
|
}
|
|
5028
|
-
|
|
5029
|
-
|
|
5030
|
-
|
|
5031
|
-
parsed = addDescribes(schema, parsed);
|
|
5032
|
-
}
|
|
5033
|
-
if (!refs.withoutDefaults) {
|
|
5034
|
-
parsed = addDefaults(schema, parsed);
|
|
5047
|
+
addToInstanceCache() {
|
|
5048
|
+
if (!mcpClientInstances.has(this.id)) {
|
|
5049
|
+
mcpClientInstances.set(this.id, this);
|
|
5035
5050
|
}
|
|
5036
|
-
parsed = addAnnotations(schema, parsed);
|
|
5037
|
-
}
|
|
5038
|
-
seen.r = parsed;
|
|
5039
|
-
return parsed;
|
|
5040
|
-
};
|
|
5041
|
-
var addDescribes = (schema, parsed) => {
|
|
5042
|
-
if (schema.description) {
|
|
5043
|
-
parsed += `.describe(${JSON.stringify(schema.description)})`;
|
|
5044
5051
|
}
|
|
5045
|
-
|
|
5046
|
-
|
|
5047
|
-
|
|
5048
|
-
|
|
5049
|
-
parsed += `.default(${JSON.stringify(schema.default)})`;
|
|
5052
|
+
makeId() {
|
|
5053
|
+
const text = JSON.stringify(this.serverConfigs).normalize("NFKC");
|
|
5054
|
+
const idNamespace = uuid.v5(`MCPClient`, uuid.v5.DNS);
|
|
5055
|
+
return uuid.v5(text, idNamespace);
|
|
5050
5056
|
}
|
|
5051
|
-
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
5055
|
-
|
|
5057
|
+
async disconnect() {
|
|
5058
|
+
if (this.disconnectPromise) {
|
|
5059
|
+
return this.disconnectPromise;
|
|
5060
|
+
}
|
|
5061
|
+
this.disconnectPromise = (async () => {
|
|
5062
|
+
try {
|
|
5063
|
+
mcpClientInstances.delete(this.id);
|
|
5064
|
+
await Promise.all(Array.from(this.mcpClientsById.values()).map((client) => client.disconnect()));
|
|
5065
|
+
this.mcpClientsById.clear();
|
|
5066
|
+
} finally {
|
|
5067
|
+
this.disconnectPromise = null;
|
|
5068
|
+
}
|
|
5069
|
+
})();
|
|
5070
|
+
return this.disconnectPromise;
|
|
5056
5071
|
}
|
|
5057
|
-
|
|
5058
|
-
|
|
5059
|
-
|
|
5060
|
-
|
|
5061
|
-
|
|
5062
|
-
|
|
5063
|
-
|
|
5064
|
-
|
|
5065
|
-
return
|
|
5066
|
-
} else if (its.an.anyOf(schema)) {
|
|
5067
|
-
return parseAnyOf(schema, refs);
|
|
5068
|
-
} else if (its.an.allOf(schema)) {
|
|
5069
|
-
return parseAllOf(schema, refs);
|
|
5070
|
-
} else if (its.a.oneOf(schema)) {
|
|
5071
|
-
return parseOneOf(schema, refs);
|
|
5072
|
-
} else if (its.a.not(schema)) {
|
|
5073
|
-
return parseNot(schema, refs);
|
|
5074
|
-
} else if (its.an.enum(schema)) {
|
|
5075
|
-
return parseEnum(schema);
|
|
5076
|
-
} else if (its.a.const(schema)) {
|
|
5077
|
-
return parseConst(schema);
|
|
5078
|
-
} else if (its.a.multipleType(schema)) {
|
|
5079
|
-
return parseMultipleType(schema, refs);
|
|
5080
|
-
} else if (its.a.primitive(schema, "string")) {
|
|
5081
|
-
return parseString(schema);
|
|
5082
|
-
} else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) {
|
|
5083
|
-
return parseNumber(schema);
|
|
5084
|
-
} else if (its.a.primitive(schema, "boolean")) {
|
|
5085
|
-
return parseBoolean();
|
|
5086
|
-
} else if (its.a.primitive(schema, "null")) {
|
|
5087
|
-
return parseNull();
|
|
5088
|
-
} else if (its.a.conditional(schema)) {
|
|
5089
|
-
return parseIfThenElse(schema, refs);
|
|
5090
|
-
} else {
|
|
5091
|
-
return parseDefault();
|
|
5072
|
+
async getTools() {
|
|
5073
|
+
this.addToInstanceCache();
|
|
5074
|
+
const connectedTools = {};
|
|
5075
|
+
await this.eachClientTools(async ({ serverName, tools }) => {
|
|
5076
|
+
for (const [toolName, toolConfig] of Object.entries(tools)) {
|
|
5077
|
+
connectedTools[`${serverName}_${toolName}`] = toolConfig;
|
|
5078
|
+
}
|
|
5079
|
+
});
|
|
5080
|
+
return connectedTools;
|
|
5092
5081
|
}
|
|
5093
|
-
|
|
5094
|
-
|
|
5095
|
-
|
|
5096
|
-
|
|
5097
|
-
|
|
5098
|
-
|
|
5099
|
-
|
|
5100
|
-
|
|
5101
|
-
|
|
5102
|
-
a: {
|
|
5103
|
-
nullable: (x) => x.nullable === true,
|
|
5104
|
-
multipleType: (x) => Array.isArray(x.type),
|
|
5105
|
-
not: (x) => x.not !== void 0,
|
|
5106
|
-
const: (x) => x.const !== void 0,
|
|
5107
|
-
primitive: (x, p) => x.type === p,
|
|
5108
|
-
conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
|
|
5109
|
-
oneOf: (x) => x.oneOf !== void 0
|
|
5082
|
+
async getToolsets() {
|
|
5083
|
+
this.addToInstanceCache();
|
|
5084
|
+
const connectedToolsets = {};
|
|
5085
|
+
await this.eachClientTools(async ({ serverName, tools }) => {
|
|
5086
|
+
if (tools) {
|
|
5087
|
+
connectedToolsets[serverName] = tools;
|
|
5088
|
+
}
|
|
5089
|
+
});
|
|
5090
|
+
return connectedToolsets;
|
|
5110
5091
|
}
|
|
5111
|
-
|
|
5112
|
-
|
|
5113
|
-
|
|
5114
|
-
|
|
5115
|
-
|
|
5116
|
-
|
|
5092
|
+
/**
|
|
5093
|
+
* Get the current session IDs for all connected MCP clients using the Streamable HTTP transport.
|
|
5094
|
+
* Returns an object mapping server names to their session IDs.
|
|
5095
|
+
*/
|
|
5096
|
+
get sessionIds() {
|
|
5097
|
+
const sessionIds = {};
|
|
5098
|
+
for (const [serverName, client] of this.mcpClientsById.entries()) {
|
|
5099
|
+
if (client.sessionId) {
|
|
5100
|
+
sessionIds[serverName] = client.sessionId;
|
|
5101
|
+
}
|
|
5102
|
+
}
|
|
5103
|
+
return sessionIds;
|
|
5117
5104
|
}
|
|
5118
|
-
|
|
5119
|
-
|
|
5120
|
-
|
|
5121
|
-
path: [],
|
|
5122
|
-
seen: /* @__PURE__ */ new Map(),
|
|
5123
|
-
...rest
|
|
5124
|
-
});
|
|
5125
|
-
const jsdocs = rest.withJsdocs && typeof schema !== "boolean" && schema.description ? expandJsdocs(schema.description) : "";
|
|
5126
|
-
if (module === "cjs") {
|
|
5127
|
-
result = `${jsdocs}module.exports = ${name ? `{ ${JSON.stringify(name)}: ${result} }` : result}
|
|
5128
|
-
`;
|
|
5129
|
-
if (!noImport) {
|
|
5130
|
-
result = `${jsdocs}const { z } = require("zod")
|
|
5131
|
-
|
|
5132
|
-
${result}`;
|
|
5105
|
+
async getConnectedClient(name, config) {
|
|
5106
|
+
if (this.disconnectPromise) {
|
|
5107
|
+
await this.disconnectPromise;
|
|
5133
5108
|
}
|
|
5134
|
-
|
|
5135
|
-
|
|
5136
|
-
|
|
5137
|
-
|
|
5138
|
-
|
|
5139
|
-
|
|
5140
|
-
|
|
5109
|
+
const exists = this.mcpClientsById.has(name);
|
|
5110
|
+
const existingClient = this.mcpClientsById.get(name);
|
|
5111
|
+
if (exists) {
|
|
5112
|
+
if (!existingClient) {
|
|
5113
|
+
throw new Error(`Client ${name} exists but is undefined`);
|
|
5114
|
+
}
|
|
5115
|
+
await existingClient.connect();
|
|
5116
|
+
return existingClient;
|
|
5141
5117
|
}
|
|
5142
|
-
|
|
5143
|
-
|
|
5118
|
+
this.logger.debug(`Connecting to ${name} MCP server`);
|
|
5119
|
+
const mcpClient = new InternalMastraMCPClient({
|
|
5120
|
+
name,
|
|
5121
|
+
server: config,
|
|
5122
|
+
timeout: config.timeout ?? this.defaultTimeout
|
|
5123
|
+
});
|
|
5124
|
+
this.mcpClientsById.set(name, mcpClient);
|
|
5125
|
+
try {
|
|
5126
|
+
await mcpClient.connect();
|
|
5127
|
+
} catch (e) {
|
|
5128
|
+
this.mcpClientsById.delete(name);
|
|
5129
|
+
this.logger.error(`MCPClient errored connecting to MCP server ${name}`, {
|
|
5130
|
+
error: e instanceof Error ? e.message : String(e)
|
|
5131
|
+
});
|
|
5132
|
+
throw new Error(
|
|
5133
|
+
`Failed to connect to MCP server ${name}: ${e instanceof Error ? e.stack || e.message : String(e)}`
|
|
5134
|
+
);
|
|
5135
|
+
}
|
|
5136
|
+
this.logger.debug(`Connected to ${name} MCP server`);
|
|
5137
|
+
return mcpClient;
|
|
5144
5138
|
}
|
|
5145
|
-
|
|
5146
|
-
|
|
5147
|
-
|
|
5148
|
-
|
|
5139
|
+
async eachClientTools(cb) {
|
|
5140
|
+
await Promise.all(
|
|
5141
|
+
Object.entries(this.serverConfigs).map(async ([serverName, serverConfig]) => {
|
|
5142
|
+
const client = await this.getConnectedClient(serverName, serverConfig);
|
|
5143
|
+
const tools = await client.tools();
|
|
5144
|
+
await cb({ serverName, tools, client });
|
|
5145
|
+
})
|
|
5146
|
+
);
|
|
5147
|
+
}
|
|
5148
|
+
};
|
|
5149
|
+
var MCPConfiguration = class extends MCPClient {
|
|
5150
|
+
constructor(args) {
|
|
5151
|
+
super(args);
|
|
5152
|
+
this.logger.warn(
|
|
5153
|
+
`MCPConfiguration has been renamed to MCPClient and MCPConfiguration is deprecated. The API is identical but the MCPConfiguration export will be removed in the future. Update your imports now to prevent future errors.`
|
|
5154
|
+
);
|
|
5149
5155
|
}
|
|
5150
|
-
return result;
|
|
5151
5156
|
};
|
|
5152
|
-
|
|
5153
|
-
// ../../node_modules/.pnpm/json-schema-to-zod@2.6.0/node_modules/json-schema-to-zod/dist/esm/index.js
|
|
5154
|
-
var esm_default = jsonSchemaToZod;
|
|
5155
5157
|
|
|
5156
5158
|
// ../../node_modules/.pnpm/zod-to-json-schema@3.24.5_zod@3.24.3/node_modules/zod-to-json-schema/dist/esm/Options.js
|
|
5157
5159
|
var ignoreOverride = Symbol("Let zodToJsonSchema decide on which parser to use");
|