Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/mcp/shared_test.go

raw symbols SCIP
1 // Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package mcp
6
7 // TODO(v0.3.0): rewrite this test.
8 // func TestToolValidate(t *testing.T) {
9 // // Check that the tool returned from NewServerTool properly validates its input schema.
10
11 // type req struct {
12 // I int
13 // B bool
14 // S string `json:",omitempty"`
15 // P *int `json:",omitempty"`
16 // }
17
18 // dummyHandler := func(context.Context, *CallToolRequest, req) (*CallToolResultFor[any], error) {
19 // return nil, nil
20 // }
21
22 // st, err := newServerTool(&Tool{Name: "test", Description: "test"}, dummyHandler)
23 // if err != nil {
24 // t.Fatal(err)
25 // }
26
27 // for _, tt := range []struct {
28 // desc string
29 // args map[string]any
30 // want string // error should contain this string; empty for success
31 // }{
32 // {
33 // "both required",
34 // map[string]any{"I": 1, "B": true},
35 // "",
36 // },
37 // {
38 // "optional",
39 // map[string]any{"I": 1, "B": true, "S": "foo"},
40 // "",
41 // },
42 // {
43 // "wrong type",
44 // map[string]any{"I": 1.5, "B": true},
45 // "cannot unmarshal",
46 // },
47 // {
48 // "extra property",
49 // map[string]any{"I": 1, "B": true, "C": 2},
50 // "unknown field",
51 // },
52 // {
53 // "value for pointer",
54 // map[string]any{"I": 1, "B": true, "P": 3},
55 // "",
56 // },
57 // {
58 // "null for pointer",
59 // map[string]any{"I": 1, "B": true, "P": nil},
60 // "",
61 // },
62 // } {
63 // t.Run(tt.desc, func(t *testing.T) {
64 // raw, err := json.Marshal(tt.args)
65 // if err != nil {
66 // t.Fatal(err)
67 // }
68 // _, err = st.handler(context.Background(), &ServerRequest[*CallToolParamsFor[json.RawMessage]]{
69 // Params: &CallToolParamsFor[json.RawMessage]{Arguments: json.RawMessage(raw)},
70 // })
71 // if err == nil && tt.want != "" {
72 // t.Error("got success, wanted failure")
73 // }
74 // if err != nil {
75 // if tt.want == "" {
76 // t.Fatalf("failed with:\n%s\nwanted success", err)
77 // }
78 // if !strings.Contains(err.Error(), tt.want) {
79 // t.Fatalf("got:\n%s\nwanted to contain %q", err, tt.want)
80 // }
81 // }
82 // })
83 // }
84 // }
85
86 // TestNilParamsHandling tests that nil parameters don't cause panic in unmarshalParams.
87 // This addresses a vulnerability where missing or null parameters could crash the server.
88 // func TestNilParamsHandling(t *testing.T) {
89 // // Define test types for clarity
90 // type TestArgs struct {
91 // Name string `json:"name"`
92 // Value int `json:"value"`
93 // }
94
95 // // Simple test handler
96 // testHandler := func(ctx context.Context, req *ServerRequest[**GetPromptParams]) (*GetPromptResult, error) {
97 // result := "processed: " + req.Params.Arguments.Name
98 // return &CallToolResultFor[string]{StructuredContent: result}, nil
99 // }
100
101 // methodInfo := newServerMethodInfo(testHandler, missingParamsOK)
102
103 // // Helper function to test that unmarshalParams doesn't panic and handles nil gracefully
104 // mustNotPanic := func(t *testing.T, rawMsg json.RawMessage, expectNil bool) Params {
105 // t.Helper()
106
107 // defer func() {
108 // if r := recover(); r != nil {
109 // t.Fatalf("unmarshalParams panicked: %v", r)
110 // }
111 // }()
112
113 // params, err := methodInfo.unmarshalParams(rawMsg)
114 // if err != nil {
115 // t.Fatalf("unmarshalParams failed: %v", err)
116 // }
117
118 // if expectNil {
119 // if params != nil {
120 // t.Fatalf("Expected nil params, got %v", params)
121 // }
122 // return params
123 // }
124
125 // if params == nil {
126 // t.Fatal("unmarshalParams returned unexpected nil")
127 // }
128
129 // // Verify the result can be used safely
130 // typedParams := params.(TestParams)
131 // _ = typedParams.Name
132 // _ = typedParams.Arguments.Name
133 // _ = typedParams.Arguments.Value
134
135 // return params
136 // }
137
138 // // Test different nil parameter scenarios - with missingParamsOK flag, nil/null should return nil
139 // t.Run("missing_params", func(t *testing.T) {
140 // mustNotPanic(t, nil, true) // Expect nil with missingParamsOK flag
141 // })
142
143 // t.Run("explicit_null", func(t *testing.T) {
144 // mustNotPanic(t, json.RawMessage(`null`), true) // Expect nil with missingParamsOK flag
145 // })
146
147 // t.Run("empty_object", func(t *testing.T) {
148 // mustNotPanic(t, json.RawMessage(`{}`), false) // Empty object should create valid params
149 // })
150
151 // t.Run("valid_params", func(t *testing.T) {
152 // rawMsg := json.RawMessage(`{"name":"test","arguments":{"name":"hello","value":42}}`)
153 // params := mustNotPanic(t, rawMsg, false)
154
155 // // For valid params, also verify the values are parsed correctly
156 // typedParams := params.(TestParams)
157 // if typedParams.Name != "test" {
158 // t.Errorf("Expected name 'test', got %q", typedParams.Name)
159 // }
160 // if typedParams.Arguments.Name != "hello" {
161 // t.Errorf("Expected argument name 'hello', got %q", typedParams.Arguments.Name)
162 // }
163 // if typedParams.Arguments.Value != 42 {
164 // t.Errorf("Expected argument value 42, got %d", typedParams.Arguments.Value)
165 // }
166 // })
167 // }
168
169 // TestNilParamsEdgeCases tests edge cases to ensure we don't over-fix
170 // func TestNilParamsEdgeCases(t *testing.T) {
171 // type TestArgs struct {
172 // Name string `json:"name"`
173 // Value int `json:"value"`
174 // }
175 // type TestParams = *CallToolParamsFor[TestArgs]
176
177 // testHandler := func(context.Context, *ServerRequest[TestParams]) (*CallToolResultFor[string], error) {
178 // return &CallToolResultFor[string]{StructuredContent: "test"}, nil
179 // }
180
181 // methodInfo := newServerMethodInfo(testHandler, missingParamsOK)
182
183 // // These should fail normally, not be treated as nil params
184 // invalidCases := []json.RawMessage{
185 // json.RawMessage(""), // empty string - should error
186 // json.RawMessage("[]"), // array - should error
187 // json.RawMessage(`"null"`), // string "null" - should error
188 // json.RawMessage("0"), // number - should error
189 // json.RawMessage("false"), // boolean - should error
190 // }
191
192 // for i, rawMsg := range invalidCases {
193 // t.Run(fmt.Sprintf("invalid_case_%d", i), func(t *testing.T) {
194 // params, err := methodInfo.unmarshalParams(rawMsg)
195 // if err == nil && params == nil {
196 // t.Error("Should not return nil params without error")
197 // }
198 // })
199 // }
200
201 // // Test that methods without missingParamsOK flag properly reject nil params
202 // t.Run("reject_when_params_required", func(t *testing.T) {
203 // methodInfoStrict := newServerMethodInfo(testHandler, 0) // No missingParamsOK flag
204
205 // testCases := []struct {
206 // name string
207 // params json.RawMessage
208 // }{
209 // {"nil_params", nil},
210 // {"null_params", json.RawMessage(`null`)},
211 // }
212
213 // for _, tc := range testCases {
214 // t.Run(tc.name, func(t *testing.T) {
215 // _, err := methodInfoStrict.unmarshalParams(tc.params)
216 // if err == nil {
217 // t.Error("Expected error for required params, got nil")
218 // }
219 // if !strings.Contains(err.Error(), "missing required \"params\"") {
220 // t.Errorf("Expected 'missing required params' error, got: %v", err)
221 // }
222 // })
223 // }
224 // })
225 // }