@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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {Default, JsonSchema, Name, Property,
|
|
1
|
+
import {Default, from, JsonSchema, Name, Property, s, string} from "@tsed/schema";
|
|
2
2
|
import {afterEach, beforeEach, describe, expect, it} from "vitest";
|
|
3
3
|
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
4
4
|
import {defineTool} from "./defineTool.js";
|
|
@@ -39,11 +39,10 @@ describe("defineTool", () => {
|
|
|
39
39
|
|
|
40
40
|
const result = await definition.handler({}, {} as any);
|
|
41
41
|
|
|
42
|
+
expect(result.isError).toBe(true);
|
|
42
43
|
expect(result.structuredContent).toEqual({
|
|
43
|
-
status_code: undefined,
|
|
44
44
|
code: "E_MCP_TOOL_ERROR",
|
|
45
45
|
message: "boom",
|
|
46
|
-
request_id: expect.any(String),
|
|
47
46
|
tool: "failing-tool"
|
|
48
47
|
});
|
|
49
48
|
});
|
|
@@ -62,11 +61,11 @@ describe("defineTool", () => {
|
|
|
62
61
|
const definition = inject<any>(token);
|
|
63
62
|
const result = await definition.handler({}, {} as any);
|
|
64
63
|
|
|
64
|
+
expect(result.isError).toBe(true);
|
|
65
65
|
expect(result.structuredContent).toEqual({
|
|
66
66
|
status_code: 404,
|
|
67
67
|
code: "E_MCP_TOOL_NOT_FOUND",
|
|
68
68
|
message: "Not found",
|
|
69
|
-
request_id: expect.any(String),
|
|
70
69
|
tool: "http-tool"
|
|
71
70
|
});
|
|
72
71
|
});
|
|
@@ -90,16 +89,36 @@ describe("defineTool", () => {
|
|
|
90
89
|
|
|
91
90
|
const definition = inject<any>(token);
|
|
92
91
|
|
|
93
|
-
expect(definition.inputSchema.
|
|
94
|
-
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
92
|
+
expect(definition.inputSchema["~standard"].jsonSchema.input({target: "draft-2020-12"})).toEqual({
|
|
95
93
|
type: "object",
|
|
96
94
|
properties: {
|
|
97
95
|
aliasProp: {
|
|
98
96
|
type: "string"
|
|
99
97
|
}
|
|
100
98
|
},
|
|
101
|
-
required: ["aliasProp"]
|
|
102
|
-
|
|
99
|
+
required: ["aliasProp"]
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should expose aliases declared by functional output schemas", () => {
|
|
104
|
+
const token = defineTool({
|
|
105
|
+
name: "aliased-tool-output",
|
|
106
|
+
outputSchema: s.object({prop: string().required()}).addAlias("prop", "aliasProp"),
|
|
107
|
+
handler() {
|
|
108
|
+
return {prop: "value"};
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const definition = inject<any>(token);
|
|
113
|
+
|
|
114
|
+
expect(definition.outputSchema["~standard"].jsonSchema.input({target: "draft-2020-12"})).toMatchObject({
|
|
115
|
+
type: "object",
|
|
116
|
+
properties: {
|
|
117
|
+
aliasProp: {
|
|
118
|
+
type: "string"
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
required: ["aliasProp"]
|
|
103
122
|
});
|
|
104
123
|
});
|
|
105
124
|
|
|
@@ -128,4 +147,26 @@ describe("defineTool", () => {
|
|
|
128
147
|
{} as any
|
|
129
148
|
);
|
|
130
149
|
});
|
|
150
|
+
|
|
151
|
+
it("should normalize successful tool payloads as structured content", async () => {
|
|
152
|
+
const token = defineTool({
|
|
153
|
+
name: "successful-tool",
|
|
154
|
+
outputSchema: s.object({id: string()}),
|
|
155
|
+
handler() {
|
|
156
|
+
return {id: "tool-id"};
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const definition = inject<any>(token);
|
|
161
|
+
|
|
162
|
+
await expect(definition.handler({}, {} as any)).resolves.toEqual({
|
|
163
|
+
content: [
|
|
164
|
+
{
|
|
165
|
+
type: "text",
|
|
166
|
+
text: '{\n "id": "tool-id"\n}'
|
|
167
|
+
}
|
|
168
|
+
],
|
|
169
|
+
structuredContent: {id: "tool-id"}
|
|
170
|
+
});
|
|
171
|
+
});
|
|
131
172
|
});
|
package/src/fn/defineTool.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {type AbstractType,
|
|
2
|
-
import type {CallToolResult,
|
|
1
|
+
import {type AbstractType, isArrowFn, isClass, type Type} from "@tsed/core";
|
|
2
|
+
import type {CallToolResult, ServerContext, Tool, ToolAnnotations} from "@modelcontextprotocol/server";
|
|
3
3
|
import {JsonEntityStore, JsonMethodStore, JsonSchema, s} from "@tsed/schema";
|
|
4
|
-
import {
|
|
4
|
+
import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
|
|
5
5
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
6
|
-
import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
7
6
|
import {constantCase} from "change-case";
|
|
8
7
|
import {deserialize} from "@tsed/json-mapper";
|
|
9
|
-
import {
|
|
8
|
+
import {fromJsonSchema} from "../utils/fromJsonSchema.js";
|
|
9
|
+
import {asToolResponse} from "../utils/asToolResponse.js";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Signature implemented by MCP tool handlers invoked through {@link defineTool}.
|
|
@@ -15,10 +15,10 @@ import {toZod} from "../utils/toZod.js";
|
|
|
15
15
|
* @module platform/mcp
|
|
16
16
|
* @since 8.17.0
|
|
17
17
|
*/
|
|
18
|
-
export type ToolCallback<Args = undefined> = (
|
|
18
|
+
export type ToolCallback<Args = undefined, Output = unknown> = (
|
|
19
19
|
args: Args,
|
|
20
|
-
|
|
21
|
-
) => CallToolResult | Promise<CallToolResult>;
|
|
20
|
+
ctx: ServerContext
|
|
21
|
+
) => CallToolResult | Promise<CallToolResult> | Output | Promise<Output>;
|
|
22
22
|
|
|
23
23
|
type BaseToolConfig = {
|
|
24
24
|
title?: string;
|
|
@@ -27,14 +27,14 @@ type BaseToolConfig = {
|
|
|
27
27
|
_meta?: Record<string, unknown>;
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
type BaseToolProps<Input, Output =
|
|
30
|
+
type BaseToolProps<Input, Output = unknown> = BaseToolConfig & {
|
|
31
31
|
inputSchema?: JsonSchema<Input> | (() => JsonSchema<Input>) | Tool["inputSchema"];
|
|
32
32
|
outputSchema?: JsonSchema<Output> | Tool["outputSchema"];
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
export type FnToolProps<Input, Output =
|
|
35
|
+
export type FnToolProps<Input, Output = unknown> = BaseToolProps<Input, Output> & {
|
|
36
36
|
name: string;
|
|
37
|
-
handler: ToolCallback<Input>;
|
|
37
|
+
handler: ToolCallback<Input, Output>;
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
/**
|
|
@@ -43,7 +43,7 @@ export type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output
|
|
|
43
43
|
* @module platform/mcp
|
|
44
44
|
* @since 8.17.0
|
|
45
45
|
*/
|
|
46
|
-
export type ClassToolProps<Input, Output =
|
|
46
|
+
export type ClassToolProps<Input, Output = unknown> = BaseToolProps<Input, Output> & {
|
|
47
47
|
name?: string;
|
|
48
48
|
token: Type | AbstractType<any>;
|
|
49
49
|
propertyKey: string | symbol;
|
|
@@ -55,11 +55,12 @@ export type ClassToolProps<Input, Output = undefined> = BaseToolProps<Input, Out
|
|
|
55
55
|
* @module platform/mcp
|
|
56
56
|
* @since 8.17.0
|
|
57
57
|
*/
|
|
58
|
-
export type ToolProps<Input, Output =
|
|
58
|
+
export type ToolProps<Input, Output = unknown> = FnToolProps<Input, Output> | ClassToolProps<Input, Output>;
|
|
59
59
|
|
|
60
|
-
type MappedToolOptions<Input, Output =
|
|
61
|
-
handler: ToolCallback<Input>;
|
|
60
|
+
type MappedToolOptions<Input, Output = unknown> = Omit<ToolProps<Input, Output>, "token" | "propertyKey"> & {
|
|
61
|
+
handler: ToolCallback<Input, Output>;
|
|
62
62
|
inputStore?: JsonEntityStore;
|
|
63
|
+
outputStore?: JsonMethodStore;
|
|
63
64
|
};
|
|
64
65
|
|
|
65
66
|
function getOutputSchema<Output>(methodStore: JsonMethodStore): JsonSchema<Output> {
|
|
@@ -94,8 +95,8 @@ function deserializeInput<Input>(args: Input, inputSchema: unknown, inputStore?:
|
|
|
94
95
|
}) as Input;
|
|
95
96
|
}
|
|
96
97
|
|
|
97
|
-
function mapOptions<Input, Output =
|
|
98
|
-
let handler: ToolCallback<Input>;
|
|
98
|
+
function mapOptions<Input, Output = unknown>(options: ToolProps<Input, Output>): MappedToolOptions<Input, Output> {
|
|
99
|
+
let handler: ToolCallback<Input, Output>;
|
|
99
100
|
let inputStore: JsonEntityStore | undefined;
|
|
100
101
|
|
|
101
102
|
if ("propertyKey" in options) {
|
|
@@ -140,9 +141,9 @@ function mapOptions<Input, Output = undefined>(options: ToolProps<Input, Output>
|
|
|
140
141
|
* });
|
|
141
142
|
* ```
|
|
142
143
|
*/
|
|
143
|
-
export function defineTool<Input, Output =
|
|
144
|
-
export function defineTool<Input, Output =
|
|
145
|
-
export function defineTool<Input, Output =
|
|
144
|
+
export function defineTool<Input, Output = unknown>(options: FnToolProps<Input, Output>): TokenProvider;
|
|
145
|
+
export function defineTool<Input, Output = unknown>(options: ClassToolProps<Input, Output>): TokenProvider;
|
|
146
|
+
export function defineTool<Input, Output = unknown>(options: ToolProps<Input, Output>) {
|
|
146
147
|
const provider = injectable(Symbol.for(`MCP:TOOL:${options.name}`))
|
|
147
148
|
.type(MCP_PROVIDER_TYPES.TOOL)
|
|
148
149
|
.factory(() => {
|
|
@@ -152,15 +153,27 @@ export function defineTool<Input, Output = undefined>(options: ToolProps<Input,
|
|
|
152
153
|
return {
|
|
153
154
|
...opts,
|
|
154
155
|
name: opts.name,
|
|
155
|
-
inputSchema:
|
|
156
|
+
inputSchema: fromJsonSchema(inputSchema, {
|
|
157
|
+
groups: [opts.name!, "tools"],
|
|
156
158
|
useAlias: true
|
|
157
159
|
}),
|
|
158
|
-
outputSchema:
|
|
159
|
-
|
|
160
|
+
outputSchema: fromJsonSchema(opts.outputSchema, {
|
|
161
|
+
groups: [opts.name!, "tools"],
|
|
162
|
+
useAlias: true
|
|
163
|
+
}),
|
|
164
|
+
async handler(args: Input, ctx: ServerContext) {
|
|
160
165
|
try {
|
|
161
|
-
|
|
166
|
+
const result = await handler(deserializeInput(args, inputSchema, inputStore), ctx);
|
|
167
|
+
|
|
168
|
+
logger().info({
|
|
169
|
+
event: "MCP_TOOL_END",
|
|
170
|
+
tool: opts.name
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
return asToolResponse(result as unknown as Record<string, unknown>);
|
|
162
174
|
} catch (er: any) {
|
|
163
175
|
const code = er.name && er.status ? `E_MCP_TOOL_${constantCase(er.name)}` : "E_MCP_TOOL_ERROR";
|
|
176
|
+
|
|
164
177
|
logger().error({
|
|
165
178
|
event: "MCP_TOOL_ERROR",
|
|
166
179
|
status_code: er.status,
|
|
@@ -171,16 +184,15 @@ export function defineTool<Input, Output = undefined>(options: ToolProps<Input,
|
|
|
171
184
|
tool: opts.name
|
|
172
185
|
});
|
|
173
186
|
|
|
174
|
-
return
|
|
175
|
-
|
|
176
|
-
structuredContent: {
|
|
187
|
+
return asToolResponse(
|
|
188
|
+
{
|
|
177
189
|
status_code: er.status,
|
|
178
190
|
code,
|
|
179
191
|
message: er.message,
|
|
180
|
-
request_id: context().id,
|
|
181
192
|
tool: opts.name
|
|
182
|
-
}
|
|
183
|
-
|
|
193
|
+
},
|
|
194
|
+
{isError: true}
|
|
195
|
+
);
|
|
184
196
|
}
|
|
185
197
|
}
|
|
186
198
|
};
|
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/
|
|
14
|
+
export * from "./utils/asResourceResponse.js";
|
|
15
|
+
export * from "./utils/asToolResponse.js";
|
|
16
|
+
export * from "./utils/fromJsonSchema.js";
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type {Implementation} from "@modelcontextprotocol/
|
|
2
|
-
import type {
|
|
3
|
-
import type {StreamableHTTPServerTransportOptions} from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
1
|
+
import type {Implementation, ServerOptions} from "@modelcontextprotocol/server";
|
|
2
|
+
import type {StreamableHTTPServerTransportOptions} from "@modelcontextprotocol/node";
|
|
4
3
|
import type {TokenProvider} from "@tsed/di";
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import {type TokenProvider, 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, type ResourceTemplate} from "@modelcontextprotocol/server";
|
|
4
4
|
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
5
5
|
import type {PromptsSettings} from "../fn/definePrompt.js";
|
|
6
6
|
import type {ResourceSettings} from "../fn/defineResource.js";
|
|
7
|
-
import type {ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
8
7
|
import type {ToolProps} from "../fn/defineTool.js";
|
|
9
8
|
|
|
10
9
|
function collectTokens(type: string, configured: TokenProvider[] = []): TokenProvider[] {
|
|
@@ -20,9 +20,9 @@ const {TestTransport, transportInstances} = vi.hoisted(() => {
|
|
|
20
20
|
return {TestTransport, transportInstances};
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
-
vi.mock("@modelcontextprotocol/
|
|
23
|
+
vi.mock("@modelcontextprotocol/node", () => {
|
|
24
24
|
return {
|
|
25
|
-
|
|
25
|
+
NodeStreamableHTTPServerTransport: TestTransport
|
|
26
26
|
};
|
|
27
27
|
});
|
|
28
28
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {type OnRoutesInit, PlatformContext, type PlatformRouteDetails, application} from "@tsed/platform-http";
|
|
2
2
|
import {constant, inject, injectable} from "@tsed/di";
|
|
3
3
|
import {MCP_SERVER} from "./McpServerFactory.js";
|
|
4
|
-
import type
|
|
4
|
+
import {type McpServer} from "@modelcontextprotocol/server";
|
|
5
5
|
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
6
|
-
import {
|
|
6
|
+
import {NodeStreamableHTTPServerTransport} from "@modelcontextprotocol/node";
|
|
7
7
|
import {useContextHandler} from "@tsed/platform-router";
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -45,7 +45,7 @@ export class PlatformMcpModule implements OnRoutesInit {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
protected async dispatch($ctx: PlatformContext) {
|
|
48
|
-
const transport = new
|
|
48
|
+
const transport = new NodeStreamableHTTPServerTransport({
|
|
49
49
|
sessionIdGenerator: undefined,
|
|
50
50
|
enableJsonResponse: true,
|
|
51
51
|
...this.settings.transportOptions
|
|
@@ -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 type {ReadResourceResult} from "@modelcontextprotocol/server";
|
|
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/server";
|
|
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
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {JsonSchema, Property, s, string} from "@tsed/schema";
|
|
2
|
+
import {fromJsonSchema} from "./fromJsonSchema.js";
|
|
3
|
+
|
|
4
|
+
describe("fromJsonSchema", () => {
|
|
5
|
+
it("should wrap a Ts.ED JsonSchema with the MCP SDK adapter", () => {
|
|
6
|
+
const schema = s.object({
|
|
7
|
+
prop1: string()
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const result = fromJsonSchema(schema);
|
|
11
|
+
|
|
12
|
+
expect(result?.["~standard"].jsonSchema.input({target: "draft-2020-12"})).toMatchObject({
|
|
13
|
+
type: "object",
|
|
14
|
+
properties: {
|
|
15
|
+
prop1: {
|
|
16
|
+
type: "string"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should preserve aliases when compiling a Ts.ED JsonSchema", () => {
|
|
23
|
+
const schema = new JsonSchema({
|
|
24
|
+
type: "object",
|
|
25
|
+
properties: {
|
|
26
|
+
prop: {
|
|
27
|
+
type: "string"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
required: ["prop"]
|
|
31
|
+
}).addAlias("prop", "aliasProp");
|
|
32
|
+
|
|
33
|
+
const result = fromJsonSchema(schema, {useAlias: true});
|
|
34
|
+
|
|
35
|
+
expect(result?.["~standard"].jsonSchema.input({target: "draft-2020-12"})).toMatchObject({
|
|
36
|
+
type: "object",
|
|
37
|
+
properties: {
|
|
38
|
+
aliasProp: {
|
|
39
|
+
type: "string"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
required: ["aliasProp"]
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should inline local schema references", () => {
|
|
47
|
+
class Child {
|
|
48
|
+
@Property()
|
|
49
|
+
id: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const result = fromJsonSchema(s.object({child: s.from(Child)}));
|
|
53
|
+
|
|
54
|
+
expect(result?.["~standard"].jsonSchema.input({target: "draft-2020-12"})).toMatchObject({
|
|
55
|
+
type: "object",
|
|
56
|
+
properties: {
|
|
57
|
+
child: {
|
|
58
|
+
type: "object",
|
|
59
|
+
properties: {
|
|
60
|
+
id: {
|
|
61
|
+
type: "string"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {fromJsonSchema as fromJSchema} from "@modelcontextprotocol/server";
|
|
2
|
+
import {JsonSchema, type JsonSchemaOptions, s} from "@tsed/schema";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts a Ts.ED schema into the MCP SDK v2 Standard Schema adapter.
|
|
6
|
+
*
|
|
7
|
+
* @param schema Ts.ED or plain JSON Schema document.
|
|
8
|
+
* @param opts Compilation options applied to Ts.ED schemas.
|
|
9
|
+
* @module platform/mcp
|
|
10
|
+
* @since 8.17.0
|
|
11
|
+
*/
|
|
12
|
+
export function fromJsonSchema<T = unknown>(schema: unknown, opts?: JsonSchemaOptions) {
|
|
13
|
+
if (!schema) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const jsonSchema = schema instanceof JsonSchema ? s.compile(schema, {inlineRefs: true, ...opts}) : schema;
|
|
18
|
+
|
|
19
|
+
return fromJSchema<T>(jsonSchema as any);
|
|
20
|
+
}
|
package/vitest.config.mts
CHANGED
package/lib/esm/utils/toZod.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { JsonSchema } from "@tsed/schema";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
import { jsonSchemaToZod } from "json-schema-to-zod";
|
|
4
|
-
function transform(schema, opts) {
|
|
5
|
-
const schemaFactory = new Function("z", `return ${jsonSchemaToZod(schema.toJSON(opts), { zodVersion: 4 })}`);
|
|
6
|
-
return schemaFactory(z);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Converts a {@link JsonSchema} produced by Ts.ED into an executable Zod validation object.
|
|
10
|
-
*
|
|
11
|
-
* @param schema JsonSchema instance or a pre-built Zod schema.
|
|
12
|
-
* @param opts
|
|
13
|
-
* @returns A Zod object when conversion is possible, otherwise the original schema.
|
|
14
|
-
* @module platform/mcp
|
|
15
|
-
* @since 8.17.0
|
|
16
|
-
*/
|
|
17
|
-
export function toZod(schema, opts) {
|
|
18
|
-
return schema && schema instanceof JsonSchema ? transform(schema, opts) : schema;
|
|
19
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { JsonSchemaOptions } from "@tsed/schema";
|
|
2
|
-
import { type ZodObject } from "zod";
|
|
3
|
-
/**
|
|
4
|
-
* Converts a {@link JsonSchema} produced by Ts.ED into an executable Zod validation object.
|
|
5
|
-
*
|
|
6
|
-
* @param schema JsonSchema instance or a pre-built Zod schema.
|
|
7
|
-
* @param opts
|
|
8
|
-
* @returns A Zod object when conversion is possible, otherwise the original schema.
|
|
9
|
-
* @module platform/mcp
|
|
10
|
-
* @since 8.17.0
|
|
11
|
-
*/
|
|
12
|
-
export declare function toZod(schema: unknown, opts?: JsonSchemaOptions): ZodObject | undefined;
|
package/src/utils/toZod.spec.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import {JsonSchema, s, string} from "@tsed/schema";
|
|
2
|
-
import {toZod} from "./toZod.js";
|
|
3
|
-
|
|
4
|
-
describe("toZod", () => {
|
|
5
|
-
it("should transform JsonSchema to Zod instance", async () => {
|
|
6
|
-
const schema = s.object({
|
|
7
|
-
prop1: string()
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
const result = toZod(schema);
|
|
11
|
-
|
|
12
|
-
expect(result?.toJSONSchema).toBeDefined();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it("should preserve aliases when useAlias is enabled", () => {
|
|
16
|
-
const schema = new JsonSchema({
|
|
17
|
-
type: "object",
|
|
18
|
-
properties: {
|
|
19
|
-
prop: {
|
|
20
|
-
type: "string"
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
required: ["prop"]
|
|
24
|
-
}).addAlias("prop", "aliasProp");
|
|
25
|
-
|
|
26
|
-
const result = toZod(schema, {useAlias: true});
|
|
27
|
-
|
|
28
|
-
expect(result?.toJSONSchema()).toEqual({
|
|
29
|
-
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
30
|
-
type: "object",
|
|
31
|
-
properties: {
|
|
32
|
-
aliasProp: {
|
|
33
|
-
type: "string"
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
required: ["aliasProp"],
|
|
37
|
-
additionalProperties: false
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
});
|
package/src/utils/toZod.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import {JsonSchema, JsonSchemaOptions} from "@tsed/schema";
|
|
2
|
-
import {type ZodObject, z} from "zod";
|
|
3
|
-
import {jsonSchemaToZod} from "json-schema-to-zod";
|
|
4
|
-
|
|
5
|
-
function transform(schema: JsonSchema, opts?: JsonSchemaOptions): ZodObject {
|
|
6
|
-
const schemaFactory = new Function("z", `return ${jsonSchemaToZod(schema.toJSON(opts), {zodVersion: 4})}`) as (zod: unknown) => ZodObject;
|
|
7
|
-
|
|
8
|
-
return schemaFactory(z);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Converts a {@link JsonSchema} produced by Ts.ED into an executable Zod validation object.
|
|
13
|
-
*
|
|
14
|
-
* @param schema JsonSchema instance or a pre-built Zod schema.
|
|
15
|
-
* @param opts
|
|
16
|
-
* @returns A Zod object when conversion is possible, otherwise the original schema.
|
|
17
|
-
* @module platform/mcp
|
|
18
|
-
* @since 8.17.0
|
|
19
|
-
*/
|
|
20
|
-
export function toZod(schema: unknown, opts?: JsonSchemaOptions): ZodObject | undefined {
|
|
21
|
-
return schema && schema instanceof JsonSchema ? transform(schema, opts) : (schema as ZodObject | undefined);
|
|
22
|
-
}
|