@tsed/platform-mcp 8.35.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/fn/definePrompt.js +4 -1
- package/lib/esm/fn/defineResource.js +17 -13
- package/lib/esm/fn/defineTool.js +18 -12
- package/lib/esm/index.js +2 -0
- package/lib/esm/utils/asResourceResponse.js +26 -0
- package/lib/esm/utils/asToolResponse.js +29 -0
- package/lib/esm/utils/toZod.js +2 -2
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/fn/defineResource.d.ts +8 -6
- package/lib/types/fn/defineTool.d.ts +8 -8
- package/lib/types/index.d.ts +2 -0
- package/lib/types/utils/asResourceResponse.d.ts +4 -0
- package/lib/types/utils/asToolResponse.d.ts +14 -0
- package/package.json +8 -8
- package/src/fn/definePrompt.ts +6 -3
- package/src/fn/defineResource.spec.ts +54 -29
- package/src/fn/defineResource.ts +26 -11
- package/src/fn/defineTool.spec.ts +47 -4
- package/src/fn/defineTool.ts +37 -24
- package/src/index.ts +2 -0
- 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 +24 -1
- package/src/utils/toZod.ts +5 -3
|
@@ -23,7 +23,10 @@ function mapOptions(options) {
|
|
|
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
|
}
|
|
@@ -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
|
@@ -5,6 +5,7 @@ import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
|
5
5
|
import { constantCase } from "change-case";
|
|
6
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();
|
|
@@ -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";
|
|
@@ -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";
|
|
1
|
+
import { JsonSchema, s } from "@tsed/schema";
|
|
2
2
|
import { z } from "zod";
|
|
3
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
|
/**
|