|
| 1 | +package forwardproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/kagenti/kagenti-extensions/authbridge/authlib/pipeline" |
| 12 | + "github.com/kagenti/kagenti-extensions/authbridge/authlib/plugins/inferenceparser" |
| 13 | + "github.com/kagenti/kagenti-extensions/authbridge/authlib/plugins/mcpparser" |
| 14 | + "github.com/kagenti/kagenti-extensions/authbridge/authlib/session" |
| 15 | +) |
| 16 | + |
| 17 | +// TestForwardProxy_MCP_SSEResponse_RecordsObserve reproduces the live |
| 18 | +// weather-tool-mcp scenario: an MCP tools/list call whose response is |
| 19 | +// text/event-stream (Streamable HTTP). The request is parsed (observe), and |
| 20 | +// the RESPONSE must also be parsed into a result + recorded as an mcp-parser |
| 21 | +// observe on the response event. The live cluster showed result=N, |
| 22 | +// invocations=[] on the response — this test pins whether the proxy+parser |
| 23 | +// path reproduces that. |
| 24 | +func TestForwardProxy_MCP_SSEResponse_RecordsObserve(t *testing.T) { |
| 25 | + // Upstream mimics weather-tool-mcp: tools/list -> SSE with one data frame |
| 26 | + // carrying the JSON-RPC result, exactly as FastMCP emits it. |
| 27 | + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 28 | + w.Header().Set("Content-Type", "text/event-stream") |
| 29 | + w.WriteHeader(http.StatusOK) |
| 30 | + io.WriteString(w, "event: message\ndata: {\"jsonrpc\":\"2.0\",\"id\":2,\"result\":{\"tools\":[{\"name\":\"get_weather\"}]}}\n\n") |
| 31 | + if f, ok := w.(http.Flusher); ok { |
| 32 | + f.Flush() |
| 33 | + } |
| 34 | + })) |
| 35 | + defer upstream.Close() |
| 36 | + |
| 37 | + store := session.New(5*time.Minute, 100, 0) |
| 38 | + defer store.Close() |
| 39 | + |
| 40 | + // Production builds Configurable plugins through plugins.Build, which |
| 41 | + // wraps them via pipeline.WrapConfigured. mcp-parser is Configurable + |
| 42 | + // StreamingResponder, so it MUST stay a StreamingResponder after wrapping |
| 43 | + // — that's the bug this test guards (a bare wrapper drops OnResponseFrame |
| 44 | + // and RunResponseFrame skips it, leaving the SSE response unparsed). |
| 45 | + // inference-parser is not Configurable, so it's added raw, as in production. |
| 46 | + pipe, err := pipeline.New([]pipeline.Plugin{ |
| 47 | + pipeline.WrapConfigured(mcpparser.NewMCPParser(), nil), |
| 48 | + inferenceparser.NewInferenceParser(), |
| 49 | + }) |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + srv, err := NewServer(pipeline.NewHolder(pipe), store, nil) |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("NewServer: %v", err) |
| 56 | + } |
| 57 | + proxy := httptest.NewServer(srv.Handler()) |
| 58 | + defer proxy.Close() |
| 59 | + |
| 60 | + reqBody := `{"jsonrpc":"2.0","id":2,"method":"tools/list"}` |
| 61 | + req, _ := http.NewRequest("POST", upstream.URL+"/mcp", bytes.NewReader([]byte(reqBody))) |
| 62 | + req.Header.Set("Content-Type", "application/json") |
| 63 | + proxyClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(mustParseURL(proxy.URL))}} |
| 64 | + resp, err := proxyClient.Do(req) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("request failed: %v", err) |
| 67 | + } |
| 68 | + _, _ = io.ReadAll(resp.Body) |
| 69 | + resp.Body.Close() |
| 70 | + |
| 71 | + v := store.View(session.DefaultSessionID) |
| 72 | + if v == nil { |
| 73 | + t.Fatal("no session recorded") |
| 74 | + } |
| 75 | + var reqEv, respEv *pipeline.SessionEvent |
| 76 | + for i := range v.Events { |
| 77 | + e := &v.Events[i] |
| 78 | + if e.MCP == nil || e.MCP.Method != "tools/list" { |
| 79 | + continue |
| 80 | + } |
| 81 | + switch e.Phase { |
| 82 | + case pipeline.SessionRequest: |
| 83 | + reqEv = e |
| 84 | + case pipeline.SessionResponse: |
| 85 | + respEv = e |
| 86 | + } |
| 87 | + } |
| 88 | + if reqEv == nil || respEv == nil { |
| 89 | + t.Fatalf("missing req/resp events: req=%v resp=%v", reqEv != nil, respEv != nil) |
| 90 | + } |
| 91 | + |
| 92 | + // Request side parsed (sanity). |
| 93 | + if reqEv.Invocations == nil || len(reqEv.Invocations.Outbound) == 0 { |
| 94 | + t.Errorf("request event has no mcp-parser invocation: %+v", reqEv.Invocations) |
| 95 | + } |
| 96 | + |
| 97 | + // THE ASSERTION: the response was parsed into a result and recorded an |
| 98 | + // observe. Live cluster fails both. |
| 99 | + if respEv.MCP.Result == nil { |
| 100 | + t.Errorf("response MCP.Result not populated (parser never saw the SSE result)") |
| 101 | + } |
| 102 | + respObserved := respEv.Invocations != nil && len(respEv.Invocations.Outbound) > 0 |
| 103 | + if !respObserved { |
| 104 | + t.Errorf("response event has NO mcp-parser invocation — reproduces the bug. invocations=%+v", respEv.Invocations) |
| 105 | + } |
| 106 | +} |
0 commit comments