11package middleware
22
33import (
4+ "encoding/json"
45 "io"
56 "net/http"
67 "net/http/httptest"
@@ -14,16 +15,16 @@ import (
1415
1516func TestWithMCPParse (t * testing.T ) {
1617 tests := []struct {
17- name string
18- method string
19- path string
20- body string
21- expectInfo bool
22- expectedMethod string
23- expectedItem string
24- expectedOwner string
25- expectedRepo string
26- expectedArgs map [ string ] any
18+ name string
19+ method string
20+ path string
21+ body string
22+ expectInfo bool
23+ expectedMethod string
24+ expectedItem string
25+ expectedRaw string
26+ expectedArgs map [ string ] any
27+ expectArgsError bool
2728 }{
2829 {
2930 name : "health check path is skipped" ,
@@ -92,18 +93,19 @@ func TestWithMCPParse(t *testing.T) {
9293 expectInfo : true ,
9394 expectedMethod : "tools/call" ,
9495 expectedItem : "get_file_contents" ,
95- expectedOwner : "github" ,
96- expectedRepo : "github-mcp-server" ,
96+ expectedRaw : `{"owner":"github","repo":"github-mcp-server","path":"README.md"}` ,
9797 expectedArgs : map [string ]any {"owner" : "github" , "repo" : "github-mcp-server" , "path" : "README.md" },
9898 },
9999 {
100- name : "tools/call with invalid arguments JSON continues without args" ,
101- method : http .MethodPost ,
102- path : "/mcp" ,
103- body : `{"jsonrpc":"2.0","method":"tools/call","params":{"name":"get_file_contents","arguments":"not an object"}}` ,
104- expectInfo : true ,
105- expectedMethod : "tools/call" ,
106- expectedItem : "get_file_contents" ,
100+ name : "tools/call with invalid arguments JSON continues without args" ,
101+ method : http .MethodPost ,
102+ path : "/mcp" ,
103+ body : `{"jsonrpc":"2.0","method":"tools/call","params":{"name":"get_file_contents","arguments":"not an object"}}` ,
104+ expectInfo : true ,
105+ expectedMethod : "tools/call" ,
106+ expectedItem : "get_file_contents" ,
107+ expectedRaw : `"not an object"` ,
108+ expectArgsError : true ,
107109 },
108110 {
109111 name : "prompts/get parses name" ,
@@ -156,10 +158,17 @@ func TestWithMCPParse(t *testing.T) {
156158 require .NotNil (t , capturedInfo )
157159 assert .Equal (t , tt .expectedMethod , capturedInfo .Method )
158160 assert .Equal (t , tt .expectedItem , capturedInfo .ItemName )
159- assert .Equal (t , tt .expectedOwner , capturedInfo .Owner )
160- assert .Equal (t , tt .expectedRepo , capturedInfo .Repo )
161+ if tt .expectedRaw != "" {
162+ assert .JSONEq (t , tt .expectedRaw , string (capturedInfo .RawArguments ))
163+ }
164+ decodedArgs , err := capturedInfo .DecodeArguments ()
165+ if tt .expectArgsError {
166+ assert .Error (t , err )
167+ } else {
168+ require .NoError (t , err )
169+ }
161170 if tt .expectedArgs != nil {
162- assert .Equal (t , tt .expectedArgs , capturedInfo . Arguments )
171+ assert .Equal (t , tt .expectedArgs , decodedArgs )
163172 }
164173 } else {
165174 assert .False (t , infoCaptured , "MCPMethodInfo should not be present in context" )
@@ -168,6 +177,28 @@ func TestWithMCPParse(t *testing.T) {
168177 }
169178}
170179
180+ func TestWithMCPParseRetainsLargeArgumentsWithoutMaterializingThem (t * testing.T ) {
181+ nested := map [string ]any {
182+ "items" : []any {
183+ map [string ]any {"payload" : strings .Repeat ("x" , 32 * 1024 )},
184+ []any {1.0 , 2.0 , 3.0 },
185+ },
186+ }
187+ rawArguments , err := json .Marshal (nested )
188+ require .NoError (t , err )
189+ body := `{"jsonrpc":"2.0","method":"tools/call","params":{"name":"test_tool","arguments":` + string (rawArguments ) + `}}`
190+
191+ var capturedInfo * ghcontext.MCPMethodInfo
192+ next := http .HandlerFunc (func (_ http.ResponseWriter , r * http.Request ) {
193+ capturedInfo , _ = ghcontext .MCPMethod (r .Context ())
194+ })
195+ request := httptest .NewRequest (http .MethodPost , "/mcp" , strings .NewReader (body ))
196+ WithMCPParse ()(next ).ServeHTTP (httptest .NewRecorder (), request )
197+
198+ require .NotNil (t , capturedInfo )
199+ assert .Equal (t , json .RawMessage (rawArguments ), capturedInfo .RawArguments )
200+ }
201+
171202func TestWithMCPParse_BodyRestoration (t * testing.T ) {
172203 originalBody := `{"jsonrpc":"2.0","method":"tools/call","params":{"name":"test_tool"}}`
173204
0 commit comments