@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
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider type tokens used to register MCP tools, resources, and prompts with the Ts.ED DI container.
|
|
3
|
+
*
|
|
4
|
+
* @module platform/mcp
|
|
5
|
+
* @since 8.17.0
|
|
6
|
+
*/
|
|
7
|
+
export const MCP_PROVIDER_TYPES = {
|
|
8
|
+
TOOL: "mcp:tool",
|
|
9
|
+
RESOURCE: "mcp:resource",
|
|
10
|
+
PROMPT: "mcp:prompt"
|
|
11
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { classOf } from "@tsed/core";
|
|
2
|
+
import { definePrompt } from "../fn/definePrompt.js";
|
|
3
|
+
/**
|
|
4
|
+
* Declares a Ts.ED class method as an MCP prompt and registers it through {@link definePrompt}.
|
|
5
|
+
*
|
|
6
|
+
* @param options Optional MCP prompt metadata such as name, description, and args schema.
|
|
7
|
+
* @module platform/mcp
|
|
8
|
+
* @since 8.17.0
|
|
9
|
+
*
|
|
10
|
+
* ### Usage
|
|
11
|
+
* ```ts
|
|
12
|
+
* class ConciergePrompts {
|
|
13
|
+
* @Prompt()
|
|
14
|
+
* async hotelWelcome() {
|
|
15
|
+
* return {content: [{type: "text", text: "Welcome to Ts.ED Hotel!"}]};
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function Prompt(options) {
|
|
21
|
+
return (target, propertyKey, _) => {
|
|
22
|
+
definePrompt({
|
|
23
|
+
...options,
|
|
24
|
+
name: options?.name || String(propertyKey),
|
|
25
|
+
token: classOf(target),
|
|
26
|
+
propertyKey: propertyKey
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { classOf } from "@tsed/core";
|
|
2
|
+
import { isString } from "@tsed/core/utils/isString.js";
|
|
3
|
+
import { defineResource } from "../fn/defineResource.js";
|
|
4
|
+
/**
|
|
5
|
+
* Decorator that turns a class method into an MCP resource backed by either a URI or a full {@link ResourceTemplate}.
|
|
6
|
+
*
|
|
7
|
+
* @param uriOrTemplate Direct URI string or a template describing the resource discovery metadata.
|
|
8
|
+
* @param options Optional overrides for metadata such as name, description, or visibility.
|
|
9
|
+
* @module platform/mcp
|
|
10
|
+
* @since 8.17.0
|
|
11
|
+
*
|
|
12
|
+
* ### Usage
|
|
13
|
+
* ```ts
|
|
14
|
+
* class FileCatalog {
|
|
15
|
+
* @Resource("file:///var/data/catalog")
|
|
16
|
+
* async readCatalog() {
|
|
17
|
+
* return {contents: [{uri: "file:///var/data/catalog/index.json"}]};
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function Resource(uriOrTemplate, options) {
|
|
23
|
+
return (target, propertyKey, _) => {
|
|
24
|
+
defineResource({
|
|
25
|
+
...options,
|
|
26
|
+
name: options?.name || String(propertyKey),
|
|
27
|
+
token: classOf(target),
|
|
28
|
+
propertyKey,
|
|
29
|
+
uri: isString(uriOrTemplate) ? uriOrTemplate : undefined,
|
|
30
|
+
template: !isString(uriOrTemplate) ? uriOrTemplate : undefined
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { classOf } from "@tsed/core";
|
|
2
|
+
import { defineTool } from "../fn/defineTool.js";
|
|
3
|
+
/**
|
|
4
|
+
* Decorator that registers a class method as an MCP tool and binds it to the Ts.ED DI container.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam Input JSON payload schema expected by the tool invocation.
|
|
7
|
+
* @typeParam Output Optional structured response returned by the handler.
|
|
8
|
+
* @param name Optional override for the exported tool name; defaults to the method key.
|
|
9
|
+
* @param options Additional {@link ClassToolProps} such as descriptions and schema overrides.
|
|
10
|
+
* @module platform/mcp
|
|
11
|
+
* @since 8.17.0
|
|
12
|
+
*
|
|
13
|
+
* ### Usage
|
|
14
|
+
* ```ts
|
|
15
|
+
* class MathTools {
|
|
16
|
+
* @Tool()
|
|
17
|
+
* async add({a, b}: {a: number; b: number}) {
|
|
18
|
+
* return {content: [{type: "text", text: String(a + b)}]};
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function Tool(name, options = {}) {
|
|
24
|
+
return (target, propertyKey, _) => {
|
|
25
|
+
defineTool({
|
|
26
|
+
...options,
|
|
27
|
+
name: name || String(propertyKey),
|
|
28
|
+
token: classOf(target),
|
|
29
|
+
propertyKey
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { isArrowFn } from "@tsed/core";
|
|
2
|
+
import { inject, injectable } from "@tsed/di";
|
|
3
|
+
import { JsonEntityStore } from "@tsed/schema";
|
|
4
|
+
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
5
|
+
import { toZod } from "../utils/toZod.js";
|
|
6
|
+
function mapOptions(options) {
|
|
7
|
+
let handler = undefined;
|
|
8
|
+
if ("handler" in options) {
|
|
9
|
+
handler = options.handler;
|
|
10
|
+
}
|
|
11
|
+
if ("propertyKey" in options && options.propertyKey) {
|
|
12
|
+
const { token, propertyKey } = options;
|
|
13
|
+
handler = ((...args) => {
|
|
14
|
+
const instance = inject(options.token);
|
|
15
|
+
return instance[propertyKey](...args);
|
|
16
|
+
});
|
|
17
|
+
const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
|
|
18
|
+
options.description = options.description || methodStore.operation.get("description");
|
|
19
|
+
options.title = options.title || methodStore.schema.get("title");
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
...options,
|
|
23
|
+
argsSchema: toZod(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema),
|
|
24
|
+
handler: handler
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Registers an MCP prompt provider with the Ts.ED injector and returns the associated token.
|
|
29
|
+
*
|
|
30
|
+
* @typeParam Args JSON payload expected by the prompt.
|
|
31
|
+
* @param options Functional or class-based prompt configuration.
|
|
32
|
+
* @returns The DI token representing the registered prompt provider.
|
|
33
|
+
* @module platform/mcp
|
|
34
|
+
* @since 8.17.0
|
|
35
|
+
*
|
|
36
|
+
* ### Usage
|
|
37
|
+
* ```ts
|
|
38
|
+
* definePrompt({
|
|
39
|
+
* name: "weather",
|
|
40
|
+
* description: "Summaries today's forecast",
|
|
41
|
+
* handler: async () => ({content: [{type: "text", text: "Sunny"}]})
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function definePrompt(options) {
|
|
46
|
+
const provider = injectable(Symbol.for(`MCP:PROMPT:${options.name}`))
|
|
47
|
+
.type(MCP_PROVIDER_TYPES.PROMPT)
|
|
48
|
+
.factory(() => {
|
|
49
|
+
return mapOptions(options);
|
|
50
|
+
});
|
|
51
|
+
return provider.token();
|
|
52
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { inject, injectable } from "@tsed/di";
|
|
2
|
+
import { JsonEntityStore } from "@tsed/schema";
|
|
3
|
+
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
4
|
+
function mapOptions(options) {
|
|
5
|
+
let handler;
|
|
6
|
+
if ("propertyKey" in options && options.propertyKey) {
|
|
7
|
+
const { token, propertyKey } = options;
|
|
8
|
+
handler = (...args) => {
|
|
9
|
+
const instance = inject(options.token);
|
|
10
|
+
return instance[propertyKey](...args);
|
|
11
|
+
};
|
|
12
|
+
const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
|
|
13
|
+
options.description = options.description || methodStore.operation.get("description");
|
|
14
|
+
options.title = options.title || methodStore.schema.get("title");
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
handler = options.handler;
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
...options,
|
|
21
|
+
handler
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export function defineResource(options) {
|
|
25
|
+
const provider = injectable(Symbol.for(`MCP:RESOURCE:${options.name}`))
|
|
26
|
+
.type(MCP_PROVIDER_TYPES.RESOURCE)
|
|
27
|
+
.factory(() => {
|
|
28
|
+
return mapOptions(options);
|
|
29
|
+
});
|
|
30
|
+
return provider.token();
|
|
31
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { isArrowFn } from "@tsed/core";
|
|
2
|
+
import { inject, injectable, logger } from "@tsed/di";
|
|
3
|
+
import { JsonEntityStore } from "@tsed/schema";
|
|
4
|
+
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
5
|
+
import { toZod } from "../utils/toZod.js";
|
|
6
|
+
function getOutputSchema(methodStore) {
|
|
7
|
+
const schema = methodStore.operation.getResponseOf(200)?.getMedia("application/json")?.get("schema");
|
|
8
|
+
return schema?.itemSchema();
|
|
9
|
+
}
|
|
10
|
+
function getInputSchema(token, propertyKey) {
|
|
11
|
+
return JsonEntityStore.from(token, propertyKey, 0).schema?.itemSchema();
|
|
12
|
+
}
|
|
13
|
+
function mapOptions(options) {
|
|
14
|
+
let handler;
|
|
15
|
+
if ("propertyKey" in options) {
|
|
16
|
+
const { token, propertyKey } = options;
|
|
17
|
+
handler = (args, extra) => {
|
|
18
|
+
const instance = inject(options.token);
|
|
19
|
+
return instance[options.propertyKey](args, extra);
|
|
20
|
+
};
|
|
21
|
+
const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
|
|
22
|
+
options.description = options.description || methodStore.operation.get("description");
|
|
23
|
+
options.inputSchema = options.inputSchema || getInputSchema(token, propertyKey);
|
|
24
|
+
options.outputSchema = options.outputSchema || getOutputSchema(methodStore);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
handler = options.handler;
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
...options,
|
|
31
|
+
handler
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Registers an MCP tool provider and wraps its handler with error logging plus schema conversion.
|
|
36
|
+
*
|
|
37
|
+
* @typeParam Input JSON payload expected by the tool.
|
|
38
|
+
* @typeParam Output Structured output returned by the handler.
|
|
39
|
+
* @param options Functional or class-based tool configuration.
|
|
40
|
+
* @returns The DI token referencing the registered tool provider.
|
|
41
|
+
* @module platform/mcp
|
|
42
|
+
* @since 8.17.0
|
|
43
|
+
*
|
|
44
|
+
* ### Usage
|
|
45
|
+
* ```ts
|
|
46
|
+
* defineTool({
|
|
47
|
+
* name: "add",
|
|
48
|
+
* handler: async ({a, b}) => ({content: [{type: "text", text: String(a + b)}]})
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export function defineTool(options) {
|
|
53
|
+
const provider = injectable(Symbol.for(`MCP:TOOL:${options.name}`))
|
|
54
|
+
.type(MCP_PROVIDER_TYPES.TOOL)
|
|
55
|
+
.factory(() => {
|
|
56
|
+
let { handler, ...opts } = mapOptions(options);
|
|
57
|
+
return {
|
|
58
|
+
...opts,
|
|
59
|
+
name: opts.name,
|
|
60
|
+
inputSchema: toZod(isArrowFn(opts.inputSchema) ? opts.inputSchema() : opts.inputSchema),
|
|
61
|
+
outputSchema: toZod(opts.outputSchema),
|
|
62
|
+
async handler(args, extra) {
|
|
63
|
+
try {
|
|
64
|
+
return await handler(args, extra);
|
|
65
|
+
}
|
|
66
|
+
catch (er) {
|
|
67
|
+
logger().error({
|
|
68
|
+
event: "MCP_TOOL_ERROR",
|
|
69
|
+
tool: opts.name,
|
|
70
|
+
error_message: er?.message,
|
|
71
|
+
stack: er?.stack
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
content: [],
|
|
75
|
+
structuredContent: {
|
|
76
|
+
code: "E_MCP_TOOL_ERROR",
|
|
77
|
+
message: er?.message
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
return provider.token();
|
|
85
|
+
}
|
package/lib/esm/index.js
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 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { constant, inject, injectable, injector } from "@tsed/di";
|
|
3
|
+
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
4
|
+
function collectTokens(type, configured = []) {
|
|
5
|
+
const tokens = new Set(configured);
|
|
6
|
+
injector()
|
|
7
|
+
.getProviders(type)
|
|
8
|
+
.forEach((provider) => tokens.add(provider.token));
|
|
9
|
+
return [...tokens];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Injectable MCP server instance configured with registered tools, resources, and prompts.
|
|
13
|
+
*
|
|
14
|
+
* @module platform/mcp
|
|
15
|
+
* @since 8.17.0
|
|
16
|
+
*/
|
|
17
|
+
export const MCP_SERVER = injectable(McpServer)
|
|
18
|
+
.factory(() => {
|
|
19
|
+
const settings = constant("mcp", {}) || {};
|
|
20
|
+
const name = settings.name || constant("name") || "tsed-mcp";
|
|
21
|
+
const version = settings.version || constant("version") || "0.0.0";
|
|
22
|
+
const server = new McpServer({
|
|
23
|
+
name,
|
|
24
|
+
version
|
|
25
|
+
});
|
|
26
|
+
const toolTokens = collectTokens(MCP_PROVIDER_TYPES.TOOL, settings.tools);
|
|
27
|
+
toolTokens.forEach((token) => {
|
|
28
|
+
const definition = inject(token);
|
|
29
|
+
const { name, handler, ...opts } = definition;
|
|
30
|
+
server.registerTool(name, opts, handler);
|
|
31
|
+
});
|
|
32
|
+
const resourceTokens = collectTokens(MCP_PROVIDER_TYPES.RESOURCE, settings.resources);
|
|
33
|
+
resourceTokens.forEach((token) => {
|
|
34
|
+
const definition = inject(token);
|
|
35
|
+
const { name, handler, uri, template, ...opts } = definition;
|
|
36
|
+
server.registerResource(name, (uri || template), opts, handler);
|
|
37
|
+
});
|
|
38
|
+
const promptTokens = collectTokens(MCP_PROVIDER_TYPES.PROMPT, settings.prompts);
|
|
39
|
+
promptTokens.forEach((token) => {
|
|
40
|
+
const definition = inject(token);
|
|
41
|
+
const { name, handler, ...opts } = definition;
|
|
42
|
+
server.registerPrompt(name, opts, handler);
|
|
43
|
+
});
|
|
44
|
+
return server;
|
|
45
|
+
})
|
|
46
|
+
.token();
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
2
|
+
import { constant, inject, injectable } from "@tsed/di";
|
|
3
|
+
import { application } from "@tsed/platform-http";
|
|
4
|
+
import { useContextHandler } from "@tsed/platform-router";
|
|
5
|
+
import { MCP_SERVER } from "./McpServerFactory.js";
|
|
6
|
+
/**
|
|
7
|
+
* Platform module that mounts the MCP HTTP endpoint and forwards requests to the configured server instance.
|
|
8
|
+
*
|
|
9
|
+
* @module platform/mcp
|
|
10
|
+
* @since 8.17.0
|
|
11
|
+
*/
|
|
12
|
+
export class PlatformMcpModule {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.settings = constant("mcp", {});
|
|
15
|
+
this.app = application();
|
|
16
|
+
this.server = inject(MCP_SERVER);
|
|
17
|
+
}
|
|
18
|
+
$onRoutesInit() {
|
|
19
|
+
if (this.isEnabled()) {
|
|
20
|
+
const path = this.settings?.path || "/mcp";
|
|
21
|
+
this.app.post(path, useContextHandler(async ($ctx) => this.dispatch($ctx)));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
$logRoutes(routes) {
|
|
25
|
+
return [
|
|
26
|
+
...routes,
|
|
27
|
+
this.isEnabled() && {
|
|
28
|
+
method: "POST",
|
|
29
|
+
name: "PlatformMcpModule.dispatch()",
|
|
30
|
+
url: this.settings?.path || "/mcp"
|
|
31
|
+
}
|
|
32
|
+
].filter(Boolean);
|
|
33
|
+
}
|
|
34
|
+
async dispatch($ctx) {
|
|
35
|
+
const transport = new StreamableHTTPServerTransport({
|
|
36
|
+
sessionIdGenerator: undefined,
|
|
37
|
+
enableJsonResponse: true,
|
|
38
|
+
...this.settings.transportOptions
|
|
39
|
+
});
|
|
40
|
+
const { request, response } = $ctx;
|
|
41
|
+
response.raw?.on("close", () => transport.close());
|
|
42
|
+
try {
|
|
43
|
+
await this.server.connect(transport);
|
|
44
|
+
await transport.handleRequest(request.getReq(), response.getRes(), request.body);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
await transport.close();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
isEnabled() {
|
|
51
|
+
return this.settings?.enabled !== false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
injectable(PlatformMcpModule);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { mkdirSync, writeFileSync } from "fs";
|
|
3
|
+
import { dirname } from "path";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
import { jsonSchemaToZod } from "./jsonSchemaToZod.js";
|
|
6
|
+
import { parseArgs, parseOrReadJSON, readPipe } from "./utils/cliTools.js";
|
|
7
|
+
const params = {
|
|
8
|
+
input: {
|
|
9
|
+
shorthand: "i",
|
|
10
|
+
value: "string",
|
|
11
|
+
required: process.stdin.isTTY && "input is required when no JSON or file path is piped",
|
|
12
|
+
description: "JSON or a source file path. Required if no data is piped."
|
|
13
|
+
},
|
|
14
|
+
output: {
|
|
15
|
+
shorthand: "o",
|
|
16
|
+
value: "string",
|
|
17
|
+
description: "A file path to write to. If not supplied stdout will be used."
|
|
18
|
+
},
|
|
19
|
+
name: {
|
|
20
|
+
shorthand: "n",
|
|
21
|
+
value: "string",
|
|
22
|
+
description: "The name of the schema in the output."
|
|
23
|
+
},
|
|
24
|
+
depth: {
|
|
25
|
+
shorthand: "d",
|
|
26
|
+
value: "number",
|
|
27
|
+
description: "Maximum depth of recursion before falling back to z.any(). Defaults to 0."
|
|
28
|
+
},
|
|
29
|
+
module: {
|
|
30
|
+
shorthand: "m",
|
|
31
|
+
value: ["esm", "cjs", "none"],
|
|
32
|
+
description: "Module syntax; 'esm', 'cjs' or 'none'. Defaults to 'esm'."
|
|
33
|
+
},
|
|
34
|
+
type: {
|
|
35
|
+
shorthand: "t",
|
|
36
|
+
value: "string",
|
|
37
|
+
description: "The name of the (optional) inferred type export."
|
|
38
|
+
},
|
|
39
|
+
noImport: {
|
|
40
|
+
shorthand: "ni",
|
|
41
|
+
description: "Removes the `import { z } from 'zod';` or equivalent from the output."
|
|
42
|
+
},
|
|
43
|
+
withJsdocs: {
|
|
44
|
+
shorthand: "wj",
|
|
45
|
+
description: "Generate jsdocs off of the description property."
|
|
46
|
+
},
|
|
47
|
+
zodVersion: {
|
|
48
|
+
shorthand: "zv",
|
|
49
|
+
value: "number",
|
|
50
|
+
description: "Target Zod version: 3 or 4. Defaults to 4."
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Entry point for the `json-schema-to-zod` CLI that converts JSON Schema files into Zod source code.
|
|
55
|
+
*
|
|
56
|
+
* @module platform/mcp
|
|
57
|
+
* @since 8.17.0
|
|
58
|
+
*/
|
|
59
|
+
async function main() {
|
|
60
|
+
const args = parseArgs(params, process.argv, true);
|
|
61
|
+
const input = args.input || (await readPipe());
|
|
62
|
+
const jsonSchema = parseOrReadJSON(input);
|
|
63
|
+
const zodVersion = (args.zodVersion === 3 ? 3 : 4);
|
|
64
|
+
const zodSchema = jsonSchemaToZod(jsonSchema, {
|
|
65
|
+
name: args.name,
|
|
66
|
+
depth: args.depth,
|
|
67
|
+
module: args.module || "esm",
|
|
68
|
+
noImport: args.noImport,
|
|
69
|
+
type: args.type,
|
|
70
|
+
withJsdocs: args.withJsdocs,
|
|
71
|
+
zodVersion
|
|
72
|
+
});
|
|
73
|
+
if (args.output) {
|
|
74
|
+
mkdirSync(dirname(args.output), { recursive: true });
|
|
75
|
+
writeFileSync(args.output, zodSchema);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
console.log(zodSchema);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
82
|
+
void main();
|
|
83
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
export default jsonSchemaToZod;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { parseSchema } from "./parsers/parseSchema.js";
|
|
2
|
+
import { expandJsdocs } from "./utils/jsdocs.js";
|
|
3
|
+
/**
|
|
4
|
+
* Generates Zod code from a JSON Schema, optionally targeting specific module systems and versions.
|
|
5
|
+
*
|
|
6
|
+
* @param schema JSON Schema definition to translate.
|
|
7
|
+
* @param options Generation options such as module format, naming, and version targets.
|
|
8
|
+
* @returns A string of executable JavaScript/TypeScript that constructs an equivalent Zod schema.
|
|
9
|
+
* @module platform/mcp
|
|
10
|
+
* @since 8.17.0
|
|
11
|
+
*/
|
|
12
|
+
export const jsonSchemaToZod = (schema, { module, name, type, noImport, zodVersion = 4, ...rest } = {}) => {
|
|
13
|
+
if (type && (!name || module !== "esm")) {
|
|
14
|
+
throw new Error("Option `type` requires `name` to be set and `module` to be `esm`");
|
|
15
|
+
}
|
|
16
|
+
let result = parseSchema(schema, {
|
|
17
|
+
module,
|
|
18
|
+
name,
|
|
19
|
+
path: [],
|
|
20
|
+
seen: new Map(),
|
|
21
|
+
zodVersion,
|
|
22
|
+
...rest
|
|
23
|
+
});
|
|
24
|
+
const jsdocs = rest.withJsdocs && typeof schema !== "boolean" && schema.description ? expandJsdocs(schema.description) : "";
|
|
25
|
+
if (module === "cjs") {
|
|
26
|
+
result = `${jsdocs}module.exports = ${name ? `{ ${JSON.stringify(name)}: ${result} }` : result}
|
|
27
|
+
`;
|
|
28
|
+
if (!noImport) {
|
|
29
|
+
result = `${jsdocs}const { z } = require("zod")
|
|
30
|
+
|
|
31
|
+
${result}`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else if (module === "esm") {
|
|
35
|
+
result = `${jsdocs}export ${name ? `const ${name} =` : `default`} ${result}
|
|
36
|
+
`;
|
|
37
|
+
if (!noImport) {
|
|
38
|
+
result = `import { z } from "zod"
|
|
39
|
+
|
|
40
|
+
${result}`;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (name) {
|
|
44
|
+
result = `${jsdocs}const ${name} = ${result}`;
|
|
45
|
+
}
|
|
46
|
+
if (type && name) {
|
|
47
|
+
let typeName = typeof type === "string" ? type : `${name[0].toUpperCase()}${name.substring(1)}`;
|
|
48
|
+
result += `export type ${typeName} = z.infer<typeof ${name}>
|
|
49
|
+
`;
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { half } from "../utils/half.js";
|
|
2
|
+
import { parseSchema } from "./parseSchema.js";
|
|
3
|
+
const originalIndex = Symbol("Original index");
|
|
4
|
+
const ensureOriginalIndex = (arr) => {
|
|
5
|
+
let newArr = [];
|
|
6
|
+
for (let i = 0; i < arr.length; i++) {
|
|
7
|
+
const item = arr[i];
|
|
8
|
+
if (typeof item === "boolean") {
|
|
9
|
+
newArr.push(item ? { [originalIndex]: i } : { [originalIndex]: i, not: {} });
|
|
10
|
+
}
|
|
11
|
+
else if (originalIndex in item) {
|
|
12
|
+
return arr;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
newArr.push({ ...item, [originalIndex]: i });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return newArr;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Parses an `allOf` schema by intersecting all child schemas, preserving evaluation order for accurate metadata.
|
|
22
|
+
*
|
|
23
|
+
* @module platform/mcp
|
|
24
|
+
* @since 8.17.0
|
|
25
|
+
*/
|
|
26
|
+
export function parseAllOf(schema, refs) {
|
|
27
|
+
if (schema.allOf.length === 0) {
|
|
28
|
+
return "z.never()";
|
|
29
|
+
}
|
|
30
|
+
else if (schema.allOf.length === 1) {
|
|
31
|
+
const item = schema.allOf[0];
|
|
32
|
+
return parseSchema(item, {
|
|
33
|
+
...refs,
|
|
34
|
+
path: [...refs.path, "allOf", item[originalIndex]]
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const [left, right] = half(ensureOriginalIndex(schema.allOf));
|
|
39
|
+
return `z.intersection(${parseAllOf({ allOf: left }, refs)}, ${parseAllOf({
|
|
40
|
+
allOf: right
|
|
41
|
+
}, refs)})`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { parseSchema } from "./parseSchema.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parses an `anyOf` node into a Zod union across all nested schemas.
|
|
4
|
+
*
|
|
5
|
+
* @module platform/mcp
|
|
6
|
+
* @since 8.17.0
|
|
7
|
+
*/
|
|
8
|
+
export const parseAnyOf = (schema, refs) => {
|
|
9
|
+
return schema.anyOf.length
|
|
10
|
+
? schema.anyOf.length === 1
|
|
11
|
+
? parseSchema(schema.anyOf[0], {
|
|
12
|
+
...refs,
|
|
13
|
+
path: [...refs.path, "anyOf", 0]
|
|
14
|
+
})
|
|
15
|
+
: `z.union([${schema.anyOf.map((schema, i) => parseSchema(schema, { ...refs, path: [...refs.path, "anyOf", i] })).join(", ")}])`
|
|
16
|
+
: `z.any()`;
|
|
17
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { withMessage } from "../utils/withMessage.js";
|
|
2
|
+
import { parseSchema } from "./parseSchema.js";
|
|
3
|
+
/**
|
|
4
|
+
* Parses array schemas into the appropriate tuple or array expressions with validation hints.
|
|
5
|
+
*
|
|
6
|
+
* @module platform/mcp
|
|
7
|
+
* @since 8.17.0
|
|
8
|
+
*/
|
|
9
|
+
export const parseArray = (schema, refs) => {
|
|
10
|
+
if (Array.isArray(schema.items)) {
|
|
11
|
+
return `z.tuple([${schema.items.map((v, i) => parseSchema(v, { ...refs, path: [...refs.path, "items", i] }))}])`;
|
|
12
|
+
}
|
|
13
|
+
let r = !schema.items
|
|
14
|
+
? "z.array(z.any())"
|
|
15
|
+
: `z.array(${parseSchema(schema.items, {
|
|
16
|
+
...refs,
|
|
17
|
+
path: [...refs.path, "items"]
|
|
18
|
+
})})`;
|
|
19
|
+
r += withMessage(schema, "minItems", ({ json }) => [`.min(${json}`, ", ", ")"]);
|
|
20
|
+
r += withMessage(schema, "maxItems", ({ json }) => [`.max(${json}`, ", ", ")"]);
|
|
21
|
+
return r;
|
|
22
|
+
};
|