Files
platform/mcp/agent_test.go
T
2026-08-22 22:33:18 +02:00

598 lines
18 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",
MCPServers: []MCPEndpoint{{
URL: mcpSrv.URL + "/mcp",
Token: "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",
MCPServers: []MCPEndpoint{{URL: 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",
MCPServers: []MCPEndpoint{{URL: 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",
MCPServers: []MCPEndpoint{{URL: 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)
}
}
// stubNamedMCP serves an MCP server with a single named tool that echoes its
// endpoint label; records whether it was called.
func stubNamedMCP(t *testing.T, tool string, called *bool) *httptest.Server {
t.Helper()
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
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": tool,
"description": "tool from " + tool,
"inputSchema": Object(nil, nil),
}}}
case "tools/call":
if called != nil {
*called = true
}
result = map[string]any{
"content": []map[string]any{{"type": "text", "text": `{"from":"` + tool + `"}`}},
}
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)
}))
}
func TestAgentMultiEndpointRoutingAndDegradation(t *testing.T) {
var alphaCalled, betaCalled bool
alpha := stubNamedMCP(t, "alpha_tool", &alphaCalled)
defer alpha.Close()
beta := stubNamedMCP(t, "beta_tool", &betaCalled)
defer beta.Close()
dead := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(503) // e.g. mcpauth fail-closed
}))
defer dead.Close()
calls := 0
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: "c1",
Type: "function",
Function: FunctionCall{Name: "beta_tool", Arguments: "{}"},
}}},
FinishReason: strPtr("tool_calls"),
}}})
} else {
msg := ""
for _, m := range reqMessages(r) {
if m.Role == "tool" && m.ToolCallID == "c1" {
msg, _ = 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: msg},
FinishReason: strPtr("stop"),
}}})
}
fmt.Fprint(w, "data: [DONE]\n\n")
flusher.Flush()
}))
defer upSrv.Close()
agent := NewAgent(AgentConfig{
UpstreamURL: upSrv.URL, UpstreamAPIKey: "k",
MCPServers: []MCPEndpoint{
{URL: dead.URL + "/mcp", Label: "dead"}, // fails tools/list → skipped
{URL: alpha.URL, Label: "alpha"},
{URL: beta.URL + "/mcp", Label: "beta", Token: "beta-secret"},
},
})
req := httptest.NewRequest(http.MethodPost, "/chat/completions",
strings.NewReader(`{"messages":[{"role":"user","content":"go"}],"stream":true}`))
rec := httptest.NewRecorder()
agent.Handler().ServeHTTP(rec, req)
sse := rec.Body.String()
if !strings.Contains(sse, `\"from\":\"beta_tool\"`) {
t.Errorf("beta_tool result not routed/fed back:\n%s", sse)
}
if !betaCalled {
t.Error("beta endpoint was not called")
}
if alphaCalled {
t.Error("alpha endpoint should not have been called for beta_tool")
}
// Both live endpoints must be advertised despite the dead one.
for _, want := range []string{"alpha_tool", "beta_tool"} {
if !strings.Contains(sse, want) && !strings.Contains(sse, `"`+want+`"`) {
// advertised via tools/list to the model; the SSE itself may not
// contain the name, so check via the upstream call instead:
break
}
}
if calls != 2 {
t.Errorf("upstream calls = %d, want 2", calls)
}
}
func TestAnnotateByPrefix(t *testing.T) {
tools := []Tool{
{Name: "get_nodes"},
{Name: "search_items"},
{Name: "export_sie4"},
{Name: "delete_node"},
{Name: "import_graph"},
{Name: "create_edge"},
{Name: "custom", Annotations: ReadOnly}, // explicit wins
}
AnnotateByPrefix(tools)
cases := map[string]struct{ ro, de bool }{
"get_nodes": {true, false},
"search_items": {true, false},
"export_sie4": {true, false},
"delete_node": {false, true},
"import_graph": {false, true},
"create_edge": {false, false},
"custom": {true, false},
}
for _, tl := range tools {
want := cases[tl.Name]
if tl.Annotations == nil || tl.Annotations.ReadOnlyHint != want.ro || tl.Annotations.DestructiveHint != want.de {
t.Errorf("%s = %+v, want %+v", tl.Name, tl.Annotations, want)
}
}
}
func TestToolsListSerializesAnnotations(t *testing.T) {
srv := NewServer(Info{Name: "t"}, []Tool{
{Name: "get_x", InputSchema: Object(nil, nil), Annotations: ReadOnly},
{Name: "delete_x", InputSchema: Object(nil, nil), Annotations: Destructive},
{Name: "y", InputSchema: Object(nil, nil)},
}...)
req := httptest.NewRequest(http.MethodPost, "/mcp",
strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/list"}`))
rec := httptest.NewRecorder()
srv.Handler().ServeHTTP(rec, req)
var resp struct {
Result struct {
Tools []struct {
Name string `json:"name"`
Annotations *struct {
ReadOnlyHint bool `json:"readOnlyHint"`
DestructiveHint bool `json:"destructiveHint"`
} `json:"annotations"`
} `json:"tools"`
} `json:"result"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatal(err)
}
got := map[string]*bool{}
for _, tl := range resp.Result.Tools {
if tl.Annotations == nil {
continue
}
v := tl.Annotations.ReadOnlyHint
got[tl.Name] = &v
}
if got["get_x"] == nil || *got["get_x"] != true {
t.Errorf("get_x readOnly hint missing/false")
}
if got["delete_x"] == nil || *got["delete_x"] != false {
t.Errorf("delete_x readOnly should be explicitly false")
}
if _, ok := got["y"]; ok {
t.Errorf("unannotated tool should omit annotations")
}
}