@kadi.build/core 0.0.1-alpha.1 → 0.0.1-alpha.11
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/README.md +361 -230
- package/dist/abilities/AbilityCache.d.ts +242 -0
- package/dist/abilities/AbilityCache.d.ts.map +1 -0
- package/dist/abilities/AbilityCache.js +285 -0
- package/dist/abilities/AbilityCache.js.map +1 -0
- package/dist/abilities/AbilityContext.d.ts +215 -0
- package/dist/abilities/AbilityContext.d.ts.map +1 -0
- package/dist/abilities/AbilityContext.js +36 -0
- package/dist/abilities/AbilityContext.js.map +1 -0
- package/dist/abilities/AbilityLoader.d.ts +203 -0
- package/dist/abilities/AbilityLoader.d.ts.map +1 -0
- package/dist/abilities/AbilityLoader.js +343 -0
- package/dist/abilities/AbilityLoader.js.map +1 -0
- package/dist/abilities/AbilityProxy.d.ts +496 -0
- package/dist/abilities/AbilityProxy.d.ts.map +1 -0
- package/dist/abilities/AbilityProxy.js +551 -0
- package/dist/abilities/AbilityProxy.js.map +1 -0
- package/dist/abilities/AbilityValidator.d.ts +172 -0
- package/dist/abilities/AbilityValidator.d.ts.map +1 -0
- package/dist/abilities/AbilityValidator.js +253 -0
- package/dist/abilities/AbilityValidator.js.map +1 -0
- package/dist/abilities/index.d.ts +26 -0
- package/dist/abilities/index.d.ts.map +1 -0
- package/dist/abilities/index.js +23 -0
- package/dist/abilities/index.js.map +1 -0
- package/dist/abilities/types.d.ts +223 -0
- package/dist/abilities/types.d.ts.map +1 -0
- package/dist/abilities/types.js +10 -0
- package/dist/abilities/types.js.map +1 -0
- package/dist/api/index.d.ts +92 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +124 -0
- package/dist/api/index.js.map +1 -0
- package/dist/broker/BrokerConnection.d.ts +253 -0
- package/dist/broker/BrokerConnection.d.ts.map +1 -0
- package/dist/broker/BrokerConnection.js +434 -0
- package/dist/broker/BrokerConnection.js.map +1 -0
- package/dist/broker/BrokerConnectionManager.d.ts +216 -0
- package/dist/broker/BrokerConnectionManager.d.ts.map +1 -0
- package/dist/broker/BrokerConnectionManager.js +305 -0
- package/dist/broker/BrokerConnectionManager.js.map +1 -0
- package/dist/broker/BrokerProtocol.d.ts +280 -0
- package/dist/broker/BrokerProtocol.d.ts.map +1 -0
- package/dist/broker/BrokerProtocol.js +466 -0
- package/dist/broker/BrokerProtocol.js.map +1 -0
- package/dist/broker/index.d.ts +9 -0
- package/dist/broker/index.d.ts.map +1 -0
- package/dist/broker/index.js +9 -0
- package/dist/broker/index.js.map +1 -0
- package/dist/client/KadiClient.d.ts +459 -0
- package/dist/client/KadiClient.d.ts.map +1 -0
- package/dist/client/KadiClient.js +902 -0
- package/dist/client/KadiClient.js.map +1 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +7 -0
- package/dist/client/index.js.map +1 -0
- package/dist/config/ConfigLoader.d.ts +138 -0
- package/dist/config/ConfigLoader.d.ts.map +1 -0
- package/dist/config/ConfigLoader.js +226 -0
- package/dist/config/ConfigLoader.js.map +1 -0
- package/dist/config/ConfigResolver.d.ts +135 -0
- package/dist/config/ConfigResolver.d.ts.map +1 -0
- package/dist/config/ConfigResolver.js +282 -0
- package/dist/config/ConfigResolver.js.map +1 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +8 -0
- package/dist/config/index.js.map +1 -0
- package/dist/errors/index.d.ts +9 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +8 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/events/EventHub.d.ts +172 -0
- package/dist/events/EventHub.d.ts.map +1 -0
- package/dist/events/EventHub.js +333 -0
- package/dist/events/EventHub.js.map +1 -0
- package/dist/events/index.d.ts +7 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +7 -0
- package/dist/events/index.js.map +1 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +67 -0
- package/dist/index.js.map +1 -0
- package/dist/messages/index.d.ts +33 -0
- package/dist/messages/index.d.ts.map +1 -0
- package/dist/messages/index.js +33 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/schemas/index.d.ts +22 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +27 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/kadi-extensions.d.ts +231 -0
- package/dist/schemas/kadi-extensions.d.ts.map +1 -0
- package/dist/schemas/kadi-extensions.js +14 -0
- package/dist/schemas/kadi-extensions.js.map +1 -0
- package/dist/schemas/mcp/schema.d.ts +1399 -0
- package/dist/schemas/mcp/schema.d.ts.map +1 -0
- package/dist/schemas/mcp/schema.js +53 -0
- package/dist/schemas/mcp/schema.js.map +1 -0
- package/dist/schemas/mcp/version.d.ts +37 -0
- package/dist/schemas/mcp/version.d.ts.map +1 -0
- package/dist/schemas/mcp/version.js +39 -0
- package/dist/schemas/mcp/version.js.map +1 -0
- package/dist/schemas/schema-builders.d.ts +178 -0
- package/dist/schemas/schema-builders.d.ts.map +1 -0
- package/dist/schemas/schema-builders.js +258 -0
- package/dist/schemas/schema-builders.js.map +1 -0
- package/dist/schemas/zod-helpers.d.ts +129 -0
- package/dist/schemas/zod-helpers.d.ts.map +1 -0
- package/dist/schemas/zod-helpers.js +225 -0
- package/dist/schemas/zod-helpers.js.map +1 -0
- package/dist/schemas/zod-to-json-schema.d.ts +159 -0
- package/dist/schemas/zod-to-json-schema.d.ts.map +1 -0
- package/dist/schemas/zod-to-json-schema.js +154 -0
- package/dist/schemas/zod-to-json-schema.js.map +1 -0
- package/dist/tools/ToolRegistry.d.ts +256 -0
- package/dist/tools/ToolRegistry.d.ts.map +1 -0
- package/dist/tools/ToolRegistry.js +340 -0
- package/dist/tools/ToolRegistry.js.map +1 -0
- package/dist/tools/index.d.ts +7 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/transports/BrokerTransport.d.ts +151 -0
- package/dist/transports/BrokerTransport.d.ts.map +1 -0
- package/dist/transports/BrokerTransport.js +261 -0
- package/dist/transports/BrokerTransport.js.map +1 -0
- package/dist/transports/NativeTransport.d.ts +178 -0
- package/dist/transports/NativeTransport.d.ts.map +1 -0
- package/dist/transports/NativeTransport.js +397 -0
- package/dist/transports/NativeTransport.js.map +1 -0
- package/dist/transports/StdioTransport.d.ts +250 -0
- package/dist/transports/StdioTransport.d.ts.map +1 -0
- package/dist/transports/StdioTransport.js +487 -0
- package/dist/transports/StdioTransport.js.map +1 -0
- package/dist/transports/index.d.ts +10 -0
- package/dist/transports/index.d.ts.map +1 -0
- package/dist/transports/index.js +9 -0
- package/dist/transports/index.js.map +1 -0
- package/dist/types/broker.d.ts +279 -0
- package/dist/types/broker.d.ts.map +1 -0
- package/dist/types/broker.js +19 -0
- package/dist/types/broker.js.map +1 -0
- package/dist/types/config.d.ts +325 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +17 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/errors.d.ts +178 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +165 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/events.d.ts +210 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +8 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +34 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +21 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/protocol.d.ts +48 -0
- package/dist/types/protocol.d.ts.map +1 -0
- package/dist/types/protocol.js +11 -0
- package/dist/types/protocol.js.map +1 -0
- package/dist/types/tools.d.ts +67 -0
- package/dist/types/tools.d.ts.map +1 -0
- package/dist/types/tools.js +16 -0
- package/dist/types/tools.js.map +1 -0
- package/dist/types/transport.d.ts +250 -0
- package/dist/types/transport.d.ts.map +1 -0
- package/dist/types/transport.js +18 -0
- package/dist/types/transport.js.map +1 -0
- package/dist/types/zod-tools.d.ts +198 -0
- package/dist/types/zod-tools.d.ts.map +1 -0
- package/dist/types/zod-tools.js +14 -0
- package/dist/types/zod-tools.js.map +1 -0
- package/dist/utils/StdioMessageReader.d.ts +122 -0
- package/dist/utils/StdioMessageReader.d.ts.map +1 -0
- package/dist/utils/StdioMessageReader.js +209 -0
- package/dist/utils/StdioMessageReader.js.map +1 -0
- package/dist/utils/StdioMessageWriter.d.ts +104 -0
- package/dist/utils/StdioMessageWriter.d.ts.map +1 -0
- package/dist/utils/StdioMessageWriter.js +162 -0
- package/dist/utils/StdioMessageWriter.js.map +1 -0
- package/dist/validation/SchemaValidator.d.ts +208 -0
- package/dist/validation/SchemaValidator.d.ts.map +1 -0
- package/dist/validation/SchemaValidator.js +411 -0
- package/dist/validation/SchemaValidator.js.map +1 -0
- package/dist/validation/index.d.ts +11 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +10 -0
- package/dist/validation/index.js.map +1 -0
- package/package.json +70 -5
- package/agent.json +0 -18
- package/broker.js +0 -214
- package/index.js +0 -370
- package/ipc.js +0 -220
- package/ipcInterfaces/pythonAbilityIPC.py +0 -177
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod-based Tool Creation Helpers
|
|
3
|
+
*
|
|
4
|
+
* This module provides high-level helper functions for creating tools using
|
|
5
|
+
* Zod schemas. These helpers provide excellent developer experience by:
|
|
6
|
+
* - Accepting Zod schemas for input/output validation
|
|
7
|
+
* - Automatically converting to MCP-compliant JSON Schema
|
|
8
|
+
* - Providing automatic runtime validation
|
|
9
|
+
* - Inferring TypeScript types from schemas
|
|
10
|
+
*
|
|
11
|
+
* @module schemas/zod-helpers
|
|
12
|
+
*/
|
|
13
|
+
import type { z } from 'zod';
|
|
14
|
+
import type { ZodToolConfig, ZodToolResult } from '../types/zod-tools.js';
|
|
15
|
+
/**
|
|
16
|
+
* Create a tool from Zod schemas with automatic validation
|
|
17
|
+
*
|
|
18
|
+
* This is the main developer-facing API for creating tools with Zod schemas.
|
|
19
|
+
* It provides:
|
|
20
|
+
* - Single source of truth (Zod schema)
|
|
21
|
+
* - Automatic TypeScript type inference
|
|
22
|
+
* - Automatic input/output validation
|
|
23
|
+
* - Conversion to MCP-compliant JSON Schema
|
|
24
|
+
* - Significantly less boilerplate than manual JSON Schema
|
|
25
|
+
*
|
|
26
|
+
* The function returns a tool object that can be registered with `client.registerTool()`.
|
|
27
|
+
*
|
|
28
|
+
* @typeParam TInput - Zod schema type for input
|
|
29
|
+
* @typeParam TOutput - Zod schema type for output
|
|
30
|
+
*
|
|
31
|
+
* @param config - Tool configuration with Zod schemas
|
|
32
|
+
* @returns Tool ready to register with KadiClient
|
|
33
|
+
*
|
|
34
|
+
* @throws {KadiError} If input/output schemas are not Zod object types
|
|
35
|
+
* @throws {KadiError} If schema conversion fails
|
|
36
|
+
*
|
|
37
|
+
* @example Basic usage
|
|
38
|
+
* ```typescript
|
|
39
|
+
* import { z } from 'zod';
|
|
40
|
+
* import { createTool } from '@kadi.build/core';
|
|
41
|
+
*
|
|
42
|
+
* const addTool = createTool({
|
|
43
|
+
* name: 'add',
|
|
44
|
+
* description: 'Add two numbers',
|
|
45
|
+
* version: '1.0.0',
|
|
46
|
+
* input: z.object({
|
|
47
|
+
* a: z.number().describe('First number'),
|
|
48
|
+
* b: z.number().describe('Second number')
|
|
49
|
+
* }),
|
|
50
|
+
* output: z.object({
|
|
51
|
+
* result: z.number().describe('Sum of a and b')
|
|
52
|
+
* }),
|
|
53
|
+
* handler: async ({ a, b }) => {
|
|
54
|
+
* // Params are fully typed!
|
|
55
|
+
* return { result: a + b };
|
|
56
|
+
* }
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* // Register with client
|
|
60
|
+
* client.registerTool(addTool.definition, addTool.handler);
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @example Complex schemas with validation
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const deployTool = createTool({
|
|
66
|
+
* name: 'deployToAkash',
|
|
67
|
+
* description: 'Deploy to Akash Network',
|
|
68
|
+
* version: '1.0.0',
|
|
69
|
+
* tags: ['deployment', 'cloud'],
|
|
70
|
+
* networks: ['global', 'akash'],
|
|
71
|
+
* input: z.object({
|
|
72
|
+
* projectRoot: z.string().min(1).describe('Project root path'),
|
|
73
|
+
* profile: z.string().min(1).describe('Deployment profile'),
|
|
74
|
+
* network: z.enum(['mainnet', 'testnet', 'sandbox'])
|
|
75
|
+
* .default('mainnet')
|
|
76
|
+
* .describe('Akash network'),
|
|
77
|
+
* verbose: z.boolean().default(false).describe('Verbose logging')
|
|
78
|
+
* }),
|
|
79
|
+
* output: z.object({
|
|
80
|
+
* success: z.boolean(),
|
|
81
|
+
* dseq: z.string().optional(),
|
|
82
|
+
* error: z.string().optional()
|
|
83
|
+
* }),
|
|
84
|
+
* handler: async (params) => {
|
|
85
|
+
* // Full type safety and validation
|
|
86
|
+
* return await deployToAkash(params);
|
|
87
|
+
* }
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example Destructuring for clean registration
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const { definition, handler } = createTool({
|
|
94
|
+
* name: 'greet',
|
|
95
|
+
* input: z.object({ name: z.string() }),
|
|
96
|
+
* output: z.object({ message: z.string() }),
|
|
97
|
+
* handler: async ({ name }) => ({ message: `Hello, ${name}!` })
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* client.registerTool(definition, handler);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export declare function createTool<TInput extends z.ZodTypeAny = z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny>(config: ZodToolConfig<TInput, TOutput>): ZodToolResult<z.infer<TInput>, z.infer<TOutput>>;
|
|
104
|
+
/**
|
|
105
|
+
* Create a simple tool with minimal configuration
|
|
106
|
+
*
|
|
107
|
+
* Convenience function for creating tools when you don't need all the
|
|
108
|
+
* configuration options. Good for quick prototyping or simple tools.
|
|
109
|
+
*
|
|
110
|
+
* @param name - Tool name
|
|
111
|
+
* @param input - Zod input schema
|
|
112
|
+
* @param output - Zod output schema
|
|
113
|
+
* @param handler - Tool handler function
|
|
114
|
+
* @returns Tool ready to register
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* const tool = createSimpleTool(
|
|
119
|
+
* 'add',
|
|
120
|
+
* z.object({ a: z.number(), b: z.number() }),
|
|
121
|
+
* z.object({ result: z.number() }),
|
|
122
|
+
* async ({ a, b }) => ({ result: a + b })
|
|
123
|
+
* );
|
|
124
|
+
*
|
|
125
|
+
* client.registerTool(tool.definition, tool.handler);
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export declare function createSimpleTool<TInput extends z.ZodTypeAny, TOutput extends z.ZodTypeAny>(name: string, input: TInput, output: TOutput, handler: (params: z.infer<TInput>) => Promise<z.infer<TOutput>> | z.infer<TOutput>): ZodToolResult<z.infer<TInput>, z.infer<TOutput>>;
|
|
129
|
+
//# sourceMappingURL=zod-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-helpers.d.ts","sourceRoot":"","sources":["../../src/schemas/zod-helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAK1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuFG;AACH,wBAAgB,UAAU,CACxB,MAAM,SAAS,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,EAC1C,OAAO,SAAS,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,EAE3C,MAAM,EAAE,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,GACrC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CA8HlD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,SAAS,CAAC,CAAC,UAAU,EAC3B,OAAO,SAAS,CAAC,CAAC,UAAU,EAE5B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,OAAO,EACf,OAAO,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,GACjF,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAOlD"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod-based Tool Creation Helpers
|
|
3
|
+
*
|
|
4
|
+
* This module provides high-level helper functions for creating tools using
|
|
5
|
+
* Zod schemas. These helpers provide excellent developer experience by:
|
|
6
|
+
* - Accepting Zod schemas for input/output validation
|
|
7
|
+
* - Automatically converting to MCP-compliant JSON Schema
|
|
8
|
+
* - Providing automatic runtime validation
|
|
9
|
+
* - Inferring TypeScript types from schemas
|
|
10
|
+
*
|
|
11
|
+
* @module schemas/zod-helpers
|
|
12
|
+
*/
|
|
13
|
+
import { zodToJsonSchema } from './zod-to-json-schema.js';
|
|
14
|
+
import { KadiError, ErrorCode } from '../errors/index.js';
|
|
15
|
+
/**
|
|
16
|
+
* Create a tool from Zod schemas with automatic validation
|
|
17
|
+
*
|
|
18
|
+
* This is the main developer-facing API for creating tools with Zod schemas.
|
|
19
|
+
* It provides:
|
|
20
|
+
* - Single source of truth (Zod schema)
|
|
21
|
+
* - Automatic TypeScript type inference
|
|
22
|
+
* - Automatic input/output validation
|
|
23
|
+
* - Conversion to MCP-compliant JSON Schema
|
|
24
|
+
* - Significantly less boilerplate than manual JSON Schema
|
|
25
|
+
*
|
|
26
|
+
* The function returns a tool object that can be registered with `client.registerTool()`.
|
|
27
|
+
*
|
|
28
|
+
* @typeParam TInput - Zod schema type for input
|
|
29
|
+
* @typeParam TOutput - Zod schema type for output
|
|
30
|
+
*
|
|
31
|
+
* @param config - Tool configuration with Zod schemas
|
|
32
|
+
* @returns Tool ready to register with KadiClient
|
|
33
|
+
*
|
|
34
|
+
* @throws {KadiError} If input/output schemas are not Zod object types
|
|
35
|
+
* @throws {KadiError} If schema conversion fails
|
|
36
|
+
*
|
|
37
|
+
* @example Basic usage
|
|
38
|
+
* ```typescript
|
|
39
|
+
* import { z } from 'zod';
|
|
40
|
+
* import { createTool } from '@kadi.build/core';
|
|
41
|
+
*
|
|
42
|
+
* const addTool = createTool({
|
|
43
|
+
* name: 'add',
|
|
44
|
+
* description: 'Add two numbers',
|
|
45
|
+
* version: '1.0.0',
|
|
46
|
+
* input: z.object({
|
|
47
|
+
* a: z.number().describe('First number'),
|
|
48
|
+
* b: z.number().describe('Second number')
|
|
49
|
+
* }),
|
|
50
|
+
* output: z.object({
|
|
51
|
+
* result: z.number().describe('Sum of a and b')
|
|
52
|
+
* }),
|
|
53
|
+
* handler: async ({ a, b }) => {
|
|
54
|
+
* // Params are fully typed!
|
|
55
|
+
* return { result: a + b };
|
|
56
|
+
* }
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* // Register with client
|
|
60
|
+
* client.registerTool(addTool.definition, addTool.handler);
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @example Complex schemas with validation
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const deployTool = createTool({
|
|
66
|
+
* name: 'deployToAkash',
|
|
67
|
+
* description: 'Deploy to Akash Network',
|
|
68
|
+
* version: '1.0.0',
|
|
69
|
+
* tags: ['deployment', 'cloud'],
|
|
70
|
+
* networks: ['global', 'akash'],
|
|
71
|
+
* input: z.object({
|
|
72
|
+
* projectRoot: z.string().min(1).describe('Project root path'),
|
|
73
|
+
* profile: z.string().min(1).describe('Deployment profile'),
|
|
74
|
+
* network: z.enum(['mainnet', 'testnet', 'sandbox'])
|
|
75
|
+
* .default('mainnet')
|
|
76
|
+
* .describe('Akash network'),
|
|
77
|
+
* verbose: z.boolean().default(false).describe('Verbose logging')
|
|
78
|
+
* }),
|
|
79
|
+
* output: z.object({
|
|
80
|
+
* success: z.boolean(),
|
|
81
|
+
* dseq: z.string().optional(),
|
|
82
|
+
* error: z.string().optional()
|
|
83
|
+
* }),
|
|
84
|
+
* handler: async (params) => {
|
|
85
|
+
* // Full type safety and validation
|
|
86
|
+
* return await deployToAkash(params);
|
|
87
|
+
* }
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example Destructuring for clean registration
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const { definition, handler } = createTool({
|
|
94
|
+
* name: 'greet',
|
|
95
|
+
* input: z.object({ name: z.string() }),
|
|
96
|
+
* output: z.object({ message: z.string() }),
|
|
97
|
+
* handler: async ({ name }) => ({ message: `Hello, ${name}!` })
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* client.registerTool(definition, handler);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export function createTool(config) {
|
|
104
|
+
// Validate tool name
|
|
105
|
+
if (!config.name || typeof config.name !== 'string' || config.name.trim().length === 0) {
|
|
106
|
+
throw new KadiError('Tool name must be a non-empty string', ErrorCode.INVALID_INPUT, 400, { config });
|
|
107
|
+
}
|
|
108
|
+
// Validate handler
|
|
109
|
+
if (typeof config.handler !== 'function') {
|
|
110
|
+
throw new KadiError(`Tool '${config.name}' handler must be a function`, ErrorCode.INVALID_INPUT, 400, { name: config.name, handlerType: typeof config.handler });
|
|
111
|
+
}
|
|
112
|
+
// Validate input schema exists
|
|
113
|
+
if (!config.input) {
|
|
114
|
+
throw new KadiError(`Tool '${config.name}' must have an input schema`, ErrorCode.INVALID_INPUT, 400, { name: config.name });
|
|
115
|
+
}
|
|
116
|
+
// Validate output schema exists
|
|
117
|
+
if (!config.output) {
|
|
118
|
+
throw new KadiError(`Tool '${config.name}' must have an output schema`, ErrorCode.INVALID_INPUT, 400, { name: config.name });
|
|
119
|
+
}
|
|
120
|
+
// Convert Zod schemas to JSON Schema
|
|
121
|
+
// This will throw KadiError if schemas are not objects or conversion fails
|
|
122
|
+
const inputSchema = zodToJsonSchema(config.input, {
|
|
123
|
+
name: `${config.name}Input`
|
|
124
|
+
});
|
|
125
|
+
const outputSchema = zodToJsonSchema(config.output, {
|
|
126
|
+
name: `${config.name}Output`
|
|
127
|
+
});
|
|
128
|
+
// Create tool definition with JSON Schema
|
|
129
|
+
// Type assertion needed because JSONSchema is more permissive than MCP's strict type
|
|
130
|
+
const definition = {
|
|
131
|
+
name: config.name,
|
|
132
|
+
description: config.description,
|
|
133
|
+
title: config.title,
|
|
134
|
+
version: config.version,
|
|
135
|
+
tags: config.tags,
|
|
136
|
+
networks: config.networks,
|
|
137
|
+
inputSchema: inputSchema,
|
|
138
|
+
outputSchema: outputSchema
|
|
139
|
+
};
|
|
140
|
+
// Create handler wrapper with automatic validation
|
|
141
|
+
// This wrapper:
|
|
142
|
+
// 1. Validates input using Zod (better error messages than AJV)
|
|
143
|
+
// 2. Calls original handler
|
|
144
|
+
// 3. Validates output using Zod
|
|
145
|
+
// 4. Returns validated result
|
|
146
|
+
const handler = async (params) => {
|
|
147
|
+
// Step 1: Validate input with Zod
|
|
148
|
+
// This provides much better error messages than JSON Schema validation
|
|
149
|
+
let validatedInput;
|
|
150
|
+
try {
|
|
151
|
+
validatedInput = config.input.parse(params);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
// Wrap Zod validation error in KadiError
|
|
155
|
+
throw new KadiError(`Input validation failed for tool '${config.name}': ${error instanceof Error ? error.message : String(error)}`, ErrorCode.INVALID_INPUT, 400, {
|
|
156
|
+
toolName: config.name,
|
|
157
|
+
input: params,
|
|
158
|
+
validationError: error
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
// Step 2: Call original handler with validated input
|
|
162
|
+
let result;
|
|
163
|
+
try {
|
|
164
|
+
result = await config.handler(validatedInput);
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
// Re-throw handler errors (they should be handled by caller)
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
// Step 3: Validate output with Zod
|
|
171
|
+
// This catches bugs in the handler implementation
|
|
172
|
+
try {
|
|
173
|
+
const validatedOutput = config.output.parse(result);
|
|
174
|
+
return validatedOutput;
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
// Output validation failure indicates a bug in the handler
|
|
178
|
+
throw new KadiError(`Output validation failed for tool '${config.name}': ${error instanceof Error ? error.message : String(error)}`, ErrorCode.INTERNAL_ERROR, 500, {
|
|
179
|
+
toolName: config.name,
|
|
180
|
+
output: result,
|
|
181
|
+
validationError: error
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
// Return tool object ready for registration
|
|
186
|
+
return {
|
|
187
|
+
definition,
|
|
188
|
+
handler,
|
|
189
|
+
inputSchema: config.input,
|
|
190
|
+
outputSchema: config.output
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Create a simple tool with minimal configuration
|
|
195
|
+
*
|
|
196
|
+
* Convenience function for creating tools when you don't need all the
|
|
197
|
+
* configuration options. Good for quick prototyping or simple tools.
|
|
198
|
+
*
|
|
199
|
+
* @param name - Tool name
|
|
200
|
+
* @param input - Zod input schema
|
|
201
|
+
* @param output - Zod output schema
|
|
202
|
+
* @param handler - Tool handler function
|
|
203
|
+
* @returns Tool ready to register
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* const tool = createSimpleTool(
|
|
208
|
+
* 'add',
|
|
209
|
+
* z.object({ a: z.number(), b: z.number() }),
|
|
210
|
+
* z.object({ result: z.number() }),
|
|
211
|
+
* async ({ a, b }) => ({ result: a + b })
|
|
212
|
+
* );
|
|
213
|
+
*
|
|
214
|
+
* client.registerTool(tool.definition, tool.handler);
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
export function createSimpleTool(name, input, output, handler) {
|
|
218
|
+
return createTool({
|
|
219
|
+
name,
|
|
220
|
+
input,
|
|
221
|
+
output,
|
|
222
|
+
handler
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=zod-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-helpers.js","sourceRoot":"","sources":["../../src/schemas/zod-helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuFG;AACH,MAAM,UAAU,UAAU,CAIxB,MAAsC;IAEtC,qBAAqB;IACrB,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvF,MAAM,IAAI,SAAS,CACjB,sCAAsC,EACtC,SAAS,CAAC,aAAa,EACvB,GAAG,EACH,EAAE,MAAM,EAAE,CACX,CAAC;IACJ,CAAC;IAED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M,IAAI,SAAS,CACjB,sCAAsC,MAAM,CAAC,IAAI,MAAM,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAC/G,SAAS,CAAC,cAAc,EACxB,GAAG,EACH;gBACE,QAAQ,EAAE,MAAM,CAAC,IAAI;gBACrB,MAAM,EAAE,MAAM;gBACd,eAAe,EAAE,KAAK;aACvB,CACF,CAAC;QACJ,CAAC;IACH,CAAC,CAAC;IAEF,4CAA4C;IAC5C,OAAO;QACL,UAAU;QACV,OAAO;QACP,WAAW,EAAE,MAAM,CAAC,KAAK;QACzB,YAAY,EAAE,MAAM,CAAC,MAAM;KAC5B,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,gBAAgB,CAI9B,IAAY,EACZ,KAAa,EACb,MAAe,EACf,OAAkF;IAElF,OAAO,UAAU,CAAC;QAChB,IAAI;QACJ,KAAK;QACL,MAAM;QACN,OAAO;KACR,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod to JSON Schema Conversion Utilities
|
|
3
|
+
*
|
|
4
|
+
* This module provides utilities for converting Zod schemas to MCP-compliant
|
|
5
|
+
* JSON Schema. The conversion ensures that all generated schemas meet the
|
|
6
|
+
* requirements of the Model Context Protocol (MCP):
|
|
7
|
+
* - Root schemas must be type 'object'
|
|
8
|
+
* - All schemas must be valid JSON Schema Draft 7
|
|
9
|
+
* - No unsupported features (e.g., recursive types, transforms)
|
|
10
|
+
*
|
|
11
|
+
* @module schemas/zod-to-json-schema
|
|
12
|
+
*/
|
|
13
|
+
import { z } from 'zod';
|
|
14
|
+
/**
|
|
15
|
+
* JSON Schema object type
|
|
16
|
+
*
|
|
17
|
+
* Represents a JSON Schema (Draft 7) object. We use a permissive type here
|
|
18
|
+
* since JSON Schema structure is flexible and validated at runtime by AJV.
|
|
19
|
+
*/
|
|
20
|
+
export type JSONSchema = {
|
|
21
|
+
type: string;
|
|
22
|
+
properties?: Record<string, unknown>;
|
|
23
|
+
required?: string[];
|
|
24
|
+
[key: string]: unknown;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Options for Zod to JSON Schema conversion
|
|
28
|
+
*
|
|
29
|
+
* These options control how the conversion happens and what features are enabled.
|
|
30
|
+
*/
|
|
31
|
+
export interface ZodToJsonSchemaOptions {
|
|
32
|
+
/**
|
|
33
|
+
* Schema name for $ref resolution
|
|
34
|
+
*
|
|
35
|
+
* If provided, the schema will use this name in $ref definitions.
|
|
36
|
+
* If not provided, $ref will be inlined (recommended for simplicity).
|
|
37
|
+
*
|
|
38
|
+
* @default undefined (inlines all $refs)
|
|
39
|
+
*/
|
|
40
|
+
name?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Include schema $id field
|
|
43
|
+
*
|
|
44
|
+
* If true, adds a $id field to the root schema for identification.
|
|
45
|
+
*
|
|
46
|
+
* @default false
|
|
47
|
+
*/
|
|
48
|
+
includeId?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Target JSON Schema version
|
|
51
|
+
*
|
|
52
|
+
* Specifies which JSON Schema draft to target.
|
|
53
|
+
*
|
|
54
|
+
* @default 'draft-7' (most compatible)
|
|
55
|
+
*/
|
|
56
|
+
target?: 'draft-4' | 'draft-7' | 'draft-2020-12' | 'openapi-3.0';
|
|
57
|
+
/**
|
|
58
|
+
* Enable strict mode
|
|
59
|
+
*
|
|
60
|
+
* If true, throws errors for Zod features that can't be fully represented
|
|
61
|
+
* in JSON Schema (e.g., transforms, refinements).
|
|
62
|
+
*
|
|
63
|
+
* @default false (converts best-effort)
|
|
64
|
+
*/
|
|
65
|
+
strict?: boolean;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Convert Zod schema to MCP-compliant JSON Schema
|
|
69
|
+
*
|
|
70
|
+
* This function converts a Zod schema to JSON Schema format that complies with
|
|
71
|
+
* the Model Context Protocol (MCP) requirements:
|
|
72
|
+
* - Root schema must be type 'object'
|
|
73
|
+
* - No transforms or refinements (validation only)
|
|
74
|
+
* - Valid JSON Schema Draft 7
|
|
75
|
+
*
|
|
76
|
+
* The conversion preserves:
|
|
77
|
+
* - Type information (string, number, boolean, etc.)
|
|
78
|
+
* - Descriptions (from .describe())
|
|
79
|
+
* - Required/optional fields
|
|
80
|
+
* - Default values (from .default())
|
|
81
|
+
* - Enums (from z.enum())
|
|
82
|
+
* - Array items
|
|
83
|
+
* - Nested objects
|
|
84
|
+
*
|
|
85
|
+
* @param zodSchema - Zod schema to convert
|
|
86
|
+
* @param options - Conversion options
|
|
87
|
+
* @returns MCP-compliant JSON Schema
|
|
88
|
+
* @throws {KadiError} If schema is not an object type (MCP requirement)
|
|
89
|
+
* @throws {KadiError} If conversion fails
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import { z } from 'zod';
|
|
94
|
+
*
|
|
95
|
+
* const schema = z.object({
|
|
96
|
+
* name: z.string().describe('User name'),
|
|
97
|
+
* age: z.number().int().positive().optional()
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* const jsonSchema = zodToJsonSchema(schema);
|
|
101
|
+
* // Result:
|
|
102
|
+
* // {
|
|
103
|
+
* // type: 'object',
|
|
104
|
+
* // properties: {
|
|
105
|
+
* // name: { type: 'string', description: 'User name' },
|
|
106
|
+
* // age: { type: 'integer', minimum: 0 }
|
|
107
|
+
* // },
|
|
108
|
+
* // required: ['name']
|
|
109
|
+
* // }
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export declare function zodToJsonSchema(zodSchema: z.ZodTypeAny, options?: ZodToJsonSchemaOptions): JSONSchema;
|
|
113
|
+
/**
|
|
114
|
+
* Validate that a Zod schema can be converted to MCP-compliant JSON Schema
|
|
115
|
+
*
|
|
116
|
+
* This function checks whether a Zod schema can be successfully converted
|
|
117
|
+
* to JSON Schema without losing validation semantics. It's useful for
|
|
118
|
+
* providing early feedback to developers.
|
|
119
|
+
*
|
|
120
|
+
* Checks performed:
|
|
121
|
+
* - Schema is a Zod object type
|
|
122
|
+
* - No unsupported features (transforms, refinements with custom logic)
|
|
123
|
+
* - Can be converted to valid JSON Schema
|
|
124
|
+
*
|
|
125
|
+
* @param zodSchema - Zod schema to validate
|
|
126
|
+
* @returns Object with validation result and error message if invalid
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const schema = z.object({ name: z.string() });
|
|
131
|
+
* const result = validateZodSchema(schema);
|
|
132
|
+
*
|
|
133
|
+
* if (!result.valid) {
|
|
134
|
+
* console.error('Invalid schema:', result.error);
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export declare function validateZodSchema(zodSchema: z.ZodTypeAny): {
|
|
139
|
+
valid: boolean;
|
|
140
|
+
error?: string;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Check if a value is a Zod schema
|
|
144
|
+
*
|
|
145
|
+
* Type guard to determine if a value is a Zod schema. Useful for
|
|
146
|
+
* runtime detection of schema type.
|
|
147
|
+
*
|
|
148
|
+
* @param value - Value to check
|
|
149
|
+
* @returns True if value is a Zod schema, false otherwise
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* if (isZodSchema(value)) {
|
|
154
|
+
* const jsonSchema = zodToJsonSchema(value);
|
|
155
|
+
* }
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
export declare function isZodSchema(value: unknown): value is z.ZodTypeAny;
|
|
159
|
+
//# sourceMappingURL=zod-to-json-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-to-json-schema.d.ts","sourceRoot":"","sources":["../../src/schemas/zod-to-json-schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;;;;;OAOG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,eAAe,GAAG,aAAa,CAAC;IAEjE;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,wBAAgB,eAAe,CAC7B,SAAS,EAAE,CAAC,CAAC,UAAU,EACvB,OAAO,GAAE,sBAA2B,GACnC,UAAU,CA8CZ;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,CAAC,CAAC,UAAU,GACtB;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAepC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,CAAC,CAAC,UAAU,CAOjE"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod to JSON Schema Conversion Utilities
|
|
3
|
+
*
|
|
4
|
+
* This module provides utilities for converting Zod schemas to MCP-compliant
|
|
5
|
+
* JSON Schema. The conversion ensures that all generated schemas meet the
|
|
6
|
+
* requirements of the Model Context Protocol (MCP):
|
|
7
|
+
* - Root schemas must be type 'object'
|
|
8
|
+
* - All schemas must be valid JSON Schema Draft 7
|
|
9
|
+
* - No unsupported features (e.g., recursive types, transforms)
|
|
10
|
+
*
|
|
11
|
+
* @module schemas/zod-to-json-schema
|
|
12
|
+
*/
|
|
13
|
+
import { z } from 'zod';
|
|
14
|
+
import { KadiError, ErrorCode } from '../errors/index.js';
|
|
15
|
+
/**
|
|
16
|
+
* Convert Zod schema to MCP-compliant JSON Schema
|
|
17
|
+
*
|
|
18
|
+
* This function converts a Zod schema to JSON Schema format that complies with
|
|
19
|
+
* the Model Context Protocol (MCP) requirements:
|
|
20
|
+
* - Root schema must be type 'object'
|
|
21
|
+
* - No transforms or refinements (validation only)
|
|
22
|
+
* - Valid JSON Schema Draft 7
|
|
23
|
+
*
|
|
24
|
+
* The conversion preserves:
|
|
25
|
+
* - Type information (string, number, boolean, etc.)
|
|
26
|
+
* - Descriptions (from .describe())
|
|
27
|
+
* - Required/optional fields
|
|
28
|
+
* - Default values (from .default())
|
|
29
|
+
* - Enums (from z.enum())
|
|
30
|
+
* - Array items
|
|
31
|
+
* - Nested objects
|
|
32
|
+
*
|
|
33
|
+
* @param zodSchema - Zod schema to convert
|
|
34
|
+
* @param options - Conversion options
|
|
35
|
+
* @returns MCP-compliant JSON Schema
|
|
36
|
+
* @throws {KadiError} If schema is not an object type (MCP requirement)
|
|
37
|
+
* @throws {KadiError} If conversion fails
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* import { z } from 'zod';
|
|
42
|
+
*
|
|
43
|
+
* const schema = z.object({
|
|
44
|
+
* name: z.string().describe('User name'),
|
|
45
|
+
* age: z.number().int().positive().optional()
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* const jsonSchema = zodToJsonSchema(schema);
|
|
49
|
+
* // Result:
|
|
50
|
+
* // {
|
|
51
|
+
* // type: 'object',
|
|
52
|
+
* // properties: {
|
|
53
|
+
* // name: { type: 'string', description: 'User name' },
|
|
54
|
+
* // age: { type: 'integer', minimum: 0 }
|
|
55
|
+
* // },
|
|
56
|
+
* // required: ['name']
|
|
57
|
+
* // }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function zodToJsonSchema(zodSchema, options = {}) {
|
|
61
|
+
try {
|
|
62
|
+
// Set default options for Zod 4's native toJSONSchema
|
|
63
|
+
const opts = {
|
|
64
|
+
target: options.target || 'draft-7',
|
|
65
|
+
unrepresentable: 'any', // Convert unsupported types to any
|
|
66
|
+
cycles: 'ref', // Use $ref for circular references
|
|
67
|
+
reused: 'inline' // Inline reused schemas
|
|
68
|
+
};
|
|
69
|
+
// Convert Zod schema to JSON Schema using Zod 4's native function
|
|
70
|
+
const jsonSchema = z.toJSONSchema(zodSchema, opts);
|
|
71
|
+
// MCP Requirement: Root schema must be type 'object'
|
|
72
|
+
if (jsonSchema.type !== 'object') {
|
|
73
|
+
throw new KadiError(`MCP requires schemas to be type 'object', got type '${jsonSchema.type}'. ` +
|
|
74
|
+
`Wrap your schema in z.object() if needed.`, ErrorCode.SCHEMA_VALIDATION_FAILED, 400, { schemaType: jsonSchema.type });
|
|
75
|
+
}
|
|
76
|
+
// Remove $schema field if present (not needed for MCP)
|
|
77
|
+
if ('$schema' in jsonSchema) {
|
|
78
|
+
delete jsonSchema.$schema;
|
|
79
|
+
}
|
|
80
|
+
return jsonSchema;
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
// Re-throw KadiError as-is
|
|
84
|
+
if (error instanceof KadiError) {
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
// Wrap other errors in KadiError
|
|
88
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
89
|
+
throw new KadiError(`Failed to convert Zod schema to JSON Schema: ${errorMessage}`, ErrorCode.SCHEMA_VALIDATION_FAILED, 400, { originalError: error instanceof Error ? error.name : String(error) });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Validate that a Zod schema can be converted to MCP-compliant JSON Schema
|
|
94
|
+
*
|
|
95
|
+
* This function checks whether a Zod schema can be successfully converted
|
|
96
|
+
* to JSON Schema without losing validation semantics. It's useful for
|
|
97
|
+
* providing early feedback to developers.
|
|
98
|
+
*
|
|
99
|
+
* Checks performed:
|
|
100
|
+
* - Schema is a Zod object type
|
|
101
|
+
* - No unsupported features (transforms, refinements with custom logic)
|
|
102
|
+
* - Can be converted to valid JSON Schema
|
|
103
|
+
*
|
|
104
|
+
* @param zodSchema - Zod schema to validate
|
|
105
|
+
* @returns Object with validation result and error message if invalid
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* const schema = z.object({ name: z.string() });
|
|
110
|
+
* const result = validateZodSchema(schema);
|
|
111
|
+
*
|
|
112
|
+
* if (!result.valid) {
|
|
113
|
+
* console.error('Invalid schema:', result.error);
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export function validateZodSchema(zodSchema) {
|
|
118
|
+
try {
|
|
119
|
+
// Attempt conversion
|
|
120
|
+
zodToJsonSchema(zodSchema);
|
|
121
|
+
// If conversion succeeds, schema is valid
|
|
122
|
+
return { valid: true };
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
// Return validation error
|
|
126
|
+
return {
|
|
127
|
+
valid: false,
|
|
128
|
+
error: error instanceof Error ? error.message : String(error)
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Check if a value is a Zod schema
|
|
134
|
+
*
|
|
135
|
+
* Type guard to determine if a value is a Zod schema. Useful for
|
|
136
|
+
* runtime detection of schema type.
|
|
137
|
+
*
|
|
138
|
+
* @param value - Value to check
|
|
139
|
+
* @returns True if value is a Zod schema, false otherwise
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* if (isZodSchema(value)) {
|
|
144
|
+
* const jsonSchema = zodToJsonSchema(value);
|
|
145
|
+
* }
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export function isZodSchema(value) {
|
|
149
|
+
return (value !== null &&
|
|
150
|
+
typeof value === 'object' &&
|
|
151
|
+
'_def' in value &&
|
|
152
|
+
typeof value._def === 'object');
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=zod-to-json-schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-to-json-schema.js","sourceRoot":"","sources":["../../src/schemas/zod-to-json-schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AA4D1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,MAAM,UAAU,eAAe,CAC7B,SAAuB,EACvB,UAAkC,EAAE;IAEpC,IAAI,CAAC;QACH,sDAAsD;QACtD,MAAM,IAAI,GAAG;YACX,MAAM,EAAE,OAAO,CAAC,MAAM,IAAK,SAAmB;YAC9C,eAAe,EAAE,KAAc,EAAE,mCAAmC;YACpE,MAAM,EAAE,KAAc,EAAY,mCAAmC;YACrE,MAAM,EAAE,QAAiB,CAAS,wBAAwB;SAC3D,CAAC;QAEF,kEAAkE;QAClE,MAAM,UAAU,GAAG,CAAC,CAAC,YAAY,CAAC,SAAS,EAAE,IAAI,CAAe,CAAC;QAEjE,qDAAqD;QACrD,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACjC,MAAM,IAAI,SAAS,CACjB,uDAAuD,UAAU,CAAC,IAAI,KAAK;gBAC3E,2CAA2C,EAC3C,SAAS,CAAC,wBAAwB,EAClC,GAAG,EACH,EAAE,UAAU,EAAE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