1+ package mcp
2+
3+ import (
4+ "encoding/json"
5+ "testing"
6+
7+ "github.com/stretchr/testify/assert"
8+ )
9+
10+ func TestCallToolRequestWithMapArguments (t * testing.T ) {
11+ // Create a request with map arguments
12+ req := CallToolRequest {}
13+ req .Params .Name = "test-tool"
14+ req .Params .Arguments = map [string ]any {
15+ "key1" : "value1" ,
16+ "key2" : 123 ,
17+ }
18+
19+ // Test GetArguments
20+ args := req .GetArguments ()
21+ assert .Equal (t , "value1" , args ["key1" ])
22+ assert .Equal (t , 123 , args ["key2" ])
23+
24+ // Test GetRawArguments
25+ rawArgs := req .GetRawArguments ()
26+ mapArgs , ok := rawArgs .(map [string ]any )
27+ assert .True (t , ok )
28+ assert .Equal (t , "value1" , mapArgs ["key1" ])
29+ assert .Equal (t , 123 , mapArgs ["key2" ])
30+ }
31+
32+ func TestCallToolRequestWithNonMapArguments (t * testing.T ) {
33+ // Create a request with non-map arguments
34+ req := CallToolRequest {}
35+ req .Params .Name = "test-tool"
36+ req .Params .Arguments = "string-argument"
37+
38+ // Test GetArguments (should return empty map)
39+ args := req .GetArguments ()
40+ assert .Empty (t , args )
41+
42+ // Test GetRawArguments
43+ rawArgs := req .GetRawArguments ()
44+ strArg , ok := rawArgs .(string )
45+ assert .True (t , ok )
46+ assert .Equal (t , "string-argument" , strArg )
47+ }
48+
49+ func TestCallToolRequestWithStructArguments (t * testing.T ) {
50+ // Create a custom struct
51+ type CustomArgs struct {
52+ Field1 string `json:"field1"`
53+ Field2 int `json:"field2"`
54+ }
55+
56+ // Create a request with struct arguments
57+ req := CallToolRequest {}
58+ req .Params .Name = "test-tool"
59+ req .Params .Arguments = CustomArgs {
60+ Field1 : "test" ,
61+ Field2 : 42 ,
62+ }
63+
64+ // Test GetArguments (should return empty map)
65+ args := req .GetArguments ()
66+ assert .Empty (t , args )
67+
68+ // Test GetRawArguments
69+ rawArgs := req .GetRawArguments ()
70+ structArg , ok := rawArgs .(CustomArgs )
71+ assert .True (t , ok )
72+ assert .Equal (t , "test" , structArg .Field1 )
73+ assert .Equal (t , 42 , structArg .Field2 )
74+ }
75+
76+ func TestCallToolRequestJSONMarshalUnmarshal (t * testing.T ) {
77+ // Create a request with map arguments
78+ req := CallToolRequest {}
79+ req .Params .Name = "test-tool"
80+ req .Params .Arguments = map [string ]any {
81+ "key1" : "value1" ,
82+ "key2" : 123 ,
83+ }
84+
85+ // Marshal to JSON
86+ data , err := json .Marshal (req )
87+ assert .NoError (t , err )
88+
89+ // Unmarshal from JSON
90+ var unmarshaledReq CallToolRequest
91+ err = json .Unmarshal (data , & unmarshaledReq )
92+ assert .NoError (t , err )
93+
94+ // Check if arguments are correctly unmarshaled
95+ args := unmarshaledReq .GetArguments ()
96+ assert .Equal (t , "value1" , args ["key1" ])
97+ assert .Equal (t , float64 (123 ), args ["key2" ]) // JSON numbers are unmarshaled as float64
98+ }
0 commit comments