@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
|
@@ -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,4 +1,4 @@
|
|
|
1
|
-
import {JsonSchema, s, string} from "@tsed/schema";
|
|
1
|
+
import {JsonSchema, Property, s, string} from "@tsed/schema";
|
|
2
2
|
import {toZod} from "./toZod.js";
|
|
3
3
|
|
|
4
4
|
describe("toZod", () => {
|
|
@@ -37,4 +37,27 @@ describe("toZod", () => {
|
|
|
37
37
|
additionalProperties: false
|
|
38
38
|
});
|
|
39
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
|
+
});
|
|
40
63
|
});
|
package/src/utils/toZod.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {JsonSchema, JsonSchemaOptions} from "@tsed/schema";
|
|
2
|
-
import {type ZodObject
|
|
1
|
+
import {JsonSchema, JsonSchemaOptions, s} from "@tsed/schema";
|
|
2
|
+
import {z, type ZodObject} from "zod";
|
|
3
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
|
}
|