mcp
This commit is contained in:
1 parent
9f578780bd
commit
5807132ac6
6 files changed
+838
-81
No files matched your search
+234
-8
@@ -156,10 +156,12 @@ func TestAgentAgenticLoop(t *testing.T) {
|
||||
agent := NewAgent(AgentConfig{
|
||||
UpstreamURL: upSrv.URL,
|
||||
UpstreamAPIKey: "sk-test",
|
||||
MCPURL: mcpSrv.URL + "/mcp",
|
||||
MCPToken: "mcp-secret",
|
||||
SystemPrompt: "You are a test.",
|
||||
ProgressEvent: "cms_progress",
|
||||
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}`
|
||||
@@ -271,7 +273,7 @@ func TestAgentLocalTool(t *testing.T) {
|
||||
authBefore := lastAuth
|
||||
agent := NewAgent(AgentConfig{
|
||||
UpstreamURL: upSrv.URL, UpstreamAPIKey: "k",
|
||||
MCPURL: mcpSrv.URL + "/mcp",
|
||||
MCPServers: []MCPEndpoint{{URL: mcpSrv.URL + "/mcp"}},
|
||||
LocalTools: []Tool{{
|
||||
Name: "local_ping",
|
||||
Description: "In-process ping",
|
||||
@@ -320,7 +322,7 @@ func TestAgentFrontendToolCallPassthrough(t *testing.T) {
|
||||
|
||||
agent := NewAgent(AgentConfig{
|
||||
UpstreamURL: upSrv.URL, UpstreamAPIKey: "k",
|
||||
MCPURL: mcpSrv.URL + "/mcp",
|
||||
MCPServers: []MCPEndpoint{{URL: mcpSrv.URL + "/mcp"}},
|
||||
})
|
||||
req := httptest.NewRequest(http.MethodPost, "/chat/completions",
|
||||
strings.NewReader(`{"messages":[{"role":"user","content":"go"}],"stream":true}`))
|
||||
@@ -347,8 +349,8 @@ func TestAgentNonStreaming(t *testing.T) {
|
||||
|
||||
agent := NewAgent(AgentConfig{
|
||||
UpstreamURL: upSrv.URL, UpstreamAPIKey: "k",
|
||||
MCPURL: mcpSrv.URL + "/mcp",
|
||||
Model: "default-model",
|
||||
MCPServers: []MCPEndpoint{{URL: mcpSrv.URL + "/mcp"}},
|
||||
Model: "default-model",
|
||||
})
|
||||
req := httptest.NewRequest(http.MethodPost, "/chat/completions",
|
||||
strings.NewReader(`{"messages":[{"role":"user","content":"hi"}]}`))
|
||||
@@ -369,3 +371,227 @@ func TestAgentNonStreaming(t *testing.T) {
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user