unity-mcp-server.bundle.js•252 kB
#!/usr/bin/env node
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// node_modules/zod/dist/esm/v3/external.js
var external_exports = {};
__export(external_exports, {
BRAND: () => BRAND,
DIRTY: () => DIRTY,
EMPTY_PATH: () => EMPTY_PATH,
INVALID: () => INVALID,
NEVER: () => NEVER,
OK: () => OK,
ParseStatus: () => ParseStatus,
Schema: () => ZodType,
ZodAny: () => ZodAny,
ZodArray: () => ZodArray,
ZodBigInt: () => ZodBigInt,
ZodBoolean: () => ZodBoolean,
ZodBranded: () => ZodBranded,
ZodCatch: () => ZodCatch,
ZodDate: () => ZodDate,
ZodDefault: () => ZodDefault,
ZodDiscriminatedUnion: () => ZodDiscriminatedUnion,
ZodEffects: () => ZodEffects,
ZodEnum: () => ZodEnum,
ZodError: () => ZodError,
ZodFirstPartyTypeKind: () => ZodFirstPartyTypeKind,
ZodFunction: () => ZodFunction,
ZodIntersection: () => ZodIntersection,
ZodIssueCode: () => ZodIssueCode,
ZodLazy: () => ZodLazy,
ZodLiteral: () => ZodLiteral,
ZodMap: () => ZodMap,
ZodNaN: () => ZodNaN,
ZodNativeEnum: () => ZodNativeEnum,
ZodNever: () => ZodNever,
ZodNull: () => ZodNull,
ZodNullable: () => ZodNullable,
ZodNumber: () => ZodNumber,
ZodObject: () => ZodObject,
ZodOptional: () => ZodOptional,
ZodParsedType: () => ZodParsedType,
ZodPipeline: () => ZodPipeline,
ZodPromise: () => ZodPromise,
ZodReadonly: () => ZodReadonly,
ZodRecord: () => ZodRecord,
ZodSchema: () => ZodType,
ZodSet: () => ZodSet,
ZodString: () => ZodString,
ZodSymbol: () => ZodSymbol,
ZodTransformer: () => ZodEffects,
ZodTuple: () => ZodTuple,
ZodType: () => ZodType,
ZodUndefined: () => ZodUndefined,
ZodUnion: () => ZodUnion,
ZodUnknown: () => ZodUnknown,
ZodVoid: () => ZodVoid,
addIssueToContext: () => addIssueToContext,
any: () => anyType,
array: () => arrayType,
bigint: () => bigIntType,
boolean: () => booleanType,
coerce: () => coerce,
custom: () => custom,
date: () => dateType,
datetimeRegex: () => datetimeRegex,
defaultErrorMap: () => en_default,
discriminatedUnion: () => discriminatedUnionType,
effect: () => effectsType,
enum: () => enumType,
function: () => functionType,
getErrorMap: () => getErrorMap,
getParsedType: () => getParsedType,
instanceof: () => instanceOfType,
intersection: () => intersectionType,
isAborted: () => isAborted,
isAsync: () => isAsync,
isDirty: () => isDirty,
isValid: () => isValid,
late: () => late,
lazy: () => lazyType,
literal: () => literalType,
makeIssue: () => makeIssue,
map: () => mapType,
nan: () => nanType,
nativeEnum: () => nativeEnumType,
never: () => neverType,
null: () => nullType,
nullable: () => nullableType,
number: () => numberType,
object: () => objectType,
objectUtil: () => objectUtil,
oboolean: () => oboolean,
onumber: () => onumber,
optional: () => optionalType,
ostring: () => ostring,
pipeline: () => pipelineType,
preprocess: () => preprocessType,
promise: () => promiseType,
quotelessJson: () => quotelessJson,
record: () => recordType,
set: () => setType,
setErrorMap: () => setErrorMap,
strictObject: () => strictObjectType,
string: () => stringType,
symbol: () => symbolType,
transformer: () => effectsType,
tuple: () => tupleType,
undefined: () => undefinedType,
union: () => unionType,
unknown: () => unknownType,
util: () => util,
void: () => voidType
});
// node_modules/zod/dist/esm/v3/helpers/util.js
var util;
(function(util2) {
util2.assertEqual = (_) => {
};
function assertIs(_arg) {
}
util2.assertIs = assertIs;
function assertNever(_x) {
throw new Error();
}
util2.assertNever = assertNever;
util2.arrayToEnum = (items) => {
const obj = {};
for (const item of items) {
obj[item] = item;
}
return obj;
};
util2.getValidEnumValues = (obj) => {
const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
const filtered = {};
for (const k of validKeys) {
filtered[k] = obj[k];
}
return util2.objectValues(filtered);
};
util2.objectValues = (obj) => {
return util2.objectKeys(obj).map(function(e) {
return obj[e];
});
};
util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
const keys = [];
for (const key in object) {
if (Object.prototype.hasOwnProperty.call(object, key)) {
keys.push(key);
}
}
return keys;
};
util2.find = (arr, checker) => {
for (const item of arr) {
if (checker(item))
return item;
}
return void 0;
};
util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
function joinValues(array, separator = " | ") {
return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
}
util2.joinValues = joinValues;
util2.jsonStringifyReplacer = (_, value) => {
if (typeof value === "bigint") {
return value.toString();
}
return value;
};
})(util || (util = {}));
var objectUtil;
(function(objectUtil2) {
objectUtil2.mergeShapes = (first, second) => {
return {
...first,
...second
// second overwrites first
};
};
})(objectUtil || (objectUtil = {}));
var ZodParsedType = util.arrayToEnum([
"string",
"nan",
"number",
"integer",
"float",
"boolean",
"date",
"bigint",
"symbol",
"function",
"undefined",
"null",
"array",
"object",
"unknown",
"promise",
"void",
"never",
"map",
"set"
]);
var getParsedType = (data) => {
const t = typeof data;
switch (t) {
case "undefined":
return ZodParsedType.undefined;
case "string":
return ZodParsedType.string;
case "number":
return Number.isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
case "boolean":
return ZodParsedType.boolean;
case "function":
return ZodParsedType.function;
case "bigint":
return ZodParsedType.bigint;
case "symbol":
return ZodParsedType.symbol;
case "object":
if (Array.isArray(data)) {
return ZodParsedType.array;
}
if (data === null) {
return ZodParsedType.null;
}
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
return ZodParsedType.promise;
}
if (typeof Map !== "undefined" && data instanceof Map) {
return ZodParsedType.map;
}
if (typeof Set !== "undefined" && data instanceof Set) {
return ZodParsedType.set;
}
if (typeof Date !== "undefined" && data instanceof Date) {
return ZodParsedType.date;
}
return ZodParsedType.object;
default:
return ZodParsedType.unknown;
}
};
// node_modules/zod/dist/esm/v3/ZodError.js
var ZodIssueCode = util.arrayToEnum([
"invalid_type",
"invalid_literal",
"custom",
"invalid_union",
"invalid_union_discriminator",
"invalid_enum_value",
"unrecognized_keys",
"invalid_arguments",
"invalid_return_type",
"invalid_date",
"invalid_string",
"too_small",
"too_big",
"invalid_intersection_types",
"not_multiple_of",
"not_finite"
]);
var quotelessJson = (obj) => {
const json = JSON.stringify(obj, null, 2);
return json.replace(/"([^"]+)":/g, "$1:");
};
var ZodError = class _ZodError extends Error {
get errors() {
return this.issues;
}
constructor(issues) {
super();
this.issues = [];
this.addIssue = (sub) => {
this.issues = [...this.issues, sub];
};
this.addIssues = (subs = []) => {
this.issues = [...this.issues, ...subs];
};
const actualProto = new.target.prototype;
if (Object.setPrototypeOf) {
Object.setPrototypeOf(this, actualProto);
} else {
this.__proto__ = actualProto;
}
this.name = "ZodError";
this.issues = issues;
}
format(_mapper) {
const mapper = _mapper || function(issue) {
return issue.message;
};
const fieldErrors = { _errors: [] };
const processError = (error) => {
for (const issue of error.issues) {
if (issue.code === "invalid_union") {
issue.unionErrors.map(processError);
} else if (issue.code === "invalid_return_type") {
processError(issue.returnTypeError);
} else if (issue.code === "invalid_arguments") {
processError(issue.argumentsError);
} else if (issue.path.length === 0) {
fieldErrors._errors.push(mapper(issue));
} else {
let curr = fieldErrors;
let i = 0;
while (i < issue.path.length) {
const el = issue.path[i];
const terminal = i === issue.path.length - 1;
if (!terminal) {
curr[el] = curr[el] || { _errors: [] };
} else {
curr[el] = curr[el] || { _errors: [] };
curr[el]._errors.push(mapper(issue));
}
curr = curr[el];
i++;
}
}
}
};
processError(this);
return fieldErrors;
}
static assert(value) {
if (!(value instanceof _ZodError)) {
throw new Error(`Not a ZodError: ${value}`);
}
}
toString() {
return this.message;
}
get message() {
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
}
get isEmpty() {
return this.issues.length === 0;
}
flatten(mapper = (issue) => issue.message) {
const fieldErrors = {};
const formErrors = [];
for (const sub of this.issues) {
if (sub.path.length > 0) {
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
fieldErrors[sub.path[0]].push(mapper(sub));
} else {
formErrors.push(mapper(sub));
}
}
return { formErrors, fieldErrors };
}
get formErrors() {
return this.flatten();
}
};
ZodError.create = (issues) => {
const error = new ZodError(issues);
return error;
};
// node_modules/zod/dist/esm/v3/locales/en.js
var errorMap = (issue, _ctx) => {
let message;
switch (issue.code) {
case ZodIssueCode.invalid_type:
if (issue.received === ZodParsedType.undefined) {
message = "Required";
} else {
message = `Expected ${issue.expected}, received ${issue.received}`;
}
break;
case ZodIssueCode.invalid_literal:
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
break;
case ZodIssueCode.unrecognized_keys:
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
break;
case ZodIssueCode.invalid_union:
message = `Invalid input`;
break;
case ZodIssueCode.invalid_union_discriminator:
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
break;
case ZodIssueCode.invalid_enum_value:
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
break;
case ZodIssueCode.invalid_arguments:
message = `Invalid function arguments`;
break;
case ZodIssueCode.invalid_return_type:
message = `Invalid function return type`;
break;
case ZodIssueCode.invalid_date:
message = `Invalid date`;
break;
case ZodIssueCode.invalid_string:
if (typeof issue.validation === "object") {
if ("includes" in issue.validation) {
message = `Invalid input: must include "${issue.validation.includes}"`;
if (typeof issue.validation.position === "number") {
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
}
} else if ("startsWith" in issue.validation) {
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
} else if ("endsWith" in issue.validation) {
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
} else {
util.assertNever(issue.validation);
}
} else if (issue.validation !== "regex") {
message = `Invalid ${issue.validation}`;
} else {
message = "Invalid";
}
break;
case ZodIssueCode.too_small:
if (issue.type === "array")
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
else if (issue.type === "string")
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
else if (issue.type === "number")
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
else if (issue.type === "date")
message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
else
message = "Invalid input";
break;
case ZodIssueCode.too_big:
if (issue.type === "array")
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
else if (issue.type === "string")
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
else if (issue.type === "number")
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
else if (issue.type === "bigint")
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
else if (issue.type === "date")
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
else
message = "Invalid input";
break;
case ZodIssueCode.custom:
message = `Invalid input`;
break;
case ZodIssueCode.invalid_intersection_types:
message = `Intersection results could not be merged`;
break;
case ZodIssueCode.not_multiple_of:
message = `Number must be a multiple of ${issue.multipleOf}`;
break;
case ZodIssueCode.not_finite:
message = "Number must be finite";
break;
default:
message = _ctx.defaultError;
util.assertNever(issue);
}
return { message };
};
var en_default = errorMap;
// node_modules/zod/dist/esm/v3/errors.js
var overrideErrorMap = en_default;
function setErrorMap(map) {
overrideErrorMap = map;
}
function getErrorMap() {
return overrideErrorMap;
}
// node_modules/zod/dist/esm/v3/helpers/parseUtil.js
var makeIssue = (params) => {
const { data, path: path3, errorMaps, issueData } = params;
const fullPath = [...path3, ...issueData.path || []];
const fullIssue = {
...issueData,
path: fullPath
};
if (issueData.message !== void 0) {
return {
...issueData,
path: fullPath,
message: issueData.message
};
}
let errorMessage = "";
const maps = errorMaps.filter((m) => !!m).slice().reverse();
for (const map of maps) {
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
}
return {
...issueData,
path: fullPath,
message: errorMessage
};
};
var EMPTY_PATH = [];
function addIssueToContext(ctx, issueData) {
const overrideMap = getErrorMap();
const issue = makeIssue({
issueData,
data: ctx.data,
path: ctx.path,
errorMaps: [
ctx.common.contextualErrorMap,
// contextual error map is first priority
ctx.schemaErrorMap,
// then schema-bound map if available
overrideMap,
// then global override map
overrideMap === en_default ? void 0 : en_default
// then global default map
].filter((x) => !!x)
});
ctx.common.issues.push(issue);
}
var ParseStatus = class _ParseStatus {
constructor() {
this.value = "valid";
}
dirty() {
if (this.value === "valid")
this.value = "dirty";
}
abort() {
if (this.value !== "aborted")
this.value = "aborted";
}
static mergeArray(status, results) {
const arrayValue = [];
for (const s of results) {
if (s.status === "aborted")
return INVALID;
if (s.status === "dirty")
status.dirty();
arrayValue.push(s.value);
}
return { status: status.value, value: arrayValue };
}
static async mergeObjectAsync(status, pairs) {
const syncPairs = [];
for (const pair of pairs) {
const key = await pair.key;
const value = await pair.value;
syncPairs.push({
key,
value
});
}
return _ParseStatus.mergeObjectSync(status, syncPairs);
}
static mergeObjectSync(status, pairs) {
const finalObject = {};
for (const pair of pairs) {
const { key, value } = pair;
if (key.status === "aborted")
return INVALID;
if (value.status === "aborted")
return INVALID;
if (key.status === "dirty")
status.dirty();
if (value.status === "dirty")
status.dirty();
if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
finalObject[key.value] = value.value;
}
}
return { status: status.value, value: finalObject };
}
};
var INVALID = Object.freeze({
status: "aborted"
});
var DIRTY = (value) => ({ status: "dirty", value });
var OK = (value) => ({ status: "valid", value });
var isAborted = (x) => x.status === "aborted";
var isDirty = (x) => x.status === "dirty";
var isValid = (x) => x.status === "valid";
var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
// node_modules/zod/dist/esm/v3/helpers/errorUtil.js
var errorUtil;
(function(errorUtil2) {
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
errorUtil2.toString = (message) => typeof message === "string" ? message : message?.message;
})(errorUtil || (errorUtil = {}));
// node_modules/zod/dist/esm/v3/types.js
var ParseInputLazyPath = class {
constructor(parent, value, path3, key) {
this._cachedPath = [];
this.parent = parent;
this.data = value;
this._path = path3;
this._key = key;
}
get path() {
if (!this._cachedPath.length) {
if (Array.isArray(this._key)) {
this._cachedPath.push(...this._path, ...this._key);
} else {
this._cachedPath.push(...this._path, this._key);
}
}
return this._cachedPath;
}
};
var handleResult = (ctx, result) => {
if (isValid(result)) {
return { success: true, data: result.value };
} else {
if (!ctx.common.issues.length) {
throw new Error("Validation failed but no issues detected.");
}
return {
success: false,
get error() {
if (this._error)
return this._error;
const error = new ZodError(ctx.common.issues);
this._error = error;
return this._error;
}
};
}
};
function processCreateParams(params) {
if (!params)
return {};
const { errorMap: errorMap2, invalid_type_error, required_error, description } = params;
if (errorMap2 && (invalid_type_error || required_error)) {
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
}
if (errorMap2)
return { errorMap: errorMap2, description };
const customMap = (iss, ctx) => {
const { message } = params;
if (iss.code === "invalid_enum_value") {
return { message: message ?? ctx.defaultError };
}
if (typeof ctx.data === "undefined") {
return { message: message ?? required_error ?? ctx.defaultError };
}
if (iss.code !== "invalid_type")
return { message: ctx.defaultError };
return { message: message ?? invalid_type_error ?? ctx.defaultError };
};
return { errorMap: customMap, description };
}
var ZodType = class {
get description() {
return this._def.description;
}
_getType(input) {
return getParsedType(input.data);
}
_getOrReturnCtx(input, ctx) {
return ctx || {
common: input.parent.common,
data: input.data,
parsedType: getParsedType(input.data),
schemaErrorMap: this._def.errorMap,
path: input.path,
parent: input.parent
};
}
_processInputParams(input) {
return {
status: new ParseStatus(),
ctx: {
common: input.parent.common,
data: input.data,
parsedType: getParsedType(input.data),
schemaErrorMap: this._def.errorMap,
path: input.path,
parent: input.parent
}
};
}
_parseSync(input) {
const result = this._parse(input);
if (isAsync(result)) {
throw new Error("Synchronous parse encountered promise.");
}
return result;
}
_parseAsync(input) {
const result = this._parse(input);
return Promise.resolve(result);
}
parse(data, params) {
const result = this.safeParse(data, params);
if (result.success)
return result.data;
throw result.error;
}
safeParse(data, params) {
const ctx = {
common: {
issues: [],
async: params?.async ?? false,
contextualErrorMap: params?.errorMap
},
path: params?.path || [],
schemaErrorMap: this._def.errorMap,
parent: null,
data,
parsedType: getParsedType(data)
};
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
return handleResult(ctx, result);
}
"~validate"(data) {
const ctx = {
common: {
issues: [],
async: !!this["~standard"].async
},
path: [],
schemaErrorMap: this._def.errorMap,
parent: null,
data,
parsedType: getParsedType(data)
};
if (!this["~standard"].async) {
try {
const result = this._parseSync({ data, path: [], parent: ctx });
return isValid(result) ? {
value: result.value
} : {
issues: ctx.common.issues
};
} catch (err) {
if (err?.message?.toLowerCase()?.includes("encountered")) {
this["~standard"].async = true;
}
ctx.common = {
issues: [],
async: true
};
}
}
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result) ? {
value: result.value
} : {
issues: ctx.common.issues
});
}
async parseAsync(data, params) {
const result = await this.safeParseAsync(data, params);
if (result.success)
return result.data;
throw result.error;
}
async safeParseAsync(data, params) {
const ctx = {
common: {
issues: [],
contextualErrorMap: params?.errorMap,
async: true
},
path: params?.path || [],
schemaErrorMap: this._def.errorMap,
parent: null,
data,
parsedType: getParsedType(data)
};
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
return handleResult(ctx, result);
}
refine(check, message) {
const getIssueProperties = (val) => {
if (typeof message === "string" || typeof message === "undefined") {
return { message };
} else if (typeof message === "function") {
return message(val);
} else {
return message;
}
};
return this._refinement((val, ctx) => {
const result = check(val);
const setError = () => ctx.addIssue({
code: ZodIssueCode.custom,
...getIssueProperties(val)
});
if (typeof Promise !== "undefined" && result instanceof Promise) {
return result.then((data) => {
if (!data) {
setError();
return false;
} else {
return true;
}
});
}
if (!result) {
setError();
return false;
} else {
return true;
}
});
}
refinement(check, refinementData) {
return this._refinement((val, ctx) => {
if (!check(val)) {
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
return false;
} else {
return true;
}
});
}
_refinement(refinement) {
return new ZodEffects({
schema: this,
typeName: ZodFirstPartyTypeKind.ZodEffects,
effect: { type: "refinement", refinement }
});
}
superRefine(refinement) {
return this._refinement(refinement);
}
constructor(def) {
this.spa = this.safeParseAsync;
this._def = def;
this.parse = this.parse.bind(this);
this.safeParse = this.safeParse.bind(this);
this.parseAsync = this.parseAsync.bind(this);
this.safeParseAsync = this.safeParseAsync.bind(this);
this.spa = this.spa.bind(this);
this.refine = this.refine.bind(this);
this.refinement = this.refinement.bind(this);
this.superRefine = this.superRefine.bind(this);
this.optional = this.optional.bind(this);
this.nullable = this.nullable.bind(this);
this.nullish = this.nullish.bind(this);
this.array = this.array.bind(this);
this.promise = this.promise.bind(this);
this.or = this.or.bind(this);
this.and = this.and.bind(this);
this.transform = this.transform.bind(this);
this.brand = this.brand.bind(this);
this.default = this.default.bind(this);
this.catch = this.catch.bind(this);
this.describe = this.describe.bind(this);
this.pipe = this.pipe.bind(this);
this.readonly = this.readonly.bind(this);
this.isNullable = this.isNullable.bind(this);
this.isOptional = this.isOptional.bind(this);
this["~standard"] = {
version: 1,
vendor: "zod",
validate: (data) => this["~validate"](data)
};
}
optional() {
return ZodOptional.create(this, this._def);
}
nullable() {
return ZodNullable.create(this, this._def);
}
nullish() {
return this.nullable().optional();
}
array() {
return ZodArray.create(this);
}
promise() {
return ZodPromise.create(this, this._def);
}
or(option) {
return ZodUnion.create([this, option], this._def);
}
and(incoming) {
return ZodIntersection.create(this, incoming, this._def);
}
transform(transform) {
return new ZodEffects({
...processCreateParams(this._def),
schema: this,
typeName: ZodFirstPartyTypeKind.ZodEffects,
effect: { type: "transform", transform }
});
}
default(def) {
const defaultValueFunc = typeof def === "function" ? def : () => def;
return new ZodDefault({
...processCreateParams(this._def),
innerType: this,
defaultValue: defaultValueFunc,
typeName: ZodFirstPartyTypeKind.ZodDefault
});
}
brand() {
return new ZodBranded({
typeName: ZodFirstPartyTypeKind.ZodBranded,
type: this,
...processCreateParams(this._def)
});
}
catch(def) {
const catchValueFunc = typeof def === "function" ? def : () => def;
return new ZodCatch({
...processCreateParams(this._def),
innerType: this,
catchValue: catchValueFunc,
typeName: ZodFirstPartyTypeKind.ZodCatch
});
}
describe(description) {
const This = this.constructor;
return new This({
...this._def,
description
});
}
pipe(target) {
return ZodPipeline.create(this, target);
}
readonly() {
return ZodReadonly.create(this);
}
isOptional() {
return this.safeParse(void 0).success;
}
isNullable() {
return this.safeParse(null).success;
}
};
var cuidRegex = /^c[^\s-]{8,}$/i;
var cuid2Regex = /^[0-9a-z]+$/;
var ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
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;
var nanoidRegex = /^[a-z0-9_-]{21}$/i;
var jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
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)?)??$/;
var emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
var _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
var emojiRegex;
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])$/;
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])$/;
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]))$/;
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])$/;
var base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
var base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
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])))`;
var dateRegex = new RegExp(`^${dateRegexSource}$`);
function timeRegexSource(args) {
let secondsRegexSource = `[0-5]\\d`;
if (args.precision) {
secondsRegexSource = `${secondsRegexSource}\\.\\d{${args.precision}}`;
} else if (args.precision == null) {
secondsRegexSource = `${secondsRegexSource}(\\.\\d+)?`;
}
const secondsQuantifier = args.precision ? "+" : "?";
return `([01]\\d|2[0-3]):[0-5]\\d(:${secondsRegexSource})${secondsQuantifier}`;
}
function timeRegex(args) {
return new RegExp(`^${timeRegexSource(args)}$`);
}
function datetimeRegex(args) {
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
const opts = [];
opts.push(args.local ? `Z?` : `Z`);
if (args.offset)
opts.push(`([+-]\\d{2}:?\\d{2})`);
regex = `${regex}(${opts.join("|")})`;
return new RegExp(`^${regex}$`);
}
function isValidIP(ip, version) {
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
return true;
}
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
return true;
}
return false;
}
function isValidJWT(jwt, alg) {
if (!jwtRegex.test(jwt))
return false;
try {
const [header] = jwt.split(".");
const base64 = header.replace(/-/g, "+").replace(/_/g, "/").padEnd(header.length + (4 - header.length % 4) % 4, "=");
const decoded = JSON.parse(atob(base64));
if (typeof decoded !== "object" || decoded === null)
return false;
if ("typ" in decoded && decoded?.typ !== "JWT")
return false;
if (!decoded.alg)
return false;
if (alg && decoded.alg !== alg)
return false;
return true;
} catch {
return false;
}
}
function isValidCidr(ip, version) {
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
return true;
}
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
return true;
}
return false;
}
var ZodString = class _ZodString extends ZodType {
_parse(input) {
if (this._def.coerce) {
input.data = String(input.data);
}
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.string) {
const ctx2 = this._getOrReturnCtx(input);
addIssueToContext(ctx2, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.string,
received: ctx2.parsedType
});
return INVALID;
}
const status = new ParseStatus();
let ctx = void 0;
for (const check of this._def.checks) {
if (check.kind === "min") {
if (input.data.length < check.value) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.too_small,
minimum: check.value,
type: "string",
inclusive: true,
exact: false,
message: check.message
});
status.dirty();
}
} else if (check.kind === "max") {
if (input.data.length > check.value) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.too_big,
maximum: check.value,
type: "string",
inclusive: true,
exact: false,
message: check.message
});
status.dirty();
}
} else if (check.kind === "length") {
const tooBig = input.data.length > check.value;
const tooSmall = input.data.length < check.value;
if (tooBig || tooSmall) {
ctx = this._getOrReturnCtx(input, ctx);
if (tooBig) {
addIssueToContext(ctx, {
code: ZodIssueCode.too_big,
maximum: check.value,
type: "string",
inclusive: true,
exact: true,
message: check.message
});
} else if (tooSmall) {
addIssueToContext(ctx, {
code: ZodIssueCode.too_small,
minimum: check.value,
type: "string",
inclusive: true,
exact: true,
message: check.message
});
}
status.dirty();
}
} else if (check.kind === "email") {
if (!emailRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "email",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "emoji") {
if (!emojiRegex) {
emojiRegex = new RegExp(_emojiRegex, "u");
}
if (!emojiRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "emoji",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "uuid") {
if (!uuidRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "uuid",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "nanoid") {
if (!nanoidRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "nanoid",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "cuid") {
if (!cuidRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "cuid",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "cuid2") {
if (!cuid2Regex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "cuid2",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "ulid") {
if (!ulidRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "ulid",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "url") {
try {
new URL(input.data);
} catch {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "url",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "regex") {
check.regex.lastIndex = 0;
const testResult = check.regex.test(input.data);
if (!testResult) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "regex",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "trim") {
input.data = input.data.trim();
} else if (check.kind === "includes") {
if (!input.data.includes(check.value, check.position)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_string,
validation: { includes: check.value, position: check.position },
message: check.message
});
status.dirty();
}
} else if (check.kind === "toLowerCase") {
input.data = input.data.toLowerCase();
} else if (check.kind === "toUpperCase") {
input.data = input.data.toUpperCase();
} else if (check.kind === "startsWith") {
if (!input.data.startsWith(check.value)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_string,
validation: { startsWith: check.value },
message: check.message
});
status.dirty();
}
} else if (check.kind === "endsWith") {
if (!input.data.endsWith(check.value)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_string,
validation: { endsWith: check.value },
message: check.message
});
status.dirty();
}
} else if (check.kind === "datetime") {
const regex = datetimeRegex(check);
if (!regex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_string,
validation: "datetime",
message: check.message
});
status.dirty();
}
} else if (check.kind === "date") {
const regex = dateRegex;
if (!regex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_string,
validation: "date",
message: check.message
});
status.dirty();
}
} else if (check.kind === "time") {
const regex = timeRegex(check);
if (!regex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_string,
validation: "time",
message: check.message
});
status.dirty();
}
} else if (check.kind === "duration") {
if (!durationRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "duration",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "ip") {
if (!isValidIP(input.data, check.version)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "ip",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "jwt") {
if (!isValidJWT(input.data, check.alg)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "jwt",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "cidr") {
if (!isValidCidr(input.data, check.version)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "cidr",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "base64") {
if (!base64Regex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "base64",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else if (check.kind === "base64url") {
if (!base64urlRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "base64url",
code: ZodIssueCode.invalid_string,
message: check.message
});
status.dirty();
}
} else {
util.assertNever(check);
}
}
return { status: status.value, value: input.data };
}
_regex(regex, validation, message) {
return this.refinement((data) => regex.test(data), {
validation,
code: ZodIssueCode.invalid_string,
...errorUtil.errToObj(message)
});
}
_addCheck(check) {
return new _ZodString({
...this._def,
checks: [...this._def.checks, check]
});
}
email(message) {
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
}
url(message) {
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
}
emoji(message) {
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
}
uuid(message) {
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
}
nanoid(message) {
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
}
cuid(message) {
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
}
cuid2(message) {
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
}
ulid(message) {
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
}
base64(message) {
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
}
base64url(message) {
return this._addCheck({
kind: "base64url",
...errorUtil.errToObj(message)
});
}
jwt(options) {
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
}
ip(options) {
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
}
cidr(options) {
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
}
datetime(options) {
if (typeof options === "string") {
return this._addCheck({
kind: "datetime",
precision: null,
offset: false,
local: false,
message: options
});
}
return this._addCheck({
kind: "datetime",
precision: typeof options?.precision === "undefined" ? null : options?.precision,
offset: options?.offset ?? false,
local: options?.local ?? false,
...errorUtil.errToObj(options?.message)
});
}
date(message) {
return this._addCheck({ kind: "date", message });
}
time(options) {
if (typeof options === "string") {
return this._addCheck({
kind: "time",
precision: null,
message: options
});
}
return this._addCheck({
kind: "time",
precision: typeof options?.precision === "undefined" ? null : options?.precision,
...errorUtil.errToObj(options?.message)
});
}
duration(message) {
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
}
regex(regex, message) {
return this._addCheck({
kind: "regex",
regex,
...errorUtil.errToObj(message)
});
}
includes(value, options) {
return this._addCheck({
kind: "includes",
value,
position: options?.position,
...errorUtil.errToObj(options?.message)
});
}
startsWith(value, message) {
return this._addCheck({
kind: "startsWith",
value,
...errorUtil.errToObj(message)
});
}
endsWith(value, message) {
return this._addCheck({
kind: "endsWith",
value,
...errorUtil.errToObj(message)
});
}
min(minLength, message) {
return this._addCheck({
kind: "min",
value: minLength,
...errorUtil.errToObj(message)
});
}
max(maxLength, message) {
return this._addCheck({
kind: "max",
value: maxLength,
...errorUtil.errToObj(message)
});
}
length(len, message) {
return this._addCheck({
kind: "length",
value: len,
...errorUtil.errToObj(message)
});
}
/**
* Equivalent to `.min(1)`
*/
nonempty(message) {
return this.min(1, errorUtil.errToObj(message));
}
trim() {
return new _ZodString({
...this._def,
checks: [...this._def.checks, { kind: "trim" }]
});
}
toLowerCase() {
return new _ZodString({
...this._def,
checks: [...this._def.checks, { kind: "toLowerCase" }]
});
}
toUpperCase() {
return new _ZodString({
...this._def,
checks: [...this._def.checks, { kind: "toUpperCase" }]
});
}
get isDatetime() {
return !!this._def.checks.find((ch) => ch.kind === "datetime");
}
get isDate() {
return !!this._def.checks.find((ch) => ch.kind === "date");
}
get isTime() {
return !!this._def.checks.find((ch) => ch.kind === "time");
}
get isDuration() {
return !!this._def.checks.find((ch) => ch.kind === "duration");
}
get isEmail() {
return !!this._def.checks.find((ch) => ch.kind === "email");
}
get isURL() {
return !!this._def.checks.find((ch) => ch.kind === "url");
}
get isEmoji() {
return !!this._def.checks.find((ch) => ch.kind === "emoji");
}
get isUUID() {
return !!this._def.checks.find((ch) => ch.kind === "uuid");
}
get isNANOID() {
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
}
get isCUID() {
return !!this._def.checks.find((ch) => ch.kind === "cuid");
}
get isCUID2() {
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
}
get isULID() {
return !!this._def.checks.find((ch) => ch.kind === "ulid");
}
get isIP() {
return !!this._def.checks.find((ch) => ch.kind === "ip");
}
get isCIDR() {
return !!this._def.checks.find((ch) => ch.kind === "cidr");
}
get isBase64() {
return !!this._def.checks.find((ch) => ch.kind === "base64");
}
get isBase64url() {
return !!this._def.checks.find((ch) => ch.kind === "base64url");
}
get minLength() {
let min = null;
for (const ch of this._def.checks) {
if (ch.kind === "min") {
if (min === null || ch.value > min)
min = ch.value;
}
}
return min;
}
get maxLength() {
let max = null;
for (const ch of this._def.checks) {
if (ch.kind === "max") {
if (max === null || ch.value < max)
max = ch.value;
}
}
return max;
}
};
ZodString.create = (params) => {
return new ZodString({
checks: [],
typeName: ZodFirstPartyTypeKind.ZodString,
coerce: params?.coerce ?? false,
...processCreateParams(params)
});
};
function floatSafeRemainder(val, step) {
const valDecCount = (val.toString().split(".")[1] || "").length;
const stepDecCount = (step.toString().split(".")[1] || "").length;
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
return valInt % stepInt / 10 ** decCount;
}
var ZodNumber = class _ZodNumber extends ZodType {
constructor() {
super(...arguments);
this.min = this.gte;
this.max = this.lte;
this.step = this.multipleOf;
}
_parse(input) {
if (this._def.coerce) {
input.data = Number(input.data);
}
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.number) {
const ctx2 = this._getOrReturnCtx(input);
addIssueToContext(ctx2, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.number,
received: ctx2.parsedType
});
return INVALID;
}
let ctx = void 0;
const status = new ParseStatus();
for (const check of this._def.checks) {
if (check.kind === "int") {
if (!util.isInteger(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: "integer",
received: "float",
message: check.message
});
status.dirty();
}
} else if (check.kind === "min") {
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
if (tooSmall) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.too_small,
minimum: check.value,
type: "number",
inclusive: check.inclusive,
exact: false,
message: check.message
});
status.dirty();
}
} else if (check.kind === "max") {
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
if (tooBig) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.too_big,
maximum: check.value,
type: "number",
inclusive: check.inclusive,
exact: false,
message: check.message
});
status.dirty();
}
} else if (check.kind === "multipleOf") {
if (floatSafeRemainder(input.data, check.value) !== 0) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.not_multiple_of,
multipleOf: check.value,
message: check.message
});
status.dirty();
}
} else if (check.kind === "finite") {
if (!Number.isFinite(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.not_finite,
message: check.message
});
status.dirty();
}
} else {
util.assertNever(check);
}
}
return { status: status.value, value: input.data };
}
gte(value, message) {
return this.setLimit("min", value, true, errorUtil.toString(message));
}
gt(value, message) {
return this.setLimit("min", value, false, errorUtil.toString(message));
}
lte(value, message) {
return this.setLimit("max", value, true, errorUtil.toString(message));
}
lt(value, message) {
return this.setLimit("max", value, false, errorUtil.toString(message));
}
setLimit(kind, value, inclusive, message) {
return new _ZodNumber({
...this._def,
checks: [
...this._def.checks,
{
kind,
value,
inclusive,
message: errorUtil.toString(message)
}
]
});
}
_addCheck(check) {
return new _ZodNumber({
...this._def,
checks: [...this._def.checks, check]
});
}
int(message) {
return this._addCheck({
kind: "int",
message: errorUtil.toString(message)
});
}
positive(message) {
return this._addCheck({
kind: "min",
value: 0,
inclusive: false,
message: errorUtil.toString(message)
});
}
negative(message) {
return this._addCheck({
kind: "max",
value: 0,
inclusive: false,
message: errorUtil.toString(message)
});
}
nonpositive(message) {
return this._addCheck({
kind: "max",
value: 0,
inclusive: true,
message: errorUtil.toString(message)
});
}
nonnegative(message) {
return this._addCheck({
kind: "min",
value: 0,
inclusive: true,
message: errorUtil.toString(message)
});
}
multipleOf(value, message) {
return this._addCheck({
kind: "multipleOf",
value,
message: errorUtil.toString(message)
});
}
finite(message) {
return this._addCheck({
kind: "finite",
message: errorUtil.toString(message)
});
}
safe(message) {
return this._addCheck({
kind: "min",
inclusive: true,
value: Number.MIN_SAFE_INTEGER,
message: errorUtil.toString(message)
})._addCheck({
kind: "max",
inclusive: true,
value: Number.MAX_SAFE_INTEGER,
message: errorUtil.toString(message)
});
}
get minValue() {
let min = null;
for (const ch of this._def.checks) {
if (ch.kind === "min") {
if (min === null || ch.value > min)
min = ch.value;
}
}
return min;
}
get maxValue() {
let max = null;
for (const ch of this._def.checks) {
if (ch.kind === "max") {
if (max === null || ch.value < max)
max = ch.value;
}
}
return max;
}
get isInt() {
return !!this._def.checks.find((ch) => ch.kind === "int" || ch.kind === "multipleOf" && util.isInteger(ch.value));
}
get isFinite() {
let max = null;
let min = null;
for (const ch of this._def.checks) {
if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
return true;
} else if (ch.kind === "min") {
if (min === null || ch.value > min)
min = ch.value;
} else if (ch.kind === "max") {
if (max === null || ch.value < max)
max = ch.value;
}
}
return Number.isFinite(min) && Number.isFinite(max);
}
};
ZodNumber.create = (params) => {
return new ZodNumber({
checks: [],
typeName: ZodFirstPartyTypeKind.ZodNumber,
coerce: params?.coerce || false,
...processCreateParams(params)
});
};
var ZodBigInt = class _ZodBigInt extends ZodType {
constructor() {
super(...arguments);
this.min = this.gte;
this.max = this.lte;
}
_parse(input) {
if (this._def.coerce) {
try {
input.data = BigInt(input.data);
} catch {
return this._getInvalidInput(input);
}
}
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.bigint) {
return this._getInvalidInput(input);
}
let ctx = void 0;
const status = new ParseStatus();
for (const check of this._def.checks) {
if (check.kind === "min") {
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
if (tooSmall) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.too_small,
type: "bigint",
minimum: check.value,
inclusive: check.inclusive,
message: check.message
});
status.dirty();
}
} else if (check.kind === "max") {
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
if (tooBig) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.too_big,
type: "bigint",
maximum: check.value,
inclusive: check.inclusive,
message: check.message
});
status.dirty();
}
} else if (check.kind === "multipleOf") {
if (input.data % check.value !== BigInt(0)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.not_multiple_of,
multipleOf: check.value,
message: check.message
});
status.dirty();
}
} else {
util.assertNever(check);
}
}
return { status: status.value, value: input.data };
}
_getInvalidInput(input) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.bigint,
received: ctx.parsedType
});
return INVALID;
}
gte(value, message) {
return this.setLimit("min", value, true, errorUtil.toString(message));
}
gt(value, message) {
return this.setLimit("min", value, false, errorUtil.toString(message));
}
lte(value, message) {
return this.setLimit("max", value, true, errorUtil.toString(message));
}
lt(value, message) {
return this.setLimit("max", value, false, errorUtil.toString(message));
}
setLimit(kind, value, inclusive, message) {
return new _ZodBigInt({
...this._def,
checks: [
...this._def.checks,
{
kind,
value,
inclusive,
message: errorUtil.toString(message)
}
]
});
}
_addCheck(check) {
return new _ZodBigInt({
...this._def,
checks: [...this._def.checks, check]
});
}
positive(message) {
return this._addCheck({
kind: "min",
value: BigInt(0),
inclusive: false,
message: errorUtil.toString(message)
});
}
negative(message) {
return this._addCheck({
kind: "max",
value: BigInt(0),
inclusive: false,
message: errorUtil.toString(message)
});
}
nonpositive(message) {
return this._addCheck({
kind: "max",
value: BigInt(0),
inclusive: true,
message: errorUtil.toString(message)
});
}
nonnegative(message) {
return this._addCheck({
kind: "min",
value: BigInt(0),
inclusive: true,
message: errorUtil.toString(message)
});
}
multipleOf(value, message) {
return this._addCheck({
kind: "multipleOf",
value,
message: errorUtil.toString(message)
});
}
get minValue() {
let min = null;
for (const ch of this._def.checks) {
if (ch.kind === "min") {
if (min === null || ch.value > min)
min = ch.value;
}
}
return min;
}
get maxValue() {
let max = null;
for (const ch of this._def.checks) {
if (ch.kind === "max") {
if (max === null || ch.value < max)
max = ch.value;
}
}
return max;
}
};
ZodBigInt.create = (params) => {
return new ZodBigInt({
checks: [],
typeName: ZodFirstPartyTypeKind.ZodBigInt,
coerce: params?.coerce ?? false,
...processCreateParams(params)
});
};
var ZodBoolean = class extends ZodType {
_parse(input) {
if (this._def.coerce) {
input.data = Boolean(input.data);
}
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.boolean) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.boolean,
received: ctx.parsedType
});
return INVALID;
}
return OK(input.data);
}
};
ZodBoolean.create = (params) => {
return new ZodBoolean({
typeName: ZodFirstPartyTypeKind.ZodBoolean,
coerce: params?.coerce || false,
...processCreateParams(params)
});
};
var ZodDate = class _ZodDate extends ZodType {
_parse(input) {
if (this._def.coerce) {
input.data = new Date(input.data);
}
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.date) {
const ctx2 = this._getOrReturnCtx(input);
addIssueToContext(ctx2, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.date,
received: ctx2.parsedType
});
return INVALID;
}
if (Number.isNaN(input.data.getTime())) {
const ctx2 = this._getOrReturnCtx(input);
addIssueToContext(ctx2, {
code: ZodIssueCode.invalid_date
});
return INVALID;
}
const status = new ParseStatus();
let ctx = void 0;
for (const check of this._def.checks) {
if (check.kind === "min") {
if (input.data.getTime() < check.value) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.too_small,
message: check.message,
inclusive: true,
exact: false,
minimum: check.value,
type: "date"
});
status.dirty();
}
} else if (check.kind === "max") {
if (input.data.getTime() > check.value) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.too_big,
message: check.message,
inclusive: true,
exact: false,
maximum: check.value,
type: "date"
});
status.dirty();
}
} else {
util.assertNever(check);
}
}
return {
status: status.value,
value: new Date(input.data.getTime())
};
}
_addCheck(check) {
return new _ZodDate({
...this._def,
checks: [...this._def.checks, check]
});
}
min(minDate, message) {
return this._addCheck({
kind: "min",
value: minDate.getTime(),
message: errorUtil.toString(message)
});
}
max(maxDate, message) {
return this._addCheck({
kind: "max",
value: maxDate.getTime(),
message: errorUtil.toString(message)
});
}
get minDate() {
let min = null;
for (const ch of this._def.checks) {
if (ch.kind === "min") {
if (min === null || ch.value > min)
min = ch.value;
}
}
return min != null ? new Date(min) : null;
}
get maxDate() {
let max = null;
for (const ch of this._def.checks) {
if (ch.kind === "max") {
if (max === null || ch.value < max)
max = ch.value;
}
}
return max != null ? new Date(max) : null;
}
};
ZodDate.create = (params) => {
return new ZodDate({
checks: [],
coerce: params?.coerce || false,
typeName: ZodFirstPartyTypeKind.ZodDate,
...processCreateParams(params)
});
};
var ZodSymbol = class extends ZodType {
_parse(input) {
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.symbol) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.symbol,
received: ctx.parsedType
});
return INVALID;
}
return OK(input.data);
}
};
ZodSymbol.create = (params) => {
return new ZodSymbol({
typeName: ZodFirstPartyTypeKind.ZodSymbol,
...processCreateParams(params)
});
};
var ZodUndefined = class extends ZodType {
_parse(input) {
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.undefined) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.undefined,
received: ctx.parsedType
});
return INVALID;
}
return OK(input.data);
}
};
ZodUndefined.create = (params) => {
return new ZodUndefined({
typeName: ZodFirstPartyTypeKind.ZodUndefined,
...processCreateParams(params)
});
};
var ZodNull = class extends ZodType {
_parse(input) {
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.null) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.null,
received: ctx.parsedType
});
return INVALID;
}
return OK(input.data);
}
};
ZodNull.create = (params) => {
return new ZodNull({
typeName: ZodFirstPartyTypeKind.ZodNull,
...processCreateParams(params)
});
};
var ZodAny = class extends ZodType {
constructor() {
super(...arguments);
this._any = true;
}
_parse(input) {
return OK(input.data);
}
};
ZodAny.create = (params) => {
return new ZodAny({
typeName: ZodFirstPartyTypeKind.ZodAny,
...processCreateParams(params)
});
};
var ZodUnknown = class extends ZodType {
constructor() {
super(...arguments);
this._unknown = true;
}
_parse(input) {
return OK(input.data);
}
};
ZodUnknown.create = (params) => {
return new ZodUnknown({
typeName: ZodFirstPartyTypeKind.ZodUnknown,
...processCreateParams(params)
});
};
var ZodNever = class extends ZodType {
_parse(input) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.never,
received: ctx.parsedType
});
return INVALID;
}
};
ZodNever.create = (params) => {
return new ZodNever({
typeName: ZodFirstPartyTypeKind.ZodNever,
...processCreateParams(params)
});
};
var ZodVoid = class extends ZodType {
_parse(input) {
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.undefined) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.void,
received: ctx.parsedType
});
return INVALID;
}
return OK(input.data);
}
};
ZodVoid.create = (params) => {
return new ZodVoid({
typeName: ZodFirstPartyTypeKind.ZodVoid,
...processCreateParams(params)
});
};
var ZodArray = class _ZodArray extends ZodType {
_parse(input) {
const { ctx, status } = this._processInputParams(input);
const def = this._def;
if (ctx.parsedType !== ZodParsedType.array) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.array,
received: ctx.parsedType
});
return INVALID;
}
if (def.exactLength !== null) {
const tooBig = ctx.data.length > def.exactLength.value;
const tooSmall = ctx.data.length < def.exactLength.value;
if (tooBig || tooSmall) {
addIssueToContext(ctx, {
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
minimum: tooSmall ? def.exactLength.value : void 0,
maximum: tooBig ? def.exactLength.value : void 0,
type: "array",
inclusive: true,
exact: true,
message: def.exactLength.message
});
status.dirty();
}
}
if (def.minLength !== null) {
if (ctx.data.length < def.minLength.value) {
addIssueToContext(ctx, {
code: ZodIssueCode.too_small,
minimum: def.minLength.value,
type: "array",
inclusive: true,
exact: false,
message: def.minLength.message
});
status.dirty();
}
}
if (def.maxLength !== null) {
if (ctx.data.length > def.maxLength.value) {
addIssueToContext(ctx, {
code: ZodIssueCode.too_big,
maximum: def.maxLength.value,
type: "array",
inclusive: true,
exact: false,
message: def.maxLength.message
});
status.dirty();
}
}
if (ctx.common.async) {
return Promise.all([...ctx.data].map((item, i) => {
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
})).then((result2) => {
return ParseStatus.mergeArray(status, result2);
});
}
const result = [...ctx.data].map((item, i) => {
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
});
return ParseStatus.mergeArray(status, result);
}
get element() {
return this._def.type;
}
min(minLength, message) {
return new _ZodArray({
...this._def,
minLength: { value: minLength, message: errorUtil.toString(message) }
});
}
max(maxLength, message) {
return new _ZodArray({
...this._def,
maxLength: { value: maxLength, message: errorUtil.toString(message) }
});
}
length(len, message) {
return new _ZodArray({
...this._def,
exactLength: { value: len, message: errorUtil.toString(message) }
});
}
nonempty(message) {
return this.min(1, message);
}
};
ZodArray.create = (schema, params) => {
return new ZodArray({
type: schema,
minLength: null,
maxLength: null,
exactLength: null,
typeName: ZodFirstPartyTypeKind.ZodArray,
...processCreateParams(params)
});
};
function deepPartialify(schema) {
if (schema instanceof ZodObject) {
const newShape = {};
for (const key in schema.shape) {
const fieldSchema = schema.shape[key];
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
}
return new ZodObject({
...schema._def,
shape: () => newShape
});
} else if (schema instanceof ZodArray) {
return new ZodArray({
...schema._def,
type: deepPartialify(schema.element)
});
} else if (schema instanceof ZodOptional) {
return ZodOptional.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodNullable) {
return ZodNullable.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodTuple) {
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
} else {
return schema;
}
}
var ZodObject = class _ZodObject extends ZodType {
constructor() {
super(...arguments);
this._cached = null;
this.nonstrict = this.passthrough;
this.augment = this.extend;
}
_getCached() {
if (this._cached !== null)
return this._cached;
const shape = this._def.shape();
const keys = util.objectKeys(shape);
this._cached = { shape, keys };
return this._cached;
}
_parse(input) {
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.object) {
const ctx2 = this._getOrReturnCtx(input);
addIssueToContext(ctx2, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.object,
received: ctx2.parsedType
});
return INVALID;
}
const { status, ctx } = this._processInputParams(input);
const { shape, keys: shapeKeys } = this._getCached();
const extraKeys = [];
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
for (const key in ctx.data) {
if (!shapeKeys.includes(key)) {
extraKeys.push(key);
}
}
}
const pairs = [];
for (const key of shapeKeys) {
const keyValidator = shape[key];
const value = ctx.data[key];
pairs.push({
key: { status: "valid", value: key },
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
alwaysSet: key in ctx.data
});
}
if (this._def.catchall instanceof ZodNever) {
const unknownKeys = this._def.unknownKeys;
if (unknownKeys === "passthrough") {
for (const key of extraKeys) {
pairs.push({
key: { status: "valid", value: key },
value: { status: "valid", value: ctx.data[key] }
});
}
} else if (unknownKeys === "strict") {
if (extraKeys.length > 0) {
addIssueToContext(ctx, {
code: ZodIssueCode.unrecognized_keys,
keys: extraKeys
});
status.dirty();
}
} else if (unknownKeys === "strip") {
} else {
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
}
} else {
const catchall = this._def.catchall;
for (const key of extraKeys) {
const value = ctx.data[key];
pairs.push({
key: { status: "valid", value: key },
value: catchall._parse(
new ParseInputLazyPath(ctx, value, ctx.path, key)
//, ctx.child(key), value, getParsedType(value)
),
alwaysSet: key in ctx.data
});
}
}
if (ctx.common.async) {
return Promise.resolve().then(async () => {
const syncPairs = [];
for (const pair of pairs) {
const key = await pair.key;
const value = await pair.value;
syncPairs.push({
key,
value,
alwaysSet: pair.alwaysSet
});
}
return syncPairs;
}).then((syncPairs) => {
return ParseStatus.mergeObjectSync(status, syncPairs);
});
} else {
return ParseStatus.mergeObjectSync(status, pairs);
}
}
get shape() {
return this._def.shape();
}
strict(message) {
errorUtil.errToObj;
return new _ZodObject({
...this._def,
unknownKeys: "strict",
...message !== void 0 ? {
errorMap: (issue, ctx) => {
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
if (issue.code === "unrecognized_keys")
return {
message: errorUtil.errToObj(message).message ?? defaultError
};
return {
message: defaultError
};
}
} : {}
});
}
strip() {
return new _ZodObject({
...this._def,
unknownKeys: "strip"
});
}
passthrough() {
return new _ZodObject({
...this._def,
unknownKeys: "passthrough"
});
}
// const AugmentFactory =
// <Def extends ZodObjectDef>(def: Def) =>
// <Augmentation extends ZodRawShape>(
// augmentation: Augmentation
// ): ZodObject<
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
// Def["unknownKeys"],
// Def["catchall"]
// > => {
// return new ZodObject({
// ...def,
// shape: () => ({
// ...def.shape(),
// ...augmentation,
// }),
// }) as any;
// };
extend(augmentation) {
return new _ZodObject({
...this._def,
shape: () => ({
...this._def.shape(),
...augmentation
})
});
}
/**
* Prior to zod@1.0.12 there was a bug in the
* inferred type of merged objects. Please
* upgrade if you are experiencing issues.
*/
merge(merging) {
const merged = new _ZodObject({
unknownKeys: merging._def.unknownKeys,
catchall: merging._def.catchall,
shape: () => ({
...this._def.shape(),
...merging._def.shape()
}),
typeName: ZodFirstPartyTypeKind.ZodObject
});
return merged;
}
// merge<
// Incoming extends AnyZodObject,
// Augmentation extends Incoming["shape"],
// NewOutput extends {
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
// ? Augmentation[k]["_output"]
// : k extends keyof Output
// ? Output[k]
// : never;
// },
// NewInput extends {
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
// ? Augmentation[k]["_input"]
// : k extends keyof Input
// ? Input[k]
// : never;
// }
// >(
// merging: Incoming
// ): ZodObject<
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
// Incoming["_def"]["unknownKeys"],
// Incoming["_def"]["catchall"],
// NewOutput,
// NewInput
// > {
// const merged: any = new ZodObject({
// unknownKeys: merging._def.unknownKeys,
// catchall: merging._def.catchall,
// shape: () =>
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
// typeName: ZodFirstPartyTypeKind.ZodObject,
// }) as any;
// return merged;
// }
setKey(key, schema) {
return this.augment({ [key]: schema });
}
// merge<Incoming extends AnyZodObject>(
// merging: Incoming
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
// ZodObject<
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
// Incoming["_def"]["unknownKeys"],
// Incoming["_def"]["catchall"]
// > {
// // const mergedShape = objectUtil.mergeShapes(
// // this._def.shape(),
// // merging._def.shape()
// // );
// const merged: any = new ZodObject({
// unknownKeys: merging._def.unknownKeys,
// catchall: merging._def.catchall,
// shape: () =>
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
// typeName: ZodFirstPartyTypeKind.ZodObject,
// }) as any;
// return merged;
// }
catchall(index) {
return new _ZodObject({
...this._def,
catchall: index
});
}
pick(mask) {
const shape = {};
for (const key of util.objectKeys(mask)) {
if (mask[key] && this.shape[key]) {
shape[key] = this.shape[key];
}
}
return new _ZodObject({
...this._def,
shape: () => shape
});
}
omit(mask) {
const shape = {};
for (const key of util.objectKeys(this.shape)) {
if (!mask[key]) {
shape[key] = this.shape[key];
}
}
return new _ZodObject({
...this._def,
shape: () => shape
});
}
/**
* @deprecated
*/
deepPartial() {
return deepPartialify(this);
}
partial(mask) {
const newShape = {};
for (const key of util.objectKeys(this.shape)) {
const fieldSchema = this.shape[key];
if (mask && !mask[key]) {
newShape[key] = fieldSchema;
} else {
newShape[key] = fieldSchema.optional();
}
}
return new _ZodObject({
...this._def,
shape: () => newShape
});
}
required(mask) {
const newShape = {};
for (const key of util.objectKeys(this.shape)) {
if (mask && !mask[key]) {
newShape[key] = this.shape[key];
} else {
const fieldSchema = this.shape[key];
let newField = fieldSchema;
while (newField instanceof ZodOptional) {
newField = newField._def.innerType;
}
newShape[key] = newField;
}
}
return new _ZodObject({
...this._def,
shape: () => newShape
});
}
keyof() {
return createZodEnum(util.objectKeys(this.shape));
}
};
ZodObject.create = (shape, params) => {
return new ZodObject({
shape: () => shape,
unknownKeys: "strip",
catchall: ZodNever.create(),
typeName: ZodFirstPartyTypeKind.ZodObject,
...processCreateParams(params)
});
};
ZodObject.strictCreate = (shape, params) => {
return new ZodObject({
shape: () => shape,
unknownKeys: "strict",
catchall: ZodNever.create(),
typeName: ZodFirstPartyTypeKind.ZodObject,
...processCreateParams(params)
});
};
ZodObject.lazycreate = (shape, params) => {
return new ZodObject({
shape,
unknownKeys: "strip",
catchall: ZodNever.create(),
typeName: ZodFirstPartyTypeKind.ZodObject,
...processCreateParams(params)
});
};
var ZodUnion = class extends ZodType {
_parse(input) {
const { ctx } = this._processInputParams(input);
const options = this._def.options;
function handleResults(results) {
for (const result of results) {
if (result.result.status === "valid") {
return result.result;
}
}
for (const result of results) {
if (result.result.status === "dirty") {
ctx.common.issues.push(...result.ctx.common.issues);
return result.result;
}
}
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_union,
unionErrors
});
return INVALID;
}
if (ctx.common.async) {
return Promise.all(options.map(async (option) => {
const childCtx = {
...ctx,
common: {
...ctx.common,
issues: []
},
parent: null
};
return {
result: await option._parseAsync({
data: ctx.data,
path: ctx.path,
parent: childCtx
}),
ctx: childCtx
};
})).then(handleResults);
} else {
let dirty = void 0;
const issues = [];
for (const option of options) {
const childCtx = {
...ctx,
common: {
...ctx.common,
issues: []
},
parent: null
};
const result = option._parseSync({
data: ctx.data,
path: ctx.path,
parent: childCtx
});
if (result.status === "valid") {
return result;
} else if (result.status === "dirty" && !dirty) {
dirty = { result, ctx: childCtx };
}
if (childCtx.common.issues.length) {
issues.push(childCtx.common.issues);
}
}
if (dirty) {
ctx.common.issues.push(...dirty.ctx.common.issues);
return dirty.result;
}
const unionErrors = issues.map((issues2) => new ZodError(issues2));
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_union,
unionErrors
});
return INVALID;
}
}
get options() {
return this._def.options;
}
};
ZodUnion.create = (types, params) => {
return new ZodUnion({
options: types,
typeName: ZodFirstPartyTypeKind.ZodUnion,
...processCreateParams(params)
});
};
var getDiscriminator = (type) => {
if (type instanceof ZodLazy) {
return getDiscriminator(type.schema);
} else if (type instanceof ZodEffects) {
return getDiscriminator(type.innerType());
} else if (type instanceof ZodLiteral) {
return [type.value];
} else if (type instanceof ZodEnum) {
return type.options;
} else if (type instanceof ZodNativeEnum) {
return util.objectValues(type.enum);
} else if (type instanceof ZodDefault) {
return getDiscriminator(type._def.innerType);
} else if (type instanceof ZodUndefined) {
return [void 0];
} else if (type instanceof ZodNull) {
return [null];
} else if (type instanceof ZodOptional) {
return [void 0, ...getDiscriminator(type.unwrap())];
} else if (type instanceof ZodNullable) {
return [null, ...getDiscriminator(type.unwrap())];
} else if (type instanceof ZodBranded) {
return getDiscriminator(type.unwrap());
} else if (type instanceof ZodReadonly) {
return getDiscriminator(type.unwrap());
} else if (type instanceof ZodCatch) {
return getDiscriminator(type._def.innerType);
} else {
return [];
}
};
var ZodDiscriminatedUnion = class _ZodDiscriminatedUnion extends ZodType {
_parse(input) {
const { ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.object) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.object,
received: ctx.parsedType
});
return INVALID;
}
const discriminator = this.discriminator;
const discriminatorValue = ctx.data[discriminator];
const option = this.optionsMap.get(discriminatorValue);
if (!option) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_union_discriminator,
options: Array.from(this.optionsMap.keys()),
path: [discriminator]
});
return INVALID;
}
if (ctx.common.async) {
return option._parseAsync({
data: ctx.data,
path: ctx.path,
parent: ctx
});
} else {
return option._parseSync({
data: ctx.data,
path: ctx.path,
parent: ctx
});
}
}
get discriminator() {
return this._def.discriminator;
}
get options() {
return this._def.options;
}
get optionsMap() {
return this._def.optionsMap;
}
/**
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
* have a different value for each object in the union.
* @param discriminator the name of the discriminator property
* @param types an array of object schemas
* @param params
*/
static create(discriminator, options, params) {
const optionsMap = /* @__PURE__ */ new Map();
for (const type of options) {
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
if (!discriminatorValues.length) {
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
}
for (const value of discriminatorValues) {
if (optionsMap.has(value)) {
throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
}
optionsMap.set(value, type);
}
}
return new _ZodDiscriminatedUnion({
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
discriminator,
options,
optionsMap,
...processCreateParams(params)
});
}
};
function mergeValues(a, b) {
const aType = getParsedType(a);
const bType = getParsedType(b);
if (a === b) {
return { valid: true, data: a };
} else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
const bKeys = util.objectKeys(b);
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
const newObj = { ...a, ...b };
for (const key of sharedKeys) {
const sharedValue = mergeValues(a[key], b[key]);
if (!sharedValue.valid) {
return { valid: false };
}
newObj[key] = sharedValue.data;
}
return { valid: true, data: newObj };
} else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
if (a.length !== b.length) {
return { valid: false };
}
const newArray = [];
for (let index = 0; index < a.length; index++) {
const itemA = a[index];
const itemB = b[index];
const sharedValue = mergeValues(itemA, itemB);
if (!sharedValue.valid) {
return { valid: false };
}
newArray.push(sharedValue.data);
}
return { valid: true, data: newArray };
} else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
return { valid: true, data: a };
} else {
return { valid: false };
}
}
var ZodIntersection = class extends ZodType {
_parse(input) {
const { status, ctx } = this._processInputParams(input);
const handleParsed = (parsedLeft, parsedRight) => {
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
return INVALID;
}
const merged = mergeValues(parsedLeft.value, parsedRight.value);
if (!merged.valid) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_intersection_types
});
return INVALID;
}
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
status.dirty();
}
return { status: status.value, value: merged.data };
};
if (ctx.common.async) {
return Promise.all([
this._def.left._parseAsync({
data: ctx.data,
path: ctx.path,
parent: ctx
}),
this._def.right._parseAsync({
data: ctx.data,
path: ctx.path,
parent: ctx
})
]).then(([left, right]) => handleParsed(left, right));
} else {
return handleParsed(this._def.left._parseSync({
data: ctx.data,
path: ctx.path,
parent: ctx
}), this._def.right._parseSync({
data: ctx.data,
path: ctx.path,
parent: ctx
}));
}
}
};
ZodIntersection.create = (left, right, params) => {
return new ZodIntersection({
left,
right,
typeName: ZodFirstPartyTypeKind.ZodIntersection,
...processCreateParams(params)
});
};
var ZodTuple = class _ZodTuple extends ZodType {
_parse(input) {
const { status, ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.array) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.array,
received: ctx.parsedType
});
return INVALID;
}
if (ctx.data.length < this._def.items.length) {
addIssueToContext(ctx, {
code: ZodIssueCode.too_small,
minimum: this._def.items.length,
inclusive: true,
exact: false,
type: "array"
});
return INVALID;
}
const rest = this._def.rest;
if (!rest && ctx.data.length > this._def.items.length) {
addIssueToContext(ctx, {
code: ZodIssueCode.too_big,
maximum: this._def.items.length,
inclusive: true,
exact: false,
type: "array"
});
status.dirty();
}
const items = [...ctx.data].map((item, itemIndex) => {
const schema = this._def.items[itemIndex] || this._def.rest;
if (!schema)
return null;
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
}).filter((x) => !!x);
if (ctx.common.async) {
return Promise.all(items).then((results) => {
return ParseStatus.mergeArray(status, results);
});
} else {
return ParseStatus.mergeArray(status, items);
}
}
get items() {
return this._def.items;
}
rest(rest) {
return new _ZodTuple({
...this._def,
rest
});
}
};
ZodTuple.create = (schemas, params) => {
if (!Array.isArray(schemas)) {
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
}
return new ZodTuple({
items: schemas,
typeName: ZodFirstPartyTypeKind.ZodTuple,
rest: null,
...processCreateParams(params)
});
};
var ZodRecord = class _ZodRecord extends ZodType {
get keySchema() {
return this._def.keyType;
}
get valueSchema() {
return this._def.valueType;
}
_parse(input) {
const { status, ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.object) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.object,
received: ctx.parsedType
});
return INVALID;
}
const pairs = [];
const keyType = this._def.keyType;
const valueType = this._def.valueType;
for (const key in ctx.data) {
pairs.push({
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
alwaysSet: key in ctx.data
});
}
if (ctx.common.async) {
return ParseStatus.mergeObjectAsync(status, pairs);
} else {
return ParseStatus.mergeObjectSync(status, pairs);
}
}
get element() {
return this._def.valueType;
}
static create(first, second, third) {
if (second instanceof ZodType) {
return new _ZodRecord({
keyType: first,
valueType: second,
typeName: ZodFirstPartyTypeKind.ZodRecord,
...processCreateParams(third)
});
}
return new _ZodRecord({
keyType: ZodString.create(),
valueType: first,
typeName: ZodFirstPartyTypeKind.ZodRecord,
...processCreateParams(second)
});
}
};
var ZodMap = class extends ZodType {
get keySchema() {
return this._def.keyType;
}
get valueSchema() {
return this._def.valueType;
}
_parse(input) {
const { status, ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.map) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.map,
received: ctx.parsedType
});
return INVALID;
}
const keyType = this._def.keyType;
const valueType = this._def.valueType;
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
return {
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"]))
};
});
if (ctx.common.async) {
const finalMap = /* @__PURE__ */ new Map();
return Promise.resolve().then(async () => {
for (const pair of pairs) {
const key = await pair.key;
const value = await pair.value;
if (key.status === "aborted" || value.status === "aborted") {
return INVALID;
}
if (key.status === "dirty" || value.status === "dirty") {
status.dirty();
}
finalMap.set(key.value, value.value);
}
return { status: status.value, value: finalMap };
});
} else {
const finalMap = /* @__PURE__ */ new Map();
for (const pair of pairs) {
const key = pair.key;
const value = pair.value;
if (key.status === "aborted" || value.status === "aborted") {
return INVALID;
}
if (key.status === "dirty" || value.status === "dirty") {
status.dirty();
}
finalMap.set(key.value, value.value);
}
return { status: status.value, value: finalMap };
}
}
};
ZodMap.create = (keyType, valueType, params) => {
return new ZodMap({
valueType,
keyType,
typeName: ZodFirstPartyTypeKind.ZodMap,
...processCreateParams(params)
});
};
var ZodSet = class _ZodSet extends ZodType {
_parse(input) {
const { status, ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.set) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.set,
received: ctx.parsedType
});
return INVALID;
}
const def = this._def;
if (def.minSize !== null) {
if (ctx.data.size < def.minSize.value) {
addIssueToContext(ctx, {
code: ZodIssueCode.too_small,
minimum: def.minSize.value,
type: "set",
inclusive: true,
exact: false,
message: def.minSize.message
});
status.dirty();
}
}
if (def.maxSize !== null) {
if (ctx.data.size > def.maxSize.value) {
addIssueToContext(ctx, {
code: ZodIssueCode.too_big,
maximum: def.maxSize.value,
type: "set",
inclusive: true,
exact: false,
message: def.maxSize.message
});
status.dirty();
}
}
const valueType = this._def.valueType;
function finalizeSet(elements2) {
const parsedSet = /* @__PURE__ */ new Set();
for (const element of elements2) {
if (element.status === "aborted")
return INVALID;
if (element.status === "dirty")
status.dirty();
parsedSet.add(element.value);
}
return { status: status.value, value: parsedSet };
}
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
if (ctx.common.async) {
return Promise.all(elements).then((elements2) => finalizeSet(elements2));
} else {
return finalizeSet(elements);
}
}
min(minSize, message) {
return new _ZodSet({
...this._def,
minSize: { value: minSize, message: errorUtil.toString(message) }
});
}
max(maxSize, message) {
return new _ZodSet({
...this._def,
maxSize: { value: maxSize, message: errorUtil.toString(message) }
});
}
size(size, message) {
return this.min(size, message).max(size, message);
}
nonempty(message) {
return this.min(1, message);
}
};
ZodSet.create = (valueType, params) => {
return new ZodSet({
valueType,
minSize: null,
maxSize: null,
typeName: ZodFirstPartyTypeKind.ZodSet,
...processCreateParams(params)
});
};
var ZodFunction = class _ZodFunction extends ZodType {
constructor() {
super(...arguments);
this.validate = this.implement;
}
_parse(input) {
const { ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.function) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.function,
received: ctx.parsedType
});
return INVALID;
}
function makeArgsIssue(args, error) {
return makeIssue({
data: args,
path: ctx.path,
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), en_default].filter((x) => !!x),
issueData: {
code: ZodIssueCode.invalid_arguments,
argumentsError: error
}
});
}
function makeReturnsIssue(returns, error) {
return makeIssue({
data: returns,
path: ctx.path,
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), en_default].filter((x) => !!x),
issueData: {
code: ZodIssueCode.invalid_return_type,
returnTypeError: error
}
});
}
const params = { errorMap: ctx.common.contextualErrorMap };
const fn = ctx.data;
if (this._def.returns instanceof ZodPromise) {
const me = this;
return OK(async function(...args) {
const error = new ZodError([]);
const parsedArgs = await me._def.args.parseAsync(args, params).catch((e) => {
error.addIssue(makeArgsIssue(args, e));
throw error;
});
const result = await Reflect.apply(fn, this, parsedArgs);
const parsedReturns = await me._def.returns._def.type.parseAsync(result, params).catch((e) => {
error.addIssue(makeReturnsIssue(result, e));
throw error;
});
return parsedReturns;
});
} else {
const me = this;
return OK(function(...args) {
const parsedArgs = me._def.args.safeParse(args, params);
if (!parsedArgs.success) {
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
}
const result = Reflect.apply(fn, this, parsedArgs.data);
const parsedReturns = me._def.returns.safeParse(result, params);
if (!parsedReturns.success) {
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
}
return parsedReturns.data;
});
}
}
parameters() {
return this._def.args;
}
returnType() {
return this._def.returns;
}
args(...items) {
return new _ZodFunction({
...this._def,
args: ZodTuple.create(items).rest(ZodUnknown.create())
});
}
returns(returnType) {
return new _ZodFunction({
...this._def,
returns: returnType
});
}
implement(func) {
const validatedFunc = this.parse(func);
return validatedFunc;
}
strictImplement(func) {
const validatedFunc = this.parse(func);
return validatedFunc;
}
static create(args, returns, params) {
return new _ZodFunction({
args: args ? args : ZodTuple.create([]).rest(ZodUnknown.create()),
returns: returns || ZodUnknown.create(),
typeName: ZodFirstPartyTypeKind.ZodFunction,
...processCreateParams(params)
});
}
};
var ZodLazy = class extends ZodType {
get schema() {
return this._def.getter();
}
_parse(input) {
const { ctx } = this._processInputParams(input);
const lazySchema = this._def.getter();
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
}
};
ZodLazy.create = (getter, params) => {
return new ZodLazy({
getter,
typeName: ZodFirstPartyTypeKind.ZodLazy,
...processCreateParams(params)
});
};
var ZodLiteral = class extends ZodType {
_parse(input) {
if (input.data !== this._def.value) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
received: ctx.data,
code: ZodIssueCode.invalid_literal,
expected: this._def.value
});
return INVALID;
}
return { status: "valid", value: input.data };
}
get value() {
return this._def.value;
}
};
ZodLiteral.create = (value, params) => {
return new ZodLiteral({
value,
typeName: ZodFirstPartyTypeKind.ZodLiteral,
...processCreateParams(params)
});
};
function createZodEnum(values, params) {
return new ZodEnum({
values,
typeName: ZodFirstPartyTypeKind.ZodEnum,
...processCreateParams(params)
});
}
var ZodEnum = class _ZodEnum extends ZodType {
_parse(input) {
if (typeof input.data !== "string") {
const ctx = this._getOrReturnCtx(input);
const expectedValues = this._def.values;
addIssueToContext(ctx, {
expected: util.joinValues(expectedValues),
received: ctx.parsedType,
code: ZodIssueCode.invalid_type
});
return INVALID;
}
if (!this._cache) {
this._cache = new Set(this._def.values);
}
if (!this._cache.has(input.data)) {
const ctx = this._getOrReturnCtx(input);
const expectedValues = this._def.values;
addIssueToContext(ctx, {
received: ctx.data,
code: ZodIssueCode.invalid_enum_value,
options: expectedValues
});
return INVALID;
}
return OK(input.data);
}
get options() {
return this._def.values;
}
get enum() {
const enumValues = {};
for (const val of this._def.values) {
enumValues[val] = val;
}
return enumValues;
}
get Values() {
const enumValues = {};
for (const val of this._def.values) {
enumValues[val] = val;
}
return enumValues;
}
get Enum() {
const enumValues = {};
for (const val of this._def.values) {
enumValues[val] = val;
}
return enumValues;
}
extract(values, newDef = this._def) {
return _ZodEnum.create(values, {
...this._def,
...newDef
});
}
exclude(values, newDef = this._def) {
return _ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
...this._def,
...newDef
});
}
};
ZodEnum.create = createZodEnum;
var ZodNativeEnum = class extends ZodType {
_parse(input) {
const nativeEnumValues = util.getValidEnumValues(this._def.values);
const ctx = this._getOrReturnCtx(input);
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
const expectedValues = util.objectValues(nativeEnumValues);
addIssueToContext(ctx, {
expected: util.joinValues(expectedValues),
received: ctx.parsedType,
code: ZodIssueCode.invalid_type
});
return INVALID;
}
if (!this._cache) {
this._cache = new Set(util.getValidEnumValues(this._def.values));
}
if (!this._cache.has(input.data)) {
const expectedValues = util.objectValues(nativeEnumValues);
addIssueToContext(ctx, {
received: ctx.data,
code: ZodIssueCode.invalid_enum_value,
options: expectedValues
});
return INVALID;
}
return OK(input.data);
}
get enum() {
return this._def.values;
}
};
ZodNativeEnum.create = (values, params) => {
return new ZodNativeEnum({
values,
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
...processCreateParams(params)
});
};
var ZodPromise = class extends ZodType {
unwrap() {
return this._def.type;
}
_parse(input) {
const { ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.promise,
received: ctx.parsedType
});
return INVALID;
}
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
return OK(promisified.then((data) => {
return this._def.type.parseAsync(data, {
path: ctx.path,
errorMap: ctx.common.contextualErrorMap
});
}));
}
};
ZodPromise.create = (schema, params) => {
return new ZodPromise({
type: schema,
typeName: ZodFirstPartyTypeKind.ZodPromise,
...processCreateParams(params)
});
};
var ZodEffects = class extends ZodType {
innerType() {
return this._def.schema;
}
sourceType() {
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects ? this._def.schema.sourceType() : this._def.schema;
}
_parse(input) {
const { status, ctx } = this._processInputParams(input);
const effect = this._def.effect || null;
const checkCtx = {
addIssue: (arg) => {
addIssueToContext(ctx, arg);
if (arg.fatal) {
status.abort();
} else {
status.dirty();
}
},
get path() {
return ctx.path;
}
};
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
if (effect.type === "preprocess") {
const processed = effect.transform(ctx.data, checkCtx);
if (ctx.common.async) {
return Promise.resolve(processed).then(async (processed2) => {
if (status.value === "aborted")
return INVALID;
const result = await this._def.schema._parseAsync({
data: processed2,
path: ctx.path,
parent: ctx
});
if (result.status === "aborted")
return INVALID;
if (result.status === "dirty")
return DIRTY(result.value);
if (status.value === "dirty")
return DIRTY(result.value);
return result;
});
} else {
if (status.value === "aborted")
return INVALID;
const result = this._def.schema._parseSync({
data: processed,
path: ctx.path,
parent: ctx
});
if (result.status === "aborted")
return INVALID;
if (result.status === "dirty")
return DIRTY(result.value);
if (status.value === "dirty")
return DIRTY(result.value);
return result;
}
}
if (effect.type === "refinement") {
const executeRefinement = (acc) => {
const result = effect.refinement(acc, checkCtx);
if (ctx.common.async) {
return Promise.resolve(result);
}
if (result instanceof Promise) {
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
}
return acc;
};
if (ctx.common.async === false) {
const inner = this._def.schema._parseSync({
data: ctx.data,
path: ctx.path,
parent: ctx
});
if (inner.status === "aborted")
return INVALID;
if (inner.status === "dirty")
status.dirty();
executeRefinement(inner.value);
return { status: status.value, value: inner.value };
} else {
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
if (inner.status === "aborted")
return INVALID;
if (inner.status === "dirty")
status.dirty();
return executeRefinement(inner.value).then(() => {
return { status: status.value, value: inner.value };
});
});
}
}
if (effect.type === "transform") {
if (ctx.common.async === false) {
const base = this._def.schema._parseSync({
data: ctx.data,
path: ctx.path,
parent: ctx
});
if (!isValid(base))
return INVALID;
const result = effect.transform(base.value, checkCtx);
if (result instanceof Promise) {
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
}
return { status: status.value, value: result };
} else {
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
if (!isValid(base))
return INVALID;
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({
status: status.value,
value: result
}));
});
}
}
util.assertNever(effect);
}
};
ZodEffects.create = (schema, effect, params) => {
return new ZodEffects({
schema,
typeName: ZodFirstPartyTypeKind.ZodEffects,
effect,
...processCreateParams(params)
});
};
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
return new ZodEffects({
schema,
effect: { type: "preprocess", transform: preprocess },
typeName: ZodFirstPartyTypeKind.ZodEffects,
...processCreateParams(params)
});
};
var ZodOptional = class extends ZodType {
_parse(input) {
const parsedType = this._getType(input);
if (parsedType === ZodParsedType.undefined) {
return OK(void 0);
}
return this._def.innerType._parse(input);
}
unwrap() {
return this._def.innerType;
}
};
ZodOptional.create = (type, params) => {
return new ZodOptional({
innerType: type,
typeName: ZodFirstPartyTypeKind.ZodOptional,
...processCreateParams(params)
});
};
var ZodNullable = class extends ZodType {
_parse(input) {
const parsedType = this._getType(input);
if (parsedType === ZodParsedType.null) {
return OK(null);
}
return this._def.innerType._parse(input);
}
unwrap() {
return this._def.innerType;
}
};
ZodNullable.create = (type, params) => {
return new ZodNullable({
innerType: type,
typeName: ZodFirstPartyTypeKind.ZodNullable,
...processCreateParams(params)
});
};
var ZodDefault = class extends ZodType {
_parse(input) {
const { ctx } = this._processInputParams(input);
let data = ctx.data;
if (ctx.parsedType === ZodParsedType.undefined) {
data = this._def.defaultValue();
}
return this._def.innerType._parse({
data,
path: ctx.path,
parent: ctx
});
}
removeDefault() {
return this._def.innerType;
}
};
ZodDefault.create = (type, params) => {
return new ZodDefault({
innerType: type,
typeName: ZodFirstPartyTypeKind.ZodDefault,
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
...processCreateParams(params)
});
};
var ZodCatch = class extends ZodType {
_parse(input) {
const { ctx } = this._processInputParams(input);
const newCtx = {
...ctx,
common: {
...ctx.common,
issues: []
}
};
const result = this._def.innerType._parse({
data: newCtx.data,
path: newCtx.path,
parent: {
...newCtx
}
});
if (isAsync(result)) {
return result.then((result2) => {
return {
status: "valid",
value: result2.status === "valid" ? result2.value : this._def.catchValue({
get error() {
return new ZodError(newCtx.common.issues);
},
input: newCtx.data
})
};
});
} else {
return {
status: "valid",
value: result.status === "valid" ? result.value : this._def.catchValue({
get error() {
return new ZodError(newCtx.common.issues);
},
input: newCtx.data
})
};
}
}
removeCatch() {
return this._def.innerType;
}
};
ZodCatch.create = (type, params) => {
return new ZodCatch({
innerType: type,
typeName: ZodFirstPartyTypeKind.ZodCatch,
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
...processCreateParams(params)
});
};
var ZodNaN = class extends ZodType {
_parse(input) {
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.nan) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.nan,
received: ctx.parsedType
});
return INVALID;
}
return { status: "valid", value: input.data };
}
};
ZodNaN.create = (params) => {
return new ZodNaN({
typeName: ZodFirstPartyTypeKind.ZodNaN,
...processCreateParams(params)
});
};
var BRAND = Symbol("zod_brand");
var ZodBranded = class extends ZodType {
_parse(input) {
const { ctx } = this._processInputParams(input);
const data = ctx.data;
return this._def.type._parse({
data,
path: ctx.path,
parent: ctx
});
}
unwrap() {
return this._def.type;
}
};
var ZodPipeline = class _ZodPipeline extends ZodType {
_parse(input) {
const { status, ctx } = this._processInputParams(input);
if (ctx.common.async) {
const handleAsync = async () => {
const inResult = await this._def.in._parseAsync({
data: ctx.data,
path: ctx.path,
parent: ctx
});
if (inResult.status === "aborted")
return INVALID;
if (inResult.status === "dirty") {
status.dirty();
return DIRTY(inResult.value);
} else {
return this._def.out._parseAsync({
data: inResult.value,
path: ctx.path,
parent: ctx
});
}
};
return handleAsync();
} else {
const inResult = this._def.in._parseSync({
data: ctx.data,
path: ctx.path,
parent: ctx
});
if (inResult.status === "aborted")
return INVALID;
if (inResult.status === "dirty") {
status.dirty();
return {
status: "dirty",
value: inResult.value
};
} else {
return this._def.out._parseSync({
data: inResult.value,
path: ctx.path,
parent: ctx
});
}
}
}
static create(a, b) {
return new _ZodPipeline({
in: a,
out: b,
typeName: ZodFirstPartyTypeKind.ZodPipeline
});
}
};
var ZodReadonly = class extends ZodType {
_parse(input) {
const result = this._def.innerType._parse(input);
const freeze = (data) => {
if (isValid(data)) {
data.value = Object.freeze(data.value);
}
return data;
};
return isAsync(result) ? result.then((data) => freeze(data)) : freeze(result);
}
unwrap() {
return this._def.innerType;
}
};
ZodReadonly.create = (type, params) => {
return new ZodReadonly({
innerType: type,
typeName: ZodFirstPartyTypeKind.ZodReadonly,
...processCreateParams(params)
});
};
function cleanParams(params, data) {
const p = typeof params === "function" ? params(data) : typeof params === "string" ? { message: params } : params;
const p2 = typeof p === "string" ? { message: p } : p;
return p2;
}
function custom(check, _params = {}, fatal) {
if (check)
return ZodAny.create().superRefine((data, ctx) => {
const r = check(data);
if (r instanceof Promise) {
return r.then((r2) => {
if (!r2) {
const params = cleanParams(_params, data);
const _fatal = params.fatal ?? fatal ?? true;
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
}
});
}
if (!r) {
const params = cleanParams(_params, data);
const _fatal = params.fatal ?? fatal ?? true;
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
}
return;
});
return ZodAny.create();
}
var late = {
object: ZodObject.lazycreate
};
var ZodFirstPartyTypeKind;
(function(ZodFirstPartyTypeKind2) {
ZodFirstPartyTypeKind2["ZodString"] = "ZodString";
ZodFirstPartyTypeKind2["ZodNumber"] = "ZodNumber";
ZodFirstPartyTypeKind2["ZodNaN"] = "ZodNaN";
ZodFirstPartyTypeKind2["ZodBigInt"] = "ZodBigInt";
ZodFirstPartyTypeKind2["ZodBoolean"] = "ZodBoolean";
ZodFirstPartyTypeKind2["ZodDate"] = "ZodDate";
ZodFirstPartyTypeKind2["ZodSymbol"] = "ZodSymbol";
ZodFirstPartyTypeKind2["ZodUndefined"] = "ZodUndefined";
ZodFirstPartyTypeKind2["ZodNull"] = "ZodNull";
ZodFirstPartyTypeKind2["ZodAny"] = "ZodAny";
ZodFirstPartyTypeKind2["ZodUnknown"] = "ZodUnknown";
ZodFirstPartyTypeKind2["ZodNever"] = "ZodNever";
ZodFirstPartyTypeKind2["ZodVoid"] = "ZodVoid";
ZodFirstPartyTypeKind2["ZodArray"] = "ZodArray";
ZodFirstPartyTypeKind2["ZodObject"] = "ZodObject";
ZodFirstPartyTypeKind2["ZodUnion"] = "ZodUnion";
ZodFirstPartyTypeKind2["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
ZodFirstPartyTypeKind2["ZodIntersection"] = "ZodIntersection";
ZodFirstPartyTypeKind2["ZodTuple"] = "ZodTuple";
ZodFirstPartyTypeKind2["ZodRecord"] = "ZodRecord";
ZodFirstPartyTypeKind2["ZodMap"] = "ZodMap";
ZodFirstPartyTypeKind2["ZodSet"] = "ZodSet";
ZodFirstPartyTypeKind2["ZodFunction"] = "ZodFunction";
ZodFirstPartyTypeKind2["ZodLazy"] = "ZodLazy";
ZodFirstPartyTypeKind2["ZodLiteral"] = "ZodLiteral";
ZodFirstPartyTypeKind2["ZodEnum"] = "ZodEnum";
ZodFirstPartyTypeKind2["ZodEffects"] = "ZodEffects";
ZodFirstPartyTypeKind2["ZodNativeEnum"] = "ZodNativeEnum";
ZodFirstPartyTypeKind2["ZodOptional"] = "ZodOptional";
ZodFirstPartyTypeKind2["ZodNullable"] = "ZodNullable";
ZodFirstPartyTypeKind2["ZodDefault"] = "ZodDefault";
ZodFirstPartyTypeKind2["ZodCatch"] = "ZodCatch";
ZodFirstPartyTypeKind2["ZodPromise"] = "ZodPromise";
ZodFirstPartyTypeKind2["ZodBranded"] = "ZodBranded";
ZodFirstPartyTypeKind2["ZodPipeline"] = "ZodPipeline";
ZodFirstPartyTypeKind2["ZodReadonly"] = "ZodReadonly";
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
var instanceOfType = (cls, params = {
message: `Input not instance of ${cls.name}`
}) => custom((data) => data instanceof cls, params);
var stringType = ZodString.create;
var numberType = ZodNumber.create;
var nanType = ZodNaN.create;
var bigIntType = ZodBigInt.create;
var booleanType = ZodBoolean.create;
var dateType = ZodDate.create;
var symbolType = ZodSymbol.create;
var undefinedType = ZodUndefined.create;
var nullType = ZodNull.create;
var anyType = ZodAny.create;
var unknownType = ZodUnknown.create;
var neverType = ZodNever.create;
var voidType = ZodVoid.create;
var arrayType = ZodArray.create;
var objectType = ZodObject.create;
var strictObjectType = ZodObject.strictCreate;
var unionType = ZodUnion.create;
var discriminatedUnionType = ZodDiscriminatedUnion.create;
var intersectionType = ZodIntersection.create;
var tupleType = ZodTuple.create;
var recordType = ZodRecord.create;
var mapType = ZodMap.create;
var setType = ZodSet.create;
var functionType = ZodFunction.create;
var lazyType = ZodLazy.create;
var literalType = ZodLiteral.create;
var enumType = ZodEnum.create;
var nativeEnumType = ZodNativeEnum.create;
var promiseType = ZodPromise.create;
var effectsType = ZodEffects.create;
var optionalType = ZodOptional.create;
var nullableType = ZodNullable.create;
var preprocessType = ZodEffects.createWithPreprocess;
var pipelineType = ZodPipeline.create;
var ostring = () => stringType().optional();
var onumber = () => numberType().optional();
var oboolean = () => booleanType().optional();
var coerce = {
string: (arg) => ZodString.create({ ...arg, coerce: true }),
number: (arg) => ZodNumber.create({ ...arg, coerce: true }),
boolean: (arg) => ZodBoolean.create({
...arg,
coerce: true
}),
bigint: (arg) => ZodBigInt.create({ ...arg, coerce: true }),
date: (arg) => ZodDate.create({ ...arg, coerce: true })
};
var NEVER = INVALID;
// node_modules/@modelcontextprotocol/sdk/dist/types.js
var LATEST_PROTOCOL_VERSION = "2024-11-05";
var SUPPORTED_PROTOCOL_VERSIONS = [
LATEST_PROTOCOL_VERSION,
"2024-10-07"
];
var JSONRPC_VERSION = "2.0";
var ProgressTokenSchema = external_exports.union([external_exports.string(), external_exports.number().int()]);
var CursorSchema = external_exports.string();
var BaseRequestParamsSchema = external_exports.object({
_meta: external_exports.optional(external_exports.object({
/**
* If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
*/
progressToken: external_exports.optional(ProgressTokenSchema)
}).passthrough())
}).passthrough();
var RequestSchema = external_exports.object({
method: external_exports.string(),
params: external_exports.optional(BaseRequestParamsSchema)
});
var BaseNotificationParamsSchema = external_exports.object({
/**
* This parameter name is reserved by MCP to allow clients and servers to attach additional metadata to their notifications.
*/
_meta: external_exports.optional(external_exports.object({}).passthrough())
}).passthrough();
var NotificationSchema = external_exports.object({
method: external_exports.string(),
params: external_exports.optional(BaseNotificationParamsSchema)
});
var ResultSchema = external_exports.object({
/**
* This result property is reserved by the protocol to allow clients and servers to attach additional metadata to their responses.
*/
_meta: external_exports.optional(external_exports.object({}).passthrough())
}).passthrough();
var RequestIdSchema = external_exports.union([external_exports.string(), external_exports.number().int()]);
var JSONRPCRequestSchema = external_exports.object({
jsonrpc: external_exports.literal(JSONRPC_VERSION),
id: RequestIdSchema
}).merge(RequestSchema).strict();
var JSONRPCNotificationSchema = external_exports.object({
jsonrpc: external_exports.literal(JSONRPC_VERSION)
}).merge(NotificationSchema).strict();
var JSONRPCResponseSchema = external_exports.object({
jsonrpc: external_exports.literal(JSONRPC_VERSION),
id: RequestIdSchema,
result: ResultSchema
}).strict();
var ErrorCode;
(function(ErrorCode2) {
ErrorCode2[ErrorCode2["ConnectionClosed"] = -1] = "ConnectionClosed";
ErrorCode2[ErrorCode2["ParseError"] = -32700] = "ParseError";
ErrorCode2[ErrorCode2["InvalidRequest"] = -32600] = "InvalidRequest";
ErrorCode2[ErrorCode2["MethodNotFound"] = -32601] = "MethodNotFound";
ErrorCode2[ErrorCode2["InvalidParams"] = -32602] = "InvalidParams";
ErrorCode2[ErrorCode2["InternalError"] = -32603] = "InternalError";
})(ErrorCode || (ErrorCode = {}));
var JSONRPCErrorSchema = external_exports.object({
jsonrpc: external_exports.literal(JSONRPC_VERSION),
id: RequestIdSchema,
error: external_exports.object({
/**
* The error type that occurred.
*/
code: external_exports.number().int(),
/**
* A short description of the error. The message SHOULD be limited to a concise single sentence.
*/
message: external_exports.string(),
/**
* Additional information about the error. The value of this member is defined by the sender (e.g. detailed error information, nested errors etc.).
*/
data: external_exports.optional(external_exports.unknown())
})
}).strict();
var JSONRPCMessageSchema = external_exports.union([
JSONRPCRequestSchema,
JSONRPCNotificationSchema,
JSONRPCResponseSchema,
JSONRPCErrorSchema
]);
var EmptyResultSchema = ResultSchema.strict();
var CancelledNotificationSchema = NotificationSchema.extend({
method: external_exports.literal("notifications/cancelled"),
params: BaseNotificationParamsSchema.extend({
/**
* The ID of the request to cancel.
*
* This MUST correspond to the ID of a request previously issued in the same direction.
*/
requestId: RequestIdSchema,
/**
* An optional string describing the reason for the cancellation. This MAY be logged or presented to the user.
*/
reason: external_exports.string().optional()
})
});
var ImplementationSchema = external_exports.object({
name: external_exports.string(),
version: external_exports.string()
}).passthrough();
var ClientCapabilitiesSchema = external_exports.object({
/**
* Experimental, non-standard capabilities that the client supports.
*/
experimental: external_exports.optional(external_exports.object({}).passthrough()),
/**
* Present if the client supports sampling from an LLM.
*/
sampling: external_exports.optional(external_exports.object({}).passthrough()),
/**
* Present if the client supports listing roots.
*/
roots: external_exports.optional(external_exports.object({
/**
* Whether the client supports issuing notifications for changes to the roots list.
*/
listChanged: external_exports.optional(external_exports.boolean())
}).passthrough())
}).passthrough();
var InitializeRequestSchema = RequestSchema.extend({
method: external_exports.literal("initialize"),
params: BaseRequestParamsSchema.extend({
/**
* The latest version of the Model Context Protocol that the client supports. The client MAY decide to support older versions as well.
*/
protocolVersion: external_exports.string(),
capabilities: ClientCapabilitiesSchema,
clientInfo: ImplementationSchema
})
});
var ServerCapabilitiesSchema = external_exports.object({
/**
* Experimental, non-standard capabilities that the server supports.
*/
experimental: external_exports.optional(external_exports.object({}).passthrough()),
/**
* Present if the server supports sending log messages to the client.
*/
logging: external_exports.optional(external_exports.object({}).passthrough()),
/**
* Present if the server offers any prompt templates.
*/
prompts: external_exports.optional(external_exports.object({
/**
* Whether this server supports issuing notifications for changes to the prompt list.
*/
listChanged: external_exports.optional(external_exports.boolean())
}).passthrough()),
/**
* Present if the server offers any resources to read.
*/
resources: external_exports.optional(external_exports.object({
/**
* Whether this server supports clients subscribing to resource updates.
*/
subscribe: external_exports.optional(external_exports.boolean()),
/**
* Whether this server supports issuing notifications for changes to the resource list.
*/
listChanged: external_exports.optional(external_exports.boolean())
}).passthrough()),
/**
* Present if the server offers any tools to call.
*/
tools: external_exports.optional(external_exports.object({
/**
* Whether this server supports issuing notifications for changes to the tool list.
*/
listChanged: external_exports.optional(external_exports.boolean())
}).passthrough())
}).passthrough();
var InitializeResultSchema = ResultSchema.extend({
/**
* The version of the Model Context Protocol that the server wants to use. This may not match the version that the client requested. If the client cannot support this version, it MUST disconnect.
*/
protocolVersion: external_exports.string(),
capabilities: ServerCapabilitiesSchema,
serverInfo: ImplementationSchema
});
var InitializedNotificationSchema = NotificationSchema.extend({
method: external_exports.literal("notifications/initialized")
});
var PingRequestSchema = RequestSchema.extend({
method: external_exports.literal("ping")
});
var ProgressSchema = external_exports.object({
/**
* The progress thus far. This should increase every time progress is made, even if the total is unknown.
*/
progress: external_exports.number(),
/**
* Total number of items to process (or total progress required), if known.
*/
total: external_exports.optional(external_exports.number())
}).passthrough();
var ProgressNotificationSchema = NotificationSchema.extend({
method: external_exports.literal("notifications/progress"),
params: BaseNotificationParamsSchema.merge(ProgressSchema).extend({
/**
* The progress token which was given in the initial request, used to associate this notification with the request that is proceeding.
*/
progressToken: ProgressTokenSchema
})
});
var PaginatedRequestSchema = RequestSchema.extend({
params: BaseRequestParamsSchema.extend({
/**
* An opaque token representing the current pagination position.
* If provided, the server should return results starting after this cursor.
*/
cursor: external_exports.optional(CursorSchema)
}).optional()
});
var PaginatedResultSchema = ResultSchema.extend({
/**
* An opaque token representing the pagination position after the last returned result.
* If present, there may be more results available.
*/
nextCursor: external_exports.optional(CursorSchema)
});
var ResourceContentsSchema = external_exports.object({
/**
* The URI of this resource.
*/
uri: external_exports.string(),
/**
* The MIME type of this resource, if known.
*/
mimeType: external_exports.optional(external_exports.string())
}).passthrough();
var TextResourceContentsSchema = ResourceContentsSchema.extend({
/**
* The text of the item. This must only be set if the item can actually be represented as text (not binary data).
*/
text: external_exports.string()
});
var BlobResourceContentsSchema = ResourceContentsSchema.extend({
/**
* A base64-encoded string representing the binary data of the item.
*/
blob: external_exports.string().base64()
});
var ResourceSchema = external_exports.object({
/**
* The URI of this resource.
*/
uri: external_exports.string(),
/**
* A human-readable name for this resource.
*
* This can be used by clients to populate UI elements.
*/
name: external_exports.string(),
/**
* A description of what this resource represents.
*
* This can be used by clients to improve the LLM's understanding of available resources. It can be thought of like a "hint" to the model.
*/
description: external_exports.optional(external_exports.string()),
/**
* The MIME type of this resource, if known.
*/
mimeType: external_exports.optional(external_exports.string())
}).passthrough();
var ResourceTemplateSchema = external_exports.object({
/**
* A URI template (according to RFC 6570) that can be used to construct resource URIs.
*/
uriTemplate: external_exports.string(),
/**
* A human-readable name for the type of resource this template refers to.
*
* This can be used by clients to populate UI elements.
*/
name: external_exports.string(),
/**
* A description of what this template is for.
*
* This can be used by clients to improve the LLM's understanding of available resources. It can be thought of like a "hint" to the model.
*/
description: external_exports.optional(external_exports.string()),
/**
* The MIME type for all resources that match this template. This should only be included if all resources matching this template have the same type.
*/
mimeType: external_exports.optional(external_exports.string())
}).passthrough();
var ListResourcesRequestSchema = PaginatedRequestSchema.extend({
method: external_exports.literal("resources/list")
});
var ListResourcesResultSchema = PaginatedResultSchema.extend({
resources: external_exports.array(ResourceSchema)
});
var ListResourceTemplatesRequestSchema = PaginatedRequestSchema.extend({
method: external_exports.literal("resources/templates/list")
});
var ListResourceTemplatesResultSchema = PaginatedResultSchema.extend({
resourceTemplates: external_exports.array(ResourceTemplateSchema)
});
var ReadResourceRequestSchema = RequestSchema.extend({
method: external_exports.literal("resources/read"),
params: BaseRequestParamsSchema.extend({
/**
* The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it.
*/
uri: external_exports.string()
})
});
var ReadResourceResultSchema = ResultSchema.extend({
contents: external_exports.array(external_exports.union([TextResourceContentsSchema, BlobResourceContentsSchema]))
});
var ResourceListChangedNotificationSchema = NotificationSchema.extend({
method: external_exports.literal("notifications/resources/list_changed")
});
var SubscribeRequestSchema = RequestSchema.extend({
method: external_exports.literal("resources/subscribe"),
params: BaseRequestParamsSchema.extend({
/**
* The URI of the resource to subscribe to. The URI can use any protocol; it is up to the server how to interpret it.
*/
uri: external_exports.string()
})
});
var UnsubscribeRequestSchema = RequestSchema.extend({
method: external_exports.literal("resources/unsubscribe"),
params: BaseRequestParamsSchema.extend({
/**
* The URI of the resource to unsubscribe from.
*/
uri: external_exports.string()
})
});
var ResourceUpdatedNotificationSchema = NotificationSchema.extend({
method: external_exports.literal("notifications/resources/updated"),
params: BaseNotificationParamsSchema.extend({
/**
* The URI of the resource that has been updated. This might be a sub-resource of the one that the client actually subscribed to.
*/
uri: external_exports.string()
})
});
var PromptArgumentSchema = external_exports.object({
/**
* The name of the argument.
*/
name: external_exports.string(),
/**
* A human-readable description of the argument.
*/
description: external_exports.optional(external_exports.string()),
/**
* Whether this argument must be provided.
*/
required: external_exports.optional(external_exports.boolean())
}).passthrough();
var PromptSchema = external_exports.object({
/**
* The name of the prompt or prompt template.
*/
name: external_exports.string(),
/**
* An optional description of what this prompt provides
*/
description: external_exports.optional(external_exports.string()),
/**
* A list of arguments to use for templating the prompt.
*/
arguments: external_exports.optional(external_exports.array(PromptArgumentSchema))
}).passthrough();
var ListPromptsRequestSchema = PaginatedRequestSchema.extend({
method: external_exports.literal("prompts/list")
});
var ListPromptsResultSchema = PaginatedResultSchema.extend({
prompts: external_exports.array(PromptSchema)
});
var GetPromptRequestSchema = RequestSchema.extend({
method: external_exports.literal("prompts/get"),
params: BaseRequestParamsSchema.extend({
/**
* The name of the prompt or prompt template.
*/
name: external_exports.string(),
/**
* Arguments to use for templating the prompt.
*/
arguments: external_exports.optional(external_exports.record(external_exports.string()))
})
});
var TextContentSchema = external_exports.object({
type: external_exports.literal("text"),
/**
* The text content of the message.
*/
text: external_exports.string()
}).passthrough();
var ImageContentSchema = external_exports.object({
type: external_exports.literal("image"),
/**
* The base64-encoded image data.
*/
data: external_exports.string().base64(),
/**
* The MIME type of the image. Different providers may support different image types.
*/
mimeType: external_exports.string()
}).passthrough();
var EmbeddedResourceSchema = external_exports.object({
type: external_exports.literal("resource"),
resource: external_exports.union([TextResourceContentsSchema, BlobResourceContentsSchema])
}).passthrough();
var PromptMessageSchema = external_exports.object({
role: external_exports.enum(["user", "assistant"]),
content: external_exports.union([
TextContentSchema,
ImageContentSchema,
EmbeddedResourceSchema
])
}).passthrough();
var GetPromptResultSchema = ResultSchema.extend({
/**
* An optional description for the prompt.
*/
description: external_exports.optional(external_exports.string()),
messages: external_exports.array(PromptMessageSchema)
});
var PromptListChangedNotificationSchema = NotificationSchema.extend({
method: external_exports.literal("notifications/prompts/list_changed")
});
var ToolSchema = external_exports.object({
/**
* The name of the tool.
*/
name: external_exports.string(),
/**
* A human-readable description of the tool.
*/
description: external_exports.optional(external_exports.string()),
/**
* A JSON Schema object defining the expected parameters for the tool.
*/
inputSchema: external_exports.object({
type: external_exports.literal("object"),
properties: external_exports.optional(external_exports.object({}).passthrough())
}).passthrough()
}).passthrough();
var ListToolsRequestSchema = PaginatedRequestSchema.extend({
method: external_exports.literal("tools/list")
});
var ListToolsResultSchema = PaginatedResultSchema.extend({
tools: external_exports.array(ToolSchema)
});
var CallToolResultSchema = ResultSchema.extend({
content: external_exports.array(external_exports.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema])),
isError: external_exports.boolean().default(false).optional()
});
var CompatibilityCallToolResultSchema = CallToolResultSchema.or(ResultSchema.extend({
toolResult: external_exports.unknown()
}));
var CallToolRequestSchema = RequestSchema.extend({
method: external_exports.literal("tools/call"),
params: BaseRequestParamsSchema.extend({
name: external_exports.string(),
arguments: external_exports.optional(external_exports.record(external_exports.unknown()))
})
});
var ToolListChangedNotificationSchema = NotificationSchema.extend({
method: external_exports.literal("notifications/tools/list_changed")
});
var LoggingLevelSchema = external_exports.enum([
"debug",
"info",
"notice",
"warning",
"error",
"critical",
"alert",
"emergency"
]);
var SetLevelRequestSchema = RequestSchema.extend({
method: external_exports.literal("logging/setLevel"),
params: BaseRequestParamsSchema.extend({
/**
* The level of logging that the client wants to receive from the server. The server should send all logs at this level and higher (i.e., more severe) to the client as notifications/logging/message.
*/
level: LoggingLevelSchema
})
});
var LoggingMessageNotificationSchema = NotificationSchema.extend({
method: external_exports.literal("notifications/message"),
params: BaseNotificationParamsSchema.extend({
/**
* The severity of this log message.
*/
level: LoggingLevelSchema,
/**
* An optional name of the logger issuing this message.
*/
logger: external_exports.optional(external_exports.string()),
/**
* The data to be logged, such as a string message or an object. Any JSON serializable type is allowed here.
*/
data: external_exports.unknown()
})
});
var ModelHintSchema = external_exports.object({
/**
* A hint for a model name.
*/
name: external_exports.string().optional()
}).passthrough();
var ModelPreferencesSchema = external_exports.object({
/**
* Optional hints to use for model selection.
*/
hints: external_exports.optional(external_exports.array(ModelHintSchema)),
/**
* How much to prioritize cost when selecting a model.
*/
costPriority: external_exports.optional(external_exports.number().min(0).max(1)),
/**
* How much to prioritize sampling speed (latency) when selecting a model.
*/
speedPriority: external_exports.optional(external_exports.number().min(0).max(1)),
/**
* How much to prioritize intelligence and capabilities when selecting a model.
*/
intelligencePriority: external_exports.optional(external_exports.number().min(0).max(1))
}).passthrough();
var SamplingMessageSchema = external_exports.object({
role: external_exports.enum(["user", "assistant"]),
content: external_exports.union([TextContentSchema, ImageContentSchema])
}).passthrough();
var CreateMessageRequestSchema = RequestSchema.extend({
method: external_exports.literal("sampling/createMessage"),
params: BaseRequestParamsSchema.extend({
messages: external_exports.array(SamplingMessageSchema),
/**
* An optional system prompt the server wants to use for sampling. The client MAY modify or omit this prompt.
*/
systemPrompt: external_exports.optional(external_exports.string()),
/**
* A request to include context from one or more MCP servers (including the caller), to be attached to the prompt. The client MAY ignore this request.
*/
includeContext: external_exports.optional(external_exports.enum(["none", "thisServer", "allServers"])),
temperature: external_exports.optional(external_exports.number()),
/**
* The maximum number of tokens to sample, as requested by the server. The client MAY choose to sample fewer tokens than requested.
*/
maxTokens: external_exports.number().int(),
stopSequences: external_exports.optional(external_exports.array(external_exports.string())),
/**
* Optional metadata to pass through to the LLM provider. The format of this metadata is provider-specific.
*/
metadata: external_exports.optional(external_exports.object({}).passthrough()),
/**
* The server's preferences for which model to select.
*/
modelPreferences: external_exports.optional(ModelPreferencesSchema)
})
});
var CreateMessageResultSchema = ResultSchema.extend({
/**
* The name of the model that generated the message.
*/
model: external_exports.string(),
/**
* The reason why sampling stopped.
*/
stopReason: external_exports.optional(external_exports.enum(["endTurn", "stopSequence", "maxTokens"]).or(external_exports.string())),
role: external_exports.enum(["user", "assistant"]),
content: external_exports.discriminatedUnion("type", [
TextContentSchema,
ImageContentSchema
])
});
var ResourceReferenceSchema = external_exports.object({
type: external_exports.literal("ref/resource"),
/**
* The URI or URI template of the resource.
*/
uri: external_exports.string()
}).passthrough();
var PromptReferenceSchema = external_exports.object({
type: external_exports.literal("ref/prompt"),
/**
* The name of the prompt or prompt template
*/
name: external_exports.string()
}).passthrough();
var CompleteRequestSchema = RequestSchema.extend({
method: external_exports.literal("completion/complete"),
params: BaseRequestParamsSchema.extend({
ref: external_exports.union([PromptReferenceSchema, ResourceReferenceSchema]),
/**
* The argument's information
*/
argument: external_exports.object({
/**
* The name of the argument
*/
name: external_exports.string(),
/**
* The value of the argument to use for completion matching.
*/
value: external_exports.string()
}).passthrough()
})
});
var CompleteResultSchema = ResultSchema.extend({
completion: external_exports.object({
/**
* An array of completion values. Must not exceed 100 items.
*/
values: external_exports.array(external_exports.string()).max(100),
/**
* The total number of completion options available. This can exceed the number of values actually sent in the response.
*/
total: external_exports.optional(external_exports.number().int()),
/**
* Indicates whether there are additional completion options beyond those provided in the current response, even if the exact total is unknown.
*/
hasMore: external_exports.optional(external_exports.boolean())
}).passthrough()
});
var RootSchema = external_exports.object({
/**
* The URI identifying the root. This *must* start with file:// for now.
*/
uri: external_exports.string().startsWith("file://"),
/**
* An optional name for the root.
*/
name: external_exports.optional(external_exports.string())
}).passthrough();
var ListRootsRequestSchema = RequestSchema.extend({
method: external_exports.literal("roots/list")
});
var ListRootsResultSchema = ResultSchema.extend({
roots: external_exports.array(RootSchema)
});
var RootsListChangedNotificationSchema = NotificationSchema.extend({
method: external_exports.literal("notifications/roots/list_changed")
});
var ClientRequestSchema = external_exports.union([
PingRequestSchema,
InitializeRequestSchema,
CompleteRequestSchema,
SetLevelRequestSchema,
GetPromptRequestSchema,
ListPromptsRequestSchema,
ListResourcesRequestSchema,
ListResourceTemplatesRequestSchema,
ReadResourceRequestSchema,
SubscribeRequestSchema,
UnsubscribeRequestSchema,
CallToolRequestSchema,
ListToolsRequestSchema
]);
var ClientNotificationSchema = external_exports.union([
CancelledNotificationSchema,
ProgressNotificationSchema,
InitializedNotificationSchema,
RootsListChangedNotificationSchema
]);
var ClientResultSchema = external_exports.union([
EmptyResultSchema,
CreateMessageResultSchema,
ListRootsResultSchema
]);
var ServerRequestSchema = external_exports.union([
PingRequestSchema,
CreateMessageRequestSchema,
ListRootsRequestSchema
]);
var ServerNotificationSchema = external_exports.union([
CancelledNotificationSchema,
ProgressNotificationSchema,
LoggingMessageNotificationSchema,
ResourceUpdatedNotificationSchema,
ResourceListChangedNotificationSchema,
ToolListChangedNotificationSchema,
PromptListChangedNotificationSchema
]);
var ServerResultSchema = external_exports.union([
EmptyResultSchema,
InitializeResultSchema,
CompleteResultSchema,
GetPromptResultSchema,
ListPromptsResultSchema,
ListResourcesResultSchema,
ListResourceTemplatesResultSchema,
ReadResourceResultSchema,
CallToolResultSchema,
ListToolsResultSchema
]);
var McpError = class extends Error {
constructor(code, message, data) {
super(`MCP error ${code}: ${message}`);
this.code = code;
this.data = data;
}
};
// node_modules/@modelcontextprotocol/sdk/dist/shared/protocol.js
var Protocol = class {
constructor(_options) {
this._options = _options;
this._requestMessageId = 0;
this._requestHandlers = /* @__PURE__ */ new Map();
this._requestHandlerAbortControllers = /* @__PURE__ */ new Map();
this._notificationHandlers = /* @__PURE__ */ new Map();
this._responseHandlers = /* @__PURE__ */ new Map();
this._progressHandlers = /* @__PURE__ */ new Map();
this.setNotificationHandler(CancelledNotificationSchema, (notification) => {
const controller = this._requestHandlerAbortControllers.get(notification.params.requestId);
controller === null || controller === void 0 ? void 0 : controller.abort(notification.params.reason);
});
this.setNotificationHandler(ProgressNotificationSchema, (notification) => {
this._onprogress(notification);
});
this.setRequestHandler(
PingRequestSchema,
// Automatic pong by default.
(_request) => ({})
);
}
/**
* Attaches to the given transport, starts it, and starts listening for messages.
*
* The Protocol object assumes ownership of the Transport, replacing any callbacks that have already been set, and expects that it is the only user of the Transport instance going forward.
*/
async connect(transport) {
this._transport = transport;
this._transport.onclose = () => {
this._onclose();
};
this._transport.onerror = (error) => {
this._onerror(error);
};
this._transport.onmessage = (message) => {
if (!("method" in message)) {
this._onresponse(message);
} else if ("id" in message) {
this._onrequest(message);
} else {
this._onnotification(message);
}
};
await this._transport.start();
}
_onclose() {
var _a;
const responseHandlers = this._responseHandlers;
this._responseHandlers = /* @__PURE__ */ new Map();
this._progressHandlers.clear();
this._transport = void 0;
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
const error = new McpError(ErrorCode.ConnectionClosed, "Connection closed");
for (const handler of responseHandlers.values()) {
handler(error);
}
}
_onerror(error) {
var _a;
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
}
_onnotification(notification) {
var _a;
const handler = (_a = this._notificationHandlers.get(notification.method)) !== null && _a !== void 0 ? _a : this.fallbackNotificationHandler;
if (handler === void 0) {
return;
}
Promise.resolve().then(() => handler(notification)).catch((error) => this._onerror(new Error(`Uncaught error in notification handler: ${error}`)));
}
_onrequest(request) {
var _a, _b;
const handler = (_a = this._requestHandlers.get(request.method)) !== null && _a !== void 0 ? _a : this.fallbackRequestHandler;
if (handler === void 0) {
(_b = this._transport) === null || _b === void 0 ? void 0 : _b.send({
jsonrpc: "2.0",
id: request.id,
error: {
code: ErrorCode.MethodNotFound,
message: "Method not found"
}
}).catch((error) => this._onerror(new Error(`Failed to send an error response: ${error}`)));
return;
}
const abortController = new AbortController();
this._requestHandlerAbortControllers.set(request.id, abortController);
Promise.resolve().then(() => handler(request, { signal: abortController.signal })).then((result) => {
var _a2;
if (abortController.signal.aborted) {
return;
}
return (_a2 = this._transport) === null || _a2 === void 0 ? void 0 : _a2.send({
result,
jsonrpc: "2.0",
id: request.id
});
}, (error) => {
var _a2, _b2;
if (abortController.signal.aborted) {
return;
}
return (_a2 = this._transport) === null || _a2 === void 0 ? void 0 : _a2.send({
jsonrpc: "2.0",
id: request.id,
error: {
code: Number.isSafeInteger(error["code"]) ? error["code"] : ErrorCode.InternalError,
message: (_b2 = error.message) !== null && _b2 !== void 0 ? _b2 : "Internal error"
}
});
}).catch((error) => this._onerror(new Error(`Failed to send response: ${error}`))).finally(() => {
this._requestHandlerAbortControllers.delete(request.id);
});
}
_onprogress(notification) {
const { progress, total, progressToken } = notification.params;
const handler = this._progressHandlers.get(Number(progressToken));
if (handler === void 0) {
this._onerror(new Error(`Received a progress notification for an unknown token: ${JSON.stringify(notification)}`));
return;
}
handler({ progress, total });
}
_onresponse(response) {
const messageId = response.id;
const handler = this._responseHandlers.get(Number(messageId));
if (handler === void 0) {
this._onerror(new Error(`Received a response for an unknown message ID: ${JSON.stringify(response)}`));
return;
}
this._responseHandlers.delete(Number(messageId));
this._progressHandlers.delete(Number(messageId));
if ("result" in response) {
handler(response);
} else {
const error = new McpError(response.error.code, response.error.message, response.error.data);
handler(error);
}
}
get transport() {
return this._transport;
}
/**
* Closes the connection.
*/
async close() {
var _a;
await ((_a = this._transport) === null || _a === void 0 ? void 0 : _a.close());
}
/**
* Sends a request and wait for a response.
*
* Do not use this method to emit notifications! Use notification() instead.
*/
request(request, resultSchema, options) {
return new Promise((resolve, reject) => {
var _a, _b, _c;
if (!this._transport) {
reject(new Error("Not connected"));
return;
}
if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.enforceStrictCapabilities) === true) {
this.assertCapabilityForMethod(request.method);
}
(_b = options === null || options === void 0 ? void 0 : options.signal) === null || _b === void 0 ? void 0 : _b.throwIfAborted();
const messageId = this._requestMessageId++;
const jsonrpcRequest = {
...request,
jsonrpc: "2.0",
id: messageId
};
if (options === null || options === void 0 ? void 0 : options.onprogress) {
this._progressHandlers.set(messageId, options.onprogress);
jsonrpcRequest.params = {
...request.params,
_meta: { progressToken: messageId }
};
}
this._responseHandlers.set(messageId, (response) => {
var _a2;
if ((_a2 = options === null || options === void 0 ? void 0 : options.signal) === null || _a2 === void 0 ? void 0 : _a2.aborted) {
return;
}
if (response instanceof Error) {
return reject(response);
}
try {
const result = resultSchema.parse(response.result);
resolve(result);
} catch (error) {
reject(error);
}
});
(_c = options === null || options === void 0 ? void 0 : options.signal) === null || _c === void 0 ? void 0 : _c.addEventListener("abort", () => {
var _a2, _b2;
const reason = (_a2 = options === null || options === void 0 ? void 0 : options.signal) === null || _a2 === void 0 ? void 0 : _a2.reason;
this._responseHandlers.delete(messageId);
this._progressHandlers.delete(messageId);
(_b2 = this._transport) === null || _b2 === void 0 ? void 0 : _b2.send({
jsonrpc: "2.0",
method: "cancelled",
params: {
requestId: messageId,
reason: String(reason)
}
});
reject(reason);
});
this._transport.send(jsonrpcRequest).catch(reject);
});
}
/**
* Emits a notification, which is a one-way message that does not expect a response.
*/
async notification(notification) {
if (!this._transport) {
throw new Error("Not connected");
}
this.assertNotificationCapability(notification.method);
const jsonrpcNotification = {
...notification,
jsonrpc: "2.0"
};
await this._transport.send(jsonrpcNotification);
}
/**
* Registers a handler to invoke when this protocol object receives a request with the given method.
*
* Note that this will replace any previous request handler for the same method.
*/
setRequestHandler(requestSchema, handler) {
const method = requestSchema.shape.method.value;
this.assertRequestHandlerCapability(method);
this._requestHandlers.set(method, (request, extra) => Promise.resolve(handler(requestSchema.parse(request), extra)));
}
/**
* Removes the request handler for the given method.
*/
removeRequestHandler(method) {
this._requestHandlers.delete(method);
}
/**
* Registers a handler to invoke when this protocol object receives a notification with the given method.
*
* Note that this will replace any previous notification handler for the same method.
*/
setNotificationHandler(notificationSchema, handler) {
this._notificationHandlers.set(notificationSchema.shape.method.value, (notification) => Promise.resolve(handler(notificationSchema.parse(notification))));
}
/**
* Removes the notification handler for the given method.
*/
removeNotificationHandler(method) {
this._notificationHandlers.delete(method);
}
};
// node_modules/@modelcontextprotocol/sdk/dist/server/index.js
var Server = class extends Protocol {
/**
* Initializes this server with the given name and version information.
*/
constructor(_serverInfo, options) {
super(options);
this._serverInfo = _serverInfo;
this._capabilities = options.capabilities;
this.setRequestHandler(InitializeRequestSchema, (request) => this._oninitialize(request));
this.setNotificationHandler(InitializedNotificationSchema, () => {
var _a;
return (_a = this.oninitialized) === null || _a === void 0 ? void 0 : _a.call(this);
});
}
assertCapabilityForMethod(method) {
var _a, _b;
switch (method) {
case "sampling/createMessage":
if (!((_a = this._clientCapabilities) === null || _a === void 0 ? void 0 : _a.sampling)) {
throw new Error(`Client does not support sampling (required for ${method})`);
}
break;
case "roots/list":
if (!((_b = this._clientCapabilities) === null || _b === void 0 ? void 0 : _b.roots)) {
throw new Error(`Client does not support listing roots (required for ${method})`);
}
break;
case "ping":
break;
}
}
assertNotificationCapability(method) {
switch (method) {
case "notifications/message":
if (!this._capabilities.logging) {
throw new Error(`Server does not support logging (required for ${method})`);
}
break;
case "notifications/resources/updated":
case "notifications/resources/list_changed":
if (!this._capabilities.resources) {
throw new Error(`Server does not support notifying about resources (required for ${method})`);
}
break;
case "notifications/tools/list_changed":
if (!this._capabilities.tools) {
throw new Error(`Server does not support notifying of tool list changes (required for ${method})`);
}
break;
case "notifications/prompts/list_changed":
if (!this._capabilities.prompts) {
throw new Error(`Server does not support notifying of prompt list changes (required for ${method})`);
}
break;
case "notifications/cancelled":
break;
case "notifications/progress":
break;
}
}
assertRequestHandlerCapability(method) {
switch (method) {
case "sampling/createMessage":
if (!this._capabilities.sampling) {
throw new Error(`Server does not support sampling (required for ${method})`);
}
break;
case "logging/setLevel":
if (!this._capabilities.logging) {
throw new Error(`Server does not support logging (required for ${method})`);
}
break;
case "prompts/get":
case "prompts/list":
if (!this._capabilities.prompts) {
throw new Error(`Server does not support prompts (required for ${method})`);
}
break;
case "resources/list":
case "resources/templates/list":
case "resources/read":
if (!this._capabilities.resources) {
throw new Error(`Server does not support resources (required for ${method})`);
}
break;
case "tools/call":
case "tools/list":
if (!this._capabilities.tools) {
throw new Error(`Server does not support tools (required for ${method})`);
}
break;
case "ping":
case "initialize":
break;
}
}
async _oninitialize(request) {
const requestedVersion = request.params.protocolVersion;
this._clientCapabilities = request.params.capabilities;
this._clientVersion = request.params.clientInfo;
return {
protocolVersion: SUPPORTED_PROTOCOL_VERSIONS.includes(requestedVersion) ? requestedVersion : LATEST_PROTOCOL_VERSION,
capabilities: this.getCapabilities(),
serverInfo: this._serverInfo
};
}
/**
* After initialization has completed, this will be populated with the client's reported capabilities.
*/
getClientCapabilities() {
return this._clientCapabilities;
}
/**
* After initialization has completed, this will be populated with information about the client's name and version.
*/
getClientVersion() {
return this._clientVersion;
}
getCapabilities() {
return this._capabilities;
}
async ping() {
return this.request({ method: "ping" }, EmptyResultSchema);
}
async createMessage(params, options) {
return this.request({ method: "sampling/createMessage", params }, CreateMessageResultSchema, options);
}
async listRoots(params, options) {
return this.request({ method: "roots/list", params }, ListRootsResultSchema, options);
}
async sendLoggingMessage(params) {
return this.notification({ method: "notifications/message", params });
}
async sendResourceUpdated(params) {
return this.notification({
method: "notifications/resources/updated",
params
});
}
async sendResourceListChanged() {
return this.notification({
method: "notifications/resources/list_changed"
});
}
async sendToolListChanged() {
return this.notification({ method: "notifications/tools/list_changed" });
}
async sendPromptListChanged() {
return this.notification({ method: "notifications/prompts/list_changed" });
}
};
// node_modules/@modelcontextprotocol/sdk/dist/server/stdio.js
import process2 from "node:process";
// node_modules/@modelcontextprotocol/sdk/dist/shared/stdio.js
var ReadBuffer = class {
append(chunk) {
this._buffer = this._buffer ? Buffer.concat([this._buffer, chunk]) : chunk;
}
readMessage() {
if (!this._buffer) {
return null;
}
const index = this._buffer.indexOf("\n");
if (index === -1) {
return null;
}
const line = this._buffer.toString("utf8", 0, index);
this._buffer = this._buffer.subarray(index + 1);
return deserializeMessage(line);
}
clear() {
this._buffer = void 0;
}
};
function deserializeMessage(line) {
return JSONRPCMessageSchema.parse(JSON.parse(line));
}
function serializeMessage(message) {
return JSON.stringify(message) + "\n";
}
// node_modules/@modelcontextprotocol/sdk/dist/server/stdio.js
var StdioServerTransport = class {
constructor(_stdin = process2.stdin, _stdout = process2.stdout) {
this._stdin = _stdin;
this._stdout = _stdout;
this._readBuffer = new ReadBuffer();
this._started = false;
this._ondata = (chunk) => {
this._readBuffer.append(chunk);
this.processReadBuffer();
};
this._onerror = (error) => {
var _a;
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
};
}
/**
* Starts listening for messages on stdin.
*/
async start() {
if (this._started) {
throw new Error("StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.");
}
this._started = true;
this._stdin.on("data", this._ondata);
this._stdin.on("error", this._onerror);
}
processReadBuffer() {
var _a, _b;
while (true) {
try {
const message = this._readBuffer.readMessage();
if (message === null) {
break;
}
(_a = this.onmessage) === null || _a === void 0 ? void 0 : _a.call(this, message);
} catch (error) {
(_b = this.onerror) === null || _b === void 0 ? void 0 : _b.call(this, error);
}
}
}
async close() {
var _a;
this._stdin.off("data", this._ondata);
this._stdin.off("error", this._onerror);
this._readBuffer.clear();
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
}
send(message) {
return new Promise((resolve) => {
const json = serializeMessage(message);
if (this._stdout.write(json)) {
resolve();
} else {
this._stdout.once("drain", resolve);
}
});
}
};
// build/adapters/unity-http-adapter.js
var UnityHttpAdapter = class {
constructor(options = {}) {
this.url = options.url || "http://localhost:23457/";
this.timeout = options.timeout || 15e3;
}
/**
* Call a method on the Unity server
*/
async call(method, params = {}) {
const startTime = Date.now();
console.error(`[Unity HTTP] Calling method: ${method}`);
const maxRetries = 3;
let lastError;
for (let retry = 0; retry < maxRetries; retry++) {
if (retry > 0) {
console.error(`[Unity HTTP] Retry ${retry}/${maxRetries - 1} for method: ${method}`);
await new Promise((resolve) => setTimeout(resolve, 1e3 * retry));
}
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => {
console.error(`[Unity HTTP] Request timeout after ${this.timeout}ms for method: ${method}`);
controller.abort();
}, this.timeout);
const response = await fetch(this.url, {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
"Accept": "application/json; charset=utf-8"
},
body: JSON.stringify({ method, ...params }),
signal: controller.signal
});
clearTimeout(timeoutId);
const elapsed = Date.now() - startTime;
console.error(`[Unity HTTP] Response received in ${elapsed}ms for method: ${method}`);
const result = await response.json();
if (!result.success) {
throw new Error(result.error || "Unknown error");
}
return result.result;
} catch (error) {
lastError = error;
if (error.name === "AbortError") {
lastError = new Error("Request timeout");
} else if (error.message?.includes("ECONNREFUSED")) {
lastError = new Error("Unity HTTP server is not running");
} else if (error.message?.includes("Failed to fetch")) {
lastError = new Error("Failed to connect to Unity HTTP server");
}
console.error(`[Unity HTTP] Error on attempt ${retry + 1}: ${lastError.message}`);
if (error.message?.includes("Method not found")) {
throw error;
}
}
}
throw lastError || new Error("Unknown error after retries");
}
/**
* Check if Unity server is connected
*/
async isConnected() {
try {
await this.call("ping");
return true;
} catch {
return false;
}
}
// Script operations
async createScript(fileName, content, folder) {
return this.call("script/create", { fileName, content, folder });
}
async readScript(path3) {
return this.call("script/read", { path: path3 });
}
async deleteScript(path3) {
return this.call("script/delete", { path: path3 });
}
async applyDiff(path3, diff, options) {
return this.call("script/applyDiff", { path: path3, diff, options });
}
// Shader operations
async createShader(name, content, folder) {
return this.call("shader/create", { name, content, folder });
}
async readShader(path3) {
return this.call("shader/read", { path: path3 });
}
async deleteShader(path3) {
return this.call("shader/delete", { path: path3 });
}
// Project operations
async getProjectInfo() {
return this.call("project/info");
}
// Folder operations
async createFolder(path3) {
return this.call("folder/create", { path: path3 });
}
async renameFolder(oldPath, newName) {
return this.call("folder/rename", { oldPath, newName });
}
async moveFolder(sourcePath, targetPath) {
return this.call("folder/move", { sourcePath, targetPath });
}
async deleteFolder(path3, recursive = true) {
return this.call("folder/delete", { path: path3, recursive });
}
async listFolder(path3, recursive = false) {
return this.call("folder/list", { path: path3, recursive });
}
};
// build/services/unity-bridge-deploy-service.js
import * as fs2 from "fs/promises";
import * as path2 from "path";
// build/embedded-scripts.js
import * as fs from "fs/promises";
import * as path from "path";
var EmbeddedScriptsProvider = class {
constructor() {
this.scripts = /* @__PURE__ */ new Map();
this.initializeScripts();
}
initializeScripts() {
this.scripts.set("UnityHttpServer.cs", {
fileName: "UnityHttpServer.cs",
version: "1.1.0",
content: `using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using UnityEngine;
using UnityEditor;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace UnityMCP
{
[InitializeOnLoad]
public static class UnityMCPInstaller
{
static UnityMCPInstaller()
{
CheckAndUpdateScripts();
}
static void CheckAndUpdateScripts()
{
var installedVersion = EditorPrefs.GetString(UnityHttpServer.VERSION_META_KEY, "0.0.0");
if (installedVersion != UnityHttpServer.SCRIPT_VERSION)
{
Debug.Log($"[UnityMCP] Updating Unity MCP scripts from version {installedVersion} to {UnityHttpServer.SCRIPT_VERSION}");
// Version update logic will be handled by the MCP server
EditorPrefs.SetString(UnityHttpServer.VERSION_META_KEY, UnityHttpServer.SCRIPT_VERSION);
}
}
}
/// <summary>
/// Simple HTTP server for Unity MCP integration
/// </summary>
public static class UnityHttpServer
{
// Version information for auto-update
public const string SCRIPT_VERSION = "1.1.0";
public const string VERSION_META_KEY = "UnityMCP.InstalledVersion";
// Configuration constants
private const int DEFAULT_PORT = 23457;
private const int REQUEST_TIMEOUT_MS = 120000; // 2 minutes
private const int THREAD_JOIN_TIMEOUT_MS = 1000; // 1 second
private const int ASSET_REFRESH_DELAY_MS = 500; // Wait after asset operations
public const string SERVER_LOG_PREFIX = "[UnityMCP]";
private const string PREFS_PORT_KEY = "UnityMCP.ServerPort";
private const string PREFS_PORT_BEFORE_PLAY_KEY = "UnityMCP.ServerPortBeforePlay";
// File path constants
private const string ASSETS_PREFIX = "Assets/";
private const int ASSETS_PREFIX_LENGTH = 7;
private const string DEFAULT_SCRIPTS_FOLDER = "Assets/Scripts";
private const string DEFAULT_SHADERS_FOLDER = "Assets/Shaders";
private const string CS_EXTENSION = ".cs";
private const string SHADER_EXTENSION = ".shader";
private static HttpListener httpListener;
private static Thread listenerThread;
private static bool isRunning = false;
// Request queue for serialization
private static readonly Queue<Action> requestQueue = new Queue<Action>();
private static bool isProcessingRequest = false;
private static int currentPort = DEFAULT_PORT;
/// <summary>
/// Gets whether the server is currently running
/// </summary>
public static bool IsRunning => isRunning;
/// <summary>
/// Gets the current port the server is running on
/// </summary>
public static int CurrentPort => currentPort;
[InitializeOnLoad]
static class AutoShutdown
{
static AutoShutdown()
{
EditorApplication.playModeStateChanged += OnPlayModeChanged;
EditorApplication.quitting += Shutdown;
// Handle script recompilation
UnityEditor.Compilation.CompilationPipeline.compilationStarted += OnCompilationStarted;
UnityEditor.Compilation.CompilationPipeline.compilationFinished += OnCompilationFinished;
// Auto-start server on Unity startup
EditorApplication.delayCall += () => {
if (!isRunning)
{
var savedPort = EditorPrefs.GetInt(PREFS_PORT_KEY, DEFAULT_PORT);
Debug.Log($"{SERVER_LOG_PREFIX} Auto-starting server on port {savedPort}");
Start(savedPort);
}
};
}
static void OnCompilationStarted(object obj)
{
Debug.Log($"{SERVER_LOG_PREFIX} Compilation started - stopping server");
if (isRunning)
{
Shutdown();
}
}
static void OnCompilationFinished(object obj)
{
Debug.Log($"{SERVER_LOG_PREFIX} Compilation finished - auto-restarting server");
// Always auto-restart after compilation
var savedPort = EditorPrefs.GetInt(PREFS_PORT_KEY, DEFAULT_PORT);
EditorApplication.delayCall += () => Start(savedPort);
}
}
/// <summary>
/// Start the HTTP server on the specified port
/// </summary>
/// <param name="port">Port to listen on</param>
public static void Start(int port = DEFAULT_PORT)
{
if (isRunning)
{
Debug.LogWarning($"{SERVER_LOG_PREFIX} Server is already running. Stop it first.");
return;
}
currentPort = port;
try
{
httpListener = new HttpListener();
httpListener.Prefixes.Add($"http://localhost:{currentPort}/");
httpListener.Start();
isRunning = true;
listenerThread = new Thread(ListenLoop)
{
IsBackground = true,
Name = "UnityMCPHttpListener"
};
listenerThread.Start();
Debug.Log($"{SERVER_LOG_PREFIX} HTTP Server started on port {currentPort}");
}
catch (Exception e)
{
isRunning = false;
Debug.LogError($"{SERVER_LOG_PREFIX} Failed to start HTTP server: {e.Message}");
throw;
}
}
/// <summary>
/// Stop the HTTP server
/// </summary>
public static void Shutdown()
{
if (!isRunning)
{
Debug.LogWarning($"{SERVER_LOG_PREFIX} Server is not running.");
return;
}
isRunning = false;
try
{
httpListener?.Stop();
httpListener?.Close();
listenerThread?.Join(THREAD_JOIN_TIMEOUT_MS);
Debug.Log($"{SERVER_LOG_PREFIX} HTTP Server stopped");
}
catch (Exception e)
{
Debug.LogError($"{SERVER_LOG_PREFIX} Error during shutdown: {e.Message}");
}
finally
{
httpListener = null;
listenerThread = null;
}
}
static void OnPlayModeChanged(PlayModeStateChange state)
{
// Stop server when entering play mode to avoid conflicts
if (state == PlayModeStateChange.ExitingEditMode)
{
if (isRunning)
{
Debug.Log($"{SERVER_LOG_PREFIX} Stopping server due to play mode change");
EditorPrefs.SetInt(PREFS_PORT_BEFORE_PLAY_KEY, currentPort);
Shutdown();
}
}
// Restart server when returning to edit mode
else if (state == PlayModeStateChange.EnteredEditMode)
{
var savedPort = EditorPrefs.GetInt(PREFS_PORT_BEFORE_PLAY_KEY, DEFAULT_PORT);
Debug.Log($"{SERVER_LOG_PREFIX} Restarting server after play mode on port {savedPort}");
EditorApplication.delayCall += () => Start(savedPort);
}
}
static void ListenLoop()
{
while (isRunning)
{
try
{
var context = httpListener.GetContext();
ThreadPool.QueueUserWorkItem(_ => HandleRequest(context));
}
catch (Exception e)
{
if (isRunning)
Debug.LogError($"{SERVER_LOG_PREFIX} Listen error: {e.Message}");
}
}
}
static void HandleRequest(HttpListenerContext context)
{
var request = context.Request;
var response = context.Response;
response.Headers.Add("Access-Control-Allow-Origin", "*");
try
{
if (request.HttpMethod != "POST")
{
SendResponse(response, 405, false, null, "Method not allowed");
return;
}
string requestBody;
// Force UTF-8 encoding for request body
using (var reader = new StreamReader(request.InputStream, Encoding.UTF8))
{
requestBody = reader.ReadToEnd();
}
var requestData = JObject.Parse(requestBody);
var method = requestData["method"]?.ToString();
if (string.IsNullOrEmpty(method))
{
SendResponse(response, 400, false, null, "Method is required");
return;
}
Debug.Log($"{SERVER_LOG_PREFIX} Processing request: {method}");
// Check if this request requires main thread
bool requiresMainThread = RequiresMainThread(method);
if (!requiresMainThread)
{
// Process directly on worker thread
try
{
var result = ProcessRequestOnWorkerThread(method, requestData);
SendResponse(response, 200, true, result, null);
}
catch (Exception e)
{
var statusCode = e is ArgumentException ? 400 : 500;
SendResponse(response, statusCode, false, null, e.Message);
}
}
else
{
// Execute on main thread for Unity API calls
object result = null;
Exception error = null;
var resetEvent = new ManualResetEvent(false);
EditorApplication.delayCall += () =>
{
try
{
Debug.Log($"{SERVER_LOG_PREFIX} Processing on main thread: {method}");
result = ProcessRequest(method, requestData);
Debug.Log($"{SERVER_LOG_PREFIX} Completed processing: {method}");
}
catch (Exception e)
{
error = e;
Debug.LogError($"{SERVER_LOG_PREFIX} Error processing {method}: {e.Message}");
}
finally
{
resetEvent.Set();
}
};
if (!resetEvent.WaitOne(REQUEST_TIMEOUT_MS))
{
SendResponse(response, 504, false, null, "Request timeout - Unity may be busy or unfocused");
return;
}
if (error != null)
{
var statusCode = error is ArgumentException ? 400 : 500;
SendResponse(response, statusCode, false, null, error.Message);
return;
}
SendResponse(response, 200, true, result, null);
}
}
catch (Exception e)
{
SendResponse(response, 400, false, null, $"Bad request: {e.Message}");
}
}
static bool RequiresMainThread(string method)
{
// These methods can run on worker thread
switch (method)
{
case "ping":
case "script/read":
case "shader/read":
return false;
// project/info now requires Unity API for render pipeline detection
// Creating, deleting files require Unity API (AssetDatabase)
default:
return true;
}
}
static object ProcessRequestOnWorkerThread(string method, JObject request)
{
switch (method)
{
case "ping":
return new { status = "ok", time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") };
case "project/info":
// project/info requires Unity API for render pipeline detection
throw new NotImplementedException("project/info requires main thread for render pipeline detection");
case "script/read":
return ReadScriptOnWorkerThread(request);
case "shader/read":
return ReadShaderOnWorkerThread(request);
// Folder operations (can run on worker thread)
case "folder/create":
return CreateFolderOnWorkerThread(request);
case "folder/rename":
return RenameFolderOnWorkerThread(request);
case "folder/move":
return MoveFolderOnWorkerThread(request);
case "folder/delete":
return DeleteFolderOnWorkerThread(request);
case "folder/list":
return ListFolderOnWorkerThread(request);
default:
throw new NotImplementedException($"Method not implemented for worker thread: {method}");
}
}
static object ReadScriptOnWorkerThread(JObject request)
{
var path = request["path"]?.ToString();
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
if (!File.Exists(fullPath))
throw new FileNotFoundException($"File not found: {path}");
return new
{
path = path,
content = File.ReadAllText(fullPath, new UTF8Encoding(true)),
guid = "" // GUID requires AssetDatabase, skip in worker thread
};
}
static object ReadShaderOnWorkerThread(JObject request)
{
var path = request["path"]?.ToString();
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
if (!File.Exists(fullPath))
throw new FileNotFoundException($"File not found: {path}");
return new
{
path = path,
content = File.ReadAllText(fullPath, new UTF8Encoding(true)),
guid = "" // GUID requires AssetDatabase, skip in worker thread
};
}
static object ProcessRequest(string method, JObject request)
{
switch (method)
{
case "ping":
return new { status = "ok", time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") };
// Script operations
case "script/create":
return CreateScript(request);
case "script/read":
return ReadScript(request);
case "script/delete":
return DeleteScript(request);
case "script/applyDiff":
return ApplyDiff(request);
// Shader operations
case "shader/create":
return CreateShader(request);
case "shader/read":
return ReadShader(request);
case "shader/delete":
return DeleteShader(request);
// Project operations
case "project/info":
return GetProjectInfo();
// Folder operations
case "folder/create":
return CreateFolder(request);
case "folder/rename":
return RenameFolder(request);
case "folder/move":
return MoveFolder(request);
case "folder/delete":
return DeleteFolder(request);
case "folder/list":
return ListFolder(request);
default:
throw new NotImplementedException($"Method not found: {method}");
}
}
static object CreateScript(JObject request)
{
var fileName = request["fileName"]?.ToString();
if (string.IsNullOrEmpty(fileName))
throw new ArgumentException("fileName is required");
if (!fileName.EndsWith(CS_EXTENSION))
fileName += CS_EXTENSION;
var content = request["content"]?.ToString();
var folder = request["folder"]?.ToString() ?? DEFAULT_SCRIPTS_FOLDER;
var path = Path.Combine(folder, fileName);
var directory = Path.GetDirectoryName(path);
// Create directory if needed
if (!AssetDatabase.IsValidFolder(directory))
{
CreateFolderRecursive(directory);
}
// Use Unity-safe file creation approach
var scriptContent = content ?? GetDefaultScriptContent(fileName);
// First, ensure the asset doesn't already exist
if (AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(path) != null)
{
throw new InvalidOperationException($"Asset already exists: {path}");
}
// Write file using UTF-8 with BOM (Unity standard)
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
var utf8WithBom = new UTF8Encoding(true);
File.WriteAllText(fullPath, scriptContent, utf8WithBom);
// Import the asset immediately and wait for completion
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);
// Verify the asset was imported successfully
var attempts = 0;
const int maxAttempts = 10;
while (AssetDatabase.AssetPathToGUID(path) == "" && attempts < maxAttempts)
{
System.Threading.Thread.Sleep(100);
AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
attempts++;
}
if (AssetDatabase.AssetPathToGUID(path) == "")
{
throw new InvalidOperationException($"Failed to import asset: {path}");
}
return new
{
path = path,
guid = AssetDatabase.AssetPathToGUID(path)
};
}
static object ReadScript(JObject request)
{
var path = request["path"]?.ToString();
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
if (!File.Exists(fullPath))
throw new FileNotFoundException($"File not found: {path}");
return new
{
path = path,
content = File.ReadAllText(fullPath, new UTF8Encoding(true)),
guid = AssetDatabase.AssetPathToGUID(path)
};
}
static object DeleteScript(JObject request)
{
var path = request["path"]?.ToString();
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
// Verify file exists before deletion
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
if (!File.Exists(fullPath))
throw new FileNotFoundException($"File not found: {path}");
// Delete using AssetDatabase
if (!AssetDatabase.DeleteAsset(path))
throw new InvalidOperationException($"Failed to delete: {path}");
// Force immediate refresh
AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
// Wait for asset database to process deletion
System.Threading.Thread.Sleep(ASSET_REFRESH_DELAY_MS);
return new { message = "Script deleted successfully" };
}
static object ApplyDiff(JObject request)
{
var path = request["path"]?.ToString();
var diff = request["diff"]?.ToString();
var options = request["options"] as JObject;
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
if (string.IsNullOrEmpty(diff))
throw new ArgumentException("diff is required");
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
if (!File.Exists(fullPath))
throw new FileNotFoundException($"File not found: {path}");
var dryRun = options?["dryRun"]?.Value<bool>() ?? false;
// Read current content using UTF-8 with BOM (Unity standard)
var utf8WithBom = new UTF8Encoding(true);
var originalContent = File.ReadAllText(fullPath, utf8WithBom);
var lines = originalContent.Split('\\n').ToList();
// Parse and apply unified diff
var diffLines = diff.Split('\\n');
var linesAdded = 0;
var linesRemoved = 0;
var currentLine = 0;
for (int i = 0; i < diffLines.Length; i++)
{
var line = diffLines[i];
if (line.StartsWith("@@"))
{
// Parse hunk header: @@ -l,s +l,s @@
var match = System.Text.RegularExpressions.Regex.Match(line, @"@@ -(\\d+),?\\d* \\+(\\d+),?\\d* @@");
if (match.Success)
{
currentLine = int.Parse(match.Groups[1].Value) - 1;
}
}
else if (line.StartsWith("-") && !line.StartsWith("---"))
{
// Remove line
if (currentLine < lines.Count)
{
lines.RemoveAt(currentLine);
linesRemoved++;
}
}
else if (line.StartsWith("+") && !line.StartsWith("+++"))
{
// Add line
lines.Insert(currentLine, line.Substring(1));
currentLine++;
linesAdded++;
}
else if (line.StartsWith(" "))
{
// Context line
currentLine++;
}
}
// Write result if not dry run
if (!dryRun)
{
var updatedContent = string.Join("\\n", lines);
// Write with UTF-8 with BOM (Unity standard)
File.WriteAllText(fullPath, updatedContent, utf8WithBom);
AssetDatabase.Refresh();
// Wait for asset database to process
System.Threading.Thread.Sleep(ASSET_REFRESH_DELAY_MS);
}
return new
{
path = path,
linesAdded = linesAdded,
linesRemoved = linesRemoved,
dryRun = dryRun,
guid = AssetDatabase.AssetPathToGUID(path)
};
}
static object CreateShader(JObject request)
{
var name = request["name"]?.ToString();
if (string.IsNullOrEmpty(name))
throw new ArgumentException("name is required");
if (!name.EndsWith(SHADER_EXTENSION))
name += SHADER_EXTENSION;
var content = request["content"]?.ToString();
var folder = request["folder"]?.ToString() ?? DEFAULT_SHADERS_FOLDER;
var path = Path.Combine(folder, name);
var directory = Path.GetDirectoryName(path);
if (!AssetDatabase.IsValidFolder(directory))
{
CreateFolderRecursive(directory);
}
// Use Unity-safe file creation approach
var shaderContent = content ?? GetDefaultShaderContent(name);
// First, ensure the asset doesn't already exist
if (AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(path) != null)
{
throw new InvalidOperationException($"Asset already exists: {path}");
}
// Write file using UTF-8 with BOM (Unity standard)
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
var utf8WithBom = new UTF8Encoding(true);
File.WriteAllText(fullPath, shaderContent, utf8WithBom);
// Import the asset immediately and wait for completion
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);
// Verify the asset was imported successfully
var attempts = 0;
const int maxAttempts = 10;
while (AssetDatabase.AssetPathToGUID(path) == "" && attempts < maxAttempts)
{
System.Threading.Thread.Sleep(100);
AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
attempts++;
}
if (AssetDatabase.AssetPathToGUID(path) == "")
{
throw new InvalidOperationException($"Failed to import asset: {path}");
}
return new
{
path = path,
guid = AssetDatabase.AssetPathToGUID(path)
};
}
static object ReadShader(JObject request)
{
var path = request["path"]?.ToString();
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
if (!File.Exists(fullPath))
throw new FileNotFoundException($"File not found: {path}");
return new
{
path = path,
content = File.ReadAllText(fullPath, new UTF8Encoding(true)),
guid = AssetDatabase.AssetPathToGUID(path)
};
}
static object DeleteShader(JObject request)
{
var path = request["path"]?.ToString();
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
if (!AssetDatabase.DeleteAsset(path))
throw new InvalidOperationException($"Failed to delete: {path}");
// Wait for asset database to process deletion
System.Threading.Thread.Sleep(ASSET_REFRESH_DELAY_MS);
return new { message = "Shader deleted successfully" };
}
static object GetProjectInfo()
{
// Detect render pipeline with multiple methods
string renderPipeline = "Built-in";
string renderPipelineVersion = "N/A";
string detectionMethod = "Default";
try
{
// Method 1: Check GraphicsSettings.renderPipelineAsset
var renderPipelineAsset = UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset;
Debug.Log($"{SERVER_LOG_PREFIX} RenderPipelineAsset: {(renderPipelineAsset != null ? renderPipelineAsset.GetType().FullName : "null")}");
if (renderPipelineAsset != null)
{
var assetType = renderPipelineAsset.GetType();
var typeName = assetType.Name;
var fullTypeName = assetType.FullName;
Debug.Log($"{SERVER_LOG_PREFIX} Asset type: {typeName}, Full type: {fullTypeName}");
if (fullTypeName.Contains("Universal") || typeName.Contains("Universal") ||
fullTypeName.Contains("URP") || typeName.Contains("URP"))
{
renderPipeline = "URP";
detectionMethod = "GraphicsSettings.renderPipelineAsset";
}
else if (fullTypeName.Contains("HighDefinition") || typeName.Contains("HighDefinition") ||
fullTypeName.Contains("HDRP") || typeName.Contains("HDRP"))
{
renderPipeline = "HDRP";
detectionMethod = "GraphicsSettings.renderPipelineAsset";
}
else
{
renderPipeline = $"Custom ({typeName})";
detectionMethod = "GraphicsSettings.renderPipelineAsset";
}
}
else
{
// Method 2: Check for installed packages if no render pipeline asset
Debug.Log($"{SERVER_LOG_PREFIX} No render pipeline asset found, checking packages...");
try
{
var urpPackage = UnityEditor.PackageManager.PackageInfo.FindForPackageName("com.unity.render-pipelines.universal");
var hdrpPackage = UnityEditor.PackageManager.PackageInfo.FindForPackageName("com.unity.render-pipelines.high-definition");
if (urpPackage != null)
{
renderPipeline = "URP (Package Available)";
renderPipelineVersion = urpPackage.version;
detectionMethod = "Package Detection";
}
else if (hdrpPackage != null)
{
renderPipeline = "HDRP (Package Available)";
renderPipelineVersion = hdrpPackage.version;
detectionMethod = "Package Detection";
}
else
{
renderPipeline = "Built-in";
detectionMethod = "No SRP packages found";
}
}
catch (System.Exception ex)
{
Debug.LogWarning($"{SERVER_LOG_PREFIX} Package detection failed: {ex.Message}");
renderPipeline = "Built-in (Package detection failed)";
detectionMethod = "Package detection error";
}
}
// Try to get version info if not already obtained
if (renderPipelineVersion == "N/A" && renderPipeline.StartsWith("URP"))
{
try
{
var packageInfo = UnityEditor.PackageManager.PackageInfo.FindForPackageName("com.unity.render-pipelines.universal");
if (packageInfo != null)
{
renderPipelineVersion = packageInfo.version;
}
}
catch (System.Exception ex)
{
Debug.LogWarning($"{SERVER_LOG_PREFIX} URP version detection failed: {ex.Message}");
renderPipelineVersion = "Version unknown";
}
}
else if (renderPipelineVersion == "N/A" && renderPipeline.StartsWith("HDRP"))
{
try
{
var packageInfo = UnityEditor.PackageManager.PackageInfo.FindForPackageName("com.unity.render-pipelines.high-definition");
if (packageInfo != null)
{
renderPipelineVersion = packageInfo.version;
}
}
catch (System.Exception ex)
{
Debug.LogWarning($"{SERVER_LOG_PREFIX} HDRP version detection failed: {ex.Message}");
renderPipelineVersion = "Version unknown";
}
}
Debug.Log($"{SERVER_LOG_PREFIX} Detected render pipeline: {renderPipeline} (v{renderPipelineVersion}) via {detectionMethod}");
}
catch (System.Exception ex)
{
Debug.LogError($"{SERVER_LOG_PREFIX} Render pipeline detection failed: {ex.Message}");
renderPipeline = "Detection Failed";
detectionMethod = "Exception occurred";
}
return new
{
projectPath = Application.dataPath.Replace("/Assets", ""),
projectName = Application.productName,
unityVersion = Application.unityVersion,
platform = Application.platform.ToString(),
isPlaying = Application.isPlaying,
renderPipeline = renderPipeline,
renderPipelineVersion = renderPipelineVersion,
detectionMethod = detectionMethod
};
}
static void CreateFolderRecursive(string path)
{
var folders = path.Split('/');
var currentPath = folders[0];
for (int i = 1; i < folders.Length; i++)
{
var newPath = currentPath + "/" + folders[i];
if (!AssetDatabase.IsValidFolder(newPath))
{
AssetDatabase.CreateFolder(currentPath, folders[i]);
}
currentPath = newPath;
}
}
static string GetDefaultScriptContent(string fileName)
{
var className = Path.GetFileNameWithoutExtension(fileName);
return "using UnityEngine;\\n\\n" +
$"public class {className} : MonoBehaviour\\n" +
"{\\n" +
" void Start()\\n" +
" {\\n" +
" \\n" +
" }\\n" +
" \\n" +
" void Update()\\n" +
" {\\n" +
" \\n" +
" }\\n" +
"}";
}
static string GetDefaultShaderContent(string fileName)
{
var shaderName = Path.GetFileNameWithoutExtension(fileName);
return $"Shader \\"Custom/{shaderName}\\"\\n" +
"{\\n" +
" Properties\\n" +
" {\\n" +
" _MainTex (\\"Texture\\", 2D) = \\"white\\" {}\\n" +
" }\\n" +
" SubShader\\n" +
" {\\n" +
" Tags { \\"RenderType\\"=\\"Opaque\\" }\\n" +
" LOD 200\\n" +
"\\n" +
" CGPROGRAM\\n" +
" #pragma surface surf Standard fullforwardshadows\\n" +
"\\n" +
" sampler2D _MainTex;\\n" +
"\\n" +
" struct Input\\n" +
" {\\n" +
" float2 uv_MainTex;\\n" +
" };\\n" +
"\\n" +
" void surf (Input IN, inout SurfaceOutputStandard o)\\n" +
" {\\n" +
" fixed4 c = tex2D (_MainTex, IN.uv_MainTex);\\n" +
" o.Albedo = c.rgb;\\n" +
" o.Alpha = c.a;\\n" +
" }\\n" +
" ENDCG\\n" +
" }\\n" +
" FallBack \\"Diffuse\\"\\n" +
"}";
}
// Folder operations
static object CreateFolder(JObject request)
{
var path = request["path"]?.ToString();
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
if (!path.StartsWith(ASSETS_PREFIX))
path = Path.Combine(DEFAULT_SCRIPTS_FOLDER, path);
// Use Unity-safe folder creation
if (AssetDatabase.IsValidFolder(path))
{
throw new InvalidOperationException($"Folder already exists: {path}");
}
// Create directory structure properly
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
Directory.CreateDirectory(fullPath);
// Import the folder immediately
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport);
// Verify the folder was imported successfully
var attempts = 0;
const int maxAttempts = 10;
while (!AssetDatabase.IsValidFolder(path) && attempts < maxAttempts)
{
System.Threading.Thread.Sleep(100);
AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
attempts++;
}
if (!AssetDatabase.IsValidFolder(path))
{
throw new InvalidOperationException($"Failed to import folder: {path}");
}
return new
{
path = path,
guid = AssetDatabase.AssetPathToGUID(path)
};
}
static object CreateFolderOnWorkerThread(JObject request)
{
var path = request["path"]?.ToString();
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
if (!path.StartsWith(ASSETS_PREFIX))
path = Path.Combine(DEFAULT_SCRIPTS_FOLDER, path);
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
Directory.CreateDirectory(fullPath);
return new
{
path = path,
guid = "" // GUID requires AssetDatabase
};
}
static object RenameFolder(JObject request)
{
var oldPath = request["oldPath"]?.ToString();
var newName = request["newName"]?.ToString();
if (string.IsNullOrEmpty(oldPath))
throw new ArgumentException("oldPath is required");
if (string.IsNullOrEmpty(newName))
throw new ArgumentException("newName is required");
var error = AssetDatabase.RenameAsset(oldPath, newName);
if (!string.IsNullOrEmpty(error))
throw new InvalidOperationException(error);
// Wait for asset database to process
System.Threading.Thread.Sleep(ASSET_REFRESH_DELAY_MS);
var newPath = Path.Combine(Path.GetDirectoryName(oldPath), newName);
return new
{
oldPath = oldPath,
newPath = newPath,
guid = AssetDatabase.AssetPathToGUID(newPath)
};
}
static object RenameFolderOnWorkerThread(JObject request)
{
var oldPath = request["oldPath"]?.ToString();
var newName = request["newName"]?.ToString();
if (string.IsNullOrEmpty(oldPath))
throw new ArgumentException("oldPath is required");
if (string.IsNullOrEmpty(newName))
throw new ArgumentException("newName is required");
var oldFullPath = Path.Combine(Application.dataPath, oldPath.Substring(ASSETS_PREFIX_LENGTH));
var parentDir = Path.GetDirectoryName(oldFullPath);
var newFullPath = Path.Combine(parentDir, newName);
if (!Directory.Exists(oldFullPath))
throw new DirectoryNotFoundException($"Directory not found: {oldPath}");
Directory.Move(oldFullPath, newFullPath);
var newPath = Path.Combine(Path.GetDirectoryName(oldPath), newName);
return new
{
oldPath = oldPath,
newPath = newPath,
guid = "" // GUID requires AssetDatabase
};
}
static object MoveFolder(JObject request)
{
var sourcePath = request["sourcePath"]?.ToString();
var targetPath = request["targetPath"]?.ToString();
if (string.IsNullOrEmpty(sourcePath))
throw new ArgumentException("sourcePath is required");
if (string.IsNullOrEmpty(targetPath))
throw new ArgumentException("targetPath is required");
var error = AssetDatabase.MoveAsset(sourcePath, targetPath);
if (!string.IsNullOrEmpty(error))
throw new InvalidOperationException(error);
// Wait for asset database to process
System.Threading.Thread.Sleep(ASSET_REFRESH_DELAY_MS);
return new
{
sourcePath = sourcePath,
targetPath = targetPath,
guid = AssetDatabase.AssetPathToGUID(targetPath)
};
}
static object MoveFolderOnWorkerThread(JObject request)
{
var sourcePath = request["sourcePath"]?.ToString();
var targetPath = request["targetPath"]?.ToString();
if (string.IsNullOrEmpty(sourcePath))
throw new ArgumentException("sourcePath is required");
if (string.IsNullOrEmpty(targetPath))
throw new ArgumentException("targetPath is required");
var sourceFullPath = Path.Combine(Application.dataPath, sourcePath.Substring(ASSETS_PREFIX_LENGTH));
var targetFullPath = Path.Combine(Application.dataPath, targetPath.Substring(ASSETS_PREFIX_LENGTH));
if (!Directory.Exists(sourceFullPath))
throw new DirectoryNotFoundException($"Directory not found: {sourcePath}");
// Ensure target parent directory exists
var targetParent = Path.GetDirectoryName(targetFullPath);
if (!Directory.Exists(targetParent))
Directory.CreateDirectory(targetParent);
Directory.Move(sourceFullPath, targetFullPath);
return new
{
sourcePath = sourcePath,
targetPath = targetPath,
guid = "" // GUID requires AssetDatabase
};
}
static object DeleteFolder(JObject request)
{
var path = request["path"]?.ToString();
var recursive = request["recursive"]?.Value<bool>() ?? true;
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
if (!Directory.Exists(fullPath))
throw new DirectoryNotFoundException($"Directory not found: {path}");
if (!AssetDatabase.DeleteAsset(path))
throw new InvalidOperationException($"Failed to delete folder: {path}");
// Wait for asset database to process deletion
System.Threading.Thread.Sleep(ASSET_REFRESH_DELAY_MS);
return new { path = path };
}
static object DeleteFolderOnWorkerThread(JObject request)
{
var path = request["path"]?.ToString();
var recursive = request["recursive"]?.Value<bool>() ?? true;
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path is required");
var fullPath = Path.Combine(Application.dataPath, path.Substring(ASSETS_PREFIX_LENGTH));
if (!Directory.Exists(fullPath))
throw new DirectoryNotFoundException($"Directory not found: {path}");
Directory.Delete(fullPath, recursive);
// Also delete .meta file
var metaPath = fullPath + ".meta";
if (File.Exists(metaPath))
File.Delete(metaPath);
return new { path = path };
}
static object ListFolder(JObject request)
{
var path = request["path"]?.ToString() ?? ASSETS_PREFIX;
var recursive = request["recursive"]?.Value<bool>() ?? false;
var fullPath = Path.Combine(Application.dataPath, path.StartsWith(ASSETS_PREFIX) ? path.Substring(ASSETS_PREFIX_LENGTH) : path);
if (!Directory.Exists(fullPath))
throw new DirectoryNotFoundException($"Directory not found: {path}");
var entries = new List<object>();
// Get directories
var dirs = Directory.GetDirectories(fullPath, "*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
foreach (var dir in dirs)
{
var relativePath = ASSETS_PREFIX + GetRelativePath(Application.dataPath, dir);
entries.Add(new
{
path = relativePath,
name = Path.GetFileName(dir),
type = "folder",
guid = AssetDatabase.AssetPathToGUID(relativePath)
});
}
// Get files
var files = Directory.GetFiles(fullPath, "*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Where(f => !f.EndsWith(".meta"));
foreach (var file in files)
{
var relativePath = ASSETS_PREFIX + GetRelativePath(Application.dataPath, file);
entries.Add(new
{
path = relativePath,
name = Path.GetFileName(file),
type = "file",
extension = Path.GetExtension(file),
guid = AssetDatabase.AssetPathToGUID(relativePath)
});
}
return new
{
path = path,
entries = entries
};
}
static object ListFolderOnWorkerThread(JObject request)
{
var path = request["path"]?.ToString() ?? ASSETS_PREFIX;
var recursive = request["recursive"]?.Value<bool>() ?? false;
var fullPath = Path.Combine(Application.dataPath, path.StartsWith(ASSETS_PREFIX) ? path.Substring(ASSETS_PREFIX_LENGTH) : path);
if (!Directory.Exists(fullPath))
throw new DirectoryNotFoundException($"Directory not found: {path}");
var entries = new List<object>();
// Get directories
var dirs = Directory.GetDirectories(fullPath, "*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
foreach (var dir in dirs)
{
var relativePath = ASSETS_PREFIX + GetRelativePath(Application.dataPath, dir);
entries.Add(new
{
path = relativePath,
name = Path.GetFileName(dir),
type = "folder",
guid = "" // GUID requires AssetDatabase
});
}
// Get files
var files = Directory.GetFiles(fullPath, "*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Where(f => !f.EndsWith(".meta"));
foreach (var file in files)
{
var relativePath = ASSETS_PREFIX + GetRelativePath(Application.dataPath, file);
entries.Add(new
{
path = relativePath,
name = Path.GetFileName(file),
type = "file",
extension = Path.GetExtension(file),
guid = "" // GUID requires AssetDatabase
});
}
return new
{
path = path,
entries = entries
};
}
static string GetRelativePath(string basePath, string fullPath)
{
if (!fullPath.StartsWith(basePath))
return fullPath;
var relativePath = fullPath.Substring(basePath.Length);
if (relativePath.StartsWith(Path.DirectorySeparatorChar.ToString()))
relativePath = relativePath.Substring(1);
return relativePath.Replace(Path.DirectorySeparatorChar, '/');
}
static void SendResponse(HttpListenerResponse response, int statusCode, bool success, object result, string error)
{
response.StatusCode = statusCode;
response.ContentType = "application/json; charset=utf-8";
response.ContentEncoding = Encoding.UTF8;
var responseData = new Dictionary<string, object>
{
["success"] = success
};
if (result != null)
responseData["result"] = result;
if (!string.IsNullOrEmpty(error))
responseData["error"] = error;
var json = JsonConvert.SerializeObject(responseData);
var buffer = Encoding.UTF8.GetBytes(json);
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
response.Close();
}
}
}`
});
this.scripts.set("UnityMCPServerWindow.cs", {
fileName: "UnityMCPServerWindow.cs",
version: "1.0.0",
content: `using System;
using UnityEngine;
using UnityEditor;
namespace UnityMCP
{
/// <summary>
/// Unity MCP Server control window
/// </summary>
public class UnityMCPServerWindow : EditorWindow
{
// Version information (should match UnityHttpServer)
private const string SCRIPT_VERSION = "1.1.0";
private int serverPort = 23457;
private bool isServerRunning = false;
private string serverStatus = "Stopped";
private string lastError = "";
[MenuItem("Window/Unity MCP Server")]
public static void ShowWindow()
{
GetWindow<UnityMCPServerWindow>("Unity MCP Server");
}
void OnEnable()
{
// Load saved settings
serverPort = EditorPrefs.GetInt("UnityMCP.ServerPort", 23457);
UpdateStatus();
}
void OnDisable()
{
// Save settings
EditorPrefs.SetInt("UnityMCP.ServerPort", serverPort);
}
void OnGUI()
{
GUILayout.Label("Unity MCP Server Control", EditorStyles.boldLabel);
GUILayout.Label($"Version: {SCRIPT_VERSION}", EditorStyles.miniLabel);
EditorGUILayout.Space();
// Server Status
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Status:", GUILayout.Width(60));
var statusColor = isServerRunning ? Color.green : Color.red;
var originalColor = GUI.color;
GUI.color = statusColor;
GUILayout.Label(serverStatus, EditorStyles.boldLabel);
GUI.color = originalColor;
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
// Port Configuration
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Port:", GUILayout.Width(60));
var newPort = EditorGUILayout.IntField(serverPort);
if (newPort != serverPort && newPort > 0 && newPort <= 65535)
{
serverPort = newPort;
EditorPrefs.SetInt("UnityMCP.ServerPort", serverPort);
}
EditorGUILayout.EndHorizontal();
// Port validation
if (serverPort < 1024)
{
EditorGUILayout.HelpBox("Warning: Ports below 1024 may require administrator privileges.", MessageType.Warning);
}
EditorGUILayout.Space();
// Control Buttons
EditorGUILayout.BeginHorizontal();
GUI.enabled = !isServerRunning;
if (GUILayout.Button("Start Server", GUILayout.Height(30)))
{
StartServer();
}
GUI.enabled = isServerRunning;
if (GUILayout.Button("Stop Server", GUILayout.Height(30)))
{
StopServer();
}
GUI.enabled = true;
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
// Connection Info
if (isServerRunning)
{
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
GUILayout.Label("Connection Information", EditorStyles.boldLabel);
EditorGUILayout.SelectableLabel($"http://localhost:{serverPort}/");
EditorGUILayout.EndVertical();
}
// Error Display
if (!string.IsNullOrEmpty(lastError))
{
EditorGUILayout.Space();
EditorGUILayout.HelpBox(lastError, MessageType.Error);
if (GUILayout.Button("Clear Error"))
{
lastError = "";
}
}
EditorGUILayout.Space();
// Instructions
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
GUILayout.Label("Instructions", EditorStyles.boldLabel);
GUILayout.Label("1. Configure the port (default: 23457)");
GUILayout.Label("2. Click 'Start Server' to begin");
GUILayout.Label("3. Use the MCP client to connect");
GUILayout.Label("4. Click 'Stop Server' when done");
EditorGUILayout.EndVertical();
}
void StartServer()
{
try
{
UnityHttpServer.Start(serverPort);
UpdateStatus();
lastError = "";
Debug.Log($"[UnityMCP] Server started on port {serverPort}");
}
catch (Exception e)
{
lastError = $"Failed to start server: {e.Message}";
Debug.LogError($"[UnityMCP] {lastError}");
}
}
void StopServer()
{
try
{
UnityHttpServer.Shutdown();
UpdateStatus();
lastError = "";
Debug.Log("[UnityMCP] Server stopped");
}
catch (Exception e)
{
lastError = $"Failed to stop server: {e.Message}";
Debug.LogError($"[UnityMCP] {lastError}");
}
}
void UpdateStatus()
{
isServerRunning = UnityHttpServer.IsRunning;
serverStatus = isServerRunning ? $"Running on port {UnityHttpServer.CurrentPort}" : "Stopped";
Repaint();
}
void Update()
{
// Update status periodically
UpdateStatus();
}
}
}`
});
}
/**
* Get script by filename
*/
async getScript(fileName) {
return this.scripts.get(fileName) || null;
}
/**
* Get script synchronously
*/
getScriptSync(fileName) {
return this.scripts.get(fileName) || null;
}
/**
* Write script to file with proper UTF-8 BOM for Unity compatibility
*/
async writeScriptToFile(fileName, targetPath) {
const script = await this.getScript(fileName);
if (!script) {
throw new Error(`Script not found: ${fileName}`);
}
await fs.mkdir(path.dirname(targetPath), { recursive: true });
const utf8BOM = Buffer.from([239, 187, 191]);
const contentBuffer = Buffer.from(script.content, "utf8");
const finalBuffer = Buffer.concat([utf8BOM, contentBuffer]);
await fs.writeFile(targetPath, finalBuffer);
}
/**
* Get all available script names
*/
getAvailableScripts() {
return Array.from(this.scripts.keys());
}
/**
* Get script version
*/
getScriptVersion(fileName) {
const script = this.scripts.get(fileName);
return script?.version || null;
}
};
// build/services/unity-bridge-deploy-service.js
var UnityBridgeDeployService = class {
constructor() {
this.logger = {
info: (msg) => console.error(`[Unity MCP Deploy] ${msg}`),
debug: (msg) => console.error(`[Unity MCP Deploy] DEBUG: ${msg}`),
error: (msg) => console.error(`[Unity MCP Deploy] ERROR: ${msg}`)
};
this.scriptsProvider = new EmbeddedScriptsProvider();
this.SCRIPTS = [
{
fileName: "UnityHttpServer.cs",
targetPath: "Assets/Editor/MCP/UnityHttpServer.cs",
version: "1.1.0"
},
{
fileName: "UnityMCPServerWindow.cs",
targetPath: "Assets/Editor/MCP/UnityMCPServerWindow.cs",
version: "1.1.0"
}
];
}
async deployScripts(options) {
const { projectPath, forceUpdate = false } = options;
const projectValidation = await this.validateUnityProject(projectPath);
if (!projectValidation.isValid) {
throw new Error(`Invalid Unity project: ${projectValidation.error}`);
}
const editorMCPPath = path2.join(projectPath, "Assets", "Editor", "MCP");
await fs2.mkdir(editorMCPPath, { recursive: true });
for (const script of this.SCRIPTS) {
await this.deployScript(projectPath, script, forceUpdate);
}
this.logger.info("Unity MCP scripts deployed successfully");
}
async deployScript(projectPath, script, forceUpdate) {
const targetPath = path2.join(projectPath, script.targetPath);
const needsUpdate = await this.checkNeedsUpdate(targetPath, script.version, forceUpdate);
if (needsUpdate) {
const embeddedScript = await this.scriptsProvider.getScript(script.fileName);
if (!embeddedScript) {
throw new Error(`Embedded script not found: ${script.fileName}`);
}
this.logger.debug(`Using embedded script: ${script.fileName} (loaded from source)`);
await this.removeExistingFiles(targetPath);
await this.scriptsProvider.writeScriptToFile(script.fileName, targetPath);
await this.generateMetaFile(targetPath);
this.logger.info(`Deployed ${script.fileName} to ${script.targetPath}`);
} else {
this.logger.debug(`${script.fileName} is up to date`);
}
}
async checkNeedsUpdate(targetPath, currentVersion, forceUpdate) {
if (forceUpdate) {
return true;
}
try {
const content = await fs2.readFile(targetPath, "utf8");
const versionMatch = content.match(/SCRIPT_VERSION\s*=\s*"([^"]+)"/);
if (versionMatch) {
const installedVersion = versionMatch[1];
return this.compareVersions(currentVersion, installedVersion) > 0;
}
return true;
} catch (error) {
return true;
}
}
compareVersions(version1, version2) {
const v1Parts = version1.split(".").map(Number);
const v2Parts = version2.split(".").map(Number);
for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) {
const v1Part = v1Parts[i] || 0;
const v2Part = v2Parts[i] || 0;
if (v1Part > v2Part)
return 1;
if (v1Part < v2Part)
return -1;
}
return 0;
}
async generateMetaFile(filePath) {
const metaPath = filePath + ".meta";
try {
await fs2.access(metaPath);
return;
} catch {
}
const guid = this.generateGUID();
const metaContent = `fileFormatVersion: 2
guid: ${guid}
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
`;
await fs2.writeFile(metaPath, metaContent, "utf8");
}
async removeExistingFiles(targetPath) {
try {
await fs2.unlink(targetPath);
this.logger.debug(`Removed existing file: ${targetPath}`);
} catch (error) {
if (error.code !== "ENOENT") {
this.logger.debug(`Failed to remove file ${targetPath}: ${error.message}`);
}
}
try {
const metaPath = targetPath + ".meta";
await fs2.unlink(metaPath);
this.logger.debug(`Removed existing meta file: ${metaPath}`);
} catch (error) {
if (error.code !== "ENOENT") {
this.logger.debug(`Failed to remove meta file ${targetPath}.meta: ${error.message}`);
}
}
}
generateGUID() {
const hex = "0123456789abcdef";
let guid = "";
for (let i = 0; i < 32; i++) {
guid += hex[Math.floor(Math.random() * 16)];
}
return guid;
}
async validateUnityProject(projectPath) {
try {
const stats = await fs2.stat(projectPath);
if (!stats.isDirectory()) {
return { isValid: false, error: "Path is not a directory" };
}
const requiredDirs = ["Assets", "ProjectSettings"];
for (const dir of requiredDirs) {
try {
const dirPath = path2.join(projectPath, dir);
const dirStats = await fs2.stat(dirPath);
if (!dirStats.isDirectory()) {
return { isValid: false, error: `Missing ${dir} directory` };
}
} catch {
return { isValid: false, error: `Missing ${dir} directory` };
}
}
return { isValid: true };
} catch (error) {
return { isValid: false, error: error.message };
}
}
};
// build/tools/unity-mcp-tools.js
var UnityMcpTools = class {
constructor() {
const port = process.env.UNITY_MCP_PORT ? parseInt(process.env.UNITY_MCP_PORT) : 23457;
const url = `http://localhost:${port}/`;
console.error(`[Unity MCP] Connecting to Unity at ${url}`);
this.adapter = new UnityHttpAdapter({
url,
timeout: parseInt(process.env.UNITY_MCP_TIMEOUT || "120000")
});
this.deployService = new UnityBridgeDeployService();
this.checkConnection();
}
async checkConnection() {
try {
const connected = await this.adapter.isConnected();
if (connected) {
console.error("[Unity MCP] Successfully connected to Unity HTTP server");
} else {
console.error("[Unity MCP] Unity HTTP server is not responding");
}
} catch (error) {
console.error(`[Unity MCP] Connection check failed: ${error.message}`);
}
}
/**
* Auto-deploy Unity MCP scripts if connected
*/
async autoDeployScripts() {
try {
const result = await this.adapter.getProjectInfo();
await this.deployService.deployScripts({
projectPath: result.projectPath,
forceUpdate: false
});
} catch (error) {
console.error(`[Unity MCP] Failed to auto-deploy scripts: ${error.message}`);
}
}
/**
* Get all available tools
*/
getTools() {
return [
// Script tools
{
name: "script_create",
description: "Create a new C# script in Unity project",
inputSchema: {
type: "object",
properties: {
fileName: {
type: "string",
description: "Name of the script file (without .cs extension)"
},
content: {
type: "string",
description: "Script content (optional, will use template if not provided)"
},
folder: {
type: "string",
description: "Target folder path (default: Assets/Scripts)"
}
},
required: ["fileName"]
}
},
{
name: "script_read",
description: "Read a C# script from Unity project",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Path to the script file"
}
},
required: ["path"]
}
},
{
name: "script_delete",
description: "Delete a C# script from Unity project",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Path to the script file"
}
},
required: ["path"]
}
},
{
name: "script_apply_diff",
description: "Apply a unified diff to a C# script",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Path to the script file"
},
diff: {
type: "string",
description: "Unified diff content to apply"
},
options: {
type: "object",
description: "Optional diff application settings",
properties: {
dryRun: {
type: "boolean",
description: "Preview changes without applying (default: false)"
}
}
}
},
required: ["path", "diff"]
}
},
// Shader tools
{
name: "shader_create",
description: "Create a new shader in Unity project",
inputSchema: {
type: "object",
properties: {
name: {
type: "string",
description: "Name of the shader (without .shader extension)"
},
content: {
type: "string",
description: "Shader content (optional, will use template if not provided)"
},
folder: {
type: "string",
description: "Target folder path (default: Assets/Shaders)"
}
},
required: ["name"]
}
},
{
name: "shader_read",
description: "Read a shader from Unity project",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Path to the shader file"
}
},
required: ["path"]
}
},
{
name: "shader_delete",
description: "Delete a shader from Unity project",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Path to the shader file"
}
},
required: ["path"]
}
},
// Project tools
{
name: "project_info",
description: "Get comprehensive Unity project information including render pipeline details, project path, Unity version, and platform info",
inputSchema: {
type: "object",
properties: {}
}
},
{
name: "project_status",
description: "Check Unity MCP server connection status (simple connectivity test only)",
inputSchema: {
type: "object",
properties: {}
}
},
{
name: "setup_unity_bridge",
description: "Install/update Unity MCP bridge scripts to a Unity project (works even if Unity server is not running)",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the Unity project"
},
forceUpdate: {
type: "boolean",
description: "Force update even if scripts are up to date",
default: false
}
},
required: ["projectPath"]
}
},
// Folder tools
{
name: "folder_create",
description: "Create a new folder in Unity project",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Path for the new folder (e.g., Assets/MyFolder)"
}
},
required: ["path"]
}
},
{
name: "folder_rename",
description: "Rename a folder in Unity project",
inputSchema: {
type: "object",
properties: {
oldPath: {
type: "string",
description: "Current path of the folder"
},
newName: {
type: "string",
description: "New name for the folder"
}
},
required: ["oldPath", "newName"]
}
},
{
name: "folder_move",
description: "Move a folder to a new location in Unity project",
inputSchema: {
type: "object",
properties: {
sourcePath: {
type: "string",
description: "Current path of the folder"
},
targetPath: {
type: "string",
description: "Target path for the folder"
}
},
required: ["sourcePath", "targetPath"]
}
},
{
name: "folder_delete",
description: "Delete a folder from Unity project",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Path of the folder to delete"
},
recursive: {
type: "boolean",
description: "Delete all contents recursively (default: true)"
}
},
required: ["path"]
}
},
{
name: "folder_list",
description: "List contents of a folder in Unity project",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Path of the folder to list (default: Assets)"
},
recursive: {
type: "boolean",
description: "List all subdirectories recursively (default: false)"
}
}
}
}
];
}
/**
* Execute a tool
*/
async executeTool(toolName, args) {
try {
switch (toolName) {
// Script operations
case "script_create": {
if (!args.fileName) {
throw new Error("fileName is required");
}
const result = await this.adapter.createScript(args.fileName, args.content, args.folder);
return {
content: [{
type: "text",
text: `Script created successfully:
Path: ${result.path}
GUID: ${result.guid}`
}]
};
}
case "script_read": {
if (!args.path) {
throw new Error("path is required");
}
const result = await this.adapter.readScript(args.path);
return {
content: [{
type: "text",
text: `Script content from ${result.path}:
${result.content}`
}]
};
}
case "script_delete": {
if (!args.path) {
throw new Error("path is required");
}
await this.adapter.deleteScript(args.path);
return {
content: [{
type: "text",
text: `Script deleted successfully: ${args.path}`
}]
};
}
case "script_apply_diff": {
if (!args.path || !args.diff) {
throw new Error("path and diff are required");
}
const result = await this.adapter.applyDiff(args.path, args.diff, args.options);
return {
content: [{
type: "text",
text: `Diff applied successfully:
Path: ${result.path}
Lines added: ${result.linesAdded}
Lines removed: ${result.linesRemoved}`
}]
};
}
// Shader operations
case "shader_create": {
if (!args.name) {
throw new Error("name is required");
}
const result = await this.adapter.createShader(args.name, args.content, args.folder);
return {
content: [{
type: "text",
text: `Shader created successfully:
Path: ${result.path}
GUID: ${result.guid}`
}]
};
}
case "shader_read": {
if (!args.path) {
throw new Error("path is required");
}
const result = await this.adapter.readShader(args.path);
return {
content: [{
type: "text",
text: `Shader content from ${result.path}:
${result.content}`
}]
};
}
case "shader_delete": {
if (!args.path) {
throw new Error("path is required");
}
await this.adapter.deleteShader(args.path);
return {
content: [{
type: "text",
text: `Shader deleted successfully: ${args.path}`
}]
};
}
// Project operations
case "project_info": {
const result = await this.adapter.getProjectInfo();
await this.autoDeployScripts();
return {
content: [{
type: "text",
text: `Unity Project Information:
Project Path: ${result.projectPath}
Project Name: ${result.projectName || "N/A"}
Unity Version: ${result.unityVersion}
Platform: ${result.platform}
Is Playing: ${result.isPlaying}
Render Pipeline: ${result.renderPipeline || "Unknown"}
Render Pipeline Version: ${result.renderPipelineVersion || "N/A"}`
}]
};
}
case "project_status": {
const connected = await this.adapter.isConnected();
const status = connected ? "Unity server is connected and ready" : "Unity server is not connected or not responding";
return {
content: [{
type: "text",
text: status
}]
};
}
case "setup_unity_bridge": {
const { projectPath, forceUpdate } = args;
if (!projectPath) {
throw new Error("projectPath is required");
}
try {
await this.deployService.deployScripts({ projectPath, forceUpdate });
return {
content: [{
type: "text",
text: `Unity MCP bridge scripts installed successfully to:
${projectPath}/Assets/Editor/MCP/
Please restart Unity Editor or open Window > Unity MCP Server to start the server.`
}]
};
} catch (error) {
throw new Error(`Failed to install scripts: ${error.message}`);
}
}
// Folder operations
case "folder_create": {
if (!args.path) {
throw new Error("path is required");
}
const result = await this.adapter.createFolder(args.path);
return {
content: [{
type: "text",
text: `Folder created successfully:
Path: ${result.path}
GUID: ${result.guid}`
}]
};
}
case "folder_rename": {
if (!args.oldPath || !args.newName) {
throw new Error("oldPath and newName are required");
}
const result = await this.adapter.renameFolder(args.oldPath, args.newName);
return {
content: [{
type: "text",
text: `Folder renamed successfully:
Old Path: ${result.oldPath}
New Path: ${result.newPath}
GUID: ${result.guid}`
}]
};
}
case "folder_move": {
if (!args.sourcePath || !args.targetPath) {
throw new Error("sourcePath and targetPath are required");
}
const result = await this.adapter.moveFolder(args.sourcePath, args.targetPath);
return {
content: [{
type: "text",
text: `Folder moved successfully:
From: ${result.sourcePath}
To: ${result.targetPath}
GUID: ${result.guid}`
}]
};
}
case "folder_delete": {
if (!args.path) {
throw new Error("path is required");
}
await this.adapter.deleteFolder(args.path, args.recursive);
return {
content: [{
type: "text",
text: `Folder deleted successfully: ${args.path}`
}]
};
}
case "folder_list": {
const result = await this.adapter.listFolder(args.path, args.recursive);
const entries = result.entries.map((e) => {
const prefix = e.type === "folder" ? "\u{1F4C1}" : "\u{1F4C4}";
const info = e.type === "file" ? ` (${e.extension})` : "";
return `${prefix} ${e.name}${info} - ${e.path}`;
}).join("\n");
return {
content: [{
type: "text",
text: `Contents of ${result.path}:
${entries || "(empty)"}`
}]
};
}
default:
throw new Error(`Unknown tool: ${toolName}`);
}
} catch (error) {
return {
content: [{
type: "text",
text: `Error: ${error.message}`
}]
};
}
}
};
// build/simple-index.js
var UnityMcpServer = class {
constructor() {
this.tools = new UnityMcpTools();
this.server = new Server({
name: "unity-mcp-server",
version: "1.0.0"
}, {
capabilities: {
tools: {}
}
});
this.setupHandlers();
}
setupHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: this.tools.getTools()
}));
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
return this.tools.executeTool(request.params.name, request.params.arguments || {});
});
}
async run() {
try {
console.error("[Unity MCP] Starting server...");
const transport = new StdioServerTransport();
console.error("[Unity MCP] Connecting to transport...");
await this.server.connect(transport);
console.error("[Unity MCP] Server connected successfully");
process.stdin.resume();
process.on("SIGINT", () => {
console.error("[Unity MCP] Received SIGINT, shutting down...");
process.exit(0);
});
process.on("SIGTERM", () => {
console.error("[Unity MCP] Received SIGTERM, shutting down...");
process.exit(0);
});
console.error("[Unity MCP] Server is ready and listening");
} catch (error) {
console.error("[Unity MCP] Failed to start server:", error);
throw error;
}
}
};
var server = new UnityMcpServer();
server.run().catch((error) => {
console.error("[Unity MCP] Fatal error:", error);
process.exit(1);
});
process.on("uncaughtException", (error) => {
console.error("[Unity MCP] Uncaught exception:", error);
process.exit(1);
});
process.on("unhandledRejection", (reason, promise) => {
console.error("[Unity MCP] Unhandled rejection at:", promise, "reason:", reason);
process.exit(1);
});