@tsed/platform-mcp 8.28.1 → 8.28.4

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,4 +1,6 @@
1
+ import type { ServerOptions } from "@modelcontextprotocol/sdk/server/index.js";
1
2
  import type { StreamableHTTPServerTransportOptions } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
3
+ import type { Implementation } from "@modelcontextprotocol/sdk/types.js";
2
4
  import type { TokenProvider } from "@tsed/di";
3
5
  /**
4
6
  * Configuration fragment accepted under `configuration.mcp` to wire the Platform MCP module.
@@ -23,12 +25,32 @@ export interface PlatformMcpSettings {
23
25
  * Optional version reported to MCP clients. Falls back to the Ts.ED app version.
24
26
  */
25
27
  version?: string;
28
+ /**
29
+ * Optional human-readable server title exposed to MCP clients.
30
+ */
31
+ title?: Implementation["title"];
32
+ /**
33
+ * Optional server description exposed to MCP clients.
34
+ */
35
+ description?: Implementation["description"];
36
+ /**
37
+ * Optional canonical website URL exposed to MCP clients.
38
+ */
39
+ websiteUrl?: Implementation["websiteUrl"];
40
+ /**
41
+ * Optional icon descriptors exposed to MCP clients.
42
+ */
43
+ icons?: Implementation["icons"];
26
44
  /**
27
45
  * Optional tokens registered through configuration.
28
46
  */
29
47
  tools?: TokenProvider[];
30
48
  resources?: TokenProvider[];
31
49
  prompts?: TokenProvider[];
50
+ /**
51
+ * Optional low-level MCP server options forwarded to `new McpServer(serverInfo, options)`.
52
+ */
53
+ serverOptions?: ServerOptions;
32
54
  /**
33
55
  * Optional transport options for the MCP server.
34
56
  */
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/platform-mcp",
3
3
  "description": "Model Context Protocol integration for Ts.ED HTTP adapters.",
4
4
  "type": "module",
5
- "version": "8.28.1",
5
+ "version": "8.28.4",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -25,17 +25,18 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@modelcontextprotocol/sdk": ">=1.29.0",
28
+ "change-case": ">=5.4.4",
28
29
  "tslib": ">=2.7.0",
29
30
  "uuid": ">=11.0.2",
30
31
  "zod": ">=4.0.0"
31
32
  },
