Skip to content

Commit a5d9783

Browse files
committed
DEV review update
1 parent 6c7244d commit a5d9783

File tree

4 files changed

+476
-109
lines changed

4 files changed

+476
-109
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`normalizeCreatorSchema should attempt to normalize a schema, with JSON inputSchema 1`] = `
4+
{
5+
"manifestSchema": {
6+
"additionalProperties": true,
7+
"type": "object",
8+
},
9+
"normalizedSchema": "[object Object], isZod=true",
10+
"tool": [
11+
"lorem ipsum",
12+
{
13+
"description": "lorem ipsum",
14+
"inputSchema": "[object Object], isZod=true",
15+
},
16+
[Function],
17+
],
18+
"warnings": [],
19+
}
20+
`;
21+
22+
exports[`normalizeCreatorSchema should attempt to normalize a schema, with invalid JSON inputSchema 1`] = `
23+
{
24+
"manifestSchema": {
25+
"additionalProperties": "busted",
26+
"type": "object",
27+
},
28+
"normalizedSchema": "[object Object], isZod=true",
29+
"tool": [
30+
"lorem ipsum",
31+
{
32+
"description": "lorem ipsum",
33+
"inputSchema": "[object Object], isZod=true",
34+
},
35+
[Function],
36+
],
37+
"warnings": [],
38+
}
39+
`;
40+
41+
exports[`normalizeCreatorSchema should attempt to normalize a schema, with partial 1`] = `
42+
{
43+
"manifestSchema": {
44+
"additionalProperties": true,
45+
"type": "object",
46+
},
47+
"normalizedSchema": "undefined, isZod=false",
48+
"tool": [
49+
"lorem ipsum",
50+
{
51+
"description": "lorem ipsum",
52+
"inputSchema": "undefined, isZod=false",
53+
},
54+
[Function],
55+
],
56+
"warnings": [
57+
"Using permissive JSON Schema fallback. Failed to convert Zod to JSON Schema for lorem ipsum.",
58+
"Permissive JSON schemas may have unintended side-effects. Review lorem ipsum's inputSchema and ensure it's a valid JSON or Zod schema.",
59+
],
60+
}
61+
`;
62+
63+
exports[`normalizeCreatorSchema should attempt to normalize a schema, with undefined name 1`] = `
64+
{
65+
"manifestSchema": {
66+
"additionalProperties": true,
67+
"type": "object",
68+
},
69+
"normalizedSchema": "[object Object], isZod=true",
70+
"tool": [
71+
undefined,
72+
{
73+
"description": "lorem ipsum",
74+
"inputSchema": "[object Object], isZod=true",
75+
},
76+
[Function],
77+
],
78+
"warnings": [],
79+
}
80+
`;
81+
82+
exports[`normalizeCreatorSchema should attempt to normalize a schema, with undefined name, schema 1`] = `
83+
{
84+
"manifestSchema": {
85+
"additionalProperties": true,
86+
"type": "object",
87+
},
88+
"normalizedSchema": "undefined, isZod=false",
89+
"tool": [
90+
undefined,
91+
{
92+
"description": undefined,
93+
"inputSchema": "undefined, isZod=false",
94+
},
95+
[Function],
96+
],
97+
"warnings": [
98+
"Using permissive JSON Schema fallback. Failed to convert Zod to JSON Schema for creator.",
99+
"Permissive JSON schemas may have unintended side-effects. Review creator's inputSchema and ensure it's a valid JSON or Zod schema.",
100+
],
101+
}
102+
`;
103+
104+
exports[`normalizeCreatorSchema should attempt to normalize a schema, with undefined schema 1`] = `
105+
{
106+
"manifestSchema": {
107+
"additionalProperties": true,
108+
"type": "object",
109+
},
110+
"normalizedSchema": "undefined, isZod=false",
111+
"tool": [
112+
"lorem ipsum",
113+
{
114+
"description": undefined,
115+
"inputSchema": "undefined, isZod=false",
116+
},
117+
[Function],
118+
],
119+
"warnings": [
120+
"Using permissive JSON Schema fallback. Failed to convert Zod to JSON Schema for lorem ipsum.",
121+
"Permissive JSON schemas may have unintended side-effects. Review lorem ipsum's inputSchema and ensure it's a valid JSON or Zod schema.",
122+
],
123+
}
124+
`;
125+
126+
exports[`normalizeCreatorSchema should attempt to normalize a schema, with valid zod inputSchema 1`] = `
127+
{
128+
"manifestSchema": {
129+
"$schema": "https://json-schema.org/draft/2020-12/schema",
130+
},
131+
"normalizedSchema": "[object Object], isZod=true",
132+
"tool": [
133+
"lorem ipsum",
134+
{
135+
"description": "lorem ipsum",
136+
"inputSchema": "[object Object], isZod=true",
137+
},
138+
[Function],
139+
],
140+
"warnings": [],
141+
}
142+
`;

