@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.
- package/lib/esm/decorators/prompt.js +1 -1
- package/lib/esm/decorators/resource.js +1 -1
- package/lib/esm/decorators/tool.js +1 -1
- package/lib/esm/fn/definePrompt.js +7 -4
- package/lib/esm/fn/defineResource.js +20 -16
- package/lib/esm/fn/defineTool.js +24 -18
- package/lib/esm/index.js +2 -0
- package/lib/esm/services/McpServerFactory.js +1 -1
- package/lib/esm/services/PlatformMcpModule.js +3 -3
- package/lib/esm/utils/asResourceResponse.js +26 -0
- package/lib/esm/utils/asToolResponse.js +29 -0
- package/lib/esm/utils/toZod.js +3 -3
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/decorators/resource.d.ts +1 -1
- package/lib/types/fn/definePrompt.d.ts +3 -3
- package/lib/types/fn/defineResource.d.ts +8 -6
- package/lib/types/fn/defineTool.d.ts +11 -11
- package/lib/types/index.d.ts +2 -0
- package/lib/types/interfaces/PlatformMcpSettings.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/package.json +9 -10
- package/src/decorators/prompt.spec.ts +1 -2
- package/src/decorators/prompt.ts +1 -2
- package/src/decorators/resource.spec.ts +1 -2
- package/src/decorators/resource.ts +1 -2
- package/src/decorators/tool.spec.ts +1 -2
- package/src/decorators/tool.ts +1 -2
- package/src/fn/definePrompt.spec.ts +2 -3
- package/src/fn/definePrompt.ts +9 -7
- package/src/fn/defineResource.spec.ts +56 -32
- package/src/fn/defineResource.ts +28 -14
- package/src/fn/defineTool.spec.ts +50 -8
- package/src/fn/defineTool.ts +43 -31
- package/src/index.ts +2 -0
- package/src/interfaces/PlatformMcpSettings.ts +1 -1
- package/src/services/McpServerFactory.ts +4 -5
- package/src/services/PlatformMcpModule.spec.ts +3 -5
- package/src/services/PlatformMcpModule.ts +4 -5
- 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/toZod.spec.ts +25 -3
- package/src/utils/toZod.ts +5 -3
- package/vitest.config.mts +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { defineResource } from "../fn/defineResource.js";
|
|
1
2
|
import { classOf } from "@tsed/core";
|
|
2
3
|
import { isString } from "@tsed/core/utils/isString.js";
|
|
3
|
-
import { defineResource } from "../fn/defineResource.js";
|
|
4
4
|
/**
|
|
5
5
|
* Decorator that turns a class method into an MCP resource backed by either a URI or a full {@link ResourceTemplate}.
|
|
6
6
|
*
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { isArrowFn } from "@tsed/core";
|
|
2
|
+
import { s } from "@tsed/schema";
|
|
2
3
|
import { context, inject, injectable, logger } from "@tsed/di";
|
|
3
|
-
import { JsonEntityStore } from "@tsed/schema";
|
|
4
|
-
import { constantCase } from "change-case";
|
|
5
4
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
5
|
+
import { constantCase } from "change-case";
|
|
6
6
|
import { toZod } from "../utils/toZod.js";
|
|
7
7
|
function mapOptions(options) {
|
|
8
8
|
let handler = undefined;
|
|
@@ -16,14 +16,17 @@ function mapOptions(options) {
|
|
|
16
16
|
const instance = inject(options.token);
|
|
17
17
|
return instance[propertyKey](...args);
|
|
18
18
|
});
|
|
19
|
-
const methodStore =
|
|
19
|
+
const methodStore = s.store.method(token, propertyKey);
|
|
20
20
|
options.description = options.description || methodStore.operation.get("description");
|
|
21
21
|
options.title = options.title || methodStore.schema.get("title");
|
|
22
22
|
}
|
|
23
23
|
return {
|
|
24
24
|
...options,
|
|
25
25
|
name,
|
|
26
|
-
argsSchema: toZod(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema
|
|
26
|
+
argsSchema: toZod(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema, {
|
|
27
|
+
useAlias: true,
|
|
28
|
+
groups: [name, "prompt"]
|
|
29
|
+
}),
|
|
27
30
|
handler: handler
|
|
28
31
|
};
|
|
29
32
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { context, inject, injectable, logger } from "@tsed/di";
|
|
2
|
-
import { JsonEntityStore } from "@tsed/schema";
|
|
3
|
-
import { constantCase } from "change-case";
|
|
4
2
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
3
|
+
import { constantCase } from "change-case";
|
|
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
|
}
|
|
@@ -14,7 +15,7 @@ function mapOptions(options) {
|
|
|
14
15
|
const instance = inject(options.token);
|
|
15
16
|
return instance[propertyKey](...args);
|
|
16
17
|
};
|
|
17
|
-
const methodStore =
|
|
18
|
+
const methodStore = s.store.method(token, propertyKey);
|
|
18
19
|
options.description = options.description || methodStore.operation.get("description");
|
|
19
20
|
options.title = options.title || methodStore.schema.get("title");
|
|
20
21
|
}
|
|
@@ -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
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { isArrowFn, isClass } from "@tsed/core";
|
|
2
|
+
import { JsonSchema, s } from "@tsed/schema";
|
|
2
3
|
import { context, inject, injectable, logger } from "@tsed/di";
|
|
3
|
-
import { deserialize } from "@tsed/json-mapper";
|
|
4
|
-
import { JsonEntityStore, JsonSchema } from "@tsed/schema";
|
|
5
|
-
import { constantCase } from "change-case";
|
|
6
4
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
5
|
+
import { constantCase } from "change-case";
|
|
6
|
+
import { deserialize } from "@tsed/json-mapper";
|
|
7
7
|
import { toZod } from "../utils/toZod.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();
|
|
11
12
|
}
|
|
12
13
|
function getInputSchema(token, propertyKey) {
|
|
13
|
-
return
|
|
14
|
+
return s.store(token, propertyKey, 0).schema?.itemSchema();
|
|
14
15
|
}
|
|
15
16
|
function resolveInputSchema(inputSchema) {
|
|
16
17
|
return isArrowFn(inputSchema) ? inputSchema() : inputSchema;
|
|
@@ -34,12 +35,12 @@ function mapOptions(options) {
|
|
|
34
35
|
let inputStore;
|
|
35
36
|
if ("propertyKey" in options) {
|
|
36
37
|
const { token, propertyKey } = options;
|
|
37
|
-
inputStore =
|
|
38
|
+
inputStore = s.store(token, propertyKey, 0);
|
|
38
39
|
handler = (args, extra) => {
|
|
39
40
|
const instance = inject(options.token);
|
|
40
41
|
return instance[options.propertyKey](args, extra);
|
|
41
42
|
};
|
|
42
|
-
const methodStore =
|
|
43
|
+
const methodStore = s.store.method(token, propertyKey);
|
|
43
44
|
options.description = options.description || methodStore.operation.get("description");
|
|
44
45
|
options.inputSchema =
|
|
45
46
|
options.inputSchema || (inputStore.schema?.itemSchema() || getInputSchema(token, propertyKey));
|
|
@@ -64,12 +65,21 @@ export function defineTool(options) {
|
|
|
64
65
|
...opts,
|
|
65
66
|
name: opts.name,
|
|
66
67
|
inputSchema: toZod(inputSchema, {
|
|
68
|
+
groups: [opts.name, "tools"],
|
|
69
|
+
useAlias: true
|
|
70
|
+
}),
|
|
71
|
+
outputSchema: toZod(opts.outputSchema, {
|
|
72
|
+
groups: [opts.name, "tools"],
|
|
67
73
|
useAlias: true
|
|
68
74
|
}),
|
|
69
|
-
outputSchema: toZod(opts.outputSchema),
|
|
70
75
|
async handler(args, extra) {
|
|
71
76
|
try {
|
|
72
|
-
|
|
77
|
+
const result = await handler(deserializeInput(args, inputSchema, inputStore), extra);
|
|
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/asResourceResponse.js";
|
|
15
|
+
export * from "./utils/asToolResponse.js";
|
|
14
16
|
export * from "./utils/toZod.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
1
|
import { constant, inject, injectable, injector } from "@tsed/di";
|
|
3
2
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
3
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
4
|
function collectTokens(type, configured = []) {
|
|
5
5
|
const tokens = new Set(configured);
|
|
6
6
|
injector()
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
2
|
-
import { constant, inject, injectable } from "@tsed/di";
|
|
3
1
|
import { application } from "@tsed/platform-http";
|
|
4
|
-
import {
|
|
2
|
+
import { constant, inject, injectable } from "@tsed/di";
|
|
5
3
|
import { MCP_SERVER } from "./McpServerFactory.js";
|
|
4
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
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.
|
|
8
8
|
*
|
|
@@ -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
|
+
}
|
package/lib/esm/utils/toZod.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { JsonSchema } from "@tsed/schema";
|
|
2
|
-
import { jsonSchemaToZod } from "json-schema-to-zod";
|
|
1
|
+
import { JsonSchema, s } from "@tsed/schema";
|
|
3
2
|
import { z } from "zod";
|
|
3
|
+
import { jsonSchemaToZod } from "json-schema-to-zod";
|
|
4
4
|
function transform(schema, opts) {
|
|
5
|
-
const schemaFactory = new Function("z", `return ${jsonSchemaToZod(
|
|
5
|
+
const schemaFactory = new Function("z", `return ${jsonSchemaToZod(s.compile(schema, { inlineRefs: true, ...opts }), { zodVersion: 4 })}`);
|
|
6
6
|
return schemaFactory(z);
|
|
7
7
|
}
|
|
8
8
|
/**
|