32
33
  "devDependencies": {
33
- "@tsed/barrels": "8.28.1",
34
- "@tsed/core": "8.28.1",
35
- "@tsed/di": "8.28.1",
36
- "@tsed/platform-http": "8.28.1",
37
- "@tsed/schema": "8.28.1",
38
- "@tsed/typescript": "8.28.1",
34
+ "@tsed/barrels": "8.28.4",
35
+ "@tsed/core": "8.28.4",
36
+ "@tsed/di": "8.28.4",
37
+ "@tsed/platform-http": "8.28.4",
38
+ "@tsed/schema": "8.28.4",
39
+ "@tsed/typescript": "8.28.4",
39
40
  "eslint": "9.12.0",
40
41
  "typescript": "6.0.2",
41
42
  "vitest": "4.1.4"
@@ -0,0 +1,61 @@
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 {definePrompt} from "./definePrompt.js";
6
+
7
+ describe("definePrompt", () => {
8
+ beforeEach(() => PlatformTest.create());
9
+ afterEach(() => PlatformTest.reset());
10
+
11
+ it("should wrap handler errors with structured MCP payloads", async () => {
12
+ const token = definePrompt({
13
+ name: "failing-prompt",
14
+ handler() {
15
+ throw new Error("boom");
16
+ }
17
+ });
18
+
19
+ const definition = inject<any>(token);
20
+ const result = await definition.handler({} as any);
21
+
22
+ expect(result).toEqual({
23
+ description: "boom",
24
+ messages: [],
25
+ _meta: {
26
+ status_code: undefined,
27
+ code: "E_MCP_PROMPT_ERROR",
28
+ message: "boom",
29
+ request_id: expect.any(String),
30
+ prompt: "failing-prompt"
31
+ }
32
+ });
33
+ });
34
+
35
+ it("should derive error code from error name and status when available", async () => {
36
+ const token = definePrompt({
37
+ name: "http-prompt",
38
+ handler() {
39
+ const er = new Error("Bad request") as Error & {status?: number; name: string};
40
+ er.name = "BadRequest";
41
+ er.status = 400;
42
+ throw er;
43
+ }
44
+ });
45
+
46
+ const definition = inject<any>(token);
47
+ const result = await definition.handler({} as any);
48
+
49
+ expect(result).toEqual({
50
+ description: "Bad request",
51
+ messages: [],
52
+ _meta: {
53
+ status_code: 400,
54
+ code: "E_MCP_PROMPT_BAD_REQUEST",
55
+ message: "Bad request",
56
+ request_id: expect.any(String),
57
+ prompt: "http-prompt"
58
+ }
59
+ });
60
+ });
61
+ });
@@ -1,8 +1,9 @@
1
1
  import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
2
2
  import type {GetPromptResult, ServerNotification, ServerRequest} from "@modelcontextprotocol/sdk/types.js";
3
3
  import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
4
- import {inject, injectable, type TokenProvider} from "@tsed/di";
4
+ import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
5
5
  import {JsonEntityStore, JsonSchema} from "@tsed/schema";
6
+ import {constantCase} from "change-case";
6
7
 
7
8
  import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
8
9
  import {toZod} from "../utils/toZod.js";
@@ -101,7 +102,39 @@ export function definePrompt<Args = any>(options: PromptProps<Args>) {
101
102
  const provider = injectable(Symbol.for(`MCP:PROMPT:${options.name}`))
102
103
  .type(MCP_PROVIDER_TYPES.PROMPT)
103
104
  .factory(() => {
104
- return mapOptions<Args>(options);
105
+ const {handler, ...opts} = mapOptions<Args>(options);
106
+
107
+ return {
108
+ ...opts,
109
+ async handler(...args: any[]) {
110
+ try {
111
+ return await (handler as any)(...args);
112
+ } catch (er: any) {
113
+ const code = er.name && er.status ? `E_MCP_PROMPT_${constantCase(er.name)}` : "E_MCP_PROMPT_ERROR";
114
+ logger().error({
115
+ event: "MCP_PROMPT_ERROR",
116
+ status_code: er.status,
117
+ code,
118
+ error_name: er.name,
119
+ message: er.message,
120
+ request_id: context().id,
121
+ prompt: opts.name
122
+ });
123
+
124
+ return {
125
+ description: er.message,
126
+ messages: [],
127
+ _meta: {
128
+ status_code: er.status,
129
+ code,
130
+ message: er.message,
131
+ request_id: context().id,
132
+ prompt: opts.name
133
+ }
134
+ } satisfies GetPromptResult;
135
+ }
136
+ }
137
+ };
105
138
  });
106
139
 
107
140
  return provider.token();
@@ -0,0 +1,88 @@
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 {defineResource} from "./defineResource.js";
6
+
7
+ describe("defineResource", () => {
8
+ beforeEach(() => PlatformTest.create());
9
+ afterEach(() => PlatformTest.reset());
10
+
11
+ it("should wrap handler errors with structured MCP payloads", async () => {
12
+ const token = defineResource({
13
+ name: "failing-resource",
14
+ uri: "tsed://resource",
15
+ handler() {
16
+ throw new Error("boom");
17
+ }
18
+ });
19
+
20
+ const definition = inject<any>(token);
21
+ const result = await definition.handler(new URL("tsed://resource"), {} as any);
22
+
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
+ }
33
+ });
34
+ });
35
+
36
+ it("should derive error code from error name and status when available", async () => {
37
+ const token = defineResource({
38
+ name: "http-resource",
39
+ uri: "tsed://resource",
40
+ handler() {
41
+ const er = new Error("Not found") as Error & {status?: number; name: string};
42
+ er.name = "NotFound";
43
+ er.status = 404;
44
+ throw er;
45
+ }
46
+ });
47
+
48
+ const definition = inject<any>(token);
49
+ const result = await definition.handler(new URL("tsed://resource"), {} as any);
50
+
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
+ }
61
+ });
62
+ });
63
+
64
+ it("should handle primitive throws safely", async () => {
65
+ const token = defineResource({
66
+ name: "primitive-error-resource",
67
+ uri: "tsed://resource",
68
+ handler() {
69
+ throw "boom";
70
+ }
71
+ });
72
+
73
+ const definition = inject<any>(token);
74
+ const result = await definition.handler(new URL("tsed://resource"), {} as any);
75
+
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"
85
+ }
86
+ });
87
+ });
88
+ });
@@ -1,6 +1,7 @@
1
1
  import type {ReadResourceCallback, ResourceMetadata, ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
2
- import {inject, injectable, type TokenProvider} from "@tsed/di";
2
+ import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
3
3
  import {JsonEntityStore} from "@tsed/schema";
4
+ import {constantCase} from "change-case";
4
5
 
5
6
  import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
6
7
 
@@ -97,7 +98,47 @@ export function defineResource(options: ResourceProps): TokenProvider {
97
98
  const provider = injectable(Symbol.for(`MCP:RESOURCE:${options.name}`))
98
99
  .type(MCP_PROVIDER_TYPES.RESOURCE)
99
100
  .factory(() => {
100
- return mapOptions(options);
101
+ const {handler, ...opts} = mapOptions(options);
102
+
103
+ return {
104
+ ...opts,
105
+ async handler(...args: Parameters<ReadResourceCallback>) {
106
+ try {
107
+ return await handler(...args);
108
+ } catch (er: any) {
109
+ const safeErr =
110
+ er && typeof er === "object"
111
+ ? er
112
+ : {
113
+ message: String(er),
114
+ name: undefined,
115
+ status: undefined
116
+ };
117
+ const code = safeErr.name && safeErr.status ? `E_MCP_RESOURCE_${constantCase(safeErr.name)}` : "E_MCP_RESOURCE_ERROR";
118
+ logger().error({
119
+ event: "MCP_RESOURCE_ERROR",
120
+ status_code: safeErr.status,
121
+ code,
122
+ error_name: safeErr.name,
123
+ message: safeErr.message,
124
+ request_id: context().id,
125
+ resource: opts.name
126
+ });
127
+
128
+ return {
129
+ contents: [],
130
+ _meta: {
131
+ status_code: safeErr.status,
132
+ code,
133
+ error_name: safeErr.name,
134
+ message: safeErr.message,
135
+ request_id: context().id,
136
+ resource: opts.name
137
+ }
138
+ };
139
+ }
140
+ }
141
+ };
101
142
  });
102
143
 
103
144
  return provider.token();
@@ -22,8 +22,34 @@ describe("defineTool", () => {
22
22
  const result = await definition.handler({}, {} as any);
23
23
 
24
24
  expect(result.structuredContent).toEqual({
25
+ status_code: undefined,
25
26
  code: "E_MCP_TOOL_ERROR",
26
- message: "boom"
27
+ message: "boom",
28
+ request_id: expect.any(String),
29
+ tool: "failing-tool"
30
+ });
31
+ });
32
+
33
+ it("should derive error code from error name and status when available", async () => {
34
+ const token = defineTool<any>({
35
+ name: "http-tool",
36
+ handler() {
37
+ const er = new Error("Not found") as Error & {status?: number; name: string};
38
+ er.name = "NotFound";
39
+ er.status = 404;
40
+ throw er;
41
+ }
42
+ });
43
+
44
+ const definition = inject<any>(token);
45
+ const result = await definition.handler({}, {} as any);
46
+
47
+ expect(result.structuredContent).toEqual({
48
+ status_code: 404,
49
+ code: "E_MCP_TOOL_NOT_FOUND",
50
+ message: "Not found",
51
+ request_id: expect.any(String),
52
+ tool: "http-tool"
27
53
  });
28
54
  });
29
55
  });
