@tsed/platform-mcp 8.35.0 → 8.37.0-rc.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.
Files changed (45) hide show
  1. package/lib/esm/fn/definePrompt.js +5 -2
  2. package/lib/esm/fn/defineResource.js +17 -13
  3. package/lib/esm/fn/defineTool.js +21 -15
  4. package/lib/esm/index.js +3 -1
  5. package/lib/esm/services/McpServerFactory.js +1 -1
  6. package/lib/esm/services/PlatformMcpModule.js +2 -2
  7. package/lib/esm/utils/asResourceResponse.js +26 -0
  8. package/lib/esm/utils/asToolResponse.js +29 -0
  9. package/lib/esm/utils/fromJsonSchema.js +17 -0
  10. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  11. package/lib/types/decorators/resource.d.ts +1 -1
  12. package/lib/types/fn/definePrompt.d.ts +4 -5
  13. package/lib/types/fn/defineResource.d.ts +24 -23
  14. package/lib/types/fn/defineTool.d.ts +9 -10
  15. package/lib/types/index.d.ts +3 -1
  16. package/lib/types/interfaces/PlatformMcpSettings.d.ts +2 -3
  17. package/lib/types/services/McpServerFactory.d.ts +1 -1
  18. package/lib/types/services/PlatformMcpModule.d.ts +1 -1
  19. package/lib/types/utils/asResourceResponse.d.ts +4 -0
  20. package/lib/types/utils/asToolResponse.d.ts +14 -0
  21. package/lib/types/utils/fromJsonSchema.d.ts +10 -0
  22. package/package.json +15 -13
  23. package/readme.md +1 -1
  24. package/src/decorators/resource.ts +1 -1
  25. package/src/fn/definePrompt.ts +10 -8
  26. package/src/fn/defineResource.spec.ts +54 -29
  27. package/src/fn/defineResource.ts +26 -12
  28. package/src/fn/defineTool.spec.ts +49 -8
  29. package/src/fn/defineTool.ts +42 -30
  30. package/src/index.ts +3 -1
  31. package/src/interfaces/PlatformMcpSettings.ts +2 -3
  32. package/src/services/McpServerFactory.ts +1 -2
  33. package/src/services/PlatformMcpModule.spec.ts +2 -2
  34. package/src/services/PlatformMcpModule.ts +3 -3
  35. package/src/utils/asResourceResponse.spec.ts +32 -0
  36. package/src/utils/asResourceResponse.ts +36 -0
  37. package/src/utils/asToolResponse.spec.ts +30 -0
  38. package/src/utils/asToolResponse.ts +49 -0
  39. package/src/utils/fromJsonSchema.spec.ts +68 -0
  40. package/src/utils/fromJsonSchema.ts +20 -0
  41. package/vitest.config.mts +0 -1
  42. package/lib/esm/utils/toZod.js +0 -19
  43. package/lib/types/utils/toZod.d.ts +0 -12
  44. package/src/utils/toZod.spec.ts +0 -40
  45. package/src/utils/toZod.ts +0 -22
@@ -1,5 +1,5 @@
1
1
  import { type ClassResourceReadProps } from "../fn/defineResource.js";