src/__tests__/server.toolsHost.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { z } from 'zod';
12
import {
3+
normalizeCreatorSchema,
24
requestHello,
35
requestLoad,
46
requestManifestGet,
@@ -10,6 +12,7 @@ import {
1012
import { type IpcRequest, type ToolDescriptor } from '../server.toolsIpc';
1113
import { type McpTool } from '../server';
1214
import { DEFAULT_OPTIONS } from '../options.defaults';
15+
import {isZodSchema} from "../server.schema";
1316

1417
// Mock dependencies
1518
jest.mock('../server.toolsHostCreator', () => ({
@@ -605,3 +608,94 @@ describe('setHandlers', () => {
605608
delete (process as any).exit;
606609
});
607610
});
611+
612+
describe('normalizeCreatorSchema', () => {
613+
it.each([
614+
{
615+
description: 'with undefined name, schema',
616+
creator: () => [
617+
undefined,
618+
undefined,
619+
() => null
620+
]
621+
},
622+
{
623+
description: 'with undefined name',
624+
creator: () => [
625+
undefined,
626+
{
627+
description: 'lorem ipsum',
628+
inputSchema: { type: 'object', additionalProperties: true }
629+
},
630+
() => null
631+
]
632+
},
633+
{
634+
description: 'with undefined schema',
635+
creator: () => [
636+
'lorem ipsum',
637+
undefined,
638+
() => null
639+
]
640+
},
641+
{
642+
description: 'with partial',
643+
creator: () => [
644+
'lorem ipsum',
645+
{
646+
description: 'lorem ipsum',
647+
inputSchema: undefined
648+
},
649+
() => null
650+
]
651+
},
652+
{
653+
description: 'with JSON inputSchema',
654+
creator: () => [
655+
'lorem ipsum',
656+
{
657+
description: 'lorem ipsum',
658+
inputSchema: { type: 'object', additionalProperties: true }
659+
},
660+
() => null
661+
]
662+
},
663+
{
664+
description: 'with invalid JSON inputSchema',
665+
creator: () => [
666+
'lorem ipsum',
667+
{
668+
description: 'lorem ipsum',
669+
inputSchema: { type: 'object', additionalProperties: 'busted' }
670+
},
671+
() => null
672+
]
673+
},
674+
{
675+
description: 'with valid zod inputSchema',
676+
creator: () => [
677+
'lorem ipsum',
678+
{
679+
description: 'lorem ipsum',
680+
inputSchema: z.any()
681+
},
682+
() => null
683+
]
684+
}
685+
])('should attempt to normalize a schema, $description', ({ creator }) => {
686+
const { normalizedSchema, tool, ...rest } = normalizeCreatorSchema(creator);
687+
688+
expect({
689+
normalizedSchema: `${String(normalizedSchema)}, isZod=${isZodSchema(normalizedSchema)}`,
690+
tool: [
691+
tool[0],
692+
{
693+
description: tool[1]?.description,
694+
inputSchema: `${String(tool[1]?.inputSchema)}, isZod=${isZodSchema(tool[1]?.inputSchema)}`
695+
},
696+
tool[2]
697+
],
698+
...rest
699+
}).toMatchSnapshot();
700+
});
701+
});

0 commit comments

Comments
 (0)