1
1
/**
2
- * Example showing the difference between regular types and strict types
2
+ * Example showing the difference between extensible types and safe types
3
+ *
4
+ * - Extensible types (types.js): Use .passthrough() - keep all fields
5
+ * - Safe types (strictTypes.js): Use .strip() - remove unknown fields
3
6
*/
4
7
5
- import { ToolSchema as OpenToolSchema } from "../types.js" ;
6
- import { ToolSchema as StrictToolSchema } from "../strictTypes.js" ;
8
+ import { ToolSchema as ExtensibleToolSchema } from "../types.js" ;
9
+ import { ToolSchema } from "../strictTypes.js" ;
7
10
8
- // With regular (open) types - this is valid
9
- const openTool = OpenToolSchema . parse ( {
11
+ const toolData = {
10
12
name : "get-weather" ,
11
13
description : "Get weather for a location" ,
12
14
inputSchema : {
@@ -15,43 +17,19 @@ const openTool = OpenToolSchema.parse({
15
17
location : { type : "string" }
16
18
}
17
19
} ,
18
- // Extra properties are allowed
19
- customField : "This is allowed in open types" ,
20
- anotherExtra : 123
21
- } ) ;
20
+ // Extra properties that aren't in the schema
21
+ customField : "This is an extension" ,
22
+ } ;
22
23
23
- console . log ( "Open tool accepts extra properties:" , openTool ) ;
24
+ // With extensible types - ALL fields are preserved
25
+ const extensibleTool = ExtensibleToolSchema . parse ( toolData ) ;
24
26
25
- // With strict types - this would throw an error
26
- try {
27
- StrictToolSchema . parse ( {
28
- name : "get-weather" ,
29
- description : "Get weather for a location" ,
30
- inputSchema : {
31
- type : "object" ,
32
- properties : {
33
- location : { type : "string" }
34
- }
35
- } ,
36
- // Extra properties cause validation to fail
37
- customField : "This is NOT allowed in strict types" ,
38
- anotherExtra : 123
39
- } ) ;
40
- } catch ( error ) {
41
- console . log ( "Strict tool rejects extra properties:" , error instanceof Error ? error . message : String ( error ) ) ;
42
- }
27
+ console . log ( "Extensible tool keeps ALL properties:" ) ;
28
+ console . log ( "- name:" , extensibleTool . name ) ;
29
+ console . log ( "- customField:" , ( extensibleTool as any ) . customField ) ; // "This is an extension"
43
30
44
- // Correct usage with strict types
45
- const strictToolCorrect = StrictToolSchema . parse ( {
46
- name : "get-weather" ,
47
- description : "Get weather for a location" ,
48
- inputSchema : {
49
- type : "object" ,
50
- properties : {
51
- location : { type : "string" }
52
- }
53
- }
54
- // No extra properties
55
- } ) ;
31
+ // With safe types - unknown fields are silently stripped
32
+ const safeTool = ToolSchema . parse ( toolData ) ;
56
33
57
- console . log ( "Strict tool with no extra properties:" , strictToolCorrect ) ;
34
+ console . log ( "\nSafe tool strips unknown properties:" ) ;
35
+ console . log ( "- customField:" , ( safeTool as any ) . customField ) ; // undefined (stripped)
0 commit comments