@mcp-web/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +253 -0
- package/dist/addTool.typetest.d.ts +11 -0
- package/dist/addTool.typetest.d.ts.map +1 -0
- package/dist/addTool.typetest.js +248 -0
- package/dist/create-state-tools.d.ts +77 -0
- package/dist/create-state-tools.d.ts.map +1 -0
- package/dist/create-state-tools.js +181 -0
- package/dist/create-tool.d.ts +90 -0
- package/dist/create-tool.d.ts.map +1 -0
- package/dist/create-tool.js +82 -0
- package/dist/expanded-schema-tools/generate-fixed-shape-tools.d.ts +8 -0
- package/dist/expanded-schema-tools/generate-fixed-shape-tools.d.ts.map +1 -0
- package/dist/expanded-schema-tools/generate-fixed-shape-tools.js +53 -0
- package/dist/expanded-schema-tools/generate-fixed-shape-tools.test.d.ts +2 -0
- package/dist/expanded-schema-tools/generate-fixed-shape-tools.test.d.ts.map +1 -0
- package/dist/expanded-schema-tools/generate-fixed-shape-tools.test.js +331 -0
- package/dist/expanded-schema-tools/index.d.ts +4 -0
- package/dist/expanded-schema-tools/index.d.ts.map +1 -0
- package/dist/expanded-schema-tools/index.js +2 -0
- package/dist/expanded-schema-tools/integration.test.d.ts +2 -0
- package/dist/expanded-schema-tools/integration.test.d.ts.map +1 -0
- package/dist/expanded-schema-tools/integration.test.js +599 -0
- package/dist/expanded-schema-tools/schema-analysis.d.ts +18 -0
- package/dist/expanded-schema-tools/schema-analysis.d.ts.map +1 -0
- package/dist/expanded-schema-tools/schema-analysis.js +142 -0
- package/dist/expanded-schema-tools/schema-analysis.test.d.ts +2 -0
- package/dist/expanded-schema-tools/schema-analysis.test.d.ts.map +1 -0
- package/dist/expanded-schema-tools/schema-analysis.test.js +314 -0
- package/dist/expanded-schema-tools/schema-helpers.d.ts +69 -0
- package/dist/expanded-schema-tools/schema-helpers.d.ts.map +1 -0
- package/dist/expanded-schema-tools/schema-helpers.js +139 -0
- package/dist/expanded-schema-tools/schema-helpers.test.d.ts +2 -0
- package/dist/expanded-schema-tools/schema-helpers.test.d.ts.map +1 -0
- package/dist/expanded-schema-tools/schema-helpers.test.js +223 -0
- package/dist/expanded-schema-tools/tool-generator.d.ts +10 -0
- package/dist/expanded-schema-tools/tool-generator.d.ts.map +1 -0
- package/dist/expanded-schema-tools/tool-generator.js +430 -0
- package/dist/expanded-schema-tools/tool-generator.test.d.ts +2 -0
- package/dist/expanded-schema-tools/tool-generator.test.d.ts.map +1 -0
- package/dist/expanded-schema-tools/tool-generator.test.js +689 -0
- package/dist/expanded-schema-tools/types.d.ts +26 -0
- package/dist/expanded-schema-tools/types.d.ts.map +1 -0
- package/dist/expanded-schema-tools/types.js +1 -0
- package/dist/expanded-schema-tools/utils.d.ts +16 -0
- package/dist/expanded-schema-tools/utils.d.ts.map +1 -0
- package/dist/expanded-schema-tools/utils.js +35 -0
- package/dist/expanded-schema-tools/utils.test.d.ts +2 -0
- package/dist/expanded-schema-tools/utils.test.d.ts.map +1 -0
- package/dist/expanded-schema-tools/utils.test.js +169 -0
- package/dist/group-state.d.ts +60 -0
- package/dist/group-state.d.ts.map +1 -0
- package/dist/group-state.js +54 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/query.d.ts +104 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +128 -0
- package/dist/schema-helpers.d.ts +69 -0
- package/dist/schema-helpers.d.ts.map +1 -0
- package/dist/schema-helpers.js +139 -0
- package/dist/schemas.d.ts +140 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +70 -0
- package/dist/tool-generators/generate-basic-state-tools.d.ts +23 -0
- package/dist/tool-generators/generate-basic-state-tools.d.ts.map +1 -0
- package/dist/tool-generators/generate-basic-state-tools.js +95 -0
- package/dist/tool-generators/generate-fixed-shape-tools.d.ts +8 -0
- package/dist/tool-generators/generate-fixed-shape-tools.d.ts.map +1 -0
- package/dist/tool-generators/generate-fixed-shape-tools.js +53 -0
- package/dist/tool-generators/index.d.ts +6 -0
- package/dist/tool-generators/index.d.ts.map +1 -0
- package/dist/tool-generators/index.js +3 -0
- package/dist/tool-generators/schema-analysis.d.ts +18 -0
- package/dist/tool-generators/schema-analysis.d.ts.map +1 -0
- package/dist/tool-generators/schema-analysis.js +142 -0
- package/dist/tool-generators/schema-helpers.d.ts +87 -0
- package/dist/tool-generators/schema-helpers.d.ts.map +1 -0
- package/dist/tool-generators/schema-helpers.js +157 -0
- package/dist/tool-generators/tool-generator.d.ts +11 -0
- package/dist/tool-generators/tool-generator.d.ts.map +1 -0
- package/dist/tool-generators/tool-generator.js +437 -0
- package/dist/tool-generators/types.d.ts +26 -0
- package/dist/tool-generators/types.d.ts.map +1 -0
- package/dist/tool-generators/types.js +1 -0
- package/dist/tool-generators/utils.d.ts +16 -0
- package/dist/tool-generators/utils.d.ts.map +1 -0
- package/dist/tool-generators/utils.js +35 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +31 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +108 -0
- package/dist/web.d.ts +680 -0
- package/dist/web.d.ts.map +1 -0
- package/dist/web.js +1312 -0
- package/dist/zod-to-tools.d.ts +49 -0
- package/dist/zod-to-tools.d.ts.map +1 -0
- package/dist/zod-to-tools.js +623 -0
- package/package.json +58 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { isKeyField, unwrapSchema } from './schema-helpers.js';
|
|
3
|
+
/**
|
|
4
|
+
* Analyzes a schema to determine its shape characteristics.
|
|
5
|
+
*/
|
|
6
|
+
export function analyzeSchemaShape(schema) {
|
|
7
|
+
const unwrapped = unwrapSchema(schema);
|
|
8
|
+
// Default result
|
|
9
|
+
const result = {
|
|
10
|
+
type: 'unsupported',
|
|
11
|
+
subtype: 'unknown',
|
|
12
|
+
hasOptionalFields: false,
|
|
13
|
+
optionalPaths: [],
|
|
14
|
+
fixedPaths: [],
|
|
15
|
+
dynamicPaths: [],
|
|
16
|
+
};
|
|
17
|
+
if (unwrapped instanceof z.ZodString ||
|
|
18
|
+
unwrapped instanceof z.ZodNumber ||
|
|
19
|
+
unwrapped instanceof z.ZodBoolean ||
|
|
20
|
+
unwrapped instanceof z.ZodLiteral ||
|
|
21
|
+
unwrapped instanceof z.ZodEnum) {
|
|
22
|
+
result.type = 'fixed';
|
|
23
|
+
result.subtype = 'primitive';
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
if (unwrapped instanceof z.ZodTuple) {
|
|
27
|
+
result.type = 'fixed';
|
|
28
|
+
result.subtype = 'tuple';
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
if (unwrapped instanceof z.ZodArray) {
|
|
32
|
+
result.type = 'dynamic';
|
|
33
|
+
result.subtype = 'array';
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
if (unwrapped instanceof z.ZodRecord) {
|
|
37
|
+
result.type = 'dynamic';
|
|
38
|
+
result.subtype = 'record';
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
// Objects - analyze further
|
|
42
|
+
if (unwrapped instanceof z.ZodObject) {
|
|
43
|
+
result.subtype = 'object';
|
|
44
|
+
const shape = unwrapped.shape;
|
|
45
|
+
for (const [key, field] of Object.entries(shape)) {
|
|
46
|
+
const zodField = field;
|
|
47
|
+
const unwrappedField = unwrapSchema(zodField);
|
|
48
|
+
// Check for optional fields
|
|
49
|
+
if (zodField instanceof z.ZodOptional) {
|
|
50
|
+
result.hasOptionalFields = true;
|
|
51
|
+
result.optionalPaths.push(key);
|
|
52
|
+
}
|
|
53
|
+
// Check if field is dynamic (array or record)
|
|
54
|
+
if (unwrappedField instanceof z.ZodArray || unwrappedField instanceof z.ZodRecord) {
|
|
55
|
+
result.dynamicPaths.push(key);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
result.fixedPaths.push(key);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Determine object type
|
|
62
|
+
if (result.dynamicPaths.length > 0 && result.fixedPaths.length > 0) {
|
|
63
|
+
result.type = 'mixed';
|
|
64
|
+
}
|
|
65
|
+
else if (result.dynamicPaths.length > 0) {
|
|
66
|
+
result.type = 'dynamic';
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
result.type = 'fixed';
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Detects the ID field in an object schema.
|
|
77
|
+
* Returns information about explicit id() markers.
|
|
78
|
+
* Throws an error if multiple id() markers are found.
|
|
79
|
+
*/
|
|
80
|
+
export function findIdField(schema) {
|
|
81
|
+
const shape = schema.shape;
|
|
82
|
+
let explicitKey = null;
|
|
83
|
+
for (const [name, field] of Object.entries(shape)) {
|
|
84
|
+
if (isKeyField(field)) {
|
|
85
|
+
// Error if multiple id() markers
|
|
86
|
+
if (explicitKey) {
|
|
87
|
+
throw new Error(`Multiple fields marked with id(): '${explicitKey}' and '${name}'.\n` +
|
|
88
|
+
`Only one field can be the ID. For compound keys, use index-based addressing.`);
|
|
89
|
+
}
|
|
90
|
+
explicitKey = name;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (explicitKey) {
|
|
94
|
+
return { type: 'explicit', field: explicitKey };
|
|
95
|
+
}
|
|
96
|
+
// No id() marker → use index-based addressing
|
|
97
|
+
return { type: 'none' };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Validates that a schema only uses supported types.
|
|
101
|
+
* Throws an error if unsupported types are found.
|
|
102
|
+
*/
|
|
103
|
+
export function validateSupportedTypes(schema, path = 'root') {
|
|
104
|
+
const errors = [];
|
|
105
|
+
const unwrapped = unwrapSchema(schema);
|
|
106
|
+
const supported = [
|
|
107
|
+
z.ZodObject,
|
|
108
|
+
z.ZodArray,
|
|
109
|
+
z.ZodRecord,
|
|
110
|
+
z.ZodString,
|
|
111
|
+
z.ZodNumber,
|
|
112
|
+
z.ZodBoolean,
|
|
113
|
+
z.ZodLiteral,
|
|
114
|
+
z.ZodEnum,
|
|
115
|
+
z.ZodTuple,
|
|
116
|
+
z.ZodDate,
|
|
117
|
+
z.ZodBigInt,
|
|
118
|
+
z.ZodNull,
|
|
119
|
+
z.ZodUndefined,
|
|
120
|
+
];
|
|
121
|
+
const isSupported = supported.some((type) => unwrapped instanceof type);
|
|
122
|
+
if (!isSupported) {
|
|
123
|
+
errors.push(`Unsupported type at '${path}': ${unwrapped.constructor.name}`);
|
|
124
|
+
}
|
|
125
|
+
// Recurse for nested schemas
|
|
126
|
+
if (unwrapped instanceof z.ZodObject) {
|
|
127
|
+
for (const [key, field] of Object.entries(unwrapped.shape)) {
|
|
128
|
+
errors.push(...validateSupportedTypes(field, `${path}.${key}`));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
else if (unwrapped instanceof z.ZodArray) {
|
|
132
|
+
errors.push(...validateSupportedTypes(unwrapped.element, `${path}[]`));
|
|
133
|
+
}
|
|
134
|
+
else if (unwrapped instanceof z.ZodRecord) {
|
|
135
|
+
const def = unwrapped._def;
|
|
136
|
+
const valueType = def.valueType;
|
|
137
|
+
if (valueType) {
|
|
138
|
+
errors.push(...validateSupportedTypes(valueType, `${path}{}`));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return errors;
|
|
142
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Marks a field as the unique identifier for array elements.
|
|
4
|
+
*
|
|
5
|
+
* When an array schema has a field marked with `id()`, the generated tools
|
|
6
|
+
* use ID-based operations (e.g., `set_todos_by_id`) instead of index-based.
|
|
7
|
+
* Only one field per schema can be marked with `id()`.
|
|
8
|
+
*
|
|
9
|
+
* @param schema - The Zod schema to mark as the key field
|
|
10
|
+
* @returns The same schema with key field metadata attached
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const TodoSchema = z.object({
|
|
15
|
+
* id: id(z.string()),
|
|
16
|
+
* title: z.string(),
|
|
17
|
+
* completed: z.boolean(),
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // Generates: get_todos, set_todos_by_id, delete_todos_by_id
|
|
21
|
+
* mcp.addStateTools({ name: 'todos', schema: z.array(TodoSchema), expand: true });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function id<T extends z.ZodTypeAny>(schema: T): T;
|
|
25
|
+
/**
|
|
26
|
+
* Marks a field as system-generated and excludes it from input schemas.
|
|
27
|
+
*
|
|
28
|
+
* System fields are auto-populated and never provided by the AI. They are
|
|
29
|
+
* excluded from add/set input schemas but included in output/getter schemas.
|
|
30
|
+
* Fields marked with `system()` MUST have a `.default()` value.
|
|
31
|
+
*
|
|
32
|
+
* Common uses: auto-generated IDs, timestamps, version numbers.
|
|
33
|
+
*
|
|
34
|
+
* @param schema - The Zod schema to mark as system-generated (must have default)
|
|
35
|
+
* @returns The same schema with system field metadata attached
|
|
36
|
+
* @throws {Error} At tool generation time if schema lacks a default value
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const TodoSchema = z.object({
|
|
41
|
+
* id: id(system(z.string().default(() => crypto.randomUUID()))),
|
|
42
|
+
* createdAt: system(z.number().default(() => Date.now())),
|
|
43
|
+
* title: z.string(), // User provides this
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function system<T extends z.ZodTypeAny>(schema: T): T;
|
|
48
|
+
/**
|
|
49
|
+
* Checks if a field is marked with id().
|
|
50
|
+
*/
|
|
51
|
+
export declare function isKeyField(field: z.ZodTypeAny): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Checks if a field is marked with system().
|
|
54
|
+
*/
|
|
55
|
+
export declare function isSystemField(field: z.ZodTypeAny): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Checks if a schema has a default value.
|
|
58
|
+
*/
|
|
59
|
+
export declare function hasDefault(schema: z.ZodTypeAny): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Unwraps a schema from ZodDefault, ZodOptional, ZodNullable wrappers.
|
|
62
|
+
* Returns the innermost schema.
|
|
63
|
+
*/
|
|
64
|
+
export declare function unwrapSchema(schema: z.ZodTypeAny): z.ZodTypeAny;
|
|
65
|
+
/**
|
|
66
|
+
* Unwraps ZodDefault to get the inner schema.
|
|
67
|
+
*/
|
|
68
|
+
export declare function unwrapDefault(schema: z.ZodTypeAny): z.ZodTypeAny;
|
|
69
|
+
/**
|
|
70
|
+
* Derives an input schema for add operations.
|
|
71
|
+
* - system() fields are excluded
|
|
72
|
+
* - default() fields become optional
|
|
73
|
+
* - other fields remain required
|
|
74
|
+
*/
|
|
75
|
+
export declare function deriveAddInputSchema(schema: z.ZodObject<z.ZodRawShape>): z.ZodObject<z.ZodRawShape>;
|
|
76
|
+
/**
|
|
77
|
+
* Derives an input schema for set operations (partial updates).
|
|
78
|
+
* - system() fields are excluded
|
|
79
|
+
* - all other fields become optional
|
|
80
|
+
*/
|
|
81
|
+
export declare function deriveSetInputSchema(schema: z.ZodObject<z.ZodRawShape>): z.ZodObject<z.ZodRawShape>;
|
|
82
|
+
/**
|
|
83
|
+
* Validates that all system() fields have default values.
|
|
84
|
+
* Throws an error if a system field lacks a default.
|
|
85
|
+
*/
|
|
86
|
+
export declare function validateSystemFields(schema: z.ZodObject<z.ZodRawShape>): void;
|
|
87
|
+
//# sourceMappingURL=schema-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-helpers.d.ts","sourceRoot":"","sources":["../../src/tool-generators/schema-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAGvD;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAG3D;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,CAAC,CAAC,UAAU,GAAG,OAAO,CAEvD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,CAAC,CAAC,UAAU,GAAG,OAAO,CAE1D;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,GAAG,OAAO,CASxD;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,CAa/D;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,CAMhE;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAiBnG;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAanG;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,IAAI,CAa7E"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Marks a field as the unique identifier for array elements.
|
|
4
|
+
*
|
|
5
|
+
* When an array schema has a field marked with `id()`, the generated tools
|
|
6
|
+
* use ID-based operations (e.g., `set_todos_by_id`) instead of index-based.
|
|
7
|
+
* Only one field per schema can be marked with `id()`.
|
|
8
|
+
*
|
|
9
|
+
* @param schema - The Zod schema to mark as the key field
|
|
10
|
+
* @returns The same schema with key field metadata attached
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const TodoSchema = z.object({
|
|
15
|
+
* id: id(z.string()),
|
|
16
|
+
* title: z.string(),
|
|
17
|
+
* completed: z.boolean(),
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // Generates: get_todos, set_todos_by_id, delete_todos_by_id
|
|
21
|
+
* mcp.addStateTools({ name: 'todos', schema: z.array(TodoSchema), expand: true });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function id(schema) {
|
|
25
|
+
schema.def.__mcpWebKey = true;
|
|
26
|
+
return schema;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Marks a field as system-generated and excludes it from input schemas.
|
|
30
|
+
*
|
|
31
|
+
* System fields are auto-populated and never provided by the AI. They are
|
|
32
|
+
* excluded from add/set input schemas but included in output/getter schemas.
|
|
33
|
+
* Fields marked with `system()` MUST have a `.default()` value.
|
|
34
|
+
*
|
|
35
|
+
* Common uses: auto-generated IDs, timestamps, version numbers.
|
|
36
|
+
*
|
|
37
|
+
* @param schema - The Zod schema to mark as system-generated (must have default)
|
|
38
|
+
* @returns The same schema with system field metadata attached
|
|
39
|
+
* @throws {Error} At tool generation time if schema lacks a default value
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const TodoSchema = z.object({
|
|
44
|
+
* id: id(system(z.string().default(() => crypto.randomUUID()))),
|
|
45
|
+
* createdAt: system(z.number().default(() => Date.now())),
|
|
46
|
+
* title: z.string(), // User provides this
|
|
47
|
+
* });
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export function system(schema) {
|
|
51
|
+
schema.def.__mcpWebSystem = true;
|
|
52
|
+
return schema;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Checks if a field is marked with id().
|
|
56
|
+
*/
|
|
57
|
+
export function isKeyField(field) {
|
|
58
|
+
return field.def.__mcpWebKey === true;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Checks if a field is marked with system().
|
|
62
|
+
*/
|
|
63
|
+
export function isSystemField(field) {
|
|
64
|
+
return field.def.__mcpWebSystem === true;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Checks if a schema has a default value.
|
|
68
|
+
*/
|
|
69
|
+
export function hasDefault(schema) {
|
|
70
|
+
// Check for ZodDefault wrapper
|
|
71
|
+
if (schema instanceof z.ZodDefault) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
// Check _def for default value
|
|
75
|
+
const def = schema.def;
|
|
76
|
+
return def.defaultValue !== undefined || typeof def.defaultValue === 'function';
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Unwraps a schema from ZodDefault, ZodOptional, ZodNullable wrappers.
|
|
80
|
+
* Returns the innermost schema.
|
|
81
|
+
*/
|
|
82
|
+
export function unwrapSchema(schema) {
|
|
83
|
+
let current = schema;
|
|
84
|
+
while (current instanceof z.ZodDefault ||
|
|
85
|
+
current instanceof z.ZodOptional ||
|
|
86
|
+
current instanceof z.ZodNullable) {
|
|
87
|
+
const def = current.def;
|
|
88
|
+
current = def.innerType || current;
|
|
89
|
+
}
|
|
90
|
+
return current;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Unwraps ZodDefault to get the inner schema.
|
|
94
|
+
*/
|
|
95
|
+
export function unwrapDefault(schema) {
|
|
96
|
+
if (schema instanceof z.ZodDefault) {
|
|
97
|
+
const def = schema.def;
|
|
98
|
+
return def.innerType;
|
|
99
|
+
}
|
|
100
|
+
return schema;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Derives an input schema for add operations.
|
|
104
|
+
* - system() fields are excluded
|
|
105
|
+
* - default() fields become optional
|
|
106
|
+
* - other fields remain required
|
|
107
|
+
*/
|
|
108
|
+
export function deriveAddInputSchema(schema) {
|
|
109
|
+
const inputShape = {};
|
|
110
|
+
for (const [key, field] of Object.entries(schema.shape)) {
|
|
111
|
+
const zodField = field;
|
|
112
|
+
// Skip system fields entirely
|
|
113
|
+
if (isSystemField(zodField))
|
|
114
|
+
continue;
|
|
115
|
+
// Fields with default() become optional in add
|
|
116
|
+
if (hasDefault(zodField)) {
|
|
117
|
+
inputShape[key] = unwrapDefault(zodField).optional();
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
inputShape[key] = zodField;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return z.object(inputShape);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Derives an input schema for set operations (partial updates).
|
|
127
|
+
* - system() fields are excluded
|
|
128
|
+
* - all other fields become optional
|
|
129
|
+
*/
|
|
130
|
+
export function deriveSetInputSchema(schema) {
|
|
131
|
+
const inputShape = {};
|
|
132
|
+
for (const [key, field] of Object.entries(schema.shape)) {
|
|
133
|
+
const zodField = field;
|
|
134
|
+
// Skip system fields entirely
|
|
135
|
+
if (isSystemField(zodField))
|
|
136
|
+
continue;
|
|
137
|
+
// All fields optional for partial updates
|
|
138
|
+
inputShape[key] = unwrapDefault(zodField).optional();
|
|
139
|
+
}
|
|
140
|
+
return z.object(inputShape);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Validates that all system() fields have default values.
|
|
144
|
+
* Throws an error if a system field lacks a default.
|
|
145
|
+
*/
|
|
146
|
+
export function validateSystemFields(schema) {
|
|
147
|
+
for (const [key, field] of Object.entries(schema.shape)) {
|
|
148
|
+
const zodField = field;
|
|
149
|
+
if (isSystemField(zodField) && !hasDefault(zodField)) {
|
|
150
|
+
throw new Error(`Error: Field '${key}' is marked as system() but has no default value.\n\n` +
|
|
151
|
+
`System fields are excluded from input schemas, so a default is required.\n` +
|
|
152
|
+
`The handler uses this default to fill in the value when creating new items.\n\n` +
|
|
153
|
+
`Fix: Add a default value or generator:\n` +
|
|
154
|
+
` ${key}: system(z.number().default(() => Date.now()))`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { GeneratedTools, ToolGenerationOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Generates MCP tools for a schema based on its shape.
|
|
4
|
+
* - Fixed-shape schemas → get + set with deep merge
|
|
5
|
+
* - Dynamic-shape schemas → get + add + set + delete (arrays) or get + set + delete (records)
|
|
6
|
+
* - Mixed schemas → asymmetric get/set + collection tools
|
|
7
|
+
*
|
|
8
|
+
* Returns tool definitions without registering them - the caller is responsible for registration.
|
|
9
|
+
*/
|
|
10
|
+
export declare function generateToolsForSchema(options: ToolGenerationOptions): GeneratedTools;
|
|
11
|
+
//# sourceMappingURL=tool-generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-generator.d.ts","sourceRoot":"","sources":["../../src/tool-generators/tool-generator.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,cAAc,EAA+B,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAOrG;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,qBAAqB,GAC7B,cAAc,CAsDhB"}
|