2
- import type { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type { ResourceTemplate } from "@modelcontextprotocol/server";
3
3
  /**
4
4
  * Options accepted by the {@link Resource} decorator beyond the handler binding fields, allowing callers to override metadata.
5
5
  *
@@ -1,8 +1,7 @@
1
1
  import { type AbstractType, type Type } from "@tsed/core";
2
- import type { GetPromptResult, ServerNotification, ServerRequest } from "@modelcontextprotocol/sdk/types.js";
2
+ import type { GetPromptResult, ServerContext } from "@modelcontextprotocol/server";
3
3
  import { JsonSchema } from "@tsed/schema";
4
4
  import { type TokenProvider } from "@tsed/di";
5
- import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
6
5
  type BasePromptConfig = {
7
6
  title?: string;
8
7
  description?: string;
@@ -11,7 +10,7 @@ type BasePromptProps<Args> = BasePromptConfig & {
11
10
  name: string;
12
11
  argsSchema?: JsonSchema<Args> | (() => JsonSchema<Args>) | Record<string, unknown>;
13
12
  };
14
- export type PromptHandler<Args = undefined> = Args extends undefined ? (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => GetPromptResult | Promise<GetPromptResult> : (args: Args, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => GetPromptResult | Promise<GetPromptResult>;
13
+ export type PromptHandler<Args = undefined> = Args extends undefined ? (ctx: ServerContext) => GetPromptResult | Promise<GetPromptResult> : (args: Args, ctx: ServerContext) => GetPromptResult | Promise<GetPromptResult>;
15
14
  export type FnPromptProps<Args = any> = BasePromptProps<Args> & {
16
15
  handler: PromptHandler<Args>;
17
16
  };
@@ -30,13 +29,13 @@ export type ClassPromptProps<Args = any> = Omit<BasePromptProps<Args>, "name"> &
30
29
  export type PromptProps<Args = any> = FnPromptProps<Args> | ClassPromptProps<Args>;
31
30
  declare function mapOptions<Args = any>(options: PromptProps<Args>): {
32
31
  name: string | undefined;
33
- argsSchema: import("zod").ZodObject<import("zod/v4/core").$ZodLooseShape, import("zod/v4/core").$strip> | undefined;
32
+ argsSchema: import("@modelcontextprotocol/server").StandardSchemaWithJSON<unknown, unknown> | undefined;
34
33
  handler: PromptHandler<Args>;
35
34
  title?: string;
36
35
  description?: string;
37
36
  } | {
38
37
  name: string | undefined;
39
- argsSchema: import("zod").ZodObject<import("zod/v4/core").$ZodLooseShape, import("zod/v4/core").$strip> | undefined;
38
+ argsSchema: import("@modelcontextprotocol/server").StandardSchemaWithJSON<unknown, unknown> | undefined;
40
39
  handler: PromptHandler<Args>;
41
40
  description?: string | undefined;
42
41
  title?: string | undefined;
@@ -1,15 +1,16 @@
1
- import type { ReadResourceCallback, ResourceMetadata, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
1
+ import type { ReadResourceCallback, ReadResourceResult, ResourceMetadata, ResourceTemplate } from "@modelcontextprotocol/server";
2
2
  import { type TokenProvider } from "@tsed/di";
3
3
  type ResourceMetadataProps = ResourceMetadata & {
4
4
  name: string;
5
5
  };
6
+ export type ResourceCallback = (...args: Parameters<ReadResourceCallback>) => ReadResourceResult | Record<string, unknown> | Promise<ReadResourceResult | Record<string, unknown>>;
6
7
  export type FnResourceReadProps = ResourceMetadataProps & {
7
8
  uri: string;
8
- handler: ReadResourceCallback;
9
+ handler: ResourceCallback;
9
10
  };
10
11
  export type FnResourceTemplateProps = ResourceMetadataProps & {
11
12
  template: ResourceTemplate;
12
- handler: ReadResourceCallback;
13
+ handler: ResourceCallback;
13
14
  };
14
15
  type ClassResourceBaseProps = Omit<ResourceMetadataProps, "name"> & {
15
16
  name?: string;
@@ -31,93 +32,93 @@ export type ClassResourceTemplateProps = ClassResourceBaseProps & {
31
32
  export type ResourceProps = FnResourceReadProps | FnResourceTemplateProps | ClassResourceReadProps | ClassResourceTemplateProps;
32
33
  declare function mapOptions(options: ResourceProps): {
33
34
  name: string | undefined;
34
- handler: ReadResourceCallback;
35
+ handler: ResourceCallback;
36
+ description?: string | undefined;
37
+ title?: string | undefined;
35
38
  _meta?: {
36
39
  [x: string]: unknown;
37
40
  } | undefined;
38
- description?: string | undefined;
39
41
  annotations?: {
40
42
  audience?: ("user" | "assistant")[] | undefined;
41
43
  priority?: number | undefined;
42
44
  lastModified?: string | undefined;
43
45
  } | undefined;
46
+ mimeType?: string | undefined;
47
+ size?: number | undefined;
44
48
  icons?: {
45
49
  src: string;
46
50
  mimeType?: string | undefined;
47
51
  sizes?: string[] | undefined;
48
52
  theme?: "light" | "dark" | undefined;
49
53
  }[] | undefined;
50
- title?: string | undefined;
51
- mimeType?: string | undefined;
52
- size?: number | undefined;
53
54
  uri: string;
54
55
  } | {
55
56
  name: string | undefined;
56
- handler: ReadResourceCallback;
57
+ handler: ResourceCallback;
58
+ description?: string | undefined;
59
+ title?: string | undefined;
57
60
  _meta?: {
58
61
  [x: string]: unknown;
59
62
  } | undefined;
60
- description?: string | undefined;
61
63
  annotations?: {
62
64
  audience?: ("user" | "assistant")[] | undefined;
63
65
  priority?: number | undefined;
64
66
  lastModified?: string | undefined;
65
67
  } | undefined;
68
+ mimeType?: string | undefined;
69
+ size?: number | undefined;
66
70
  icons?: {
67
71
  src: string;
68
72
  mimeType?: string | undefined;
69
73
  sizes?: string[] | undefined;
70
74
  theme?: "light" | "dark" | undefined;
71
75
  }[] | undefined;
72
- title?: string | undefined;
73
- mimeType?: string | undefined;
74
- size?: number | undefined;
75
76
  template: ResourceTemplate;
76
77
  } | {
77
78
  name: string | undefined;
78
- handler: ReadResourceCallback;
79
+ handler: ResourceCallback;
80
+ description?: string | undefined;
81
+ title?: string | undefined;
79
82
  _meta?: {
80
83
  [x: string]: unknown;
81
84
  } | undefined;
82
- description?: string | undefined;
83
85
  annotations?: {
84
86
  audience?: ("user" | "assistant")[] | undefined;
85
87
  priority?: number | undefined;
86
88
  lastModified?: string | undefined;
87
89
  } | undefined;
90
+ mimeType?: string | undefined;
91
+ size?: number | undefined;
88
92
  icons?: {
89
93
  src: string;
90
94
  mimeType?: string | undefined;
91
95
  sizes?: string[] | undefined;
92
96
  theme?: "light" | "dark" | undefined;
93
97
  }[] | undefined;
94
- title?: string | undefined;
95
- mimeType?: string | undefined;
96
- size?: number | undefined;
97
98
  token: TokenProvider;
98
99
  propertyKey: string | symbol;
99
100
  uri: string;
100
101
  } | {
101
102
  name: string | undefined;
102
- handler: ReadResourceCallback;
103
+ handler: ResourceCallback;
104
+ description?: string | undefined;
105
+ title?: string | undefined;
103
106
  _meta?: {
104
107
  [x: string]: unknown;
105
108
  } | undefined;
106
- description?: string | undefined;
107
109
  annotations?: {
108
110
  audience?: ("user" | "assistant")[] | undefined;
109
111
  priority?: number | undefined;
110
112
  lastModified?: string | undefined;
111
113
  } | undefined;
114
+ mimeType?: string | undefined;
115
+ size?: number | undefined;
112
116
  icons?: {
113
117
  src: string;
114
118
  mimeType?: string | undefined;
115
119
  sizes?: string[] | undefined;
116
120
  theme?: "light" | "dark" | undefined;
117
121
  }[] | undefined;
118
- title?: string | undefined;
119
- mimeType?: string | undefined;
120
- size?: number | undefined;
121
122
  token: TokenProvider;
122
123
  propertyKey: string | symbol;
123
124
  template: ResourceTemplate;
@@ -1,8 +1,7 @@
1
1
  import { type AbstractType, type Type } from "@tsed/core";
2
- import type { CallToolResult, ServerNotification, ServerRequest, Tool, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
2
+ import type { CallToolResult, ServerContext, Tool, ToolAnnotations } from "@modelcontextprotocol/server";
3
3
  import { JsonSchema } from "@tsed/schema";
4
4
  import { type TokenProvider } from "@tsed/di";
5
- import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
6
5
  /**
7
6
  * Signature implemented by MCP tool handlers invoked through {@link defineTool}.
8
7
  *
@@ -10,20 +9,20 @@ import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/proto
10
9
  * @module platform/mcp
11
10
  * @since 8.17.0
12
11
  */
13
- export type ToolCallback<Args = undefined> = (args: Args, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult | Promise<CallToolResult>;
12
+ export type ToolCallback<Args = undefined, Output = unknown> = (args: Args, ctx: ServerContext) => CallToolResult | Promise<CallToolResult> | Output | Promise<Output>;
14
13
  type BaseToolConfig = {
15
14
  title?: string;
16
15
  description?: string;
17
16
  annotations?: ToolAnnotations;
18
17
  _meta?: Record<string, unknown>;
19
18
  };
20
- type BaseToolProps<Input, Output = undefined> = BaseToolConfig & {
19
+ type BaseToolProps<Input, Output = unknown> = BaseToolConfig & {
21
20
  inputSchema?: JsonSchema<Input> | (() => JsonSchema<Input>) | Tool["inputSchema"];
22
21
  outputSchema?: JsonSchema<Output> | Tool["outputSchema"];
23
22
  };
24
- export type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output> & {
23
+ export type FnToolProps<Input, Output = unknown> = BaseToolProps<Input, Output> & {
25
24
  name: string;
26
- handler: ToolCallback<Input>;
25
+ handler: ToolCallback<Input, Output>;
27
26
  };
28
27
  /**
29
28
  * Configuration accepted when decorating class methods as MCP tools.
@@ -31,7 +30,7 @@ export type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output
31
30
  * @module platform/mcp
32
31
  * @since 8.17.0
33
32
  */
34
- export type ClassToolProps<Input, Output = undefined> = BaseToolProps<Input, Output> & {
33
+ export type ClassToolProps<Input, Output = unknown> = BaseToolProps<Input, Output> & {
35
34
  name?: string;
36
35
  token: Type | AbstractType<any>;
37
36
  propertyKey: string | symbol;
@@ -42,7 +41,7 @@ export type ClassToolProps<Input, Output = undefined> = BaseToolProps<Input, Out
42
41
  * @module platform/mcp
43
42
  * @since 8.17.0
44
43
  */
45
- export type ToolProps<Input, Output = undefined> = FnToolProps<Input, Output> | ClassToolProps<Input, Output>;
44
+ export type ToolProps<Input, Output = unknown> = FnToolProps<Input, Output> | ClassToolProps<Input, Output>;
46
45
  /**
47
46
  * Registers an MCP tool provider and wraps its handler with error logging plus schema conversion.
48
47
  *
@@ -61,6 +60,6 @@ export type ToolProps<Input, Output = undefined> = FnToolProps<Input, Output> |
61
60
  * });
62
61
  * ```
63
62
  */
64
- export declare function defineTool<Input, Output = undefined>(options: FnToolProps<Input, Output>): TokenProvider;
65
- export declare function defineTool<Input, Output = undefined>(options: ClassToolProps<Input, Output>): TokenProvider;
63
+ export declare function defineTool<Input, Output = unknown>(options: FnToolProps<Input, Output>): TokenProvider;
64
+ export declare function defineTool<Input, Output = unknown>(options: ClassToolProps<Input, Output>): TokenProvider;
66
65
  export {};
@@ -11,4 +11,6 @@ export * from "./fn/defineTool.js";
11
11
  export * from "./interfaces/PlatformMcpSettings.js";
12
12
  export * from "./services/McpServerFactory.js";
13
13
  export * from "./services/PlatformMcpModule.js";
14
- export * from "./utils/toZod.js";
14
+ export * from "./utils/asResourceResponse.js";
15
+ export * from "./utils/asToolResponse.js";
16
+ export * from "./utils/fromJsonSchema.js";
@@ -1,6 +1,5 @@
1
- import type { Implementation } from "@modelcontextprotocol/sdk/types.js";
2
- import type { ServerOptions } from "@modelcontextprotocol/sdk/server/index.js";
3
- import type { StreamableHTTPServerTransportOptions } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
1
+ import type { Implementation, ServerOptions } from "@modelcontextprotocol/server";
2
+ import type { StreamableHTTPServerTransportOptions } from "@modelcontextprotocol/node";
4
3
  import type { TokenProvider } from "@tsed/di";
5
4
  /**
6
5
  * Configuration fragment accepted under `configuration.mcp` to wire the Platform MCP module.
@@ -1,4 +1,4 @@
1
- import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
1
+ import { McpServer } from "@modelcontextprotocol/server";
2
2
  /**
3
3
  * Injectable MCP server instance configured with registered tools, resources, and prompts.
4
4
  *
@@ -1,5 +1,5 @@
1
1
  import { type OnRoutesInit, PlatformContext, type PlatformRouteDetails } from "@tsed/platform-http";
2
- import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import { type McpServer } from "@modelcontextprotocol/server";
3
3
  import type { PlatformMcpSettings } from "../interfaces/PlatformMcpSettings.js";
4
4
  /**
5
5
  * Platform module that mounts the MCP HTTP endpoint and forwards requests to the configured server instance.
@@ -0,0 +1,4 @@
1
+ import type { ReadResourceResult } from "@modelcontextprotocol/server";
2
+ export declare function asResourceResponse(uri: string, payload: Record<string, unknown> | ReadResourceResult, opts?: {
3
+ isError?: boolean;
4
+ }): ReadResourceResult;
@@ -0,0 +1,14 @@
1
+ import type { CallToolResult } from "@modelcontextprotocol/server";
2
+ export interface StructuredResponse<T extends {
3
+ [x: string]: unknown;
4
+ } = {
5
+ [x: string]: unknown;
6
+ }> extends CallToolResult {
7
+ structuredContent?: T | undefined;
8
+ isError?: true;
9
+ }
10
+ export declare function asToolResponse<T extends {
11
+ [x: string]: unknown;
12
+ }>(payload: T | StructuredResponse, opts?: {
13
+ isError?: true;
14
+ }): StructuredResponse<T>;
@@ -0,0 +1,10 @@
1
+ import { type JsonSchemaOptions } from "@tsed/schema";
2
+ /**
3
+ * Converts a Ts.ED schema into the MCP SDK v2 Standard Schema adapter.
4
+ *
5
+ * @param schema Ts.ED or plain JSON Schema document.
6
+ * @param opts Compilation options applied to Ts.ED schemas.
7
+ * @module platform/mcp
8
+ * @since 8.17.0
9
+ */
10
+ export declare function fromJsonSchema<T = unknown>(schema: unknown, opts?: JsonSchemaOptions): import("@modelcontextprotocol/server").StandardSchemaWithJSON<T, T> | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/platform-mcp",
3
- "version": "8.35.0",
3
+ "version": "8.37.0-rc.1",
4
4
  "description": "Model Context Protocol integration for Ts.ED HTTP adapters.",
5
5
  "source": "./src/index.ts",
6
6
  "type": "module",
@@ -24,21 +24,20 @@
24
24
  "test:ci": "vitest run --coverage.thresholds.autoUpdate=true"
25
25
  },
26
26
  "dependencies": {
27
- "@modelcontextprotocol/sdk": ">=1.29.0",
27
+ "@modelcontextprotocol/node": "^2.0.0-beta.4",
28
+ "@modelcontextprotocol/server": "^2.0.0-beta.4",
28
29
  "change-case": ">=5.4.4",
29
- "json-schema-to-zod": ">=2.8.1",
30
30
  "tslib": ">=2.7.0",
31
- "uuid": ">=11.0.2",
32
- "zod": ">=4.0.0"
31
+ "uuid": ">=11.0.2"
33
32
  },
34
33
  "devDependencies": {
35
- "@tsed/barrels": "8.35.0",
36
- "@tsed/core": "8.35.0",
37
- "@tsed/di": "8.35.0",
38
- "@tsed/json-mapper": "8.35.0",
39
- "@tsed/platform-http": "8.35.0",
40
- "@tsed/schema": "8.35.0",
41
- "@tsed/typescript": "8.35.0",
34
+ "@tsed/barrels": "8.37.0-rc.1",
35
+ "@tsed/core": "8.37.0-rc.1",
36
+ "@tsed/di": "8.37.0-rc.1",
37
+ "@tsed/json-mapper": "8.37.0-rc.1",
38
+ "@tsed/platform-http": "8.37.0-rc.1",
39
+ "@tsed/schema": "8.37.0-rc.1",
40
+ "@tsed/typescript": "8.37.0-rc.1",
42
41
  "typescript": "6.0.2",
43
42
  "vitest": "4.1.4"
44
43
  },
@@ -72,5 +71,8 @@
72
71
  },
73
72
  "homepage": "https://github.com/tsedio/tsed/tree/production/packages/platform/platform-mcp",
74
73
  "author": "Romain Lenzotti",
75
- "license": "MIT"
74
+ "license": "MIT",
75
+ "publishConfig": {
76
+ "tag": "rc"
77
+ }
76
78
  }
package/readme.md CHANGED
@@ -34,7 +34,7 @@
34
34
  - A `PlatformMcpModule` that mounts a configurable `/mcp` endpoint on top of `@tsed/platform-http`.
35
35
  - Functional helpers (`defineTool`, `defineResource`, `definePrompt`) to register MCP handlers via DI tokens.
36
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.
37
+ - Shared utilities (`MCP_SERVER`, transport helpers) that wire the MCP SDK v2 server packages into Ts.ED's dependency injection system.
38
38
 
39
39
  ## Installation
40
40
 
@@ -1,5 +1,5 @@
1
1
  import {type ClassResourceReadProps, type ClassResourceTemplateProps, defineResource} from "../fn/defineResource.js";
2
- import type {ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type {ResourceTemplate} from "@modelcontextprotocol/server";
3
3
  import {classOf} from "@tsed/core";
4
4
  import {isString} from "@tsed/core/utils/isString.js";
5
5
 
@@ -1,11 +1,10 @@
1
- import {type AbstractType, type Type, isArrowFn} from "@tsed/core";
2
- import type {GetPromptResult, ServerNotification, ServerRequest} from "@modelcontextprotocol/sdk/types.js";
1
+ import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
2
+ import type {GetPromptResult, ServerContext} from "@modelcontextprotocol/server";
3
3
  import {JsonSchema, s} from "@tsed/schema";
4
- import {type TokenProvider, context, inject, injectable, logger} from "@tsed/di";
4
+ import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
5
5
  import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
6
- import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
7
6
  import {constantCase} from "change-case";
8
- import {toZod} from "../utils/toZod.js";
7
+ import {fromJsonSchema} from "../utils/fromJsonSchema.js";
9
8
 
10
9
  type BasePromptConfig = {
11
10
  title?: string;
@@ -18,8 +17,8 @@ type BasePromptProps<Args> = BasePromptConfig & {
18
17
  };
19
18
 
20
19
  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>;
20
+ ? (ctx: ServerContext) => GetPromptResult | Promise<GetPromptResult>
21
+ : (args: Args, ctx: ServerContext) => GetPromptResult | Promise<GetPromptResult>;
23
22
 
24
23
  export type FnPromptProps<Args = any> = BasePromptProps<Args> & {
25
24
  handler: PromptHandler<Args>;
@@ -64,7 +63,10 @@ function mapOptions<Args = any>(options: PromptProps<Args>) {
64
63
  return {
65
64
  ...options,
66
65
  name,
67
- argsSchema: toZod(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema),
66
+ argsSchema: fromJsonSchema(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema, {
67
+ useAlias: true,
68
+ groups: [name!, "prompt"]
69
+ }),
68
70
  handler: handler
69
71
  };
70
72
  }
@@ -7,6 +7,23 @@ describe("defineResource", () => {
7
7
  beforeEach(() => PlatformTest.create());
8
8
  afterEach(() => PlatformTest.reset());
9
9
 
10
+ function expectError(result: any, error: Record<string, unknown>) {
11
+ expect(result.contents[0]).toEqual({
12
+ uri: "tsed://resource",
13
+ mimeType: "plain/text",
14
+ text: error.message
15
+ });
16
+ expect(result.contents[1]).toEqual({
17
+ uri: "tsed://resource",
18
+ mimeType: "application/json",
19
+ text: expect.any(String)
20
+ });
21
+ expect(JSON.parse(result.contents[1].text)).toEqual({
22
+ ...error,
23
+ request_id: expect.any(String)
24
+ });
25
+ }
26
+
10
27
  it("should wrap handler errors with structured MCP payloads", async () => {
11
28
  const token = defineResource({
12
29
  name: "failing-resource",
@@ -19,16 +36,11 @@ describe("defineResource", () => {
19
36
  const definition = inject<any>(token);
20
37
  const result = await definition.handler(new URL("tsed://resource"), {} as any);
21
38
 
22
- expect(result).toEqual({
23
- contents: [],
24
- _meta: {
25
- status_code: undefined,
26
- code: "E_MCP_RESOURCE_ERROR",
27
- error_name: "Error",
28
- message: "boom",
29
- request_id: expect.any(String),
30
- resource: "failing-resource"
31
- }
39
+ expectError(result, {
40
+ code: "E_MCP_RESOURCE_ERROR",
41
+ error_name: "Error",
42
+ message: "boom",
43
+ resource: "failing-resource"
32
44
  });
33
45
  });
34
46
 
@@ -47,16 +59,12 @@ describe("defineResource", () => {
47
59
  const definition = inject<any>(token);
48
60
  const result = await definition.handler(new URL("tsed://resource"), {} as any);
49
61
 
50
- expect(result).toEqual({
51
- contents: [],
52
- _meta: {
53
- status_code: 404,
54
- code: "E_MCP_RESOURCE_NOT_FOUND",
55
- error_name: "NotFound",
56
- message: "Not found",
57
- request_id: expect.any(String),
58
- resource: "http-resource"
59
- }
62
+ expectError(result, {
63
+ status_code: 404,
64
+ code: "E_MCP_RESOURCE_NOT_FOUND",
65
+ error_name: "NotFound",
66
+ message: "Not found",
67
+ resource: "http-resource"
60
68
  });
61
69
  });
62
70
 
@@ -72,16 +80,33 @@ describe("defineResource", () => {
72
80
  const definition = inject<any>(token);
73
81
  const result = await definition.handler(new URL("tsed://resource"), {} as any);
74
82
 
75
- expect(result).toEqual({
76
- contents: [],
77
- _meta: {
78
- status_code: undefined,
79
- code: "E_MCP_RESOURCE_ERROR",
80
- error_name: undefined,
81
- message: "boom",
82
- request_id: expect.any(String),
83
- resource: "primitive-error-resource"
83
+ expectError(result, {
84
+ code: "E_MCP_RESOURCE_ERROR",
85
+ message: "boom",
86
+ resource: "primitive-error-resource"
87
+ });
88
+ });
89
+
90
+ it("should normalize successful resource payloads", async () => {
91
+ const token = defineResource({
92
+ name: "resource",
93
+ uri: "tsed://resource",
94
+ handler() {
95
+ return {id: "resource-id"} as any;
84
96
  }
85
97
  });
98
+
99
+ const definition = inject<any>(token);
100
+ const result = await definition.handler(new URL("tsed://resource"), {} as any);
101
+
102
+ expect(result).toEqual({
103
+ contents: [
104
+ {
105
+ uri: "tsed://resource",
106
+ mimeType: "application/json",
107
+ text: '{\n "id": "resource-id"\n}'
108
+ }
109
+ ]
110
+ });
86
111
  });
87
112
  });
@@ -1,21 +1,26 @@
1
- import type {ReadResourceCallback, ResourceMetadata, ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
2
- import {type TokenProvider, context, inject, injectable, logger} from "@tsed/di";
1
+ import type {ReadResourceCallback, ReadResourceResult, ResourceMetadata, ResourceTemplate} from "@modelcontextprotocol/server";
2
+ import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
3
3
  import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
4
4
  import {constantCase} from "change-case";
5
5
  import {s} from "@tsed/schema";
6
+ import {asResourceResponse} from "../utils/asResourceResponse.js";
6
7
 
7
8
  type ResourceMetadataProps = ResourceMetadata & {
8
9
  name: string;
9
10
  };
10
11
 
12
+ export type ResourceCallback = (
13
+ ...args: Parameters<ReadResourceCallback>
14
+ ) => ReadResourceResult | Record<string, unknown> | Promise<ReadResourceResult | Record<string, unknown>>;
15
+
11
16
  export type FnResourceReadProps = ResourceMetadataProps & {
12
17
  uri: string;
13
- handler: ReadResourceCallback;
18
+ handler: ResourceCallback;
14
19
  };
15
20
 
16
21
  export type FnResourceTemplateProps = ResourceMetadataProps & {
17
22
  template: ResourceTemplate;
18
- handler: ReadResourceCallback;
23
+ handler: ResourceCallback;
19
24
  };
20
25
 
21
26
  type ClassResourceBaseProps = Omit<ResourceMetadataProps, "name"> & {
@@ -45,7 +50,7 @@ function isClassResourceProps(options: ResourceProps): options is ClassResourceR
45
50
  }
46
51
 
47
52
  function mapOptions(options: ResourceProps) {
48
- let handler: ReadResourceCallback;
53
+ let handler: ResourceCallback;
49
54
  const name = options.name || ("propertyKey" in options ? String(options.propertyKey) : undefined);
50
55
 
51
56
  if (isClassResourceProps(options)) {
@@ -103,7 +108,14 @@ export function defineResource(options: ResourceProps): TokenProvider {
103
108
  ...opts,
104
109
  async handler(...args: Parameters<ReadResourceCallback>) {
105
110
  try {
106
- return await handler(...args);
111
+ const result = await handler(...args);
112
+
113
+ logger().info({
114
+ event: "MCP_TOOL_END",
115
+ tool: opts.name
116
+ });
117
+
118
+ return asResourceResponse(args[0]?.toString(), result);
107
119
  } catch (er: any) {
108
120
  const safeErr =
109
121
  er && typeof er === "object"
@@ -114,27 +126,29 @@ export function defineResource(options: ResourceProps): TokenProvider {
114
126
  status: undefined
115
127
  };
116
128
  const code = safeErr.name && safeErr.status ? `E_MCP_RESOURCE_${constantCase(safeErr.name)}` : "E_MCP_RESOURCE_ERROR";
129
+
117
130
  logger().error({
118
131
  event: "MCP_RESOURCE_ERROR",
119
132
  status_code: safeErr.status,
120
133
  code,
121
134
  error_name: safeErr.name,
122
135
  message: safeErr.message,
123
- request_id: context().id,
124
136
  resource: opts.name
125
137
  });
126
138
 
127
- return {
128
- contents: [],
129
- _meta: {
139
+ return asResourceResponse(
140
+ args[0]?.toString(),
141
+ {
130
142
  status_code: safeErr.status,
131
143
  code,
132
144
  error_name: safeErr.name,
133
145
  message: safeErr.message,
134
146
  request_id: context().id,
135
147
  resource: opts.name
136
- }
137
- };
148
+ },
149
+ {isError: true}
150
+ );
151
+ } finally {
138
152
  }
139
153
  }
140
154
  };