@@ -93,3 +93,115 @@ func getTextResult(t *testing.T, result *mcp.CallToolResult) mcp.TextContent {
93
93
assert .Equal (t , "text" , textContent .Type )
94
94
return textContent
95
95
}
96
+
97
+ func TestOptionalParamOK (t * testing.T ) {
98
+ tests := []struct {
99
+ name string
100
+ args map [string ]interface {}
101
+ paramName string
102
+ expectedVal interface {}
103
+ expectedOk bool
104
+ expectError bool
105
+ errorMsg string
106
+ }{
107
+ {
108
+ name : "present and correct type (string)" ,
109
+ args : map [string ]interface {}{"myParam" : "hello" },
110
+ paramName : "myParam" ,
111
+ expectedVal : "hello" ,
112
+ expectedOk : true ,
113
+ expectError : false ,
114
+ },
115
+ {
116
+ name : "present and correct type (bool)" ,
117
+ args : map [string ]interface {}{"myParam" : true },
118
+ paramName : "myParam" ,
119
+ expectedVal : true ,
120
+ expectedOk : true ,
121
+ expectError : false ,
122
+ },
123
+ {
124
+ name : "present and correct type (number)" ,
125
+ args : map [string ]interface {}{"myParam" : float64 (123 )},
126
+ paramName : "myParam" ,
127
+ expectedVal : float64 (123 ),
128
+ expectedOk : true ,
129
+ expectError : false ,
130
+ },
131
+ {
132
+ name : "present but wrong type (string expected, got bool)" ,
133
+ args : map [string ]interface {}{"myParam" : true },
134
+ paramName : "myParam" ,
135
+ expectedVal : "" , // Zero value for string
136
+ expectedOk : true , // ok is true because param exists
137
+ expectError : true ,
138
+ errorMsg : "parameter myParam is not of type string, is bool" ,
139
+ },
140
+ {
141
+ name : "present but wrong type (bool expected, got string)" ,
142
+ args : map [string ]interface {}{"myParam" : "true" },
143
+ paramName : "myParam" ,
144
+ expectedVal : false , // Zero value for bool
145
+ expectedOk : true , // ok is true because param exists
146
+ expectError : true ,
147
+ errorMsg : "parameter myParam is not of type bool, is string" ,
148
+ },
149
+ {
150
+ name : "parameter not present" ,
151
+ args : map [string ]interface {}{"anotherParam" : "value" },
152
+ paramName : "myParam" ,
153
+ expectedVal : "" , // Zero value for string
154
+ expectedOk : false ,
155
+ expectError : false ,
156
+ },
157
+ }
158
+
159
+ for _ , tc := range tests {
160
+ t .Run (tc .name , func (t * testing.T ) {
161
+ request := createMCPRequest (tc .args )
162
+
163
+ // Test with string type assertion
164
+ if _ , isString := tc .expectedVal .(string ); isString || tc .errorMsg == "parameter myParam is not of type string, is bool" {
165
+ val , ok , err := optionalParamOK [string ](request , tc .paramName )
166
+ if tc .expectError {
167
+ require .Error (t , err )
168
+ assert .Contains (t , err .Error (), tc .errorMsg )
169
+ assert .Equal (t , tc .expectedOk , ok ) // Check ok even on error
170
+ assert .Equal (t , tc .expectedVal , val ) // Check zero value on error
171
+ } else {
172
+ require .NoError (t , err )
173
+ assert .Equal (t , tc .expectedOk , ok )
174
+ assert .Equal (t , tc .expectedVal , val )
175
+ }
176
+ }
177
+
178
+ // Test with bool type assertion
179
+ if _ , isBool := tc .expectedVal .(bool ); isBool || tc .errorMsg == "parameter myParam is not of type bool, is string" {
180
+ val , ok , err := optionalParamOK [bool ](request , tc .paramName )
181
+ if tc .expectError {
182
+ require .Error (t , err )
183
+ assert .Contains (t , err .Error (), tc .errorMsg )
184
+ assert .Equal (t , tc .expectedOk , ok ) // Check ok even on error
185
+ assert .Equal (t , tc .expectedVal , val ) // Check zero value on error
186
+ } else {
187
+ require .NoError (t , err )
188
+ assert .Equal (t , tc .expectedOk , ok )
189
+ assert .Equal (t , tc .expectedVal , val )
190
+ }
191
+ }
192
+
193
+ // Test with float64 type assertion (for number case)
194
+ if _ , isFloat := tc .expectedVal .(float64 ); isFloat {
195
+ val , ok , err := optionalParamOK [float64 ](request , tc .paramName )
196
+ if tc .expectError {
197
+ // This case shouldn't happen for float64 in the defined tests
198
+ require .Fail (t , "Unexpected error case for float64" )
199
+ } else {
200
+ require .NoError (t , err )
201
+ assert .Equal (t , tc .expectedOk , ok )
202
+ assert .Equal (t , tc .expectedVal , val )
203
+ }
204
+ }
205
+ })
206
+ }
207
+ }
0 commit comments