@tsed/platform-mcp 8.34.0 → 8.36.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.
Files changed (47) hide show
  1. package/lib/esm/decorators/prompt.js +1 -1
  2. package/lib/esm/decorators/resource.js +1 -1
  3. package/lib/esm/decorators/tool.js +1 -1
  4. package/lib/esm/fn/definePrompt.js +7 -4
  5. package/lib/esm/fn/defineResource.js +20 -16
  6. package/lib/esm/fn/defineTool.js +24 -18
  7. package/lib/esm/index.js +2 -0
  8. package/lib/esm/services/McpServerFactory.js +1 -1
  9. package/lib/esm/services/PlatformMcpModule.js +3 -3
  10. package/lib/esm/utils/asResourceResponse.js +26 -0
  11. package/lib/esm/utils/asToolResponse.js +29 -0
  12. package/lib/esm/utils/toZod.js +3 -3
  13. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  14. package/lib/types/decorators/resource.d.ts +1 -1
  15. package/lib/types/fn/definePrompt.d.ts +3 -3
  16. package/lib/types/fn/defineResource.d.ts +8 -6
  17. package/lib/types/fn/defineTool.d.ts +11 -11
  18. package/lib/types/index.d.ts +2 -0
  19. package/lib/types/interfaces/PlatformMcpSettings.d.ts +1 -1
  20. package/lib/types/services/PlatformMcpModule.d.ts +1 -1
  21. package/lib/types/utils/asResourceResponse.d.ts +4 -0
  22. package/lib/types/utils/asToolResponse.d.ts +14 -0
  23. package/package.json +9 -10
  24. package/src/decorators/prompt.spec.ts +1 -2
  25. package/src/decorators/prompt.ts +1 -2
  26. package/src/decorators/resource.spec.ts +1 -2
  27. package/src/decorators/resource.ts +1 -2
  28. package/src/decorators/tool.spec.ts +1 -2
  29. package/src/decorators/tool.ts +1 -2
  30. package/src/fn/definePrompt.spec.ts +2 -3
  31. package/src/fn/definePrompt.ts +9 -7
  32. package/src/fn/defineResource.spec.ts +56 -32
  33. package/src/fn/defineResource.ts +28 -14
  34. package/src/fn/defineTool.spec.ts +50 -8
  35. package/src/fn/defineTool.ts +43 -31
  36. package/src/index.ts +2 -0
  37. package/src/interfaces/PlatformMcpSettings.ts +1 -1
  38. package/src/services/McpServerFactory.ts +4 -5
  39. package/src/services/PlatformMcpModule.spec.ts +3 -5
  40. package/src/services/PlatformMcpModule.ts +4 -5
  41. package/src/utils/asResourceResponse.spec.ts +32 -0
  42. package/src/utils/asResourceResponse.ts +36 -0
  43. package/src/utils/asToolResponse.spec.ts +30 -0
  44. package/src/utils/asToolResponse.ts +49 -0
  45. package/src/utils/toZod.spec.ts +25 -3
  46. package/src/utils/toZod.ts +5 -3
  47. package/vitest.config.mts +1 -1
