MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestApplySchema

Function TestApplySchema

mcp/tool_test.go:20–60  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18)
19
20func TestApplySchema(t *testing.T) {
21 schema := &jsonschema.Schema{
22 Type: "object",
23 Properties: map[string]*jsonschema.Schema{
24 "x": {Type: "integer", Default: json.RawMessage("3")},
25 },
26 }
27 resolved, err := schema.Resolve(&jsonschema.ResolveOptions{ValidateDefaults: true})
28 if err != nil {
29 t.Fatal(err)
30 }
31
32 type S struct {
33 X int `json:"x"`
34 }
35
36 for _, tt := range []struct {
37 data string
38 v any
39 want any
40 }{
41 {`{"x": 1}`, new(S), &S{X: 1}},
42 {`{}`, new(S), &S{X: 3}}, // default applied
43 {`{"x": 0}`, new(S), &S{X: 0}},
44 {`{"x": 1}`, new(map[string]any), &map[string]any{"x": 1.0}},
45 {`{}`, new(map[string]any), &map[string]any{"x": 3.0}}, // default applied
46 {`{"x": 0}`, new(map[string]any), &map[string]any{"x": 0.0}},
47 } {
48 raw := json.RawMessage(tt.data)
49 raw, err = applySchema(raw, resolved)
50 if err != nil {
51 t.Fatal(err)
52 }
53 if err := json.Unmarshal(raw, &tt.v); err != nil {
54 t.Fatal(err)
55 }
56 if !reflect.DeepEqual(tt.v, tt.want) {
57 t.Errorf("got %#v, want %#v", tt.v, tt.want)
58 }
59 }
60}
61
62func TestToolErrorHandling(t *testing.T) {
63 // Construct server and add both tools at the top level

Callers

nothing calls this directly

Calls 1

applySchemaFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…