@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
package/src/fn/defineTool.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
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, isArrowFn, isClass, type Type} from "@tsed/core";
|
|
2
|
+
import type {CallToolResult, ServerNotification, ServerRequest, Tool, ToolAnnotations} from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
import {JsonEntityStore, JsonMethodStore, JsonSchema, s} from "@tsed/schema";
|
|
4
4
|
import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
|
|
5
|
-
import {deserialize} from "@tsed/json-mapper";
|
|
6
|
-
import {JsonEntityStore, JsonMethodStore, JsonSchema} from "@tsed/schema";
|
|
7
|
-
import {constantCase} from "change-case";
|
|
8
|
-
|
|
9
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";
|
|
8
|
+
import {deserialize} from "@tsed/json-mapper";
|
|
10
9
|
import {toZod} from "../utils/toZod.js";
|
|
10
|
+
import {asToolResponse} from "../utils/asToolResponse.js";
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Signature implemented by MCP tool handlers invoked through {@link defineTool}.
|
|
@@ -16,10 +16,10 @@ import {toZod} from "../utils/toZod.js";
|
|
|
16
16
|
* @module platform/mcp
|
|
17
17
|
* @since 8.17.0
|
|
18
18
|
*/
|
|
19
|
-
export type ToolCallback<Args = undefined> = (
|
|
19
|
+
export type ToolCallback<Args = undefined, Output = unknown> = (
|
|
20
20
|
args: Args,
|
|
21
21
|
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
|
|
22
|
-
) => CallToolResult | Promise<CallToolResult>;
|
|
22
|
+
) => CallToolResult | Promise<CallToolResult> | Output | Promise<Output>;
|
|
23
23
|
|
|
24
24
|
type BaseToolConfig = {
|
|
25
25
|
title?: string;
|
|
@@ -28,14 +28,14 @@ type BaseToolConfig = {
|
|
|
28
28
|
_meta?: Record<string, unknown>;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
type BaseToolProps<Input, Output =
|
|
31
|
+
type BaseToolProps<Input, Output = unknown> = BaseToolConfig & {
|
|
32
32
|
inputSchema?: JsonSchema<Input> | (() => JsonSchema<Input>) | Tool["inputSchema"];
|
|
33
33
|
outputSchema?: JsonSchema<Output> | Tool["outputSchema"];
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
-
export type FnToolProps<Input, Output =
|
|
36
|
+
export type FnToolProps<Input, Output = unknown> = BaseToolProps<Input, Output> & {
|
|
37
37
|
name: string;
|
|
38
|
-
handler: ToolCallback<Input>;
|
|
38
|
+
handler: ToolCallback<Input, Output>;
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
/**
|
|
@@ -44,7 +44,7 @@ export type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output
|
|
|
44
44
|
* @module platform/mcp
|
|
45
45
|
* @since 8.17.0
|
|
46
46
|
*/
|
|
47
|
-
export type ClassToolProps<Input, Output =
|
|
47
|
+
export type ClassToolProps<Input, Output = unknown> = BaseToolProps<Input, Output> & {
|
|
48
48
|
name?: string;
|
|
49
49
|
token: Type | AbstractType<any>;
|
|
50
50
|
propertyKey: string | symbol;
|
|
@@ -56,11 +56,12 @@ export type ClassToolProps<Input, Output = undefined> = BaseToolProps<Input, Out
|
|
|
56
56
|
* @module platform/mcp
|
|
57
57
|
* @since 8.17.0
|
|
58
58
|
*/
|
|
59
|
-
export type ToolProps<Input, Output =
|
|
59
|
+
export type ToolProps<Input, Output = unknown> = FnToolProps<Input, Output> | ClassToolProps<Input, Output>;
|
|
60
60
|
|
|
61
|
-
type MappedToolOptions<Input, Output =
|
|
62
|
-
handler: ToolCallback<Input>;
|
|
61
|
+
type MappedToolOptions<Input, Output = unknown> = Omit<ToolProps<Input, Output>, "token" | "propertyKey"> & {
|
|
62
|
+
handler: ToolCallback<Input, Output>;
|
|
63
63
|
inputStore?: JsonEntityStore;
|
|
64
|
+
outputStore?: JsonMethodStore;
|
|
64
65
|
};
|
|
65
66
|
|
|
66
67
|
function getOutputSchema<Output>(methodStore: JsonMethodStore): JsonSchema<Output> {
|
|
@@ -70,7 +71,7 @@ function getOutputSchema<Output>(methodStore: JsonMethodStore): JsonSchema<Outpu
|
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
function getInputSchema<Input>(token: Type<any> | AbstractType<any>, propertyKey: string | symbol): JsonSchema<Input> {
|
|
73
|
-
return
|
|
74
|
+
return s.store(token, propertyKey, 0).schema?.itemSchema() as JsonSchema<Input>;
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
function resolveInputSchema<Input>(inputSchema: JsonSchema<Input> | (() => JsonSchema<Input>) | Tool["inputSchema"] | undefined) {
|
|
@@ -95,19 +96,19 @@ function deserializeInput<Input>(args: Input, inputSchema: unknown, inputStore?:
|
|
|
95
96
|
}) as Input;
|
|
96
97
|
}
|
|
97
98
|
|
|
98
|
-
function mapOptions<Input, Output =
|
|
99
|
-
let handler: ToolCallback<Input>;
|
|
99
|
+
function mapOptions<Input, Output = unknown>(options: ToolProps<Input, Output>): MappedToolOptions<Input, Output> {
|
|
100
|
+
let handler: ToolCallback<Input, Output>;
|
|
100
101
|
let inputStore: JsonEntityStore | undefined;
|
|
101
102
|
|
|
102
103
|
if ("propertyKey" in options) {
|
|
103
104
|
const {token, propertyKey} = options;
|
|
104
|
-
inputStore =
|
|
105
|
+
inputStore = s.store(token, propertyKey, 0);
|
|
105
106
|
handler = (args: Input, extra: any) => {
|
|
106
107
|
const instance = inject(options.token) as any;
|
|
107
108
|
return instance[options.propertyKey](args, extra);
|
|
108
109
|
};
|
|
109
110
|
|
|
110
|
-
const methodStore =
|
|
111
|
+
const methodStore = s.store.method(token, propertyKey);
|
|
111
112
|
options.description = options.description || methodStore.operation.get("description");
|
|
112
113
|
options.inputSchema =
|
|
113
114
|
options.inputSchema || ((inputStore.schema?.itemSchema() || getInputSchema(token, propertyKey)) as JsonSchema<Input>);
|
|
@@ -141,9 +142,9 @@ function mapOptions<Input, Output = undefined>(options: ToolProps<Input, Output>
|
|
|
141
142
|
* });
|
|
142
143
|
* ```
|
|
143
144
|
*/
|
|
144
|
-
export function defineTool<Input, Output =
|
|
145
|
-
export function defineTool<Input, Output =
|
|
146
|
-
export function defineTool<Input, Output =
|
|
145
|
+
export function defineTool<Input, Output = unknown>(options: FnToolProps<Input, Output>): TokenProvider;
|
|
146
|
+
export function defineTool<Input, Output = unknown>(options: ClassToolProps<Input, Output>): TokenProvider;
|
|
147
|
+
export function defineTool<Input, Output = unknown>(options: ToolProps<Input, Output>) {
|
|
147
148
|
const provider = injectable(Symbol.for(`MCP:TOOL:${options.name}`))
|
|
148
149
|
.type(MCP_PROVIDER_TYPES.TOOL)
|
|
149
150
|
.factory(() => {
|
|
@@ -154,14 +155,26 @@ export function defineTool<Input, Output = undefined>(options: ToolProps<Input,
|
|
|
154
155
|
...opts,
|
|
155
156
|
name: opts.name,
|
|
156
157
|
inputSchema: toZod(inputSchema, {
|
|
158
|
+
groups: [opts.name!, "tools"],
|
|
159
|
+
useAlias: true
|
|
160
|
+
}),
|
|
161
|
+
outputSchema: toZod(opts.outputSchema, {
|
|
162
|
+
groups: [opts.name!, "tools"],
|
|
157
163
|
useAlias: true
|
|
158
164
|
}),
|
|
159
|
-
outputSchema: toZod(opts.outputSchema),
|
|
160
165
|
async handler(args: Input, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) {
|
|
161
166
|
try {
|
|
162
|
-
|
|
167
|
+
const result = await handler(deserializeInput(args, inputSchema, inputStore), extra);
|
|
168
|
+
|
|
169
|
+
logger().info({
|
|
170
|
+
event: "MCP_TOOL_END",
|
|
171
|
+
tool: opts.name
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
return asToolResponse(result as unknown as Record<string, unknown>);
|
|
163
175
|
} catch (er: any) {
|
|
164
176
|
const code = er.name && er.status ? `E_MCP_TOOL_${constantCase(er.name)}` : "E_MCP_TOOL_ERROR";
|
|
177
|
+
|
|
165
178
|
logger().error({
|
|
166
179
|
event: "MCP_TOOL_ERROR",
|
|
167
180
|
status_code: er.status,
|
|
@@ -172,16 +185,15 @@ export function defineTool<Input, Output = undefined>(options: ToolProps<Input,
|
|
|
172
185
|
tool: opts.name
|
|
173
186
|
});
|
|
174
187
|
|
|
175
|
-
return
|
|
176
|
-
|
|
177
|
-
structuredContent: {
|
|
188
|
+
return asToolResponse(
|
|
189
|
+
{
|
|
178
190
|
status_code: er.status,
|
|
179
191
|
code,
|
|
180
192
|
message: er.message,
|
|
181
|
-
request_id: context().id,
|
|
182
193
|
tool: opts.name
|
|
183
|
-
}
|
|
184
|
-
|
|
194
|
+
},
|
|
195
|
+
{isError: true}
|
|
196
|
+
);
|
|
185
197
|
}
|
|
186
198
|
}
|
|
187
199
|
};
|
package/src/index.ts
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 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
|
/**
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import {McpServer} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
-
import {constant, inject, injectable, injector, type TokenProvider} from "@tsed/di";
|
|
4
|
-
|
|
1
|
+
import {type TokenProvider, constant, inject, injectable, injector} from "@tsed/di";
|
|
5
2
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
3
|
+
import {McpServer} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
|
+
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
6
5
|
import type {PromptsSettings} from "../fn/definePrompt.js";
|
|
7
6
|
import type {ResourceSettings} from "../fn/defineResource.js";
|
|
7
|
+
import type {ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
8
8
|
import type {ToolProps} from "../fn/defineTool.js";
|
|
9
|
-
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
10
9
|
|
|
11
10
|
function collectTokens(type: string, configured: TokenProvider[] = []): TokenProvider[] {
|
|
12
11
|
const tokens = new Set<TokenProvider>(configured);
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest";
|
|
1
2
|
import {EventEmitter} from "node:events";
|
|
2
|
-
|
|
3
3
|
import {PlatformFastifyResponse} from "@tsed/platform-fastify";
|
|
4
|
-
import {application} from "@tsed/platform-http";
|
|
5
|
-
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
6
|
-
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest";
|
|
7
|
-
|
|
8
4
|
import {PlatformMcpModule} from "./PlatformMcpModule.js";
|
|
5
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
6
|
+
import {application} from "@tsed/platform-http";
|
|
9
7
|
|
|
10
8
|
const {TestTransport, transportInstances} = vi.hoisted(() => {
|
|
11
9
|
const transportInstances: TestTransport[] = [];
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
import {type OnRoutesInit, PlatformContext, type PlatformRouteDetails, application} from "@tsed/platform-http";
|
|
2
|
+
import {constant, inject, injectable} from "@tsed/di";
|
|
3
|
+
import {MCP_SERVER} from "./McpServerFactory.js";
|
|
1
4
|
import type {McpServer} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
5
|
+
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
2
6
|
import {StreamableHTTPServerTransport} from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
3
|
-
import {constant, inject, injectable} from "@tsed/di";
|
|
4
|
-
import {application, type OnRoutesInit, PlatformContext, type PlatformRouteDetails} from "@tsed/platform-http";
|
|
5
7
|
import {useContextHandler} from "@tsed/platform-router";
|
|
6
8
|
|
|
7
|
-
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
8
|
-
import {MCP_SERVER} from "./McpServerFactory.js";
|
|
9
|
-
|
|
10
9
|
/**
|
|
11
10
|
* Platform module that mounts the MCP HTTP endpoint and forwards requests to the configured server instance.
|
|
12
11
|
*
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {asResourceResponse} from "./asResourceResponse.js";
|
|
2
|
+
|
|
3
|
+
describe("asResponse", () => {
|
|
4
|
+
it("should return a JSON content item for object payloads", () => {
|
|
5
|
+
expect(asResourceResponse("tsed://resource", {id: "resource-id"})).toEqual({
|
|
6
|
+
contents: [
|
|
7
|
+
{
|
|
8
|
+
uri: "tsed://resource",
|
|
9
|
+
mimeType: "application/json",
|
|
10
|
+
text: '{\n "id": "resource-id"\n}'
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should preserve an existing resource response", () => {
|
|
17
|
+
const response = {
|
|
18
|
+
contents: [{url: "tsed://resource", mimeType: "text/plain", text: "ready"}]
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
expect(asResourceResponse("tsed://ignored", response)).toBe(response);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should add a text error item before the JSON error payload", () => {
|
|
25
|
+
expect(asResourceResponse("tsed://resource", {message: "boom"}, {isError: true})).toEqual({
|
|
26
|
+
contents: [
|
|
27
|
+
{uri: "tsed://resource", mimeType: "plain/text", text: "boom"},
|
|
28
|
+
{uri: "tsed://resource", mimeType: "application/json", text: '{\n "message": "boom"\n}'}
|
|
29
|
+
]
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {serialize} from "@tsed/json-mapper";
|
|
2
|
+
import {isObject} from "@tsed/core";
|
|
3
|
+
import {ReadResourceResult} from "@modelcontextprotocol/sdk/types.js";
|
|
4
|
+
|
|
5
|
+
export function asResourceResponse(
|
|
6
|
+
uri: string,
|
|
7
|
+
payload: Record<string, unknown> | ReadResourceResult,
|
|
8
|
+
opts?: {isError?: boolean}
|
|
9
|
+
): ReadResourceResult {
|
|
10
|
+
if (payload.contents) {
|
|
11
|
+
return payload as ReadResourceResult;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const contents: ReadResourceResult["contents"] = [];
|
|
15
|
+
|
|
16
|
+
if (opts?.isError) {
|
|
17
|
+
contents.push({
|
|
18
|
+
uri,
|
|
19
|
+
mimeType: "plain/text" as const,
|
|
20
|
+
text: (payload as any).message || ""
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (isObject(payload)) {
|
|
25
|
+
payload = serialize(payload, {useAlias: true, groups: ["resources"]});
|
|
26
|
+
contents.push({
|
|
27
|
+
uri,
|
|
28
|
+
mimeType: "application/json" as const,
|
|
29
|
+
text: JSON.stringify(payload, null, 2)
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
contents
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {asToolResponse} from "./asToolResponse.js";
|
|
2
|
+
|
|
3
|
+
describe("asStructuredResponse", () => {
|
|
4
|
+
it("should expose object payloads as text and structured content", () => {
|
|
5
|
+
expect(asToolResponse({id: "tool-id"})).toEqual({
|
|
6
|
+
content: [{type: "text", text: '{\n "id": "tool-id"\n}'}],
|
|
7
|
+
structuredContent: {id: "tool-id"}
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("should preserve an existing structured response", () => {
|
|
12
|
+
const response = {content: [{type: "text", text: "ready"}], structuredContent: {ready: true}};
|
|
13
|
+
|
|
14
|
+
expect(asToolResponse(response)).toBe(response);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should mark structured error responses", () => {
|
|
18
|
+
expect(asToolResponse({message: "boom"}, {isError: true})).toEqual({
|
|
19
|
+
isError: true,
|
|
20
|
+
content: [{type: "text", text: '{\n "message": "boom"\n}'}],
|
|
21
|
+
structuredContent: {message: "boom"}
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should expose primitive payloads as text-only content", () => {
|
|
26
|
+
expect(asToolResponse("ready" as never)).toEqual({
|
|
27
|
+
content: [{type: "text", text: '"ready"'}]
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {serialize} from "@tsed/json-mapper";
|
|
2
|
+
import {isObject} from "@tsed/core";
|
|
3
|
+
import type {CallToolResult} from "@modelcontextprotocol/sdk/types.js";
|
|
4
|
+
|
|
5
|
+
export interface StructuredResponse<
|
|
6
|
+
T extends {
|
|
7
|
+
[x: string]: unknown;
|
|
8
|
+
} = {
|
|
9
|
+
[x: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
> extends CallToolResult {
|
|
12
|
+
structuredContent?: T | undefined;
|
|
13
|
+
isError?: true;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function asToolResponse<
|
|
17
|
+
T extends {
|
|
18
|
+
[x: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
>(payload: T | StructuredResponse, opts?: {isError?: true}): StructuredResponse<T> {
|
|
21
|
+
if (isObject(payload)) {
|
|
22
|
+
if ("content" in payload || "structuredContent" in payload) {
|
|
23
|
+
return payload as StructuredResponse<T>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
payload = serialize(payload, {useAlias: true, groups: ["tools"]});
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
...opts,
|
|
30
|
+
content: [
|
|
31
|
+
{
|
|
32
|
+
type: "text" as const,
|
|
33
|
+
text: JSON.stringify(payload, null, 2)
|
|
34
|
+
}
|
|
35
|
+
],
|
|
36
|
+
structuredContent: payload as T
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
...opts,
|
|
42
|
+
content: [
|
|
43
|
+
{
|
|
44
|
+
type: "text" as const,
|
|
45
|
+
text: JSON.stringify(payload, null, 2)
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
};
|
|
49
|
+
}
|
package/src/utils/toZod.spec.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {JsonSchema, s, string} from "@tsed/schema";
|
|
2
|
-
|
|
1
|
+
import {JsonSchema, Property, s, string} from "@tsed/schema";
|
|
3
2
|
import {toZod} from "./toZod.js";
|
|
4
3
|
|
|
5
4
|
describe("toZod", () => {
|
|
@@ -14,7 +13,7 @@ describe("toZod", () => {
|
|
|
14
13
|
});
|
|
15
14
|
|
|
16
15
|
it("should preserve aliases when useAlias is enabled", () => {
|
|
17
|
-
const schema = JsonSchema
|
|
16
|
+
const schema = new JsonSchema({
|
|
18
17
|
type: "object",
|
|
19
18
|
properties: {
|
|
20
19
|
prop: {
|
|
@@ -38,4 +37,27 @@ describe("toZod", () => {
|
|
|
38
37
|
additionalProperties: false
|
|
39
38
|
});
|
|
40
39
|
});
|
|
40
|
+
|
|
41
|
+
it("should inline local schema references before converting to Zod", () => {
|
|
42
|
+
class Child {
|
|
43
|
+
@Property()
|
|
44
|
+
id: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const result = toZod(s.object({child: s.from(Child)}));
|
|
48
|
+
|
|
49
|
+
expect(result?.toJSONSchema()).toMatchObject({
|
|
50
|
+
type: "object",
|
|
51
|
+
properties: {
|
|
52
|
+
child: {
|
|
53
|
+
type: "object",
|
|
54
|
+
properties: {
|
|
55
|
+
id: {
|
|
56
|
+
type: "string"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
});
|
|
41
63
|
});
|
package/src/utils/toZod.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {JsonSchema, JsonSchemaOptions} from "@tsed/schema";
|
|
2
|
-
import {jsonSchemaToZod} from "json-schema-to-zod";
|
|
1
|
+
import {JsonSchema, JsonSchemaOptions, s} from "@tsed/schema";
|
|
3
2
|
import {z, type ZodObject} from "zod";
|
|
3
|
+
import {jsonSchemaToZod} from "json-schema-to-zod";
|
|
4
4
|
|
|
5
5
|
function transform(schema: JsonSchema, opts?: JsonSchemaOptions): ZodObject {
|
|
6
|
-
const schemaFactory = new Function("z", `return ${jsonSchemaToZod(
|
|
6
|
+
const schemaFactory = new Function("z", `return ${jsonSchemaToZod(s.compile(schema, {inlineRefs: true, ...opts}), {zodVersion: 4})}`) as (
|
|
7
|
+
zod: unknown
|
|
8
|
+
) => ZodObject;
|
|
7
9
|
|
|
8
10
|
return schemaFactory(z);
|
|
9
11
|
}
|