@tsed/platform-mcp 8.26.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/lib/esm/constants/constants.js +11 -0
- package/lib/esm/decorators/prompt.js +29 -0
- package/lib/esm/decorators/resource.js +33 -0
- package/lib/esm/decorators/tool.js +32 -0
- package/lib/esm/fn/definePrompt.js +52 -0
- package/lib/esm/fn/defineResource.js +31 -0
- package/lib/esm/fn/defineTool.js +85 -0
- package/lib/esm/index.js +40 -0
- package/lib/esm/interfaces/PlatformMcpSettings.js +1 -0
- package/lib/esm/services/McpServerFactory.js +46 -0
- package/lib/esm/services/PlatformMcpModule.js +54 -0
- package/lib/esm/utils/json-schema-to-zod/Types.js +1 -0
- package/lib/esm/utils/json-schema-to-zod/cli.js +83 -0
- package/lib/esm/utils/json-schema-to-zod/index.js +25 -0
- package/lib/esm/utils/json-schema-to-zod/jsonSchemaToZod.js +52 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseAllOf.js +43 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseAnyOf.js +17 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseArray.js +22 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseBoolean.js +9 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseConst.js +9 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseDefault.js +9 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseEnum.js +21 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseIfThenElse.js +26 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseMultipleType.js +10 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNot.js +13 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNull.js +9 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNullable.js +11 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseNumber.js +52 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseObject.js +177 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseOneOf.js +40 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseSchema.js +153 -0
- package/lib/esm/utils/json-schema-to-zod/parsers/parseString.js +63 -0
- package/lib/esm/utils/json-schema-to-zod/utils/cliTools.js +126 -0
- package/lib/esm/utils/json-schema-to-zod/utils/half.js +9 -0
- package/lib/esm/utils/json-schema-to-zod/utils/jsdocs.js +24 -0
- package/lib/esm/utils/json-schema-to-zod/utils/omit.js +12 -0
- package/lib/esm/utils/json-schema-to-zod/utils/withMessage.js +28 -0
- package/lib/esm/utils/toZod.js +17 -0
- package/lib/tsconfig.esm.tsbuildinfo +1 -0
- package/lib/types/constants/constants.d.ts +11 -0
- package/lib/types/decorators/prompt.d.ts +26 -0
- package/lib/types/decorators/resource.d.ts +28 -0
- package/lib/types/decorators/tool.d.ts +22 -0
- package/lib/types/fn/definePrompt.d.ts +78 -0
- package/lib/types/fn/defineResource.d.ts +41 -0
- package/lib/types/fn/defineTool.d.ts +284 -0
- package/lib/types/index.d.ts +40 -0
- package/lib/types/interfaces/PlatformMcpSettings.d.ts +43 -0
- package/lib/types/services/McpServerFactory.d.ts +15 -0
- package/lib/types/services/PlatformMcpModule.d.ts +22 -0
- package/lib/types/utils/json-schema-to-zod/Types.d.ts +116 -0
- package/lib/types/utils/json-schema-to-zod/cli.d.ts +2 -0
- package/lib/types/utils/json-schema-to-zod/index.d.ts +25 -0
- package/lib/types/utils/json-schema-to-zod/jsonSchemaToZod.d.ts +11 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseAllOf.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseAnyOf.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseArray.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseBoolean.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseConst.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseDefault.d.ts +8 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseEnum.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseIfThenElse.d.ts +12 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseMultipleType.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseNot.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseNull.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseNullable.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseNumber.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseObject.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseOneOf.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseSchema.d.ts +61 -0
- package/lib/types/utils/json-schema-to-zod/parsers/parseString.d.ts +10 -0
- package/lib/types/utils/json-schema-to-zod/utils/cliTools.d.ts +67 -0
- package/lib/types/utils/json-schema-to-zod/utils/half.d.ts +7 -0
- package/lib/types/utils/json-schema-to-zod/utils/jsdocs.d.ts +15 -0
- package/lib/types/utils/json-schema-to-zod/utils/omit.d.ts +7 -0
- package/lib/types/utils/json-schema-to-zod/utils/withMessage.d.ts +19 -0
- package/lib/types/utils/toZod.d.ts +10 -0
- package/package.json +69 -0
- package/readme.md +73 -0
- package/src/constants/constants.ts +11 -0
- package/src/decorators/prompt.spec.ts +30 -0
- package/src/decorators/prompt.ts +39 -0
- package/src/decorators/resource.spec.ts +33 -0
- package/src/decorators/resource.ts +44 -0
- package/src/decorators/tool.spec.ts +68 -0
- package/src/decorators/tool.ts +34 -0
- package/src/fn/definePrompt.ts +93 -0
- package/src/fn/defineResource.ts +81 -0
- package/src/fn/defineTool.spec.ts +29 -0
- package/src/fn/defineTool.ts +141 -0
- package/src/index.ts +40 -0
- package/src/interfaces/PlatformMcpSettings.ts +45 -0
- package/src/services/McpServerFactory.ts +69 -0
- package/src/services/PlatformMcpModule.ts +67 -0
- package/src/utils/json-schema-to-zod/Types.ts +123 -0
- package/src/utils/json-schema-to-zod/cli.ts +88 -0
- package/src/utils/json-schema-to-zod/index.ts +26 -0
- package/src/utils/json-schema-to-zod/jsonSchemaToZod.ts +60 -0
- package/src/utils/json-schema-to-zod/parsers/parseAllOf.ts +50 -0
- package/src/utils/json-schema-to-zod/parsers/parseAnyOf.ts +19 -0
- package/src/utils/json-schema-to-zod/parsers/parseArray.ts +28 -0
- package/src/utils/json-schema-to-zod/parsers/parseBoolean.ts +11 -0
- package/src/utils/json-schema-to-zod/parsers/parseConst.ts +11 -0
- package/src/utils/json-schema-to-zod/parsers/parseDefault.ts +11 -0
- package/src/utils/json-schema-to-zod/parsers/parseEnum.ts +20 -0
- package/src/utils/json-schema-to-zod/parsers/parseIfThenElse.ts +35 -0
- package/src/utils/json-schema-to-zod/parsers/parseMultipleType.ts +12 -0
- package/src/utils/json-schema-to-zod/parsers/parseNot.ts +15 -0
- package/src/utils/json-schema-to-zod/parsers/parseNull.ts +11 -0
- package/src/utils/json-schema-to-zod/parsers/parseNullable.ts +13 -0
- package/src/utils/json-schema-to-zod/parsers/parseNumber.ts +56 -0
- package/src/utils/json-schema-to-zod/parsers/parseObject.ts +220 -0
- package/src/utils/json-schema-to-zod/parsers/parseOneOf.ts +44 -0
- package/src/utils/json-schema-to-zod/parsers/parseSchema.ts +190 -0
- package/src/utils/json-schema-to-zod/parsers/parseString.ts +73 -0
- package/src/utils/json-schema-to-zod/utils/cliTools.ts +189 -0
- package/src/utils/json-schema-to-zod/utils/half.ts +9 -0
- package/src/utils/json-schema-to-zod/utils/jsdocs.ts +29 -0
- package/src/utils/json-schema-to-zod/utils/omit.ts +14 -0
- package/src/utils/json-schema-to-zod/utils/withMessage.ts +42 -0
- package/src/utils/toZod.spec.ts +15 -0
- package/src/utils/toZod.ts +20 -0
- package/tsconfig.esm.json +14 -0
- package/vitest.config.mts +36 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by @tsed/barrels.
|
|
3
|
+
*/
|
|
4
|
+
export * from "./constants/constants.js";
|
|
5
|
+
export * from "./decorators/prompt.js";
|
|
6
|
+
export * from "./decorators/resource.js";
|
|
7
|
+
export * from "./decorators/tool.js";
|
|
8
|
+
export * from "./fn/definePrompt.js";
|
|
9
|
+
export * from "./fn/defineResource.js";
|
|
10
|
+
export * from "./fn/defineTool.js";
|
|
11
|
+
export * from "./interfaces/PlatformMcpSettings.js";
|
|
12
|
+
export * from "./services/McpServerFactory.js";
|
|
13
|
+
export * from "./services/PlatformMcpModule.js";
|
|
14
|
+
export * from "./utils/json-schema-to-zod/cli.js";
|
|
15
|
+
export * from "./utils/json-schema-to-zod/index.js";
|
|
16
|
+
export * from "./utils/json-schema-to-zod/jsonSchemaToZod.js";
|
|
17
|
+
export * from "./utils/json-schema-to-zod/parsers/parseAllOf.js";
|
|
18
|
+
export * from "./utils/json-schema-to-zod/parsers/parseAnyOf.js";
|
|
19
|
+
export * from "./utils/json-schema-to-zod/parsers/parseArray.js";
|
|
20
|
+
export * from "./utils/json-schema-to-zod/parsers/parseBoolean.js";
|
|
21
|
+
export * from "./utils/json-schema-to-zod/parsers/parseConst.js";
|
|
22
|
+
export * from "./utils/json-schema-to-zod/parsers/parseDefault.js";
|
|
23
|
+
export * from "./utils/json-schema-to-zod/parsers/parseEnum.js";
|
|
24
|
+
export * from "./utils/json-schema-to-zod/parsers/parseIfThenElse.js";
|
|
25
|
+
export * from "./utils/json-schema-to-zod/parsers/parseMultipleType.js";
|
|
26
|
+
export * from "./utils/json-schema-to-zod/parsers/parseNot.js";
|
|
27
|
+
export * from "./utils/json-schema-to-zod/parsers/parseNull.js";
|
|
28
|
+
export * from "./utils/json-schema-to-zod/parsers/parseNullable.js";
|
|
29
|
+
export * from "./utils/json-schema-to-zod/parsers/parseNumber.js";
|
|
30
|
+
export * from "./utils/json-schema-to-zod/parsers/parseObject.js";
|
|
31
|
+
export * from "./utils/json-schema-to-zod/parsers/parseOneOf.js";
|
|
32
|
+
export * from "./utils/json-schema-to-zod/parsers/parseSchema.js";
|
|
33
|
+
export * from "./utils/json-schema-to-zod/parsers/parseString.js";
|
|
34
|
+
export * from "./utils/json-schema-to-zod/Types.js";
|
|
35
|
+
export * from "./utils/json-schema-to-zod/utils/cliTools.js";
|
|
36
|
+
export * from "./utils/json-schema-to-zod/utils/half.js";
|
|
37
|
+
export * from "./utils/json-schema-to-zod/utils/jsdocs.js";
|
|
38
|
+
export * from "./utils/json-schema-to-zod/utils/omit.js";
|
|
39
|
+
export * from "./utils/json-schema-to-zod/utils/withMessage.js";
|
|
40
|
+
export * from "./utils/toZod.js";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type {StreamableHTTPServerTransportOptions} from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
2
|
+
import type {TokenProvider} from "@tsed/di";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Configuration fragment accepted under `configuration.mcp` to wire the Platform MCP module.
|
|
6
|
+
*
|
|
7
|
+
* @module platform/mcp
|
|
8
|
+
* @since 8.17.0
|
|
9
|
+
*/
|
|
10
|
+
export interface PlatformMcpSettings {
|
|
11
|
+
/**
|
|
12
|
+
* Mount path for the MCP endpoint. Defaults to `/mcp`.
|
|
13
|
+
*/
|
|
14
|
+
path?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Whether the HTTP endpoint should be enabled. Defaults to `true`.
|
|
17
|
+
*/
|
|
18
|
+
enabled?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Optional name reported to MCP clients. Falls back to the Ts.ED app name.
|
|
21
|
+
*/
|
|
22
|
+
name?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Optional version reported to MCP clients. Falls back to the Ts.ED app version.
|
|
25
|
+
*/
|
|
26
|
+
version?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Optional tokens registered through configuration.
|
|
29
|
+
*/
|
|
30
|
+
tools?: TokenProvider[];
|
|
31
|
+
resources?: TokenProvider[];
|
|
32
|
+
prompts?: TokenProvider[];
|
|
33
|
+
/**
|
|
34
|
+
* Optional transport options for the MCP server.
|
|
35
|
+
*/
|
|
36
|
+
transportOptions?: StreamableHTTPServerTransportOptions;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare global {
|
|
40
|
+
namespace TsED {
|
|
41
|
+
interface Configuration {
|
|
42
|
+
mcp?: PlatformMcpSettings;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type {ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import {McpServer} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
+
import {constant, inject, injectable, injector, type TokenProvider} from "@tsed/di";
|
|
4
|
+
|
|
5
|
+
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
6
|
+
import type {PromptsSettings} from "../fn/definePrompt.js";
|
|
7
|
+
import type {ResourceProps} from "../fn/defineResource.js";
|
|
8
|
+
import type {ToolProps} from "../fn/defineTool.js";
|
|
9
|
+
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
10
|
+
|
|
11
|
+
function collectTokens(type: string, configured: TokenProvider[] = []): TokenProvider[] {
|
|
12
|
+
const tokens = new Set<TokenProvider>(configured);
|
|
13
|
+
|
|
14
|
+
injector()
|
|
15
|
+
.getProviders(type)
|
|
16
|
+
.forEach((provider) => tokens.add(provider.token));
|
|
17
|
+
|
|
18
|
+
return [...tokens];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Injectable MCP server instance configured with registered tools, resources, and prompts.
|
|
23
|
+
*
|
|
24
|
+
* @module platform/mcp
|
|
25
|
+
* @since 8.17.0
|
|
26
|
+
*/
|
|
27
|
+
export const MCP_SERVER = injectable(McpServer)
|
|
28
|
+
.factory(() => {
|
|
29
|
+
const settings = constant<PlatformMcpSettings>("mcp", {}) || {};
|
|
30
|
+
const name = settings.name || constant<string>("name") || "tsed-mcp";
|
|
31
|
+
const version = settings.version || constant<string>("version") || "0.0.0";
|
|
32
|
+
|
|
33
|
+
const server = new McpServer({
|
|
34
|
+
name,
|
|
35
|
+
version
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const toolTokens = collectTokens(MCP_PROVIDER_TYPES.TOOL, settings.tools);
|
|
39
|
+
toolTokens.forEach((token) => {
|
|
40
|
+
const definition = inject<ToolProps<any, any> & {handler: any}>(token);
|
|
41
|
+
const {name, handler, ...opts} = definition;
|
|
42
|
+
server.registerTool(name!, opts as any, handler as any);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const resourceTokens = collectTokens(MCP_PROVIDER_TYPES.RESOURCE, settings.resources);
|
|
46
|
+
resourceTokens.forEach((token) => {
|
|
47
|
+
const definition = inject<ResourceProps & {uri?: string; template?: ResourceTemplate}>(token);
|
|
48
|
+
const {name, handler, uri, template, ...opts} = definition;
|
|
49
|
+
server.registerResource(name, (uri || template)! as any, opts, handler as any);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const promptTokens = collectTokens(MCP_PROVIDER_TYPES.PROMPT, settings.prompts);
|
|
53
|
+
promptTokens.forEach((token) => {
|
|
54
|
+
const definition = inject<PromptsSettings>(token);
|
|
55
|
+
const {name, handler, ...opts} = definition;
|
|
56
|
+
server.registerPrompt(name, opts, handler as any);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return server;
|
|
60
|
+
})
|
|
61
|
+
.token();
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Type alias referencing the MCP server provider token.
|
|
65
|
+
*
|
|
66
|
+
* @module platform/mcp
|
|
67
|
+
* @since 8.17.0
|
|
68
|
+
*/
|
|
69
|
+
export type MCP_SERVER = typeof MCP_SERVER;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type {McpServer} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import {StreamableHTTPServerTransport} from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
3
|
+
import {constant, inject, injectable} from "@tsed/di";
|
|
4
|
+
import {application, type OnRoutesInit, PlatformContext, type PlatformRouteDetails} from "@tsed/platform-http";
|
|
5
|
+
import {useContextHandler} from "@tsed/platform-router";
|
|
6
|
+
|
|
7
|
+
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
8
|
+
import {MCP_SERVER} from "./McpServerFactory.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Platform module that mounts the MCP HTTP endpoint and forwards requests to the configured server instance.
|
|
12
|
+
*
|
|
13
|
+
* @module platform/mcp
|
|
14
|
+
* @since 8.17.0
|
|
15
|
+
*/
|
|
16
|
+
export class PlatformMcpModule implements OnRoutesInit {
|
|
17
|
+
protected settings = constant<PlatformMcpSettings>("mcp", {});
|
|
18
|
+
protected app = application();
|
|
19
|
+
protected server = inject<McpServer>(MCP_SERVER);
|
|
20
|
+
|
|
21
|
+
$onRoutesInit() {
|
|
22
|
+
if (this.isEnabled()) {
|
|
23
|
+
const path = this.settings?.path || "/mcp";
|
|
24
|
+
|
|
25
|
+
this.app.post(
|
|
26
|
+
path,
|
|
27
|
+
useContextHandler(async ($ctx: PlatformContext) => this.dispatch($ctx))
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
$logRoutes(routes: PlatformRouteDetails[]) {
|
|
33
|
+
return [
|
|
34
|
+
...routes,
|
|
35
|
+
this.isEnabled() && {
|
|
36
|
+
method: "POST",
|
|
37
|
+
name: "PlatformMcpModule.dispatch()",
|
|
38
|
+
url: this.settings?.path || "/mcp"
|
|
39
|
+
}
|
|
40
|
+
].filter(Boolean);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected async dispatch($ctx: PlatformContext) {
|
|
44
|
+
const transport = new StreamableHTTPServerTransport({
|
|
45
|
+
sessionIdGenerator: undefined,
|
|
46
|
+
enableJsonResponse: true,
|
|
47
|
+
...this.settings.transportOptions
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const {request, response} = $ctx;
|
|
51
|
+
|
|
52
|
+
response.raw?.on("close", () => transport.close());
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
await this.server.connect(transport as any);
|
|
56
|
+
await transport.handleRequest(request.getReq(), response.getRes(), request.body);
|
|
57
|
+
} finally {
|
|
58
|
+
await transport.close();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private isEnabled() {
|
|
63
|
+
return this.settings?.enabled !== false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
injectable(PlatformMcpModule);
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-compatible shape preserved when translating between schemas.
|
|
3
|
+
*
|
|
4
|
+
* @module platform/mcp
|
|
5
|
+
* @since 8.17.0
|
|
6
|
+
*/
|
|
7
|
+
export type Serializable = {[key: string]: Serializable} | Serializable[] | string | number | boolean | null;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Minimal JSON Schema representation supported by the conversion utilities.
|
|
11
|
+
*
|
|
12
|
+
* @module platform/mcp
|
|
13
|
+
* @since 8.17.0
|
|
14
|
+
*/
|
|
15
|
+
export type JsonSchema = JsonSchemaObject | boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Object-based JSON Schema node with permissive indexing for vendor keywords.
|
|
18
|
+
*
|
|
19
|
+
* @module platform/mcp
|
|
20
|
+
* @since 8.17.0
|
|
21
|
+
*/
|
|
22
|
+
export type JsonSchemaObject = {
|
|
23
|
+
// left permissive by design
|
|
24
|
+
type?: string | string[];
|
|
25
|
+
|
|
26
|
+
// object
|
|
27
|
+
properties?: {[key: string]: JsonSchema};
|
|
28
|
+
additionalProperties?: JsonSchema;
|
|
29
|
+
unevaluatedProperties?: JsonSchema;
|
|
30
|
+
patternProperties?: {[key: string]: JsonSchema};
|
|
31
|
+
minProperties?: number;
|
|
32
|
+
maxProperties?: number;
|
|
33
|
+
required?: string[] | boolean;
|
|
34
|
+
propertyNames?: JsonSchema;
|
|
35
|
+
|
|
36
|
+
// array
|
|
37
|
+
items?: JsonSchema | JsonSchema[];
|
|
38
|
+
additionalItems?: JsonSchema;
|
|
39
|
+
minItems?: number;
|
|
40
|
+
maxItems?: number;
|
|
41
|
+
uniqueItems?: boolean;
|
|
42
|
+
|
|
43
|
+
// string
|
|
44
|
+
minLength?: number;
|
|
45
|
+
maxLength?: number;
|
|
46
|
+
pattern?: string;
|
|
47
|
+
format?: string;
|
|
48
|
+
|
|
49
|
+
// number
|
|
50
|
+
minimum?: number;
|
|
51
|
+
maximum?: number;
|
|
52
|
+
exclusiveMinimum?: number | boolean;
|
|
53
|
+
exclusiveMaximum?: number | boolean;
|
|
54
|
+
multipleOf?: number;
|
|
55
|
+
|
|
56
|
+
// unions
|
|
57
|
+
anyOf?: JsonSchema[];
|
|
58
|
+
allOf?: JsonSchema[];
|
|
59
|
+
oneOf?: JsonSchema[];
|
|
60
|
+
|
|
61
|
+
if?: JsonSchema;
|
|
62
|
+
then?: JsonSchema;
|
|
63
|
+
else?: JsonSchema;
|
|
64
|
+
|
|
65
|
+
// shared
|
|
66
|
+
const?: Serializable;
|
|
67
|
+
enum?: Serializable[];
|
|
68
|
+
|
|
69
|
+
errorMessage?: {[key: string]: string | undefined};
|
|
70
|
+
} & {[key: string]: any};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Function signature used to pick the correct parser for a JSON Schema node.
|
|
74
|
+
*
|
|
75
|
+
* @module platform/mcp
|
|
76
|
+
* @since 8.17.0
|
|
77
|
+
*/
|
|
78
|
+
export type ParserSelector = (schema: JsonSchemaObject, refs: Refs) => string;
|
|
79
|
+
/**
|
|
80
|
+
* Optional hook that allows callers to override parsing logic for specific nodes.
|
|
81
|
+
*
|
|
82
|
+
* @module platform/mcp
|
|
83
|
+
* @since 8.17.0
|
|
84
|
+
*/
|
|
85
|
+
export type ParserOverride = (schema: JsonSchemaObject, refs: Refs) => string | void;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Supported Zod major versions targeted by the generator.
|
|
89
|
+
*
|
|
90
|
+
* @module platform/mcp
|
|
91
|
+
* @since 8.17.0
|
|
92
|
+
*/
|
|
93
|
+
export type ZodVersion = 3 | 4;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Options accepted by {@link jsonSchemaToZod} and downstream helpers.
|
|
97
|
+
*
|
|
98
|
+
* @module platform/mcp
|
|
99
|
+
* @since 8.17.0
|
|
100
|
+
*/
|
|
101
|
+
export type Options = {
|
|
102
|
+
name?: string;
|
|
103
|
+
module?: "cjs" | "esm" | "none";
|
|
104
|
+
withoutDefaults?: boolean;
|
|
105
|
+
withoutDescribes?: boolean;
|
|
106
|
+
withJsdocs?: boolean;
|
|
107
|
+
parserOverride?: ParserOverride;
|
|
108
|
+
depth?: number;
|
|
109
|
+
type?: boolean | string;
|
|
110
|
+
noImport?: boolean;
|
|
111
|
+
zodVersion?: ZodVersion;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Internal reference tracker used during recursive parsing.
|
|
116
|
+
*
|
|
117
|
+
* @module platform/mcp
|
|
118
|
+
* @since 8.17.0
|
|
119
|
+
*/
|
|
120
|
+
export type Refs = Options & {
|
|
121
|
+
path: (string | number)[];
|
|
122
|
+
seen: Map<object | boolean, {n: number; r: string | undefined}>;
|
|
123
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {mkdirSync, writeFileSync} from "fs";
|
|
3
|
+
import {dirname} from "path";
|
|
4
|
+
import {fileURLToPath} from "url";
|
|
5
|
+
|
|
6
|
+
import {jsonSchemaToZod} from "./jsonSchemaToZod.js";
|
|
7
|
+
import {JsonSchema, ZodVersion} from "./Types.js";
|
|
8
|
+
import {parseArgs, parseOrReadJSON, readPipe} from "./utils/cliTools.js";
|
|
9
|
+
|
|
10
|
+
const params = {
|
|
11
|
+
input: {
|
|
12
|
+
shorthand: "i",
|
|
13
|
+
value: "string",
|
|
14
|
+
required: process.stdin.isTTY && "input is required when no JSON or file path is piped",
|
|
15
|
+
description: "JSON or a source file path. Required if no data is piped."
|
|
16
|
+
},
|
|
17
|
+
output: {
|
|
18
|
+
shorthand: "o",
|
|
19
|
+
value: "string",
|
|
20
|
+
description: "A file path to write to. If not supplied stdout will be used."
|
|
21
|
+
},
|
|
22
|
+
name: {
|
|
23
|
+
shorthand: "n",
|
|
24
|
+
value: "string",
|
|
25
|
+
description: "The name of the schema in the output."
|
|
26
|
+
},
|
|
27
|
+
depth: {
|
|
28
|
+
shorthand: "d",
|
|
29
|
+
value: "number",
|
|
30
|
+
description: "Maximum depth of recursion before falling back to z.any(). Defaults to 0."
|
|
31
|
+
},
|
|
32
|
+
module: {
|
|
33
|
+
shorthand: "m",
|
|
34
|
+
value: ["esm", "cjs", "none"],
|
|
35
|
+
description: "Module syntax; 'esm', 'cjs' or 'none'. Defaults to 'esm'."
|
|
36
|
+
},
|
|
37
|
+
type: {
|
|
38
|
+
shorthand: "t",
|
|
39
|
+
value: "string",
|
|
40
|
+
description: "The name of the (optional) inferred type export."
|
|
41
|
+
},
|
|
42
|
+
noImport: {
|
|
43
|
+
shorthand: "ni",
|
|
44
|
+
description: "Removes the `import { z } from 'zod';` or equivalent from the output."
|
|
45
|
+
},
|
|
46
|
+
withJsdocs: {
|
|
47
|
+
shorthand: "wj",
|
|
48
|
+
description: "Generate jsdocs off of the description property."
|
|
49
|
+
},
|
|
50
|
+
zodVersion: {
|
|
51
|
+
shorthand: "zv",
|
|
52
|
+
value: "number",
|
|
53
|
+
description: "Target Zod version: 3 or 4. Defaults to 4."
|
|
54
|
+
}
|
|
55
|
+
} as const;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Entry point for the `json-schema-to-zod` CLI that converts JSON Schema files into Zod source code.
|
|
59
|
+
*
|
|
60
|
+
* @module platform/mcp
|
|
61
|
+
* @since 8.17.0
|
|
62
|
+
*/
|
|
63
|
+
async function main() {
|
|
64
|
+
const args = parseArgs(params, process.argv, true);
|
|
65
|
+
const input = args.input || (await readPipe());
|
|
66
|
+
const jsonSchema = parseOrReadJSON(input);
|
|
67
|
+
const zodVersion = (args.zodVersion === 3 ? 3 : 4) as ZodVersion;
|
|
68
|
+
const zodSchema = jsonSchemaToZod(jsonSchema as JsonSchema, {
|
|
69
|
+
name: args.name,
|
|
70
|
+
depth: args.depth,
|
|
71
|
+
module: args.module || "esm",
|
|
72
|
+
noImport: args.noImport,
|
|
73
|
+
type: args.type,
|
|
74
|
+
withJsdocs: args.withJsdocs,
|
|
75
|
+
zodVersion
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (args.output) {
|
|
79
|
+
mkdirSync(dirname(args.output), {recursive: true});
|
|
80
|
+
writeFileSync(args.output, zodSchema);
|
|
81
|
+
} else {
|
|
82
|
+
console.log(zodSchema);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
87
|
+
void main();
|
|
88
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export * from "./jsonSchemaToZod.js";
|
|
2
|
+
export * from "./parsers/parseAllOf.js";
|
|
3
|
+
export * from "./parsers/parseAnyOf.js";
|
|
4
|
+
export * from "./parsers/parseArray.js";
|
|
5
|
+
export * from "./parsers/parseBoolean.js";
|
|
6
|
+
export * from "./parsers/parseConst.js";
|
|
7
|
+
export * from "./parsers/parseDefault.js";
|
|
8
|
+
export * from "./parsers/parseEnum.js";
|
|
9
|
+
export * from "./parsers/parseIfThenElse.js";
|
|
10
|
+
export * from "./parsers/parseMultipleType.js";
|
|
11
|
+
export * from "./parsers/parseNot.js";
|
|
12
|
+
export * from "./parsers/parseNull.js";
|
|
13
|
+
export * from "./parsers/parseNullable.js";
|
|
14
|
+
export * from "./parsers/parseNumber.js";
|
|
15
|
+
export * from "./parsers/parseObject.js";
|
|
16
|
+
export * from "./parsers/parseOneOf.js";
|
|
17
|
+
export * from "./parsers/parseSchema.js";
|
|
18
|
+
export * from "./parsers/parseString.js";
|
|
19
|
+
export * from "./Types.js";
|
|
20
|
+
export * from "./utils/half.js";
|
|
21
|
+
export * from "./utils/jsdocs.js";
|
|
22
|
+
export * from "./utils/omit.js";
|
|
23
|
+
export * from "./utils/withMessage.js";
|
|
24
|
+
import {jsonSchemaToZod} from "./jsonSchemaToZod.js";
|
|
25
|
+
|
|
26
|
+
export default jsonSchemaToZod;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {parseSchema} from "./parsers/parseSchema.js";
|
|
2
|
+
import {JsonSchema, Options} from "./Types.js";
|
|
3
|
+
import {expandJsdocs} from "./utils/jsdocs.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generates Zod code from a JSON Schema, optionally targeting specific module systems and versions.
|
|
7
|
+
*
|
|
8
|
+
* @param schema JSON Schema definition to translate.
|
|
9
|
+
* @param options Generation options such as module format, naming, and version targets.
|
|
10
|
+
* @returns A string of executable JavaScript/TypeScript that constructs an equivalent Zod schema.
|
|
11
|
+
* @module platform/mcp
|
|
12
|
+
* @since 8.17.0
|
|
13
|
+
*/
|
|
14
|
+
export const jsonSchemaToZod = (schema: JsonSchema, {module, name, type, noImport, zodVersion = 4, ...rest}: Options = {}): string => {
|
|
15
|
+
if (type && (!name || module !== "esm")) {
|
|
16
|
+
throw new Error("Option `type` requires `name` to be set and `module` to be `esm`");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let result = parseSchema(schema, {
|
|
20
|
+
module,
|
|
21
|
+
name,
|
|
22
|
+
path: [],
|
|
23
|
+
seen: new Map(),
|
|
24
|
+
zodVersion,
|
|
25
|
+
...rest
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const jsdocs = rest.withJsdocs && typeof schema !== "boolean" && schema.description ? expandJsdocs(schema.description) : "";
|
|
29
|
+
|
|
30
|
+
if (module === "cjs") {
|
|
31
|
+
result = `${jsdocs}module.exports = ${name ? `{ ${JSON.stringify(name)}: ${result} }` : result}
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
if (!noImport) {
|
|
35
|
+
result = `${jsdocs}const { z } = require("zod")
|
|
36
|
+
|
|
37
|
+
${result}`;
|
|
38
|
+
}
|
|
39
|
+
} else if (module === "esm") {
|
|
40
|
+
result = `${jsdocs}export ${name ? `const ${name} =` : `default`} ${result}
|
|
41
|
+
`;
|
|
42
|
+
|
|
43
|
+
if (!noImport) {
|
|
44
|
+
result = `import { z } from "zod"
|
|
45
|
+
|
|
46
|
+
${result}`;
|
|
47
|
+
}
|
|
48
|
+
} else if (name) {
|
|
49
|
+
result = `${jsdocs}const ${name} = ${result}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (type && name) {
|
|
53
|
+
let typeName = typeof type === "string" ? type : `${name[0].toUpperCase()}${name.substring(1)}`;
|
|
54
|
+
|
|
55
|
+
result += `export type ${typeName} = z.infer<typeof ${name}>
|
|
56
|
+
`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return result;
|
|
60
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {JsonSchema, JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
+
import {half} from "../utils/half.js";
|
|
3
|
+
import {parseSchema} from "./parseSchema.js";
|
|
4
|
+
|
|
5
|
+
const originalIndex = Symbol("Original index");
|
|
6
|
+
|
|
7
|
+
const ensureOriginalIndex = (arr: JsonSchema[]) => {
|
|
8
|
+
let newArr = [];
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < arr.length; i++) {
|
|
11
|
+
const item = arr[i];
|
|
12
|
+
if (typeof item === "boolean") {
|
|
13
|
+
newArr.push(item ? {[originalIndex]: i} : {[originalIndex]: i, not: {}});
|
|
14
|
+
} else if (originalIndex in item) {
|
|
15
|
+
return arr;
|
|
16
|
+
} else {
|
|
17
|
+
newArr.push({...item, [originalIndex]: i});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return newArr;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Parses an `allOf` schema by intersecting all child schemas, preserving evaluation order for accurate metadata.
|
|
26
|
+
*
|
|
27
|
+
* @module platform/mcp
|
|
28
|
+
* @since 8.17.0
|
|
29
|
+
*/
|
|
30
|
+
export function parseAllOf(schema: JsonSchemaObject & {allOf: JsonSchema[]}, refs: Refs): string {
|
|
31
|
+
if (schema.allOf.length === 0) {
|
|
32
|
+
return "z.never()";
|
|
33
|
+
} else if (schema.allOf.length === 1) {
|
|
34
|
+
const item = schema.allOf[0];
|
|
35
|
+
|
|
36
|
+
return parseSchema(item, {
|
|
37
|
+
...refs,
|
|
38
|
+
path: [...refs.path, "allOf", (item as any)[originalIndex]]
|
|
39
|
+
});
|
|
40
|
+
} else {
|
|
41
|
+
const [left, right] = half(ensureOriginalIndex(schema.allOf)) as any;
|
|
42
|
+
|
|
43
|
+
return `z.intersection(${parseAllOf({allOf: left}, refs)}, ${parseAllOf(
|
|
44
|
+
{
|
|
45
|
+
allOf: right
|
|
46
|
+
},
|
|
47
|
+
refs
|
|
48
|
+
)})`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {JsonSchema, JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
+
import {parseSchema} from "./parseSchema.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parses an `anyOf` node into a Zod union across all nested schemas.
|
|
6
|
+
*
|
|
7
|
+
* @module platform/mcp
|
|
8
|
+
* @since 8.17.0
|
|
9
|
+
*/
|
|
10
|
+
export const parseAnyOf = (schema: JsonSchemaObject & {anyOf: JsonSchema[]}, refs: Refs) => {
|
|
11
|
+
return schema.anyOf.length
|
|
12
|
+
? schema.anyOf.length === 1
|
|
13
|
+
? parseSchema(schema.anyOf[0], {
|
|
14
|
+
...refs,
|
|
15
|
+
path: [...refs.path, "anyOf", 0]
|
|
16
|
+
})
|
|
17
|
+
: `z.union([${schema.anyOf.map((schema, i) => parseSchema(schema, {...refs, path: [...refs.path, "anyOf", i]})).join(", ")}])`
|
|
18
|
+
: `z.any()`;
|
|
19
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {JsonSchemaObject, Refs} from "../Types.js";
|
|
2
|
+
import {withMessage} from "../utils/withMessage.js";
|
|
3
|
+
import {parseSchema} from "./parseSchema.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parses array schemas into the appropriate tuple or array expressions with validation hints.
|
|
7
|
+
*
|
|
8
|
+
* @module platform/mcp
|
|
9
|
+
* @since 8.17.0
|
|
10
|
+
*/
|
|
11
|
+
export const parseArray = (schema: JsonSchemaObject & {type: "array"}, refs: Refs) => {
|
|
12
|
+
if (Array.isArray(schema.items)) {
|
|
13
|
+
return `z.tuple([${schema.items.map((v, i) => parseSchema(v, {...refs, path: [...refs.path, "items", i]}))}])`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let r = !schema.items
|
|
17
|
+
? "z.array(z.any())"
|
|
18
|
+
: `z.array(${parseSchema(schema.items, {
|
|
19
|
+
...refs,
|
|
20
|
+
path: [...refs.path, "items"]
|
|
21
|
+
})})`;
|
|
22
|
+
|
|
23
|
+
r += withMessage(schema, "minItems", ({json}) => [`.min(${json}`, ", ", ")"]);
|
|
24
|
+
|
|
25
|
+
r += withMessage(schema, "maxItems", ({json}) => [`.max(${json}`, ", ", ")"]);
|
|
26
|
+
|
|
27
|
+
return r;
|
|
28
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {JsonSchemaObject} from "../Types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parses boolean schemas into `z.boolean()`.
|
|
5
|
+
*
|
|
6
|
+
* @module platform/mcp
|
|
7
|
+
* @since 8.17.0
|
|
8
|
+
*/
|
|
9
|
+
export const parseBoolean = (_schema: JsonSchemaObject & {type: "boolean"}) => {
|
|
10
|
+
return "z.boolean()";
|
|
11
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {JsonSchemaObject, Serializable} from "../Types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parses constant schemas into `z.literal` expressions.
|
|
5
|
+
*
|
|
6
|
+
* @module platform/mcp
|
|
7
|
+
* @since 8.17.0
|
|
8
|
+
*/
|
|
9
|
+
export const parseConst = (schema: JsonSchemaObject & {const: Serializable}) => {
|
|
10
|
+
return `z.literal(${JSON.stringify(schema.const)})`;
|
|
11
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {JsonSchemaObject, Serializable} from "../Types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parses enum schemas into either `z.enum` or unions of literals depending on value types.
|
|
5
|
+
*
|
|
6
|
+
* @module platform/mcp
|
|
7
|
+
* @since 8.17.0
|
|
8
|
+
*/
|
|
9
|
+
export const parseEnum = (schema: JsonSchemaObject & {enum: Serializable[]}) => {
|
|
10
|
+
if (schema.enum.length === 0) {
|
|
11
|
+
return "z.never()";
|
|
12
|
+
} else if (schema.enum.length === 1) {
|
|
13
|
+
// union does not work when there is only one element
|
|
14
|
+
return `z.literal(${JSON.stringify(schema.enum[0])})`;
|
|
15
|
+
} else if (schema.enum.every((x) => typeof x === "string")) {
|
|
16
|
+
return `z.enum([${schema.enum.map((x) => JSON.stringify(x))}])`;
|
|
17
|
+
} else {
|
|
18
|
+
return `z.union([${schema.enum.map((x) => `z.literal(${JSON.stringify(x)})`).join(", ")}])`;
|
|
19
|
+
}
|
|
20
|
+
};
|