@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/readme.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
<p style="text-align: center" align="center">
|
|
2
|
+
<a href="https://tsed.dev" target="_blank"><img src="https://tsed.dev/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<h1>@tsed/platform-mcp</h1>
|
|
7
|
+
|
|
8
|
+
[](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22)
|
|
9
|
+
[](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md)
|
|
10
|
+
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
11
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
12
|
+
[](https://github.com/prettier/prettier)
|
|
13
|
+
[](https://github.com/sponsors/romakita)
|
|
14
|
+
[](https://opencollective.com/tsed)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
<div align="center">
|
|
19
|
+
<a href="https://tsed.dev/">Website</a>
|
|
20
|
+
<span> • </span>
|
|
21
|
+
<a href="https://tsed.dev/getting-started/">Getting started</a>
|
|
22
|
+
<span> • </span>
|
|
23
|
+
<a href="https://slack.tsed.dev">Slack</a>
|
|
24
|
+
<span> • </span>
|
|
25
|
+
<a href="https://twitter.com/TsED_io">Twitter</a>
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
<hr />
|
|
29
|
+
|
|
30
|
+
> Ts.ED integration for the Model Context Protocol (MCP).
|
|
31
|
+
|
|
32
|
+
`@tsed/platform-mcp` lets any Ts.ED HTTP adapter (Express, Fastify, Koa) expose an MCP endpoint and register tools, resources, and prompts through familiar decorators or functional helpers. It ships with:
|
|
33
|
+
|
|
34
|
+
- A `PlatformMcpModule` that mounts a configurable `/mcp` endpoint on top of `@tsed/platform-http`.
|
|
35
|
+
- Functional helpers (`defineTool`, `defineResource`, `definePrompt`) to register MCP handlers via DI tokens.
|
|
36
|
+
- Decorators (`@Tool`, `@Prompt`, `@Resource`) for declarative registration inside services/controllers.
|
|
37
|
+
- Shared utilities (`MCP_SERVER`, transport helpers) that wire `@modelcontextprotocol/sdk` into Ts.ED's dependency injection system.
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
yarn add @tsed/platform-mcp
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Documentation
|
|
46
|
+
|
|
47
|
+
Full usage docs live under `https://tsed.dev/docs/mcp.html` inside the Ts.ED repository.
|
|
48
|
+
|
|
49
|
+
## Contributors
|
|
50
|
+
|
|
51
|
+
<a href="https://github.com/tsedio/tsed/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
52
|
+
|
|
53
|
+
## Backers
|
|
54
|
+
|
|
55
|
+
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
56
|
+
|
|
57
|
+
<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
|
|
58
|
+
|
|
59
|
+
## Sponsors
|
|
60
|
+
|
|
61
|
+
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
The MIT License (MIT)
|
|
66
|
+
|
|
67
|
+
Copyright (c) 2016 - Today Romain Lenzotti
|
|
68
|
+
|
|
69
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
70
|
+
|
|
71
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -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
|
+
} as const;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {DITest, inject, Injectable} from "@tsed/di";
|
|
2
|
+
import {Description, Title} from "@tsed/schema";
|
|
3
|
+
|
|
4
|
+
import {Prompt} from "./prompt.js";
|
|
5
|
+
|
|
6
|
+
@Injectable()
|
|
7
|
+
class TestPrompt {
|
|
8
|
+
@Prompt()
|
|
9
|
+
@Title("Title")
|
|
10
|
+
@Description("Description")
|
|
11
|
+
prompt() {}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe("Prompt", () => {
|
|
15
|
+
beforeEach(() => DITest.create());
|
|
16
|
+
afterEach(() => DITest.reset());
|
|
17
|
+
|
|
18
|
+
it("should returns metadata without explicit given options", () => {
|
|
19
|
+
const tool = inject<any>(Symbol.for(`MCP:PROMPT:prompt`));
|
|
20
|
+
|
|
21
|
+
expect(tool).toEqual({
|
|
22
|
+
propertyKey: "prompt",
|
|
23
|
+
token: TestPrompt,
|
|
24
|
+
name: "prompt",
|
|
25
|
+
title: "Title",
|
|
26
|
+
description: "Description",
|
|
27
|
+
handler: expect.any(Function)
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {classOf} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {definePrompt, type PromptProps} from "../fn/definePrompt.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options accepted by {@link Prompt} when decorating a class method. Matches {@link PromptProps} minus the handler reference.
|
|
7
|
+
*
|
|
8
|
+
* @module platform/mcp
|
|
9
|
+
* @since 8.17.0
|
|
10
|
+
*/
|
|
11
|
+
export type PromptDecoratorOptions = Omit<PromptProps, "handler">;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Declares a Ts.ED class method as an MCP prompt and registers it through {@link definePrompt}.
|
|
15
|
+
*
|
|
16
|
+
* @param options Optional MCP prompt metadata such as name, description, and args schema.
|
|
17
|
+
* @module platform/mcp
|
|
18
|
+
* @since 8.17.0
|
|
19
|
+
*
|
|
20
|
+
* ### Usage
|
|
21
|
+
* ```ts
|
|
22
|
+
* class ConciergePrompts {
|
|
23
|
+
* @Prompt()
|
|
24
|
+
* async hotelWelcome() {
|
|
25
|
+
* return {content: [{type: "text", text: "Welcome to Ts.ED Hotel!"}]};
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function Prompt(options?: PromptDecoratorOptions) {
|
|
31
|
+
return (target: any, propertyKey: string | symbol, _: PropertyDescriptor) => {
|
|
32
|
+
definePrompt({
|
|
33
|
+
...(options as PromptProps),
|
|
34
|
+
name: options?.name || String(propertyKey),
|
|
35
|
+
token: classOf(target),
|
|
36
|
+
propertyKey: propertyKey
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {DITest, inject, Injectable} from "@tsed/di";
|
|
2
|
+
import {ContentType, Description, Title} from "@tsed/schema";
|
|
3
|
+
|
|
4
|
+
import {Resource} from "./resource.js";
|
|
5
|
+
|
|
6
|
+
@Injectable()
|
|
7
|
+
class TestResource {
|
|
8
|
+
@Resource("tsed://init/options")
|
|
9
|
+
@Title("Title")
|
|
10
|
+
@Description("Description")
|
|
11
|
+
@ContentType("application/json")
|
|
12
|
+
resource() {}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe("Resource", () => {
|
|
16
|
+
beforeEach(() => DITest.create());
|
|
17
|
+
afterEach(() => DITest.reset());
|
|
18
|
+
|
|
19
|
+
it("should returns metadata", () => {
|
|
20
|
+
const tool = inject<any>(Symbol.for(`MCP:RESOURCE:resource`));
|
|
21
|
+
|
|
22
|
+
expect(tool).toEqual({
|
|
23
|
+
propertyKey: "resource",
|
|
24
|
+
token: TestResource,
|
|
25
|
+
name: "resource",
|
|
26
|
+
title: "Title",
|
|
27
|
+
template: undefined,
|
|
28
|
+
description: "Description",
|
|
29
|
+
uri: "tsed://init/options",
|
|
30
|
+
handler: expect.any(Function)
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type {ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import {classOf} from "@tsed/core";
|
|
3
|
+
import {isString} from "@tsed/core/utils/isString.js";
|
|
4
|
+
|
|
5
|
+
import {defineResource, type ResourceProps} from "../fn/defineResource.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options accepted by the {@link Resource} decorator beyond the handler binding fields, allowing callers to override metadata.
|
|
9
|
+
*
|
|
10
|
+
* @module platform/mcp
|
|
11
|
+
* @since 8.17.0
|
|
12
|
+
*/
|
|
13
|
+
export type ResourceDecoratorOptions = Omit<ResourceProps, "handler" | "uri" | "template">;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Decorator that turns a class method into an MCP resource backed by either a URI or a full {@link ResourceTemplate}.
|
|
17
|
+
*
|
|
18
|
+
* @param uriOrTemplate Direct URI string or a template describing the resource discovery metadata.
|
|
19
|
+
* @param options Optional overrides for metadata such as name, description, or visibility.
|
|
20
|
+
* @module platform/mcp
|
|
21
|
+
* @since 8.17.0
|
|
22
|
+
*
|
|
23
|
+
* ### Usage
|
|
24
|
+
* ```ts
|
|
25
|
+
* class FileCatalog {
|
|
26
|
+
* @Resource("file:///var/data/catalog")
|
|
27
|
+
* async readCatalog() {
|
|
28
|
+
* return {contents: [{uri: "file:///var/data/catalog/index.json"}]};
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function Resource(uriOrTemplate: string | ResourceTemplate, options?: Partial<ResourceDecoratorOptions>) {
|
|
34
|
+
return (target: any, propertyKey: string | symbol, _: PropertyDescriptor) => {
|
|
35
|
+
defineResource({
|
|
36
|
+
...(options as ResourceProps),
|
|
37
|
+
name: options?.name || String(propertyKey),
|
|
38
|
+
token: classOf(target),
|
|
39
|
+
propertyKey,
|
|
40
|
+
uri: isString(uriOrTemplate) ? uriOrTemplate : undefined,
|
|
41
|
+
template: !isString(uriOrTemplate) ? uriOrTemplate : undefined
|
|
42
|
+
} as any);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {DITest, inject, Injectable} from "@tsed/di";
|
|
2
|
+
import {CollectionOf, Description, Property, Returns} from "@tsed/schema";
|
|
3
|
+
|
|
4
|
+
import {Tool} from "./tool.js";
|
|
5
|
+
|
|
6
|
+
class Model {
|
|
7
|
+
@Property()
|
|
8
|
+
public name: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class Item {
|
|
12
|
+
@Property()
|
|
13
|
+
public name: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
class Output {
|
|
17
|
+
@CollectionOf(Item)
|
|
18
|
+
structuredContent: Item[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@Injectable()
|
|
22
|
+
class TestTool {
|
|
23
|
+
@Tool("test-tool")
|
|
24
|
+
@Description("Test description")
|
|
25
|
+
@Returns(200, Output)
|
|
26
|
+
tool(input: Model) {
|
|
27
|
+
return new Output();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@Tool()
|
|
31
|
+
@Description("Test description")
|
|
32
|
+
@Returns(200, Output)
|
|
33
|
+
tool2(input: Model) {
|
|
34
|
+
return new Output();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("Tool", () => {
|
|
39
|
+
beforeEach(() => DITest.create());
|
|
40
|
+
afterEach(() => DITest.reset());
|
|
41
|
+
|
|
42
|
+
it("should returns metadata with name", () => {
|
|
43
|
+
const tool = inject<any>(Symbol.for(`MCP:TOOL:test-tool`));
|
|
44
|
+
|
|
45
|
+
expect(tool).toEqual({
|
|
46
|
+
propertyKey: "tool",
|
|
47
|
+
token: TestTool,
|
|
48
|
+
name: "test-tool",
|
|
49
|
+
description: "Test description",
|
|
50
|
+
handler: expect.any(Function),
|
|
51
|
+
inputSchema: expect.any(Object),
|
|
52
|
+
outputSchema: expect.any(Object)
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
it("should returns metadata with propertyKey as tool name", () => {
|
|
56
|
+
const tool = inject<any>(Symbol.for(`MCP:TOOL:tool2`));
|
|
57
|
+
|
|
58
|
+
expect(tool).toEqual({
|
|
59
|
+
propertyKey: "tool2",
|
|
60
|
+
token: TestTool,
|
|
61
|
+
name: "tool2",
|
|
62
|
+
description: "Test description",
|
|
63
|
+
handler: expect.any(Function),
|
|
64
|
+
inputSchema: expect.any(Object),
|
|
65
|
+
outputSchema: expect.any(Object)
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {classOf} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {type ClassToolProps, defineTool} from "../fn/defineTool.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Decorator that registers a class method as an MCP tool and binds it to the Ts.ED DI container.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam Input JSON payload schema expected by the tool invocation.
|
|
9
|
+
* @typeParam Output Optional structured response returned by the handler.
|
|
10
|
+
* @param name Optional override for the exported tool name; defaults to the method key.
|
|
11
|
+
* @param options Additional {@link ClassToolProps} such as descriptions and schema overrides.
|
|
12
|
+
* @module platform/mcp
|
|
13
|
+
* @since 8.17.0
|
|
14
|
+
*
|
|
15
|
+
* ### Usage
|
|
16
|
+
* ```ts
|
|
17
|
+
* class MathTools {
|
|
18
|
+
* @Tool()
|
|
19
|
+
* async add({a, b}: {a: number; b: number}) {
|
|
20
|
+
* return {content: [{type: "text", text: String(a + b)}]};
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function Tool<Input = any, Output = any>(name?: string, options: Partial<ClassToolProps<Input, Output>> = {}) {
|
|
26
|
+
return (target: any, propertyKey: string | symbol, _: PropertyDescriptor) => {
|
|
27
|
+
defineTool<Input, Output>({
|
|
28
|
+
...(options as ClassToolProps<Input, Output>),
|
|
29
|
+
name: name || String(propertyKey),
|
|
30
|
+
token: classOf(target),
|
|
31
|
+
propertyKey
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type {McpServer, PromptCallback} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
|
|
3
|
+
import {inject, injectable} from "@tsed/di";
|
|
4
|
+
import {JsonEntityStore, JsonSchema} from "@tsed/schema";
|
|
5
|
+
|
|
6
|
+
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
7
|
+
import {toZod} from "../utils/toZod.js";
|
|
8
|
+
|
|
9
|
+
type BasePromptProps<Args> = Omit<Parameters<McpServer["registerPrompt"]>[1], "argsSchema"> & {
|
|
10
|
+
name: string;
|
|
11
|
+
argsSchema?: Parameters<McpServer["registerPrompt"]>[1]["argsSchema"] | JsonSchema<Args> | (() => JsonSchema<Args>);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type FnPromptProps<Args extends undefined = any> = BasePromptProps<Args> & {
|
|
15
|
+
handler: PromptCallback<Args>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type ClassPromptProps<Args extends undefined = any> = BasePromptProps<Args> & {
|
|
19
|
+
token: Type | AbstractType<any>;
|
|
20
|
+
propertyKey: string | symbol;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Union of prompt configuration accepted by {@link definePrompt}, supporting both functional and class-based handlers.
|
|
25
|
+
*
|
|
26
|
+
* @typeParam Args JSON payload expected by the prompt handler.
|
|
27
|
+
* @module platform/mcp
|
|
28
|
+
* @since 8.17.0
|
|
29
|
+
*/
|
|
30
|
+
export type PromptProps<Args extends undefined = any> = FnPromptProps<Args> | ClassPromptProps<Args>;
|
|
31
|
+
|
|
32
|
+
function mapOptions<Args extends undefined = any>(options: PromptProps<Args>) {
|
|
33
|
+
let handler: PromptCallback<Args> = undefined as any;
|
|
34
|
+
|
|
35
|
+
if ("handler" in options) {
|
|
36
|
+
handler = options.handler;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if ("propertyKey" in options && options.propertyKey) {
|
|
40
|
+
const {token, propertyKey} = options;
|
|
41
|
+
|
|
42
|
+
handler = ((...args: any[]) => {
|
|
43
|
+
const instance = inject(options.token) as any;
|
|
44
|
+
return instance[propertyKey](...args);
|
|
45
|
+
}) as unknown as PromptCallback<Args>;
|
|
46
|
+
|
|
47
|
+
const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
|
|
48
|
+
options.description = options.description || methodStore.operation.get("description");
|
|
49
|
+
options.title = options.title || methodStore.schema.get("title");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
...options,
|
|
54
|
+
argsSchema: toZod(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema),
|
|
55
|
+
handler: handler
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Normalized settings registered in the DI container for an MCP prompt.
|
|
61
|
+
*
|
|
62
|
+
* @module platform/mcp
|
|
63
|
+
* @since 8.17.0
|
|
64
|
+
*/
|
|
65
|
+
export type PromptsSettings = ReturnType<typeof mapOptions>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Registers an MCP prompt provider with the Ts.ED injector and returns the associated token.
|
|
69
|
+
*
|
|
70
|
+
* @typeParam Args JSON payload expected by the prompt.
|
|
71
|
+
* @param options Functional or class-based prompt configuration.
|
|
72
|
+
* @returns The DI token representing the registered prompt provider.
|
|
73
|
+
* @module platform/mcp
|
|
74
|
+
* @since 8.17.0
|
|
75
|
+
*
|
|
76
|
+
* ### Usage
|
|
77
|
+
* ```ts
|
|
78
|
+
* definePrompt({
|
|
79
|
+
* name: "weather",
|
|
80
|
+
* description: "Summaries today's forecast",
|
|
81
|
+
* handler: async () => ({content: [{type: "text", text: "Sunny"}]})
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export function definePrompt<Args extends undefined = any>(options: PromptProps<Args>) {
|
|
86
|
+
const provider = injectable(Symbol.for(`MCP:PROMPT:${options.name}`))
|
|
87
|
+
.type(MCP_PROVIDER_TYPES.PROMPT)
|
|
88
|
+
.factory(() => {
|
|
89
|
+
return mapOptions<Args>(options);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return provider.token();
|
|
93
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type {ReadResourceCallback, ResourceMetadata, ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import {inject, injectable, type TokenProvider} from "@tsed/di";
|
|
3
|
+
import {JsonEntityStore} from "@tsed/schema";
|
|
4
|
+
|
|
5
|
+
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
6
|
+
|
|
7
|
+
type ResourceBaseProps = ResourceMetadata & {
|
|
8
|
+
name: string;
|
|
9
|
+
handler: ReadResourceCallback;
|
|
10
|
+
token?: TokenProvider;
|
|
11
|
+
propertyKey?: string | symbol;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type ResourceReadProps = ResourceBaseProps & {
|
|
15
|
+
uri: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type ResourceTemplateProps = ResourceBaseProps & {
|
|
19
|
+
template: ResourceTemplate;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Configuration accepted by {@link defineResource}, supporting either URI-based or template-based resources.
|
|
24
|
+
*
|
|
25
|
+
* @module platform/mcp
|
|
26
|
+
* @since 8.17.0
|
|
27
|
+
*/
|
|
28
|
+
export type ResourceProps = ResourceReadProps | ResourceTemplateProps;
|
|
29
|
+
|
|
30
|
+
function mapOptions(options: ResourceProps) {
|
|
31
|
+
let handler: ReadResourceCallback;
|
|
32
|
+
|
|
33
|
+
if ("propertyKey" in options && options.propertyKey) {
|
|
34
|
+
const {token, propertyKey} = options;
|
|
35
|
+
|
|
36
|
+
handler = (...args: any[]) => {
|
|
37
|
+
const instance = inject(options.token) as any;
|
|
38
|
+
return instance[propertyKey](...args);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
|
|
42
|
+
options.description = options.description || methodStore.operation.get("description");
|
|
43
|
+
options.title = options.title || methodStore.schema.get("title");
|
|
44
|
+
} else {
|
|
45
|
+
handler = options.handler!;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
...options,
|
|
50
|
+
handler
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Registers an MCP resource provider with the Ts.ED injector and returns its token.
|
|
56
|
+
*
|
|
57
|
+
* @param options Resource metadata describing either a static URI or a template builder.
|
|
58
|
+
* @returns The DI token referencing the registered resource provider.
|
|
59
|
+
* @module platform/mcp
|
|
60
|
+
* @since 8.17.0
|
|
61
|
+
*
|
|
62
|
+
* ### Usage
|
|
63
|
+
* ```ts
|
|
64
|
+
* defineResource({
|
|
65
|
+
* name: "docs",
|
|
66
|
+
* uri: "file:///var/data/docs",
|
|
67
|
+
* handler: async () => ({contents: [{uri: "file:///var/data/docs/readme.md"}]})
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function defineResource(options: ResourceReadProps): TokenProvider;
|
|
72
|
+
export function defineResource(options: ResourceTemplateProps): TokenProvider;
|
|
73
|
+
export function defineResource(options: ResourceProps): TokenProvider {
|
|
74
|
+
const provider = injectable(Symbol.for(`MCP:RESOURCE:${options.name}`))
|
|
75
|
+
.type(MCP_PROVIDER_TYPES.RESOURCE)
|
|
76
|
+
.factory(() => {
|
|
77
|
+
return mapOptions(options);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return provider.token();
|
|
81
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {inject} from "@tsed/di";
|
|
2
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
3
|
+
import {afterEach, beforeEach, describe, expect, it} from "vitest";
|
|
4
|
+
|
|
5
|
+
import {defineTool} from "./defineTool.js";
|
|
6
|
+
|
|
7
|
+
describe("defineTool", () => {
|
|
8
|
+
beforeEach(() => PlatformTest.create());
|
|
9
|
+
afterEach(() => PlatformTest.reset());
|
|
10
|
+
|
|
11
|
+
it("should wrap handler errors with structured MCP payloads", async () => {
|
|
12
|
+
const token = defineTool({
|
|
13
|
+
name: "failing-tool",
|
|
14
|
+
description: "Always throws",
|
|
15
|
+
handler() {
|
|
16
|
+
throw new Error("boom");
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const definition = inject<any>(token);
|
|
21
|
+
|
|
22
|
+
const result = await definition.handler({}, {} as any);
|
|
23
|
+
|
|
24
|
+
expect(result.structuredContent).toEqual({
|
|
25
|
+
code: "E_MCP_TOOL_ERROR",
|
|
26
|
+
message: "boom"
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type {McpServer} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
3
|
+
import type {CallToolResult, ServerNotification, ServerRequest, Tool} from "@modelcontextprotocol/sdk/types.js";
|
|
4
|
+
import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
|
|
5
|
+
import {inject, injectable, logger} from "@tsed/di";
|
|
6
|
+
import {JsonEntityStore, JsonMethodStore, JsonSchema} from "@tsed/schema";
|
|
7
|
+
|
|
8
|
+
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
9
|
+
import {toZod} from "../utils/toZod.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Signature implemented by MCP tool handlers invoked through {@link defineTool}.
|
|
13
|
+
*
|
|
14
|
+
* @typeParam Args JSON payload accepted by the handler.
|
|
15
|
+
* @module platform/mcp
|
|
16
|
+
* @since 8.17.0
|
|
17
|
+
*/
|
|
18
|
+
export type ToolCallback<Args = undefined> = (
|
|
19
|
+
args: Args,
|
|
20
|
+
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
|
|
21
|
+
) => CallToolResult | Promise<CallToolResult>;
|
|
22
|
+
|
|
23
|
+
type ToolConfig = Parameters<McpServer["registerTool"]>[1];
|
|
24
|
+
type BaseToolProps<Input, Output = undefined> = Omit<ToolConfig, "inputSchema" | "outputSchema"> & {
|
|
25
|
+
inputSchema?: JsonSchema<Input> | (() => JsonSchema<Input>) | Tool["inputSchema"];
|
|
26
|
+
outputSchema?: JsonSchema<Output> | Tool["outputSchema"];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output> & {
|
|
30
|
+
name: string;
|
|
31
|
+
handler: ToolCallback<Input>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Configuration accepted when decorating class methods as MCP tools.
|
|
36
|
+
*
|
|
37
|
+
* @module platform/mcp
|
|
38
|
+
* @since 8.17.0
|
|
39
|
+
*/
|
|
40
|
+
export type ClassToolProps<Input, Output = undefined> = BaseToolProps<Input, Output> & {
|
|
41
|
+
name?: string;
|
|
42
|
+
token: Type | AbstractType<any>;
|
|
43
|
+
propertyKey: string | symbol;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Union of functional and class-based tool definitions.
|
|
48
|
+
*
|
|
49
|
+
* @module platform/mcp
|
|
50
|
+
* @since 8.17.0
|
|
51
|
+
*/
|
|
52
|
+
export type ToolProps<Input, Output = undefined> = FnToolProps<Input, Output> | ClassToolProps<Input, Output>;
|
|
53
|
+
|
|
54
|
+
function getOutputSchema<Output>(methodStore: JsonMethodStore): JsonSchema<Output> {
|
|
55
|
+
const schema: JsonSchema = methodStore.operation.getResponseOf(200)?.getMedia("application/json")?.get("schema");
|
|
56
|
+
|
|
57
|
+
return schema?.itemSchema() as JsonSchema<Output>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function getInputSchema<Input>(token: Type<any> | AbstractType<any>, propertyKey: string | symbol): JsonSchema<Input> {
|
|
61
|
+
return JsonEntityStore.from(token, propertyKey, 0).schema?.itemSchema() as JsonSchema<Input>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function mapOptions<Input, Output = undefined>(options: ToolProps<Input, Output>) {
|
|
65
|
+
let handler: ToolCallback<Input>;
|
|
66
|
+
|
|
67
|
+
if ("propertyKey" in options) {
|
|
68
|
+
const {token, propertyKey} = options;
|
|
69
|
+
handler = (args: Input, extra: any) => {
|
|
70
|
+
const instance = inject(options.token) as any;
|
|
71
|
+
return instance[options.propertyKey](args, extra);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
|
|
75
|
+
options.description = options.description || methodStore.operation.get("description");
|
|
76
|
+
options.inputSchema = options.inputSchema || getInputSchema(token, propertyKey);
|
|
77
|
+
options.outputSchema = options.outputSchema || getOutputSchema(methodStore);
|
|
78
|
+
} else {
|
|
79
|
+
handler = options.handler;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
...options,
|
|
84
|
+
handler
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Registers an MCP tool provider and wraps its handler with error logging plus schema conversion.
|
|
90
|
+
*
|
|
91
|
+
* @typeParam Input JSON payload expected by the tool.
|
|
92
|
+
* @typeParam Output Structured output returned by the handler.
|
|
93
|
+
* @param options Functional or class-based tool configuration.
|
|
94
|
+
* @returns The DI token referencing the registered tool provider.
|
|
95
|
+
* @module platform/mcp
|
|
96
|
+
* @since 8.17.0
|
|
97
|
+
*
|
|
98
|
+
* ### Usage
|
|
99
|
+
* ```ts
|
|
100
|
+
* defineTool({
|
|
101
|
+
* name: "add",
|
|
102
|
+
* handler: async ({a, b}) => ({content: [{type: "text", text: String(a + b)}]})
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export function defineTool<Input, Output = undefined>(options: ToolProps<Input, Output>) {
|
|
107
|
+
const provider = injectable(Symbol.for(`MCP:TOOL:${options.name}`))
|
|
108
|
+
.type(MCP_PROVIDER_TYPES.TOOL)
|
|
109
|
+
.factory(() => {
|
|
110
|
+
let {handler, ...opts} = mapOptions(options);
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
...opts,
|
|
114
|
+
name: opts.name,
|
|
115
|
+
inputSchema: toZod(isArrowFn(opts.inputSchema) ? opts.inputSchema() : opts.inputSchema),
|
|
116
|
+
outputSchema: toZod(opts.outputSchema),
|
|
117
|
+
async handler(args: Input, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) {
|
|
118
|
+
try {
|
|
119
|
+
return await handler(args as Input, extra);
|
|
120
|
+
} catch (er: any) {
|
|
121
|
+
logger().error({
|
|
122
|
+
event: "MCP_TOOL_ERROR",
|
|
123
|
+
tool: opts.name,
|
|
124
|
+
error_message: er?.message,
|
|
125
|
+
stack: er?.stack
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
content: [],
|
|
130
|
+
structuredContent: {
|
|
131
|
+
code: "E_MCP_TOOL_ERROR",
|
|
132
|
+
message: er?.message
|
|
133
|
+
}
|
|
134
|
+
} satisfies CallToolResult;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return provider.token();
|
|
141
|
+
}
|