@tsed/platform-mcp 8.28.0 → 8.28.1
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/decorators/resource.js +13 -4
- package/lib/esm/fn/definePrompt.js +2 -18
- package/lib/esm/fn/defineResource.js +6 -1
- package/lib/esm/fn/defineTool.js +0 -18
- package/lib/esm/services/McpServerFactory.js +8 -2
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/decorators/prompt.d.ts +3 -3
- package/lib/types/decorators/resource.d.ts +2 -2
- package/lib/types/fn/definePrompt.d.ts +24 -29
- package/lib/types/fn/defineResource.d.ts +114 -8
- package/lib/types/fn/defineTool.d.ts +12 -222
- package/package.json +8 -7
- package/src/decorators/prompt.ts +5 -5
- package/src/decorators/resource.ts +20 -8
- package/src/decorators/tool.ts +1 -1
- package/src/fn/definePrompt.ts +27 -12
- package/src/fn/defineResource.ts +33 -10
- package/src/fn/defineTool.spec.ts +1 -1
- package/src/fn/defineTool.ts +13 -6
- package/src/services/McpServerFactory.ts +10 -4
package/src/fn/definePrompt.ts
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
2
|
+
import type {GetPromptResult, ServerNotification, ServerRequest} from "@modelcontextprotocol/sdk/types.js";
|
|
2
3
|
import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
|
|
3
|
-
import {inject, injectable} from "@tsed/di";
|
|
4
|
+
import {inject, injectable, type TokenProvider} from "@tsed/di";
|
|
4
5
|
import {JsonEntityStore, JsonSchema} from "@tsed/schema";
|
|
5
6
|
|
|
6
7
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
7
8
|
import {toZod} from "../utils/toZod.js";
|
|
8
9
|
|
|
9
|
-
type
|
|
10
|
+
type BasePromptConfig = {
|
|
11
|
+
title?: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type BasePromptProps<Args> = BasePromptConfig & {
|
|
10
16
|
name: string;
|
|
11
|
-
argsSchema?:
|
|
17
|
+
argsSchema?: JsonSchema<Args> | (() => JsonSchema<Args>) | Record<string, unknown>;
|
|
12
18
|
};
|
|
13
19
|
|
|
14
|
-
type
|
|
15
|
-
|
|
20
|
+
export type PromptHandler<Args = undefined> = Args extends undefined
|
|
21
|
+
? (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => GetPromptResult | Promise<GetPromptResult>
|
|
22
|
+
: (args: Args, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => GetPromptResult | Promise<GetPromptResult>;
|
|
23
|
+
|
|
24
|
+
export type FnPromptProps<Args = any> = BasePromptProps<Args> & {
|
|
25
|
+
handler: PromptHandler<Args>;
|
|
16
26
|
};
|
|
17
27
|
|
|
18
|
-
type ClassPromptProps<Args
|
|
28
|
+
export type ClassPromptProps<Args = any> = Omit<BasePromptProps<Args>, "name"> & {
|
|
29
|
+
name?: string;
|
|
19
30
|
token: Type | AbstractType<any>;
|
|
20
31
|
propertyKey: string | symbol;
|
|
21
32
|
};
|
|
@@ -27,10 +38,11 @@ type ClassPromptProps<Args extends undefined = any> = BasePromptProps<Args> & {
|
|
|
27
38
|
* @module platform/mcp
|
|
28
39
|
* @since 8.17.0
|
|
29
40
|
*/
|
|
30
|
-
export type PromptProps<Args
|
|
41
|
+
export type PromptProps<Args = any> = FnPromptProps<Args> | ClassPromptProps<Args>;
|
|
31
42
|
|
|
32
|
-
function mapOptions<Args
|
|
33
|
-
let handler:
|
|
43
|
+
function mapOptions<Args = any>(options: PromptProps<Args>) {
|
|
44
|
+
let handler: PromptHandler<Args> = undefined as any;
|
|
45
|
+
const name = options.name || ("propertyKey" in options ? String(options.propertyKey) : undefined);
|
|
34
46
|
|
|
35
47
|
if ("handler" in options) {
|
|
36
48
|
handler = options.handler;
|
|
@@ -42,7 +54,7 @@ function mapOptions<Args extends undefined = any>(options: PromptProps<Args>) {
|
|
|
42
54
|
handler = ((...args: any[]) => {
|
|
43
55
|
const instance = inject(options.token) as any;
|
|
44
56
|
return instance[propertyKey](...args);
|
|
45
|
-
}) as unknown as
|
|
57
|
+
}) as unknown as PromptHandler<Args>;
|
|
46
58
|
|
|
47
59
|
const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
|
|
48
60
|
options.description = options.description || methodStore.operation.get("description");
|
|
@@ -51,6 +63,7 @@ function mapOptions<Args extends undefined = any>(options: PromptProps<Args>) {
|
|
|
51
63
|
|
|
52
64
|
return {
|
|
53
65
|
...options,
|
|
66
|
+
name,
|
|
54
67
|
argsSchema: toZod(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema),
|
|
55
68
|
handler: handler
|
|
56
69
|
};
|
|
@@ -82,7 +95,9 @@ export type PromptsSettings = ReturnType<typeof mapOptions>;
|
|
|
82
95
|
* });
|
|
83
96
|
* ```
|
|
84
97
|
*/
|
|
85
|
-
export function definePrompt<Args
|
|
98
|
+
export function definePrompt<Args = any>(options: FnPromptProps<Args>): TokenProvider;
|
|
99
|
+
export function definePrompt<Args = any>(options: ClassPromptProps<Args>): TokenProvider;
|
|
100
|
+
export function definePrompt<Args = any>(options: PromptProps<Args>) {
|
|
86
101
|
const provider = injectable(Symbol.for(`MCP:PROMPT:${options.name}`))
|
|
87
102
|
.type(MCP_PROVIDER_TYPES.PROMPT)
|
|
88
103
|
.factory(() => {
|
package/src/fn/defineResource.ts
CHANGED
|
@@ -4,18 +4,31 @@ import {JsonEntityStore} from "@tsed/schema";
|
|
|
4
4
|
|
|
5
5
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
6
6
|
|
|
7
|
-
type
|
|
7
|
+
type ResourceMetadataProps = ResourceMetadata & {
|
|
8
8
|
name: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type FnResourceReadProps = ResourceMetadataProps & {
|
|
12
|
+
uri: string;
|
|
13
|
+
handler: ReadResourceCallback;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type FnResourceTemplateProps = ResourceMetadataProps & {
|
|
17
|
+
template: ResourceTemplate;
|
|
9
18
|
handler: ReadResourceCallback;
|
|
10
|
-
token?: TokenProvider;
|
|
11
|
-
propertyKey?: string | symbol;
|
|
12
19
|
};
|
|
13
20
|
|
|
14
|
-
type
|
|
21
|
+
type ClassResourceBaseProps = Omit<ResourceMetadataProps, "name"> & {
|
|
22
|
+
name?: string;
|
|
23
|
+
token: TokenProvider;
|
|
24
|
+
propertyKey: string | symbol;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type ClassResourceReadProps = ClassResourceBaseProps & {
|
|
15
28
|
uri: string;
|
|
16
29
|
};
|
|
17
30
|
|
|
18
|
-
type
|
|
31
|
+
export type ClassResourceTemplateProps = ClassResourceBaseProps & {
|
|
19
32
|
template: ResourceTemplate;
|
|
20
33
|
};
|
|
21
34
|
|
|
@@ -25,12 +38,17 @@ type ResourceTemplateProps = ResourceBaseProps & {
|
|
|
25
38
|
* @module platform/mcp
|
|
26
39
|
* @since 8.17.0
|
|
27
40
|
*/
|
|
28
|
-
export type ResourceProps =
|
|
41
|
+
export type ResourceProps = FnResourceReadProps | FnResourceTemplateProps | ClassResourceReadProps | ClassResourceTemplateProps;
|
|
42
|
+
|
|
43
|
+
function isClassResourceProps(options: ResourceProps): options is ClassResourceReadProps | ClassResourceTemplateProps {
|
|
44
|
+
return "token" in options && "propertyKey" in options;
|
|
45
|
+
}
|
|
29
46
|
|
|
30
47
|
function mapOptions(options: ResourceProps) {
|
|
31
48
|
let handler: ReadResourceCallback;
|
|
49
|
+
const name = options.name || ("propertyKey" in options ? String(options.propertyKey) : undefined);
|
|
32
50
|
|
|
33
|
-
if (
|
|
51
|
+
if (isClassResourceProps(options)) {
|
|
34
52
|
const {token, propertyKey} = options;
|
|
35
53
|
|
|
36
54
|
handler = (...args: any[]) => {
|
|
@@ -42,15 +60,18 @@ function mapOptions(options: ResourceProps) {
|
|
|
42
60
|
options.description = options.description || methodStore.operation.get("description");
|
|
43
61
|
options.title = options.title || methodStore.schema.get("title");
|
|
44
62
|
} else {
|
|
45
|
-
handler = options.handler
|
|
63
|
+
handler = options.handler;
|
|
46
64
|
}
|
|
47
65
|
|
|
48
66
|
return {
|
|
49
67
|
...options,
|
|
68
|
+
name,
|
|
50
69
|
handler
|
|
51
70
|
};
|
|
52
71
|
}
|
|
53
72
|
|
|
73
|
+
export type ResourceSettings = ReturnType<typeof mapOptions>;
|
|
74
|
+
|
|
54
75
|
/**
|
|
55
76
|
* Registers an MCP resource provider with the Ts.ED injector and returns its token.
|
|
56
77
|
*
|
|
@@ -68,8 +89,10 @@ function mapOptions(options: ResourceProps) {
|
|
|
68
89
|
* });
|
|
69
90
|
* ```
|
|
70
91
|
*/
|
|
71
|
-
export function defineResource(options:
|
|
72
|
-
export function defineResource(options:
|
|
92
|
+
export function defineResource(options: FnResourceReadProps): TokenProvider;
|
|
93
|
+
export function defineResource(options: FnResourceTemplateProps): TokenProvider;
|
|
94
|
+
export function defineResource(options: ClassResourceReadProps): TokenProvider;
|
|
95
|
+
export function defineResource(options: ClassResourceTemplateProps): TokenProvider;
|
|
73
96
|
export function defineResource(options: ResourceProps): TokenProvider {
|
|
74
97
|
const provider = injectable(Symbol.for(`MCP:RESOURCE:${options.name}`))
|
|
75
98
|
.type(MCP_PROVIDER_TYPES.RESOURCE)
|
package/src/fn/defineTool.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type {McpServer} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
1
|
import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
3
|
-
import type {CallToolResult, ServerNotification, ServerRequest, Tool} from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
import type {CallToolResult, ServerNotification, ServerRequest, Tool, ToolAnnotations} from "@modelcontextprotocol/sdk/types.js";
|
|
4
3
|
import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
|
|
5
|
-
import {inject, injectable, logger} from "@tsed/di";
|
|
4
|
+
import {inject, injectable, logger, type TokenProvider} from "@tsed/di";
|
|
6
5
|
import {JsonEntityStore, JsonMethodStore, JsonSchema} from "@tsed/schema";
|
|
7
6
|
|
|
8
7
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
@@ -20,13 +19,19 @@ export type ToolCallback<Args = undefined> = (
|
|
|
20
19
|
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
|
|
21
20
|
) => CallToolResult | Promise<CallToolResult>;
|
|
22
21
|
|
|
23
|
-
type
|
|
24
|
-
|
|
22
|
+
type BaseToolConfig = {
|
|
23
|
+
title?: string;
|
|
24
|
+
description?: string;
|
|
25
|
+
annotations?: ToolAnnotations;
|
|
26
|
+
_meta?: Record<string, unknown>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type BaseToolProps<Input, Output = undefined> = BaseToolConfig & {
|
|
25
30
|
inputSchema?: JsonSchema<Input> | (() => JsonSchema<Input>) | Tool["inputSchema"];
|
|
26
31
|
outputSchema?: JsonSchema<Output> | Tool["outputSchema"];
|
|
27
32
|
};
|
|
28
33
|
|
|
29
|
-
type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output> & {
|
|
34
|
+
export type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output> & {
|
|
30
35
|
name: string;
|
|
31
36
|
handler: ToolCallback<Input>;
|
|
32
37
|
};
|
|
@@ -103,6 +108,8 @@ function mapOptions<Input, Output = undefined>(options: ToolProps<Input, Output>
|
|
|
103
108
|
* });
|
|
104
109
|
* ```
|
|
105
110
|
*/
|
|
111
|
+
export function defineTool<Input, Output = undefined>(options: FnToolProps<Input, Output>): TokenProvider;
|
|
112
|
+
export function defineTool<Input, Output = undefined>(options: ClassToolProps<Input, Output>): TokenProvider;
|
|
106
113
|
export function defineTool<Input, Output = undefined>(options: ToolProps<Input, Output>) {
|
|
107
114
|
const provider = injectable(Symbol.for(`MCP:TOOL:${options.name}`))
|
|
108
115
|
.type(MCP_PROVIDER_TYPES.TOOL)
|
|
@@ -4,7 +4,7 @@ import {constant, inject, injectable, injector, type TokenProvider} from "@tsed/
|
|
|
4
4
|
|
|
5
5
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
6
6
|
import type {PromptsSettings} from "../fn/definePrompt.js";
|
|
7
|
-
import type {
|
|
7
|
+
import type {ResourceSettings} from "../fn/defineResource.js";
|
|
8
8
|
import type {ToolProps} from "../fn/defineTool.js";
|
|
9
9
|
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
10
10
|
|
|
@@ -44,16 +44,22 @@ export const MCP_SERVER = injectable(McpServer)
|
|
|
44
44
|
|
|
45
45
|
const resourceTokens = collectTokens(MCP_PROVIDER_TYPES.RESOURCE, settings.resources);
|
|
46
46
|
resourceTokens.forEach((token) => {
|
|
47
|
-
const definition = inject<
|
|
47
|
+
const definition = inject<ResourceSettings & {uri?: string; template?: ResourceTemplate}>(token);
|
|
48
48
|
const {name, handler, uri, template, ...opts} = definition;
|
|
49
|
-
|
|
49
|
+
const resourceName = name || String(token);
|
|
50
|
+
|
|
51
|
+
if (uri) {
|
|
52
|
+
server.registerResource(resourceName, uri, opts, handler as any);
|
|
53
|
+
} else {
|
|
54
|
+
server.registerResource(resourceName, template as ResourceTemplate, opts, handler as any);
|
|
55
|
+
}
|
|
50
56
|
});
|
|
51
57
|
|
|
52
58
|
const promptTokens = collectTokens(MCP_PROVIDER_TYPES.PROMPT, settings.prompts);
|
|
53
59
|
promptTokens.forEach((token) => {
|
|
54
60
|
const definition = inject<PromptsSettings>(token);
|
|
55
61
|
const {name, handler, ...opts} = definition;
|
|
56
|
-
server.registerPrompt(name, opts, handler as any);
|
|
62
|
+
server.registerPrompt(name || String(token), opts as any, handler as any);
|
|
57
63
|
});
|
|
58
64
|
|
|
59
65
|
return server;
|