@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
package/dist/utils.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import Ajv from 'ajv';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
const ajv = new Ajv({ strict: false });
|
|
4
|
+
// Type guard to check if a schema is a Zod schema
|
|
5
|
+
export function isZodSchema(schema) {
|
|
6
|
+
return typeof schema === 'object' && 'safeParse' in schema;
|
|
7
|
+
}
|
|
8
|
+
export function toJSONSchema(schema) {
|
|
9
|
+
if (schema && typeof schema === 'object' && 'safeParse' in schema) {
|
|
10
|
+
const jsonSchema = z.toJSONSchema(schema);
|
|
11
|
+
// Remove $schema property to avoid AJV Draft 2020-12 validation errors
|
|
12
|
+
// Our AJV instance only supports Draft 7
|
|
13
|
+
if (typeof jsonSchema === 'object' && '$schema' in jsonSchema) {
|
|
14
|
+
const { $schema: _unused, ...rest } = jsonSchema;
|
|
15
|
+
return rest;
|
|
16
|
+
}
|
|
17
|
+
return jsonSchema;
|
|
18
|
+
}
|
|
19
|
+
return schema;
|
|
20
|
+
}
|
|
21
|
+
export function toToolZodSchema(schema) {
|
|
22
|
+
// If it's already an object schema, use as-is
|
|
23
|
+
if (schema instanceof z.ZodObject) {
|
|
24
|
+
return schema;
|
|
25
|
+
}
|
|
26
|
+
// For non-objects, wrap in a value property for consistent tool interface
|
|
27
|
+
return z.object({ value: schema });
|
|
28
|
+
}
|
|
29
|
+
// Convert schema to appropriate format for tools
|
|
30
|
+
export function toToolSchema(schema) {
|
|
31
|
+
if (!schema) {
|
|
32
|
+
return z.object({});
|
|
33
|
+
}
|
|
34
|
+
if (isZodSchema(schema)) {
|
|
35
|
+
return toToolZodSchema(schema);
|
|
36
|
+
}
|
|
37
|
+
// Handle JSON Schema case
|
|
38
|
+
return schema;
|
|
39
|
+
}
|
|
40
|
+
export function validateInput(input, schema) {
|
|
41
|
+
if (!schema) {
|
|
42
|
+
return input;
|
|
43
|
+
}
|
|
44
|
+
if (isZodSchema(schema)) {
|
|
45
|
+
const result = schema.safeParse(input);
|
|
46
|
+
if (!result.success) {
|
|
47
|
+
throw new Error(`Invalid input: ${result.error.message}`);
|
|
48
|
+
}
|
|
49
|
+
return result.data;
|
|
50
|
+
}
|
|
51
|
+
// JSON Schema validation
|
|
52
|
+
const validate = ajv.compile(schema);
|
|
53
|
+
const isValid = validate(input);
|
|
54
|
+
if (!isValid) {
|
|
55
|
+
throw new Error(`Invalid input: ${ajv.errorsText(validate.errors)}`);
|
|
56
|
+
}
|
|
57
|
+
return input;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Convert a ToolDefinition to ToolMetadataJson for wire transmission.
|
|
61
|
+
* Removes the handler and converts Zod schemas to JSON Schema.
|
|
62
|
+
*
|
|
63
|
+
* @param tool - The tool definition to convert
|
|
64
|
+
* @returns Serializable tool metadata without handler, with JSON Schema schemas
|
|
65
|
+
*/
|
|
66
|
+
export function toToolMetadataJson(tool) {
|
|
67
|
+
return {
|
|
68
|
+
name: tool.name,
|
|
69
|
+
description: tool.description,
|
|
70
|
+
inputSchema: tool.inputSchema ? toJSONSchema(tool.inputSchema) : undefined,
|
|
71
|
+
outputSchema: tool.outputSchema ? toJSONSchema(tool.outputSchema) : undefined,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Deep merge two objects recursively.
|
|
76
|
+
* Used for partial updates to state objects.
|
|
77
|
+
*
|
|
78
|
+
* @param target - The target object to merge into
|
|
79
|
+
* @param source - The source object to merge from
|
|
80
|
+
* @returns The merged result
|
|
81
|
+
*
|
|
82
|
+
* Key behaviors:
|
|
83
|
+
* - `undefined` in source → keep target value (no change)
|
|
84
|
+
* - `null` in source → set to null (explicit clear)
|
|
85
|
+
* - Nested objects → recursively merged
|
|
86
|
+
* - Arrays → replaced entirely (not merged)
|
|
87
|
+
*/
|
|
88
|
+
export function deepMerge(target, source) {
|
|
89
|
+
// Base cases
|
|
90
|
+
if (source === null)
|
|
91
|
+
return source; // Explicit null clears the value
|
|
92
|
+
if (source === undefined)
|
|
93
|
+
return target; // Undefined means "no change"
|
|
94
|
+
if (typeof source !== 'object')
|
|
95
|
+
return source;
|
|
96
|
+
if (Array.isArray(source))
|
|
97
|
+
return source; // Arrays are replaced, not merged
|
|
98
|
+
if (typeof target !== 'object' || target === null)
|
|
99
|
+
return source;
|
|
100
|
+
if (Array.isArray(target))
|
|
101
|
+
return source;
|
|
102
|
+
// Object merge: recursively merge properties
|
|
103
|
+
const result = { ...target };
|
|
104
|
+
for (const [key, value] of Object.entries(source)) {
|
|
105
|
+
result[key] = deepMerge(result[key], value);
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|