@@ -1,5 +1,5 @@
1
- import type { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
2
1
  import { type ClassResourceReadProps } from "../fn/defineResource.js";
2
+ import type { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
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,8 @@
1
- import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
2
- import type { GetPromptResult, ServerNotification, ServerRequest } from "@modelcontextprotocol/sdk/types.js";
3
1
  import { type AbstractType, type Type } from "@tsed/core";
4
- import { type TokenProvider } from "@tsed/di";
2
+ import type { GetPromptResult, ServerNotification, ServerRequest } from "@modelcontextprotocol/sdk/types.js";
5
3
  import { JsonSchema } from "@tsed/schema";
4
+ import { type TokenProvider } from "@tsed/di";
5
+ import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
6
6
  type BasePromptConfig = {
7
7
  title?: string;
8
8
  description?: string;
@@ -1,15 +1,17 @@
1
1
  import type { ReadResourceCallback, ResourceMetadata, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
2
3
  import { type TokenProvider } from "@tsed/di";
3
4
  type ResourceMetadataProps = ResourceMetadata & {
4
5
  name: string;
5
6
  };
7
+ export type ResourceCallback = (...args: Parameters<ReadResourceCallback>) => ReadResourceResult | Record<string, unknown> | Promise<ReadResourceResult | Record<string, unknown>>;
6
8
  export type FnResourceReadProps = ResourceMetadataProps & {
7
9
  uri: string;
8
- handler: ReadResourceCallback;
10
+ handler: ResourceCallback;
9
11
  };
10
12
  export type FnResourceTemplateProps = ResourceMetadataProps & {
11
13
  template: ResourceTemplate;
12
- handler: ReadResourceCallback;
14
+ handler: ResourceCallback;
13
15
  };
14
16
  type ClassResourceBaseProps = Omit<ResourceMetadataProps, "name"> & {
15
17
  name?: string;
@@ -31,7 +33,7 @@ export type ClassResourceTemplateProps = ClassResourceBaseProps & {
31
33
  export type ResourceProps = FnResourceReadProps | FnResourceTemplateProps | ClassResourceReadProps | ClassResourceTemplateProps;
32
34
  declare function mapOptions(options: ResourceProps): {
33
35
  name: string | undefined;
34
- handler: ReadResourceCallback;
36
+ handler: ResourceCallback;
35
37
  _meta?: {
36
38
  [x: string]: unknown;
37
39
  } | undefined;
@@ -53,7 +55,7 @@ declare function mapOptions(options: ResourceProps): {
53
55
  uri: string;
54
56
  } | {
55
57
  name: string | undefined;
56
- handler: ReadResourceCallback;
58
+ handler: ResourceCallback;
57
59
  _meta?: {
58
60
  [x: string]: unknown;
59
61
  } | undefined;
@@ -75,7 +77,7 @@ declare function mapOptions(options: ResourceProps): {
75
77
  template: ResourceTemplate;
76
78
  } | {
77
79
  name: string | undefined;
78
- handler: ReadResourceCallback;
80
+ handler: ResourceCallback;
79
81
  _meta?: {
80
82
  [x: string]: unknown;
81
83
  } | undefined;
@@ -99,7 +101,7 @@ declare function mapOptions(options: ResourceProps): {
99
101
  uri: string;
100
102
  } | {
101
103
  name: string | undefined;
102
- handler: ReadResourceCallback;
104
+ handler: ResourceCallback;
103
105
  _meta?: {
104
106
  [x: string]: unknown;
105
107
  } | undefined;
@@ -1,8 +1,8 @@
1
- import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
2
- import type { CallToolResult, ServerNotification, ServerRequest, Tool, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
3
1
  import { type AbstractType, type Type } from "@tsed/core";
4
- import { type TokenProvider } from "@tsed/di";
2
+ import type { CallToolResult, ServerNotification, ServerRequest, Tool, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
5
3
  import { JsonSchema } from "@tsed/schema";
4
+ import { type TokenProvider } from "@tsed/di";
5
+ import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
6
6
  /**
7
7
  * Signature implemented by MCP tool handlers invoked through {@link defineTool}.
8
8
  *
@@ -10,20 +10,20 @@ import { JsonSchema } from "@tsed/schema";
10
10
  * @module platform/mcp
11
11
  * @since 8.17.0
12
12
  */
13
- export type ToolCallback<Args = undefined> = (args: Args, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult | Promise<CallToolResult>;
13
+ export type ToolCallback<Args = undefined, Output = unknown> = (args: Args, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult | Promise<CallToolResult> | Output | Promise<Output>;
14
14
  type BaseToolConfig = {
15
15
  title?: string;
16
16
  description?: string;
17
17
  annotations?: ToolAnnotations;
18
18
  _meta?: Record<string, unknown>;
19
19
  };
20
- type BaseToolProps<Input, Output = undefined> = BaseToolConfig & {
20
+ type BaseToolProps<Input, Output = unknown> = BaseToolConfig & {
21
21
  inputSchema?: JsonSchema<Input> | (() => JsonSchema<Input>) | Tool["inputSchema"];
22
22
  outputSchema?: JsonSchema<Output> | Tool["outputSchema"];
23
23
  };
24
- export type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output> & {
24
+ export type FnToolProps<Input, Output = unknown> = BaseToolProps<Input, Output> & {
25
25
  name: string;
26
- handler: ToolCallback<Input>;
26
+ handler: ToolCallback<Input, Output>;
27
27
  };
28
28
  /**
29
29
  * Configuration accepted when decorating class methods as MCP tools.
@@ -31,7 +31,7 @@ export type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output
31
31
  * @module platform/mcp
32
32
  * @since 8.17.0
33
33
  */
34
- export type ClassToolProps<Input, Output = undefined> = BaseToolProps<Input, Output> & {
34
+ export type ClassToolProps<Input, Output = unknown> = BaseToolProps<Input, Output> & {
35
35
  name?: string;
36
36
  token: Type | AbstractType<any>;
37
37
  propertyKey: string | symbol;
@@ -42,7 +42,7 @@ export type ClassToolProps<Input, Output = undefined> = BaseToolProps<Input, Out
42
42
  * @module platform/mcp
43
43
  * @since 8.17.0
44
44
  */
45
- export type ToolProps<Input, Output = undefined> = FnToolProps<Input, Output> | ClassToolProps<Input, Output>;
45
+ export type ToolProps<Input, Output = unknown> = FnToolProps<Input, Output> | ClassToolProps<Input, Output>;
46
46
  /**
47
47
  * Registers an MCP tool provider and wraps its handler with error logging plus schema conversion.
48
48
  *
@@ -61,6 +61,6 @@ export type ToolProps<Input, Output = undefined> = FnToolProps<Input, Output> |
61
61
  * });
62
62
  * ```
63
63
  */
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;
64
+ export declare function defineTool<Input, Output = unknown>(options: FnToolProps<Input, Output>): TokenProvider;
65
+ export declare function defineTool<Input, Output = unknown>(options: ClassToolProps<Input, Output>): TokenProvider;
66
66
  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/asResourceResponse.js";
15
+ export * from "./utils/asToolResponse.js";
14
16
  export * from "./utils/toZod.js";
@@ -1,6 +1,6 @@
1
+ import type { Implementation } from "@modelcontextprotocol/sdk/types.js";
1
2
  import type { ServerOptions } from "@modelcontextprotocol/sdk/server/index.js";
2
3
  import type { StreamableHTTPServerTransportOptions } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
3
- import type { Implementation } from "@modelcontextprotocol/sdk/types.js";
4
4
  import type { TokenProvider } from "@tsed/di";
5
5
  /**
6
6
  * Configuration fragment accepted under `configuration.mcp` to wire the Platform MCP module.
@@ -1,5 +1,5 @@
1
- import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
1
  import { type OnRoutesInit, PlatformContext, type PlatformRouteDetails } from "@tsed/platform-http";
2
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
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 { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
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/sdk/types.js";
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>;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@tsed/platform-mcp",
3
+ "version": "8.36.0",
3
4
  "description": "Model Context Protocol integration for Ts.ED HTTP adapters.",
4
- "type": "module",
5
- "version": "8.34.0",
6
5
  "source": "./src/index.ts",
6
+ "type": "module",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
9
9
  "typings": "./lib/types/index.d.ts",
@@ -32,14 +32,13 @@
32
32
  "zod": ">=4.0.0"
33
33
  },
34
34
  "devDependencies": {
35
- "@tsed/barrels": "8.34.0",
36
- "@tsed/core": "8.34.0",
37
- "@tsed/di": "8.34.0",
38
- "@tsed/json-mapper": "8.34.0",
39
- "@tsed/platform-http": "8.34.0",
40
- "@tsed/schema": "8.34.0",
41
- "@tsed/typescript": "8.34.0",
42
- "eslint": "9.12.0",
35
+ "@tsed/barrels": "8.36.0",
36
+ "@tsed/core": "8.36.0",
37
+ "@tsed/di": "8.36.0",
38
+ "@tsed/json-mapper": "8.36.0",
39
+ "@tsed/platform-http": "8.36.0",
40
+ "@tsed/schema": "8.36.0",
41
+ "@tsed/typescript": "8.36.0",
43
42
  "typescript": "6.0.2",
44
43
  "vitest": "4.1.4"
45
44
  },
@@ -1,6 +1,5 @@
1
- import {DITest, inject, Injectable} from "@tsed/di";
1
+ import {DITest, Injectable, inject} from "@tsed/di";
2
2
  import {Description, Title} from "@tsed/schema";
3
-
4
3
  import {Prompt} from "./prompt.js";
5
4
 
6
5
  @Injectable()
@@ -1,6 +1,5 @@
1
- import {classOf} from "@tsed/core";
2
-
3
1
  import {type ClassPromptProps, definePrompt} from "../fn/definePrompt.js";
2
+ import {classOf} from "@tsed/core";
4
3
 
5
4
  /**
6
5
  * Options accepted by {@link Prompt} when decorating a class method. Matches {@link PromptProps} minus the handler reference.
@@ -1,6 +1,5 @@
1
- import {DITest, inject, Injectable} from "@tsed/di";
2
1
  import {ContentType, Description, Title} from "@tsed/schema";
3
-
2
+ import {DITest, Injectable, inject} from "@tsed/di";
4
3
  import {Resource} from "./resource.js";
5
4
 
6
5
  @Injectable()
@@ -1,9 +1,8 @@
1
+ import {type ClassResourceReadProps, type ClassResourceTemplateProps, defineResource} from "../fn/defineResource.js";
1
2
  import type {ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
2
3
  import {classOf} from "@tsed/core";
3
4
  import {isString} from "@tsed/core/utils/isString.js";
4
5
 
5
- import {type ClassResourceReadProps, type ClassResourceTemplateProps, defineResource} from "../fn/defineResource.js";
6
-
7
6
  /**
8
7
  * Options accepted by the {@link Resource} decorator beyond the handler binding fields, allowing callers to override metadata.
9
8
  *
@@ -1,6 +1,5 @@
1
- import {DITest, inject, Injectable} from "@tsed/di";
2
1
  import {CollectionOf, Default, Description, Name, Property, Returns} from "@tsed/schema";
3
-
2
+ import {DITest, Injectable, inject} from "@tsed/di";
4
3
  import {Tool} from "./tool.js";
5
4
 
6
5
  class Model {
@@ -1,6 +1,5 @@
1
- import {classOf} from "@tsed/core";
2
-
3
1
  import {type ClassToolProps, defineTool} from "../fn/defineTool.js";
2
+ import {classOf} from "@tsed/core";
4
3
 
5
4
  /**
6
5
  * Decorator that registers a class method as an MCP tool and binds it to the Ts.ED DI container.
@@ -1,8 +1,7 @@
1
- import {inject} from "@tsed/di";
2
- import {PlatformTest} from "@tsed/platform-http/testing";
3
1
  import {afterEach, beforeEach, describe, expect, it} from "vitest";
4
-
2
+ import {PlatformTest} from "@tsed/platform-http/testing";
5
3
  import {definePrompt} from "./definePrompt.js";
4
+ import {inject} from "@tsed/di";
6
5
 
7
6
  describe("definePrompt", () => {
8
7
  beforeEach(() => PlatformTest.create());
@@ -1,11 +1,10 @@
1
- import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
2
- import type {GetPromptResult, ServerNotification, ServerRequest} from "@modelcontextprotocol/sdk/types.js";
3
1
  import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
2
+ import type {GetPromptResult, ServerNotification, ServerRequest} from "@modelcontextprotocol/sdk/types.js";
3
+ import {JsonSchema, s} from "@tsed/schema";
4
4
  import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
5
- import {JsonEntityStore, JsonSchema} from "@tsed/schema";
6
- import {constantCase} from "change-case";
7
-
8
5
  import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
6
+ import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
7
+ import {constantCase} from "change-case";
9
8
  import {toZod} from "../utils/toZod.js";
10
9
 
11
10
  type BasePromptConfig = {
@@ -57,7 +56,7 @@ function mapOptions<Args = any>(options: PromptProps<Args>) {
57
56
  return instance[propertyKey](...args);
58
57
  }) as unknown as PromptHandler<Args>;
59
58
 
60
- const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
59
+ const methodStore = s.store.method(token, propertyKey);
61
60
  options.description = options.description || methodStore.operation.get("description");
62
61
  options.title = options.title || methodStore.schema.get("title");
63
62
  }
@@ -65,7 +64,10 @@ function mapOptions<Args = any>(options: PromptProps<Args>) {
65
64
  return {
66
65
  ...options,
67
66
  name,
68
- argsSchema: toZod(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema),
67
+ argsSchema: toZod(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema, {
68
+ useAlias: true,
69
+ groups: [name!, "prompt"]
70
+ }),
69
71
  handler: handler
70
72
  };
71
73
  }
@@ -1,13 +1,29 @@
1
- import {inject} from "@tsed/di";
2
- import {PlatformTest} from "@tsed/platform-http/testing";
3
1
  import {afterEach, beforeEach, describe, expect, it} from "vitest";
4
-
2
+ import {PlatformTest} from "@tsed/platform-http/testing";
5
3
  import {defineResource} from "./defineResource.js";
4
+ import {inject} from "@tsed/di";
6
5
 
7
6
  describe("defineResource", () => {
8
7
  beforeEach(() => PlatformTest.create());
9
8
  afterEach(() => PlatformTest.reset());
10
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
+
11
27
  it("should wrap handler errors with structured MCP payloads", async () => {
12
28
  const token = defineResource({
13
29
  name: "failing-resource",
@@ -20,16 +36,11 @@ describe("defineResource", () => {
20
36
  const definition = inject<any>(token);
21
37
  const result = await definition.handler(new URL("tsed://resource"), {} as any);
22
38
 
23
- expect(result).toEqual({
24
- contents: [],
25
- _meta: {
26
- status_code: undefined,
27
- code: "E_MCP_RESOURCE_ERROR",
28
- error_name: "Error",
29
- message: "boom",
30
- request_id: expect.any(String),
31
- resource: "failing-resource"
32
- }
39
+ expectError(result, {
40
+ code: "E_MCP_RESOURCE_ERROR",
41
+ error_name: "Error",
42
+ message: "boom",
43
+ resource: "failing-resource"
33
44
  });
34
45
  });
35
46
 
@@ -48,16 +59,12 @@ describe("defineResource", () => {
48
59
  const definition = inject<any>(token);
49
60
  const result = await definition.handler(new URL("tsed://resource"), {} as any);
50
61
 
51
- expect(result).toEqual({
52
- contents: [],
53
- _meta: {
54
- status_code: 404,
55
- code: "E_MCP_RESOURCE_NOT_FOUND",
56
- error_name: "NotFound",
57
- message: "Not found",
58
- request_id: expect.any(String),
59
- resource: "http-resource"
60
- }
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"
61
68
  });
62
69
  });
63
70
 
@@ -73,16 +80,33 @@ describe("defineResource", () => {
73
80
  const definition = inject<any>(token);
74
81
  const result = await definition.handler(new URL("tsed://resource"), {} as any);
75
82
 
76
- expect(result).toEqual({
77
- contents: [],
78
- _meta: {
79
- status_code: undefined,
80
- code: "E_MCP_RESOURCE_ERROR",
81
- error_name: undefined,
82
- message: "boom",
83
- request_id: expect.any(String),
84
- 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;
85
96
  }
86
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
+ });
87
111
  });
88
112
  });
@@ -1,22 +1,27 @@
1
1
  import type {ReadResourceCallback, ResourceMetadata, ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type {ReadResourceResult} from "@modelcontextprotocol/sdk/types.js";
2
3
  import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
3
- import {JsonEntityStore} from "@tsed/schema";
4
- import {constantCase} from "change-case";
5
-
6
4
  import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
5
+ import {constantCase} from "change-case";
6
+ import {s} from "@tsed/schema";
7
+ import {asResourceResponse} from "../utils/asResourceResponse.js";
7
8
 
8
9
  type ResourceMetadataProps = ResourceMetadata & {
9
10
  name: string;
10
11
  };
11
12
 
13
+ export type ResourceCallback = (
14
+ ...args: Parameters<ReadResourceCallback>
15
+ ) => ReadResourceResult | Record<string, unknown> | Promise<ReadResourceResult | Record<string, unknown>>;
16
+
12
17
  export type FnResourceReadProps = ResourceMetadataProps & {
13
18
  uri: string;
14
- handler: ReadResourceCallback;
19
+ handler: ResourceCallback;
15
20
  };
16
21
 
17
22
  export type FnResourceTemplateProps = ResourceMetadataProps & {
18
23
  template: ResourceTemplate;
19
- handler: ReadResourceCallback;
24
+ handler: ResourceCallback;
20
25
  };
21
26
 
22
27
  type ClassResourceBaseProps = Omit<ResourceMetadataProps, "name"> & {
@@ -46,7 +51,7 @@ function isClassResourceProps(options: ResourceProps): options is ClassResourceR
46
51
  }
47
52
 
48
53
  function mapOptions(options: ResourceProps) {
49
- let handler: ReadResourceCallback;
54
+ let handler: ResourceCallback;
50
55
  const name = options.name || ("propertyKey" in options ? String(options.propertyKey) : undefined);
51
56
 
52
57
  if (isClassResourceProps(options)) {
@@ -57,7 +62,7 @@ function mapOptions(options: ResourceProps) {
57
62
  return instance[propertyKey](...args);
58
63
  };
59
64
 
60
- const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
65
+ const methodStore = s.store.method(token, propertyKey);
61
66
  options.description = options.description || methodStore.operation.get("description");
62
67
  options.title = options.title || methodStore.schema.get("title");
63
68
  } else {
@@ -104,7 +109,14 @@ export function defineResource(options: ResourceProps): TokenProvider {
104
109
  ...opts,
105
110
  async handler(...args: Parameters<ReadResourceCallback>) {
106
111
  try {
107
- return await handler(...args);
112
+ const result = await handler(...args);
113
+
114
+ logger().info({
115
+ event: "MCP_TOOL_END",
116
+ tool: opts.name
117
+ });
118
+
119
+ return asResourceResponse(args[0]?.toString(), result);
108
120
  } catch (er: any) {
109
121
  const safeErr =
110
122
  er && typeof er === "object"
@@ -115,27 +127,29 @@ export function defineResource(options: ResourceProps): TokenProvider {
115
127
  status: undefined
116
128
  };
117
129
  const code = safeErr.name && safeErr.status ? `E_MCP_RESOURCE_${constantCase(safeErr.name)}` : "E_MCP_RESOURCE_ERROR";
130
+
118
131
  logger().error({
119
132
  event: "MCP_RESOURCE_ERROR",
120
133
  status_code: safeErr.status,
121
134
  code,
122
135
  error_name: safeErr.name,
123
136
  message: safeErr.message,
124
- request_id: context().id,
125
137
  resource: opts.name
126
138
  });
127
139
 
128
- return {
129
- contents: [],
130
- _meta: {
140
+ return asResourceResponse(
141
+ args[0]?.toString(),
142
+ {
131
143
  status_code: safeErr.status,
132
144
  code,
133
145
  error_name: safeErr.name,
134
146
  message: safeErr.message,
135
147
  request_id: context().id,
136
148
  resource: opts.name
137
- }
138
- };
149
+ },
150
+ {isError: true}
151
+ );
152
+ } finally {
139
153
  }
140
154
  }
141
155
  };
@@ -1,9 +1,8 @@
1
- import {inject} from "@tsed/di";
2
- import {PlatformTest} from "@tsed/platform-http/testing";
3
- import {Default, from, JsonSchema, Name, Property} from "@tsed/schema";
1
+ import {Default, from, JsonSchema, Name, Property, s, string} from "@tsed/schema";
4
2
  import {afterEach, beforeEach, describe, expect, it} from "vitest";
5
-
3
+ import {PlatformTest} from "@tsed/platform-http/testing";
6
4
  import {defineTool} from "./defineTool.js";
5
+ import {inject} from "@tsed/di";
7
6
 
8
7
  class KnowledgeSearchRequest {
9
8
  @Property()
@@ -40,11 +39,10 @@ describe("defineTool", () => {
40
39
 
41
40
  const result = await definition.handler({}, {} as any);
42
41
 
42
+ expect(result.isError).toBe(true);
43
43
  expect(result.structuredContent).toEqual({
44
- status_code: undefined,
45
44
  code: "E_MCP_TOOL_ERROR",
46
45
  message: "boom",
47
- request_id: expect.any(String),
48
46
  tool: "failing-tool"
49
47
  });
50
48
  });
@@ -63,11 +61,11 @@ describe("defineTool", () => {
63
61
  const definition = inject<any>(token);
64
62
  const result = await definition.handler({}, {} as any);
65
63
 
64
+ expect(result.isError).toBe(true);
66
65
  expect(result.structuredContent).toEqual({
67
66
  status_code: 404,
68
67
  code: "E_MCP_TOOL_NOT_FOUND",
69
68
  message: "Not found",
70
- request_id: expect.any(String),
71
69
  tool: "http-tool"
72
70
  });
73
71
  });
@@ -75,7 +73,7 @@ describe("defineTool", () => {
75
73
  it("should expose aliased input schema properties", () => {
76
74
  const token = defineTool<any>({
77
75
  name: "aliased-tool",
78
- inputSchema: JsonSchema.from({
76
+ inputSchema: new JsonSchema({
79
77
  type: "object",
80
78
  properties: {
81
79
  prop: {
@@ -104,6 +102,28 @@ describe("defineTool", () => {
104
102
  });
105
103
  });
106
104
 
105
+ it("should expose aliases declared by functional output schemas", () => {
106
+ const token = defineTool({
107
+ name: "aliased-tool-output",
108
+ outputSchema: s.object({prop: string().required()}).addAlias("prop", "aliasProp"),
109
+ handler() {
110
+ return {prop: "value"};
111
+ }
112
+ });
113
+
114
+ const definition = inject<any>(token);
115
+
116
+ expect(definition.outputSchema.toJSONSchema()).toMatchObject({
117
+ type: "object",
118
+ properties: {
119
+ aliasProp: {
120
+ type: "string"
121
+ }
122
+ },
123
+ required: ["aliasProp"]
124
+ });
125
+ });
126
+
107
127
  it("should deserialize functional tool input from a Ts.ED model schema", async () => {
108
128
  const token = defineTool<KnowledgeSearchRequest>({
109
129
  name: "knowledge-search",
@@ -129,4 +149,26 @@ describe("defineTool", () => {
129
149
  {} as any
130
150
  );
131
151
  });
152
+
153
+ it("should normalize successful tool payloads as structured content", async () => {
154
+ const token = defineTool({
155
+ name: "successful-tool",
156
+ outputSchema: s.object({id: string()}),
157
+ handler() {
158
+ return {id: "tool-id"};
159
+ }
160
+ });
161
+
162
+ const definition = inject<any>(token);
163
+
164
+ await expect(definition.handler({}, {} as any)).resolves.toEqual({
165
+ content: [
166
+ {
167
+ type: "text",
168
+ text: '{\n "id": "tool-id"\n}'
169
+ }
170
+ ],
171
+ structuredContent: {id: "tool-id"}
172
+ });
173
+ });
132
174
  });