@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.
@@ -1,21 +1,32 @@
1
- import type {McpServer, PromptCallback} from "@modelcontextprotocol/sdk/server/mcp.js";
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 BasePromptProps<Args> = Omit<Parameters<McpServer["registerPrompt"]>[1], "argsSchema"> & {
10
+ type BasePromptConfig = {
11
+ title?: string;
12
+ description?: string;
13
+ };
14
+
15
+ type BasePromptProps<Args> = BasePromptConfig & {
10
16
  name: string;
11
- argsSchema?: Parameters<McpServer["registerPrompt"]>[1]["argsSchema"] | JsonSchema<Args> | (() => JsonSchema<Args>);
17
+ argsSchema?: JsonSchema<Args> | (() => JsonSchema<Args>) | Record<string, unknown>;
12
18
  };
13
19
 
14
- type FnPromptProps<Args extends undefined = any> = BasePromptProps<Args> & {
15
- handler: PromptCallback<Args>;
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 extends undefined = any> = BasePromptProps<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 extends undefined = any> = FnPromptProps<Args> | ClassPromptProps<Args>;
41
+ export type PromptProps<Args = any> = FnPromptProps<Args> | ClassPromptProps<Args>;
31
42
 
32
- function mapOptions<Args extends undefined = any>(options: PromptProps<Args>) {
33
- let handler: PromptCallback<Args> = undefined as any;
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 PromptCallback<Args>;
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 extends undefined = any>(options: PromptProps<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(() => {
@@ -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 ResourceBaseProps = ResourceMetadata & {
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 ResourceReadProps = ResourceBaseProps & {
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 ResourceTemplateProps = ResourceBaseProps & {
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 = ResourceReadProps | ResourceTemplateProps;
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 ("propertyKey" in options && options.propertyKey) {
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: ResourceReadProps): TokenProvider;
72
- export function defineResource(options: ResourceTemplateProps): TokenProvider;
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)
@@ -15,7 +15,7 @@ describe("defineTool", () => {
15
15
  handler() {
16
16
  throw new Error("boom");
17
17
  }
18
- } as any);
18
+ });
19
19
 
20
20
  const definition = inject<any>(token);
21
21
 
@@ -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 ToolConfig = Parameters<McpServer["registerTool"]>[1];
24
- type BaseToolProps<Input, Output = undefined> = Omit<ToolConfig, "inputSchema" | "outputSchema"> & {
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 {ResourceProps} from "../fn/defineResource.js";
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<ResourceProps & {uri?: string; template?: ResourceTemplate}>(token);
47
+ const definition = inject<ResourceSettings & {uri?: string; template?: ResourceTemplate}>(token);
48
48
  const {name, handler, uri, template, ...opts} = definition;
49
- server.registerResource(name, (uri || template)! as any, opts, handler as any);
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;