MCPServer gains ParallelToolCalls (default false). The client's per-server callMu is now nil for a server that opted in, so CallTool stops serializing that server's calls and the caller's own concurrency decides; every other server keeps exactly one tools/call in flight. The flag is threaded from config through the provider constructor - the only place MCPServer configs become Client instances - into NewClient, in both the daemon and the agent worker.
504 lines
14 KiB
Go
504 lines
14 KiB
Go
package mcp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/tools"
|
|
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
|
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
|
)
|
|
|
|
func TestProviderInitializesDiscoversAndCallsOriginalMCPTool(t *testing.T) {
|
|
transport := newFakeTransport()
|
|
transport.listed = []protocol.ToolSchema{{
|
|
Name: "Search Repos!",
|
|
Description: "Search repositories",
|
|
InputSchema: tools.JSONSchema{"type": "object"},
|
|
}}
|
|
provider := New("Git Hub/Prod", transport, false)
|
|
|
|
manifest := provider.Manifest()
|
|
if manifest.ID != "mcp.git-hub-prod" {
|
|
t.Fatalf("Manifest().ID = %q, want mcp.git-hub-prod", manifest.ID)
|
|
}
|
|
if err := manifest.Validate(); err != nil {
|
|
t.Fatalf("Manifest().Validate() = %v", err)
|
|
}
|
|
if err := provider.Start(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := transport.notifications(); len(got) != 1 || got[0] != "notifications/initialized" {
|
|
t.Fatalf("notifications = %v, want initialized", got)
|
|
}
|
|
|
|
entries, err := provider.Discover(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(entries) != 1 {
|
|
t.Fatalf("Discover() count = %d, want 1", len(entries))
|
|
}
|
|
entry := entries[0]
|
|
if entry.Name != "mcp.git_hub_prod.search_repos" {
|
|
t.Fatalf("tool name = %q, want sanitized mapped name", entry.Name)
|
|
}
|
|
if entry.Toolset != "mcp" || entry.Description != transport.listed[0].Description {
|
|
t.Fatalf("tool metadata = %+v", entry)
|
|
}
|
|
|
|
got, err := entry.Handler(context.Background(), map[string]any{"query": "archie"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != "firstsecond" {
|
|
t.Fatalf("handler result = %v, want concatenated MCP text", got)
|
|
}
|
|
name, args := transport.lastCall()
|
|
if name != "Search Repos!" || args["query"] != "archie" {
|
|
t.Fatalf("MCP call = %q %#v, want original name and args", name, args)
|
|
}
|
|
if err := provider.Stop(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if transport.State() != protocol.StateStopped {
|
|
t.Fatalf("transport state = %s, want stopped", transport.State())
|
|
}
|
|
}
|
|
|
|
// callOverlapWindow bounds how long a test gives tool calls to reach the
|
|
// transport. A call allowed to overlap gets there microseconds after its
|
|
// goroutine starts, so one still absent after this window is held back by
|
|
// the client, not by the scheduler.
|
|
const callOverlapWindow = 500 * time.Millisecond
|
|
|
|
// reachedSend reports how many of n calls reached Send within window.
|
|
func reachedSend(entered <-chan struct{}, n int, window time.Duration) int {
|
|
count := 0
|
|
for range n {
|
|
select {
|
|
case <-entered:
|
|
count++
|
|
case <-time.After(window):
|
|
return count
|
|
}
|
|
}
|
|
return count
|
|
}
|
|
|
|
// TestProviderParallelToolCallsReachTheMCPClient pins the wiring half of the
|
|
// per-server flag: the choice made at construction has to reach the client
|
|
// the provider builds in Start, or a server that opted in stays serialized
|
|
// with no other symptom.
|
|
func TestProviderParallelToolCallsReachTheMCPClient(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
parallelToolCalls bool
|
|
wantInFlight int
|
|
}{
|
|
{
|
|
name: "provider for a server that has not opted in keeps one tool call in flight",
|
|
wantInFlight: 1,
|
|
},
|
|
{
|
|
name: "provider for an opted-in server overlaps tool calls",
|
|
parallelToolCalls: true,
|
|
wantInFlight: 2,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
transport := newFakeTransport()
|
|
transport.listed = []protocol.ToolSchema{{Name: "slow"}}
|
|
transport.callEntered = make(chan struct{}, 2)
|
|
transport.callRelease = make(chan struct{})
|
|
provider := New("slow-server", transport, tt.parallelToolCalls)
|
|
if err := provider.Start(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entries, err := provider.Discover(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(entries) != 1 {
|
|
t.Fatalf("Discover() count = %d, want 1", len(entries))
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
for range 2 {
|
|
wg.Go(func() {
|
|
if _, err := entries[0].Handler(context.Background(), nil); err != nil {
|
|
t.Errorf("handler: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
if got := reachedSend(transport.callEntered, 2, callOverlapWindow); got != tt.wantInFlight {
|
|
t.Fatalf("tool calls in flight at once = %d, want %d", got, tt.wantInFlight)
|
|
}
|
|
|
|
close(transport.callRelease)
|
|
wg.Wait()
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProviderSurfacesMCPToolLevelErrors(t *testing.T) {
|
|
transport := newFakeTransport()
|
|
transport.listed = []protocol.ToolSchema{{Name: "fails"}}
|
|
transport.callResult = protocol.CallToolResult{
|
|
Content: []protocol.ContentBlock{{Type: "text", Text: "denied"}},
|
|
IsError: true,
|
|
}
|
|
provider := New("errors", transport, false)
|
|
if err := provider.Start(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entries, err := provider.Discover(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(entries) != 1 {
|
|
t.Fatalf("Discover() count = %d, want 1", len(entries))
|
|
}
|
|
|
|
if _, err := entries[0].Handler(context.Background(), nil); err == nil ||
|
|
!strings.Contains(err.Error(), "denied") {
|
|
t.Fatalf("handler error = %v, want MCP tool failure content", err)
|
|
}
|
|
}
|
|
|
|
func TestProviderPreservesResourceTextAndMultimodalResults(t *testing.T) {
|
|
transport := newFakeTransport()
|
|
transport.listed = []protocol.ToolSchema{{Name: "capture"}}
|
|
transport.callResult = protocol.CallToolResult{Content: []protocol.ContentBlock{
|
|
{Type: "text", Text: "captured"},
|
|
{Type: "resource", Resource: &protocol.ResourceContent{Text: " resource context"}},
|
|
{Type: "image", Data: "aW1hZ2U=", MimeType: "image/png"},
|
|
}}
|
|
provider := New("media", transport, false)
|
|
if err := provider.Start(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entries, err := provider.Discover(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
output, err := entries[0].Handler(context.Background(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, ok := output.(tools.MultimodalResult)
|
|
if !ok {
|
|
t.Fatalf("handler result type = %T, want tools.MultimodalResult", output)
|
|
}
|
|
if !result.IsMultimodal || result.Summary != "captured resource context" {
|
|
t.Fatalf("multimodal result = %+v", result)
|
|
}
|
|
}
|
|
|
|
func TestProviderWritesBinaryContentToDeliverableMediaRefs(t *testing.T) {
|
|
transport := newFakeTransport()
|
|
transport.listed = []protocol.ToolSchema{{Name: "media"}}
|
|
transport.callResult = protocol.CallToolResult{Content: []protocol.ContentBlock{
|
|
{Type: "text", Text: "summary "},
|
|
{Type: "image", Data: "aW1hZ2U=", MimeType: "image/png"},
|
|
{Type: "audio", Data: "YXVkaW8=", MimeType: "audio/wav"},
|
|
{Type: "resource", Resource: &protocol.ResourceContent{MimeType: "application/pdf", Blob: "cGRm"}},
|
|
}}
|
|
provider := New("media", transport, false)
|
|
if err := provider.Start(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = provider.Stop(context.Background()) })
|
|
|
|
entries, err := provider.Discover(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(entries) != 1 {
|
|
t.Fatalf("Discover() count = %d, want 1", len(entries))
|
|
}
|
|
|
|
output, err := entries[0].Handler(context.Background(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, ok := output.(tools.MultimodalResult)
|
|
if !ok {
|
|
t.Fatalf("handler result type = %T, want tools.MultimodalResult", output)
|
|
}
|
|
if !result.IsMultimodal {
|
|
t.Error("IsMultimodal = false, want true")
|
|
}
|
|
if result.Summary != "summary " {
|
|
t.Errorf("Summary = %q, want %q", result.Summary, "summary ")
|
|
}
|
|
if result.SubdirHint == "" {
|
|
t.Fatal("SubdirHint is empty, want the written media directory")
|
|
}
|
|
if len(result.Files) != 3 {
|
|
t.Fatalf("Files = %v, want 3 written files", result.Files)
|
|
}
|
|
if len(result.URLs) != 3 {
|
|
t.Fatalf("URLs = %v, want 3 deliverable media refs", result.URLs)
|
|
}
|
|
|
|
want := []struct {
|
|
bytes []byte
|
|
typ string
|
|
}{
|
|
{[]byte("image"), "image"},
|
|
{[]byte("audio"), "audio"},
|
|
{[]byte("pdf"), "document"},
|
|
}
|
|
for i, ref := range result.URLs {
|
|
if ref.Path == "" {
|
|
t.Fatalf("URLs[%d].Path is empty, want a local file for delivery", i)
|
|
}
|
|
if ref.Type != want[i].typ {
|
|
t.Errorf("URLs[%d].Type = %q, want %q", i, ref.Type, want[i].typ)
|
|
}
|
|
got, err := os.ReadFile(ref.Path)
|
|
if err != nil {
|
|
t.Fatalf("URLs[%d].Path not readable: %v", i, err)
|
|
}
|
|
if string(got) != string(want[i].bytes) {
|
|
t.Errorf("URLs[%d] bytes = %q, want %q", i, got, want[i].bytes)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProviderStartFailureStopsTransport(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
startErr error
|
|
sendErr error
|
|
wantStops int
|
|
}{
|
|
{name: "transport start", startErr: errors.New("start failed")},
|
|
{name: "initialize", sendErr: errors.New("initialize failed"), wantStops: 1},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
transport := newFakeTransport()
|
|
transport.startErr = tt.startErr
|
|
transport.sendErr = tt.sendErr
|
|
provider := New("failure", transport, false)
|
|
if err := provider.Start(context.Background()); err == nil {
|
|
t.Fatal("Start() succeeded")
|
|
}
|
|
if transport.stopCount != tt.wantStops {
|
|
t.Fatalf("transport Stop() calls = %d, want %d", transport.stopCount, tt.wantStops)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProviderRejectsInvalidConfiguration(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
server string
|
|
transport LifecycleTransport
|
|
}{
|
|
{name: "empty name", transport: newFakeTransport()},
|
|
{name: "nil transport", server: "server"},
|
|
{name: "typed nil transport", server: "server", transport: (*fakeTransport)(nil)},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
provider := New(tt.server, tt.transport, false)
|
|
if err := provider.Start(context.Background()); err == nil {
|
|
t.Fatal("Start() succeeded")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProviderSatisfiesEngine(t *testing.T) {
|
|
var _ toolprovider.Engine = New("compile", newFakeTransport(), false)
|
|
}
|
|
|
|
// Compile-time checks: HTTP and SSE transports satisfy LifecycleTransport.
|
|
func TestTransportsSatisfyLifecycleTransport(t *testing.T) {
|
|
var _ LifecycleTransport = (*protocol.HTTPTransport)(nil)
|
|
var _ LifecycleTransport = (*protocol.SSETransport)(nil)
|
|
// Also check StdioTransport, which already satisfied it.
|
|
var _ LifecycleTransport = (*protocol.StdioTransport)(nil)
|
|
}
|
|
|
|
func TestSanitizeSegmentsAreDeterministicASCII(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
manifest string
|
|
tool string
|
|
}{
|
|
{name: "spaces symbols and unicode", input: " 42 Répo/搜索 ", manifest: "x-42-r-po", tool: "x_42_r_po"},
|
|
{name: "separator runs", input: "A...B", manifest: "a-b", tool: "a_b"},
|
|
{name: "no identifier content", input: "---", manifest: "", tool: ""},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := sanitizeManifestSegment(tt.input); got != tt.manifest {
|
|
t.Fatalf("sanitizeManifestSegment(%q) = %q, want %q", tt.input, got, tt.manifest)
|
|
}
|
|
if got := sanitizeToolSegment(tt.input); got != tt.tool {
|
|
t.Fatalf("sanitizeToolSegment(%q) = %q, want %q", tt.input, got, tt.tool)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type fakeTransport struct {
|
|
mu sync.Mutex
|
|
|
|
state protocol.TransportState
|
|
startErr error
|
|
sendErr error
|
|
stopErr error
|
|
stopCount int
|
|
listed []protocol.ToolSchema
|
|
callResult protocol.CallToolResult
|
|
notifyMethods []string
|
|
calledName string
|
|
calledArgs map[string]any
|
|
|
|
// callEntered receives one token per tools/call that reaches Send, and
|
|
// the call then blocks until callRelease is closed. Set both before
|
|
// Start to hold tool calls open and observe how many the client let
|
|
// overlap.
|
|
callEntered chan struct{}
|
|
callRelease chan struct{}
|
|
}
|
|
|
|
func newFakeTransport() *fakeTransport {
|
|
return &fakeTransport{
|
|
state: protocol.StateStopped,
|
|
callResult: protocol.CallToolResult{
|
|
Content: []protocol.ContentBlock{
|
|
{Type: "text", Text: "first"},
|
|
{Type: "text", Text: "second"},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (t *fakeTransport) Start(context.Context) error {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
if t.startErr != nil {
|
|
return t.startErr
|
|
}
|
|
t.state = protocol.StateRunning
|
|
return nil
|
|
}
|
|
|
|
func (t *fakeTransport) Stop(context.Context) error {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
t.stopCount++
|
|
t.state = protocol.StateStopped
|
|
return t.stopErr
|
|
}
|
|
|
|
func (t *fakeTransport) State() protocol.TransportState {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
return t.state
|
|
}
|
|
|
|
func (t *fakeTransport) Send(_ context.Context, body []byte) ([]byte, error) {
|
|
var request struct {
|
|
ID json.RawMessage `json:"id"`
|
|
Method string `json:"method"`
|
|
Params json.RawMessage `json:"params"`
|
|
}
|
|
if err := json.Unmarshal(body, &request); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Outside the lock: holding it here would serialize the calls this hook
|
|
// exists to observe, making a parallel client look serialized.
|
|
if request.Method == "tools/call" {
|
|
if t.callEntered != nil {
|
|
t.callEntered <- struct{}{}
|
|
}
|
|
if t.callRelease != nil {
|
|
<-t.callRelease
|
|
}
|
|
}
|
|
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
if t.sendErr != nil {
|
|
return nil, t.sendErr
|
|
}
|
|
var result any
|
|
switch request.Method {
|
|
case "initialize":
|
|
result = protocol.InitializeResult{
|
|
ProtocolVersion: "2024-11-05",
|
|
ServerInfo: protocol.ServerInfo{Name: "fake", Version: "1.0.0"},
|
|
Capabilities: protocol.Capabilities{Tools: json.RawMessage(`{}`)},
|
|
}
|
|
case "tools/list":
|
|
result = map[string]any{"tools": t.listed}
|
|
case "tools/call":
|
|
var params struct {
|
|
Name string `json:"name"`
|
|
Arguments map[string]any `json:"arguments"`
|
|
}
|
|
if err := json.Unmarshal(request.Params, ¶ms); err != nil {
|
|
return nil, err
|
|
}
|
|
t.calledName = params.Name
|
|
t.calledArgs = params.Arguments
|
|
result = t.callResult
|
|
default:
|
|
return nil, errors.New("unexpected method: " + request.Method)
|
|
}
|
|
resultJSON, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return json.Marshal(protocol.Message{
|
|
JSONRPC: "2.0",
|
|
ID: request.ID,
|
|
Result: resultJSON,
|
|
})
|
|
}
|
|
|
|
func (t *fakeTransport) Notify(_ context.Context, body []byte) error {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
var notification struct {
|
|
Method string `json:"method"`
|
|
}
|
|
if err := json.Unmarshal(body, ¬ification); err != nil {
|
|
return err
|
|
}
|
|
t.notifyMethods = append(t.notifyMethods, notification.Method)
|
|
return nil
|
|
}
|
|
|
|
func (t *fakeTransport) notifications() []string {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
return append([]string(nil), t.notifyMethods...)
|
|
}
|
|
|
|
func (t *fakeTransport) lastCall() (string, map[string]any) {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
return t.calledName, t.calledArgs
|
|
}
|