@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.
- package/lib/esm/fn/definePrompt.js +5 -2
- package/lib/esm/fn/defineResource.js +17 -13
- package/lib/esm/fn/defineTool.js +21 -15
- package/lib/esm/index.js +3 -1
- package/lib/esm/services/McpServerFactory.js +1 -1
- package/lib/esm/services/PlatformMcpModule.js +2 -2
- package/lib/esm/utils/asResourceResponse.js +26 -0
- package/lib/esm/utils/asToolResponse.js +29 -0
- package/lib/esm/utils/fromJsonSchema.js +17 -0
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/decorators/resource.d.ts +1 -1
- package/lib/types/fn/definePrompt.d.ts +4 -5
- package/lib/types/fn/defineResource.d.ts +24 -23
- package/lib/types/fn/defineTool.d.ts +9 -10
- package/lib/types/index.d.ts +3 -1
- package/lib/types/interfaces/PlatformMcpSettings.d.ts +2 -3
- package/lib/types/services/McpServerFactory.d.ts +1 -1
- package/lib/types/services/PlatformMcpModule.d.ts +1 -1
- package/lib/types/utils/asResourceResponse.d.ts +4 -0
- package/lib/types/utils/asToolResponse.d.ts +14 -0
- package/lib/types/utils/fromJsonSchema.d.ts +10 -0
- package/package.json +15 -13
- package/readme.md +1 -1
- package/src/decorators/resource.ts +1 -1
- package/src/fn/definePrompt.ts +10 -8
- package/src/fn/defineResource.spec.ts +54 -29
- package/src/fn/defineResource.ts +26 -12
- package/src/fn/defineTool.spec.ts +49 -8
- package/src/fn/defineTool.ts +42 -30
- package/src/index.ts +3 -1
- package/src/interfaces/PlatformMcpSettings.ts +2 -3
- package/src/services/McpServerFactory.ts +1 -2
- package/src/services/PlatformMcpModule.spec.ts +2 -2
- package/src/services/PlatformMcpModule.ts +3 -3
- package/src/utils/asResourceResponse.spec.ts +32 -0
- package/src/utils/asResourceResponse.ts +36 -0
- package/src/utils/asToolResponse.spec.ts +30 -0
- package/src/utils/asToolResponse.ts +49 -0
- package/src/utils/fromJsonSchema.spec.ts +68 -0
- package/src/utils/fromJsonSchema.ts +20 -0
- package/vitest.config.mts +0 -1
- package/lib/esm/utils/toZod.js +0 -19
- package/lib/types/utils/toZod.d.ts +0 -12
- package/src/utils/toZod.spec.ts +0 -40
- package/src/utils/toZod.ts +0 -22
|
@@ -3,7 +3,7 @@ import { s } from "@tsed/schema";
|
|
|
3
3
|
import { context, inject, injectable, logger } from "@tsed/di";
|
|
4
4
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
5
5
|
import { constantCase } from "change-case";
|
|
6
|
-
import {
|
|
6
|
+
import { fromJsonSchema } from "../utils/fromJsonSchema.js";
|
|
7
7
|
function mapOptions(options) {
|
|
8
8
|
let handler = undefined;
|
|
9
9
|
const name = options.name || ("propertyKey" in options ? String(options.propertyKey) : undefined);
|
|
@@ -23,7 +23,10 @@ function mapOptions(options) {
|
|
|
23
23
|
return {
|
|
24
24
|
...options,
|
|
25
25
|
name,
|
|
26
|
-
argsSchema:
|
|
26
|
+
argsSchema: fromJsonSchema(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema, {
|
|
27
|
+
useAlias: true,
|
|
28
|
+
groups: [name, "prompt"]
|
|
29
|
+
}),
|
|
27
30
|
handler: handler
|
|
28
31
|
};
|
|
29
32
|
}
|
|
@@ -2,6 +2,7 @@ import { context, inject, injectable, logger } from "@tsed/di";
|
|
|
2
2
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
3
3
|
import { constantCase } from "change-case";
|
|
4
4
|
import { s } from "@tsed/schema";
|
|
5
|
+
import { asResourceResponse } from "../utils/asResourceResponse.js";
|
|
5
6
|
function isClassResourceProps(options) {
|
|
6
7
|
return "token" in options && "propertyKey" in options;
|
|
7
8
|
}
|
|
@@ -36,7 +37,12 @@ export function defineResource(options) {
|
|
|
36
37
|
...opts,
|
|
37
38
|
async handler(...args) {
|
|
38
39
|
try {
|
|
39
|
-
|
|
40
|
+
const result = await handler(...args);
|
|
41
|
+
logger().info({
|
|
42
|
+
event: "MCP_TOOL_END",
|
|
43
|
+
tool: opts.name
|
|
44
|
+
});
|
|
45
|
+
return asResourceResponse(args[0]?.toString(), result);
|
|
40
46
|
}
|
|
41
47
|
catch (er) {
|
|
42
48
|
const safeErr = er && typeof er === "object"
|
|
@@ -53,20 +59,18 @@ export function defineResource(options) {
|
|
|
53
59
|
code,
|
|
54
60
|
error_name: safeErr.name,
|
|
55
61
|
message: safeErr.message,
|
|
56
|
-
request_id: context().id,
|
|
57
62
|
resource: opts.name
|
|
58
63
|
});
|
|
59
|
-
return {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
};
|
|
64
|
+
return asResourceResponse(args[0]?.toString(), {
|
|
65
|
+
status_code: safeErr.status,
|
|
66
|
+
code,
|
|
67
|
+
error_name: safeErr.name,
|
|
68
|
+
message: safeErr.message,
|
|
69
|
+
request_id: context().id,
|
|
70
|
+
resource: opts.name
|
|
71
|
+
}, { isError: true });
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
70
74
|
}
|
|
71
75
|
}
|
|
72
76
|
};
|
package/lib/esm/fn/defineTool.js
CHANGED
|
@@ -4,7 +4,8 @@ import { context, inject, injectable, logger } from "@tsed/di";
|
|
|
4
4
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
5
5
|
import { constantCase } from "change-case";
|
|
6
6
|
import { deserialize } from "@tsed/json-mapper";
|
|
7
|
-
import {
|
|
7
|
+
import { fromJsonSchema } from "../utils/fromJsonSchema.js";
|
|
8
|
+
import { asToolResponse } from "../utils/asToolResponse.js";
|
|
8
9
|
function getOutputSchema(methodStore) {
|
|
9
10
|
const schema = methodStore.operation.getResponseOf(200)?.getMedia("application/json")?.get("schema");
|
|
10
11
|
return schema?.itemSchema();
|
|
@@ -63,13 +64,22 @@ export function defineTool(options) {
|
|
|
63
64
|
return {
|
|
64
65
|
...opts,
|
|
65
66
|
name: opts.name,
|
|
66
|
-
inputSchema:
|
|
67
|
+
inputSchema: fromJsonSchema(inputSchema, {
|
|
68
|
+
groups: [opts.name, "tools"],
|
|
67
69
|
useAlias: true
|
|
68
70
|
}),
|
|
69
|
-
outputSchema:
|
|
70
|
-
|
|
71
|
+
outputSchema: fromJsonSchema(opts.outputSchema, {
|
|
72
|
+
groups: [opts.name, "tools"],
|
|
73
|
+
useAlias: true
|
|
74
|
+
}),
|
|
75
|
+
async handler(args, ctx) {
|
|
71
76
|
try {
|
|
72
|
-
|
|
77
|
+
const result = await handler(deserializeInput(args, inputSchema, inputStore), ctx);
|
|
78
|
+
logger().info({
|
|
79
|
+
event: "MCP_TOOL_END",
|
|
80
|
+
tool: opts.name
|
|
81
|
+
});
|
|
82
|
+
return asToolResponse(result);
|
|
73
83
|
}
|
|
74
84
|
catch (er) {
|
|
75
85
|
const code = er.name && er.status ? `E_MCP_TOOL_${constantCase(er.name)}` : "E_MCP_TOOL_ERROR";
|
|
@@ -82,16 +92,12 @@ export function defineTool(options) {
|
|
|
82
92
|
request_id: context().id,
|
|
83
93
|
tool: opts.name
|
|
84
94
|
});
|
|
85
|
-
return {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
request_id: context().id,
|
|
92
|
-
tool: opts.name
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
+
return asToolResponse({
|
|
96
|
+
status_code: er.status,
|
|
97
|
+
code,
|
|
98
|
+
message: er.message,
|
|
99
|
+
tool: opts.name
|
|
100
|
+
}, { isError: true });
|
|
95
101
|
}
|
|
96
102
|
}
|
|
97
103
|
};
|
package/lib/esm/index.js
CHANGED
|
@@ -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/
|
|
14
|
+
export * from "./utils/asResourceResponse.js";
|
|
15
|
+
export * from "./utils/asToolResponse.js";
|
|
16
|
+
export * from "./utils/fromJsonSchema.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { constant, inject, injectable, injector } from "@tsed/di";
|
|
2
2
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
3
|
-
import { McpServer } from "@modelcontextprotocol/
|
|
3
|
+
import { McpServer } from "@modelcontextprotocol/server";
|
|
4
4
|
function collectTokens(type, configured = []) {
|
|
5
5
|
const tokens = new Set(configured);
|
|
6
6
|
injector()
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { application } from "@tsed/platform-http";
|
|
2
2
|
import { constant, inject, injectable } from "@tsed/di";
|
|
3
3
|
import { MCP_SERVER } from "./McpServerFactory.js";
|
|
4
|
-
import {
|
|
4
|
+
import { NodeStreamableHTTPServerTransport } from "@modelcontextprotocol/node";
|
|
5
5
|
import { useContextHandler } from "@tsed/platform-router";
|
|
6
6
|
/**
|
|
7
7
|
* Platform module that mounts the MCP HTTP endpoint and forwards requests to the configured server instance.
|
|
@@ -35,7 +35,7 @@ export class PlatformMcpModule {
|
|
|
35
35
|
].filter(Boolean);
|
|
36
36
|
}
|
|
37
37
|
async dispatch($ctx) {
|
|
38
|
-
const transport = new
|
|
38
|
+
const transport = new NodeStreamableHTTPServerTransport({
|
|
39
39
|
sessionIdGenerator: undefined,
|
|
40
40
|
enableJsonResponse: true,
|
|
41
41
|
...this.settings.transportOptions
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { serialize } from "@tsed/json-mapper";
|
|
2
|
+
import { isObject } from "@tsed/core";
|
|
3
|
+
export function asResourceResponse(uri, payload, opts) {
|
|
4
|
+
if (payload.contents) {
|
|
5
|
+
return payload;
|
|
6
|
+
}
|
|
7
|
+
const contents = [];
|
|
8
|
+
if (opts?.isError) {
|
|
9
|
+
contents.push({
|
|
10
|
+
uri,
|
|
11
|
+
mimeType: "plain/text",
|
|
12
|
+
text: payload.message || ""
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
if (isObject(payload)) {
|
|
16
|
+
payload = serialize(payload, { useAlias: true, groups: ["resources"] });
|
|
17
|
+
contents.push({
|
|
18
|
+
uri,
|
|
19
|
+
mimeType: "application/json",
|
|
20
|
+
text: JSON.stringify(payload, null, 2)
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
contents
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { serialize } from "@tsed/json-mapper";
|
|
2
|
+
import { isObject } from "@tsed/core";
|
|
3
|
+
export function asToolResponse(payload, opts) {
|
|
4
|
+
if (isObject(payload)) {
|
|
5
|
+
if ("content" in payload || "structuredContent" in payload) {
|
|
6
|
+
return payload;
|
|
7
|
+
}
|
|
8
|
+
payload = serialize(payload, { useAlias: true, groups: ["tools"] });
|
|
9
|
+
return {
|
|
10
|
+
...opts,
|
|
11
|
+
content: [
|
|
12
|
+
{
|
|
13
|
+
type: "text",
|
|
14
|
+
text: JSON.stringify(payload, null, 2)
|
|
15
|
+
}
|
|
16
|
+
],
|
|
17
|
+
structuredContent: payload
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
...opts,
|
|
22
|
+
content: [
|
|
23
|
+
{
|
|
24
|
+
type: "text",
|
|
25
|
+
text: JSON.stringify(payload, null, 2)
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { fromJsonSchema as fromJSchema } from "@modelcontextprotocol/server";
|
|
2
|
+
import { JsonSchema, s } from "@tsed/schema";
|
|
3
|
+
/**
|
|
4
|
+
* Converts a Ts.ED schema into the MCP SDK v2 Standard Schema adapter.
|
|
5
|
+
*
|
|
6
|
+
* @param schema Ts.ED or plain JSON Schema document.
|
|
7
|
+
* @param opts Compilation options applied to Ts.ED schemas.
|
|
8
|
+
* @module platform/mcp
|
|
9
|
+
* @since 8.17.0
|
|
10
|
+
*/
|
|
11
|
+
export function fromJsonSchema(schema, opts) {
|
|
12
|
+
if (!schema) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
const jsonSchema = schema instanceof JsonSchema ? s.compile(schema, { inlineRefs: true, ...opts }) : schema;
|
|
16
|
+
return fromJSchema(jsonSchema);
|
|
17
|
+
}
|