Files
platform/mcp/agent_test.go
T
2026-08-22 20:51:37 +02:00

372 lines
11 KiB
Go

package mcp
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// stubMCP serves a minimal streamable-HTTP MCP server with one "echo" tool and
// records the Authorization header of the last request.
func stubMCP(t *testing.T, lastAuth *string) *httptest.Server {
t.Helper()
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
*lastAuth = r.Header.Get("Authorization")
var req struct {
Method string `json:"method"`
Params json.RawMessage `json:"params"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
t.Errorf("decode rpc: %v", err)
return
}
var result any
switch req.Method {
case "tools/list":
result = map[string]any{"tools": []map[string]any{{
"name": "echo",
"description": "Echo back its input text",
"inputSchema": Object(Props{"text": String("the text to echo")}, []string{"text"}),
}}}
case "tools/call":
var p struct {
Name string `json:"name"`
Args struct {
Text string `json:"text"`
} `json:"arguments"`
}
_ = json.Unmarshal(req.Params, &p)
if p.Name != "echo" {
http.Error(w, "unknown tool", http.StatusBadRequest)
return
}
result = map[string]any{
"content": []map[string]any{{"type": "text", "text": `{"echo":"` + p.Args.Text + `"}`}},
}
default:
http.Error(w, "unknown method", http.StatusBadRequest)
return
}
b, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": 1, "result": result})
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(b)
}))
}
// stubUpstream serves an OpenAI-compatible /v1/chat/completions SSE stream.
// On the first call it emits one echo tool call; afterwards it streams
// "done " + the tool message content as the final answer.
func stubUpstream(t *testing.T, calls *int, sawTools *[][]string) *httptest.Server {
t.Helper()
mux := http.NewServeMux()
mux.HandleFunc("POST /v1/chat/completions", func(w http.ResponseWriter, r *http.Request) {
var req ChatRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
t.Errorf("decode chat request: %v", err)
return
}
if sawTools != nil {
names := make([]string, 0, len(req.Tools))
for _, tl := range req.Tools {
names = append(names, tl.Function.Name)
}
*sawTools = append(*sawTools, names)
}
*calls++
w.Header().Set("Content-Type", "text/event-stream")
flusher := w.(http.Flusher)
send := func(v any) {
b, _ := json.Marshal(v)
fmt.Fprintf(w, "data: %s\n\n", b)
flusher.Flush()
}
if *calls == 1 {
send(struct {
Choices []struct {
Delta struct {
ToolCalls []ToolCall `json:"tool_calls"`
} `json:"delta"`
FinishReason *string `json:"finish_reason"`
} `json:"choices"`
}{Choices: []struct {
Delta struct {
ToolCalls []ToolCall `json:"tool_calls"`
} `json:"delta"`
FinishReason *string `json:"finish_reason"`
}{{
Delta: struct {
ToolCalls []ToolCall `json:"tool_calls"`
}{ToolCalls: []ToolCall{{
ID: "call_1",
Type: "function",
Function: FunctionCall{Name: "echo", Arguments: `{"text":"hi"}`},
}}},
FinishReason: strPtr("tool_calls"),
}}})
} else {
// Second round: the model should have seen the tool result message.
toolMsg := ""
for _, m := range req.Messages {
if m.Role == "tool" && m.ToolCallID == "call_1" {
toolMsg, _ = m.Content.(string)
}
}
send(struct {
Choices []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
FinishReason *string `json:"finish_reason"`
} `json:"choices"`
}{Choices: []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
FinishReason *string `json:"finish_reason"`
}{{
Delta: struct {
Content string `json:"content"`
}{Content: "saw:" + toolMsg},
FinishReason: strPtr("stop"),
}}})
}
fmt.Fprint(w, "data: [DONE]\n\n")
flusher.Flush()
})
return httptest.NewServer(mux)
}
func strPtr(s string) *string { return &s }
func TestAgentAgenticLoop(t *testing.T) {
var lastAuth string
mcpSrv := stubMCP(t, &lastAuth)
defer mcpSrv.Close()
var calls int
var sawTools [][]string
upSrv := stubUpstream(t, &calls, &sawTools)
defer upSrv.Close()
agent := NewAgent(AgentConfig{
UpstreamURL: upSrv.URL,
UpstreamAPIKey: "sk-test",
MCPURL: mcpSrv.URL + "/mcp",
MCPToken: "mcp-secret",
SystemPrompt: "You are a test.",
ProgressEvent: "cms_progress",
})
body := `{"model":"test-model","messages":[{"role":"user","content":"hi"}],"stream":true}`
req := httptest.NewRequest(http.MethodPost, "/chat/completions", strings.NewReader(body))
rec := httptest.NewRecorder()
agent.Handler().ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
}
sse := rec.Body.String()
// The model must have been offered the MCP tool (round 1)…
if len(sawTools) < 2 || sawTools[0][0] != "echo" {
t.Fatalf("model was not offered the echo tool: %v", sawTools)
}
// …and executed via tools/call with the bearer token appended.
if lastAuth != "Bearer mcp-secret" {
t.Errorf("MCP Authorization = %q, want Bearer mcp-secret", lastAuth)
}
// The final answer must reflect the tool result fed back to the model.
if !strings.Contains(sse, `"saw:{\"echo\":\"hi\"}"`) {
t.Errorf("final delta missing tool feedback; SSE:\n%s", sse)
}
// Wire contract: progress event, response event, [DONE].
for _, want := range []string{"event: cms_progress", `"status":"calling"`, `"status":"done"`, "event: response", `"finish_reason":"stop"`, "data: [DONE]"} {
if !strings.Contains(sse, want) {
t.Errorf("SSE missing %q;\n%s", want, sse)
}
}
if calls != 2 {
t.Errorf("upstream calls = %d, want 2", calls)
}
}
func TestAgentLocalTool(t *testing.T) {
var lastAuth string
mcpSrv := stubMCP(t, &lastAuth)
defer mcpSrv.Close()
// Upstream asks for the local tool; second round confirms the model saw
// its in-process result.
var calls int
upSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
w.Header().Set("Content-Type", "text/event-stream")
flusher := w.(http.Flusher)
send := func(v any) {
b, _ := json.Marshal(v)
fmt.Fprintf(w, "data: %s\n\n", b)
flusher.Flush()
}
if calls == 1 {
send(struct {
Choices []struct {
Delta struct {
ToolCalls []ToolCall `json:"tool_calls"`
} `json:"delta"`
FinishReason *string `json:"finish_reason"`
} `json:"choices"`
}{Choices: []struct {
Delta struct {
ToolCalls []ToolCall `json:"tool_calls"`
} `json:"delta"`
FinishReason *string `json:"finish_reason"`
}{{
Delta: struct {
ToolCalls []ToolCall `json:"tool_calls"`
}{ToolCalls: []ToolCall{{
ID: "loc_1",
Type: "function",
Function: FunctionCall{Name: "local_ping", Arguments: `{}`},
}}},
FinishReason: strPtr("tool_calls"),
}}})
} else {
toolMsg := ""
for _, m := range reqMessages(r) {
if m.Role == "tool" && m.ToolCallID == "loc_1" {
toolMsg, _ = m.Content.(string)
}
}
send(struct {
Choices []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
FinishReason *string `json:"finish_reason"`
} `json:"choices"`
}{Choices: []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
FinishReason *string `json:"finish_reason"`
}{{
Delta: struct {
Content string `json:"content"`
}{Content: toolMsg},
FinishReason: strPtr("stop"),
}}})
}
fmt.Fprint(w, "data: [DONE]\n\n")
flusher.Flush()
}))
defer upSrv.Close()
authBefore := lastAuth
agent := NewAgent(AgentConfig{
UpstreamURL: upSrv.URL, UpstreamAPIKey: "k",
MCPURL: mcpSrv.URL + "/mcp",
LocalTools: []Tool{{
Name: "local_ping",
Description: "In-process ping",
InputSchema: Object(nil, nil),
Invoke: func(ctx context.Context, args json.RawMessage) (any, error) {
return map[string]any{"pong": true}, nil
},
}},
})
req := httptest.NewRequest(http.MethodPost, "/chat/completions",
strings.NewReader(`{"messages":[{"role":"user","content":"ping"}],"stream":true}`))
rec := httptest.NewRecorder()
agent.Handler().ServeHTTP(rec, req)
sse := rec.Body.String()
if !strings.Contains(sse, `\"pong\":true`) {
t.Errorf("local tool result not fed back to the model:\n%s", sse)
}
if lastAuth != authBefore {
t.Errorf("local tool dispatch leaked to the MCP endpoint (auth header changed)")
}
}
// reqMessages decodes the chat request body from a recorded upstream call.
func reqMessages(r *http.Request) []Message {
var req ChatRequest
_ = json.NewDecoder(r.Body).Decode(&req)
return req.Messages
}
func TestAgentFrontendToolCallPassthrough(t *testing.T) {
var lastAuth string
mcpSrv := stubMCP(t, &lastAuth)
defer mcpSrv.Close()
calls := 0
// Upstream immediately requests an unknown (frontend) tool.
upSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
w.Header().Set("Content-Type", "text/event-stream")
fmt.Fprintf(w, `data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"fe_1","type":"function","function":{"name":"browser_exec","arguments":"{\"x\":1}"}}]},"finish_reason":"tool_calls"}]}`+"\n\n")
fmt.Fprint(w, "data: [DONE]\n\n")
}))
defer upSrv.Close()
agent := NewAgent(AgentConfig{
UpstreamURL: upSrv.URL, UpstreamAPIKey: "k",
MCPURL: mcpSrv.URL + "/mcp",
})
req := httptest.NewRequest(http.MethodPost, "/chat/completions",
strings.NewReader(`{"messages":[{"role":"user","content":"go"}],"stream":true}`))
rec := httptest.NewRecorder()
agent.Handler().ServeHTTP(rec, req)
if !strings.Contains(rec.Body.String(), `"name":"browser_exec"`) ||
!strings.Contains(rec.Body.String(), `"id":"fe_1"`) {
t.Errorf("frontend tool call not passed through:\n%s", rec.Body.String())
}
if calls != 1 {
t.Errorf("upstream calls = %d, want 1 (loop must stop for frontend calls)", calls)
}
}
func TestAgentNonStreaming(t *testing.T) {
var lastAuth string
mcpSrv := stubMCP(t, &lastAuth)
defer mcpSrv.Close()
var calls int
upSrv := stubUpstream(t, &calls, nil)
defer upSrv.Close()
agent := NewAgent(AgentConfig{
UpstreamURL: upSrv.URL, UpstreamAPIKey: "k",
MCPURL: mcpSrv.URL + "/mcp",
Model: "default-model",
})
req := httptest.NewRequest(http.MethodPost, "/chat/completions",
strings.NewReader(`{"messages":[{"role":"user","content":"hi"}]}`))
rec := httptest.NewRecorder()
agent.Handler().ServeHTTP(rec, req)
var resp ChatResponse
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode response: %v\n%s", err, rec.Body.String())
}
if len(resp.Choices) != 1 || resp.Choices[0].FinishReason != "stop" {
t.Fatalf("unexpected response: %+v", resp)
}
if !strings.Contains(resp.Choices[0].Message.Content.(string), `saw:{"echo":"hi"}`) {
t.Errorf("content = %q", resp.Choices[0].Message.Content)
}
if resp.Model != "default-model" {
t.Errorf("model default not applied: %q", resp.Model)
}
}