@@ -1,8 +1,9 @@
1
1
  import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
2
2
  import type {CallToolResult, ServerNotification, ServerRequest, Tool, ToolAnnotations} from "@modelcontextprotocol/sdk/types.js";
3
3
  import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
4
- import {inject, injectable, logger, type TokenProvider} from "@tsed/di";
4
+ import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
5
5
  import {JsonEntityStore, JsonMethodStore, JsonSchema} from "@tsed/schema";
6
+ import {constantCase} from "change-case";
6
7
 
7
8
  import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
8
9
  import {toZod} from "../utils/toZod.js";
@@ -125,18 +126,25 @@ export function defineTool<Input, Output = undefined>(options: ToolProps<Input,
125
126
  try {
126
127
  return await handler(args as Input, extra);
127
128
  } catch (er: any) {
129
+ const code = er.name && er.status ? `E_MCP_TOOL_${constantCase(er.name)}` : "E_MCP_TOOL_ERROR";
128
130
  logger().error({
129
131
  event: "MCP_TOOL_ERROR",
130
- tool: opts.name,
131
- error_message: er?.message,
132
- stack: er?.stack
132
+ status_code: er.status,
133
+ code,
134
+ error_name: er.name,
135
+ message: er.message,
136
+ request_id: context().id,
137
+ tool: opts.name
133
138
  });
134
139
 
135
140
  return {
136
141
  content: [],
137
142
  structuredContent: {
138
- code: "E_MCP_TOOL_ERROR",
139
- message: er?.message
143
+ status_code: er.status,
144
+ code,
145
+ message: er.message,
146
+ request_id: context().id,
147
+ tool: opts.name
140
148
  }
141
149
  } satisfies CallToolResult;
142
150
  }
@@ -1,4 +1,6 @@
1
+ import type {ServerOptions} from "@modelcontextprotocol/sdk/server/index.js";
1
2
  import type {StreamableHTTPServerTransportOptions} from "@modelcontextprotocol/sdk/server/streamableHttp.js";
3
+ import type {Implementation} from "@modelcontextprotocol/sdk/types.js";
2
4
  import type {TokenProvider} from "@tsed/di";
3
5
 
4
6
  /**
@@ -24,12 +26,32 @@ export interface PlatformMcpSettings {
24
26
  * Optional version reported to MCP clients. Falls back to the Ts.ED app version.
25
27
  */
26
28
  version?: string;
29
+ /**
30
+ * Optional human-readable server title exposed to MCP clients.
31
+ */
32
+ title?: Implementation["title"];
33
+ /**
34
+ * Optional server description exposed to MCP clients.
35
+ */
36
+ description?: Implementation["description"];
37
+ /**
38
+ * Optional canonical website URL exposed to MCP clients.
39
+ */
40
+ websiteUrl?: Implementation["websiteUrl"];
41
+ /**
42
+ * Optional icon descriptors exposed to MCP clients.
43
+ */
44
+ icons?: Implementation["icons"];
27
45
  /**
28
46
  * Optional tokens registered through configuration.
29
47
  */
30
48
  tools?: TokenProvider[];
31
49
  resources?: TokenProvider[];
32
50
  prompts?: TokenProvider[];
51
+ /**
52
+ * Optional low-level MCP server options forwarded to `new McpServer(serverInfo, options)`.
53
+ */
54
+ serverOptions?: ServerOptions;
33
55
  /**
34
56
  * Optional transport options for the MCP server.
35
57
  */
@@ -29,11 +29,19 @@ export const MCP_SERVER = injectable(McpServer)
29
29
  const settings = constant<PlatformMcpSettings>("mcp", {}) || {};
30
30
  const name = settings.name || constant<string>("name") || "tsed-mcp";
31
31
  const version = settings.version || constant<string>("version") || "0.0.0";
32
+ const {websiteUrl, description, title, icons} = settings;
32
33
 
33
- const server = new McpServer({
34
- name,
35
- version
36
- });
34
+ const server = new McpServer(
35
+ {
36
+ websiteUrl,
37
+ description,
38
+ icons,
39
+ title,
40
+ name,
41
+ version
42
+ },
43
+ settings?.serverOptions
44
+ );
37
45
 
38
46
  const toolTokens = collectTokens(MCP_PROVIDER_TYPES.TOOL, settings.tools);
39
47
  toolTokens.forEach((token) => {