diff --git a/internal/tools/mcp/client.go b/internal/tools/mcp/client.go index d29d7019f..dce95aa3f 100644 --- a/internal/tools/mcp/client.go +++ b/internal/tools/mcp/client.go @@ -2,14 +2,8 @@ package mcp import ( "context" - "encoding/base64" "encoding/json" - "errors" "fmt" - "os" - "path/filepath" - "strconv" - "strings" "sync" "sync/atomic" @@ -37,54 +31,17 @@ type Client struct { transport Transport serverName string nextID atomic.Int64 - // mediaDir is the root directory binary content blocks (images, - // audio, resource blobs) are written under, in a per-tool - // subdirectory. Empty means media is reported but not persisted -- - // see [WithMediaDir]. - mediaDir string - // mediaCallSeq disambiguates concurrent calls to the same tool so - // their written media files never share a directory. - mediaCallSeq atomic.Int64 - // callMu serializes tools/call requests to this server unless - // parallelToolCalls is set. nil (the default) means serialized -- - // most MCP servers are single-threaded processes and don't expect or - // handle concurrent requests safely. - callMu *sync.Mutex - parallelToolCalls bool -} - -// ClientOption configures optional Client behavior. -type ClientOption func(*Client) - -// WithMediaDir sets the root directory for writing binary content -// (images, audio, resource blobs) a tool call returns, instead of -// inlining base64 data into the model's context. Each tool's media -// files are written under dir//. -func WithMediaDir(dir string) ClientOption { - return func(c *Client) { c.mediaDir = dir } -} - -// WithParallelToolCalls opts this server into concurrent tool calls. -// By default (false), CallTool serializes all calls to a given Client -// one at a time -- the safe default, since most MCP servers are -// single-threaded subprocesses that don't expect concurrent requests. -// Set true only for servers documented or known to handle concurrent -// requests safely. Serialization is per-Client (per-server); it never -// blocks calls to a different server. -func WithParallelToolCalls(parallel bool) ClientOption { - return func(c *Client) { c.parallelToolCalls = parallel } + // callMu serializes tools/call requests to this server. Most MCP + // servers are single-threaded processes and don't expect or handle + // concurrent requests safely. + callMu *sync.Mutex } // NewClient builds a Client over transport. serverName identifies this -// MCP server in registered tool names (e.g. "github" produces -// "mcp.github.search_repos") and in log/error messages; it need not -// match the server's own self-reported name. -func NewClient(transport Transport, serverName string, opts ...ClientOption) *Client { - c := &Client{transport: transport, serverName: serverName, callMu: &sync.Mutex{}} - for _, opt := range opts { - opt(c) - } - return c +// MCP server in log/error messages; it need not match the server's own +// self-reported name. +func NewClient(transport Transport, serverName string) *Client { + return &Client{transport: transport, serverName: serverName, callMu: &sync.Mutex{}} } // InitializeResult is the server's response to initialize. @@ -198,10 +155,8 @@ func (c *Client) ListTools(ctx context.Context) ([]ToolSchema, error) { // reported via CallToolResult.IsError, not a Go error -- see // [CallToolResult]. func (c *Client) CallTool(ctx context.Context, name string, arguments map[string]any) (CallToolResult, error) { - if !c.parallelToolCalls { - c.callMu.Lock() - defer c.callMu.Unlock() - } + c.callMu.Lock() + defer c.callMu.Unlock() params := map[string]any{"name": name, "arguments": arguments} var result CallToolResult @@ -211,202 +166,6 @@ func (c *Client) CallTool(ctx context.Context, name string, arguments map[string return result, nil } -// RegisterTools calls ListTools and registers each discovered tool into -// reg as "mcp..", with a Handler that calls -// CallTool and surfaces CallToolResult.IsError as a Go error (a registry -// Handler has no other channel to report tool-level failure through). -// Returns the number of tools registered; a tool whose name collides -// with an existing registry entry is skipped and logged by the caller -// via the returned count being less than len(tools). -func (c *Client) RegisterTools(ctx context.Context, reg *tools.Registry) (int, error) { - toolList, err := c.ListTools(ctx) - if err != nil { - return 0, err - } - registered := 0 - for _, ts := range toolList { - entry := tools.ToolEntry{ - Name: fmt.Sprintf("mcp.%s.%s", c.serverName, ts.Name), - Toolset: "mcp", - Schema: ts.InputSchema, - Description: ts.Description, - Handler: c.handlerFor(ts.Name), - } - if err := reg.Register(entry); err != nil { - continue - } - registered++ - } - return registered, nil -} - -// handlerFor returns a tools.Handler that calls toolName via CallTool. -// A result whose content is text-only returns a plain string (backward -// compatible with callers that expect simple tool output). A result -// carrying image/audio/resource-blob content returns a -// [tools.MultimodalResult] instead of inlining base64 data into the -// model's context -- see [WithMediaDir]. -func (c *Client) handlerFor(toolName string) tools.Handler { - return func(ctx context.Context, input map[string]any) (any, error) { - result, err := c.CallTool(ctx, toolName, input) - if err != nil { - return nil, err - } - - var sb strings.Builder - var media []ContentBlock - for _, block := range result.Content { - switch { - case block.Type == "resource" && block.Resource != nil && block.Resource.Blob != "": - // A resource can carry both inline text and a binary blob - // at once (the spec doesn't forbid it); keep the text in - // the summary and still treat the blob as media. - if block.Resource.Text != "" { - sb.WriteString(block.Resource.Text) - } - media = append(media, block) - case block.Type == "resource" && block.Resource != nil: - sb.WriteString(block.Resource.Text) - case block.Data != "": - media = append(media, block) - case block.Type == "" || block.Type == "text": - sb.WriteString(block.Text) - default: - // An unrecognized content type (a future MCP block kind - // this client doesn't model yet). Note it rather than - // silently dropping it so a caller reading Summary can - // tell content was omitted. - fmt.Fprintf(&sb, "[unhandled content block type %q]", block.Type) - } - } - text := sb.String() - - if result.IsError { - return nil, fmt.Errorf("mcp: tool %s reported an error: %s", toolName, text) - } - if len(media) == 0 { - return text, nil - } - return c.writeMultimodalResult(toolName, text, media) - } -} - -// maxMediaBlockBase64Bytes caps the base64-encoded size of a single -// media content block before it is decoded and written to disk. An MCP -// server is a semi-trusted external process; without a cap, one -// misbehaving or malicious response could force the daemon to allocate -// and persist an unbounded amount of data. ~48MB decoded (base64 is -// ~4/3 the size of the decoded payload). -const maxMediaBlockBase64Bytes = 64 << 20 // 64MiB - -// writeMultimodalResult decodes and, when mediaDir is configured, writes -// each media block to disk under mediaDir///, returning the resulting envelope. Malformed base64 data is -// a hard error -- the server sent a content block it claimed was media -// but wasn't decodable. -func (c *Client) writeMultimodalResult(toolName, summaryText string, media []ContentBlock) (tools.MultimodalResult, error) { - result := tools.MultimodalResult{IsMultimodal: true, Summary: summaryText} - if c.mediaDir == "" { - return result, nil - } - - // toolName is server-supplied (it comes from the MCP server's own - // tools/list response, not from caller-controlled input) and is used - // to build a filesystem path below. Never trust it: a malicious - // server could otherwise advertise a tool name like "../../etc/cron.d/x" - // to escape mediaDir. Reject any path separator or "." component - // outright rather than trying to sanitize it into something safe. - if err := rejectPathTraversal(toolName); err != nil { - return tools.MultimodalResult{}, fmt.Errorf("mcp: tool name %q is not safe as a path component: %w", toolName, err) - } - - // Each call gets its own subdirectory so concurrent calls to the same - // tool (CallTool is safe for concurrent use -- see the atomic id - // generator in call()) never write to the same index-based filename - // and clobber or interleave each other's output. - callSeq := c.mediaCallSeq.Add(1) - subdir := filepath.Join(c.mediaDir, toolName, strconv.FormatInt(callSeq, 10)) - if err := os.MkdirAll(subdir, 0o755); err != nil { - return tools.MultimodalResult{}, fmt.Errorf("mcp: tool %s: create media dir: %w", toolName, err) - } - - files := make([]string, 0, len(media)) - for i, block := range media { - data := block.Data - mimeType := block.MimeType - if block.Resource != nil { - data = block.Resource.Blob - mimeType = block.Resource.MimeType - } - if len(data) > maxMediaBlockBase64Bytes { - return tools.MultimodalResult{}, fmt.Errorf("mcp: tool %s: media block %d (%d bytes base64) exceeds the %d byte limit", - toolName, i, len(data), maxMediaBlockBase64Bytes) - } - raw, err := base64.StdEncoding.DecodeString(data) - if err != nil { - return tools.MultimodalResult{}, fmt.Errorf("mcp: tool %s: decode media block %d: %w", toolName, i, err) - } - path := filepath.Join(subdir, fmt.Sprintf("%d%s", i, extensionForMimeType(mimeType))) - if err := os.WriteFile(path, raw, 0o644); err != nil { - return tools.MultimodalResult{}, fmt.Errorf("mcp: tool %s: write media block %d: %w", toolName, i, err) - } - files = append(files, path) - } - - result.SubdirHint = subdir - result.Files = files - return result, nil -} - -// rejectPathTraversal returns an error if name contains a path separator -// or a "." component (covers both ".." and a bare "."), i.e. anything -// that would let it escape or alter the directory it's joined into. -// Tool names have no legitimate reason to contain path structure. -func rejectPathTraversal(name string) error { - if name == "" { - return errors.New("empty name") - } - if strings.ContainsAny(name, `/\`) { - return errors.New("contains a path separator") - } - // No separators at this point, so name is a single path component -- - // only "." and ".." are themselves traversal-meaningful. - if name == "." || name == ".." { - return errors.New("is a \".\" or \"..\" path component") - } - return nil -} - -// extensionForMimeType maps common MCP media MIME types to a file -// extension. Unrecognized types get no extension -- the file is still -// written and its path returned; a missing extension doesn't stop the -// content from being usable, it's just not identifiable by filename -// alone. -func extensionForMimeType(mimeType string) string { - switch mimeType { - case "image/png": - return ".png" - case "image/jpeg": - return ".jpg" - case "image/gif": - return ".gif" - case "image/webp": - return ".webp" - case "audio/wav", "audio/wave", "audio/x-wav": - return ".wav" - case "audio/mpeg": - return ".mp3" - case "audio/ogg": - return ".ogg" - case "video/mp4": - return ".mp4" - case "application/pdf": - return ".pdf" - default: - return "" - } -} - // call sends a JSON-RPC request and decodes its result into out. It // returns an error for a transport failure, a JSON-RPC error response, // or a malformed result payload. diff --git a/internal/tools/mcp/client_test.go b/internal/tools/mcp/client_test.go index bfad31a8b..4e58269df 100644 --- a/internal/tools/mcp/client_test.go +++ b/internal/tools/mcp/client_test.go @@ -6,14 +6,11 @@ import ( "errors" "sync" "testing" - - "github.com/samcharles93/archie-core/internal/tools" ) // fakeSender is a Transport double that scripts responses by JSON-RPC // method name, so client tests never spawn a real subprocess. Safe for -// concurrent use -- RegisterTools-produced handlers are meant to be -// called concurrently by real callers, and tests exercise that. +// concurrent use. type fakeSender struct { // responses maps a request method to the raw response body to return. responses map[string][]byte @@ -225,88 +222,6 @@ func TestCallToolReturnsIsErrorWithoutGoError(t *testing.T) { } } -func TestRegisterToolsAddsPrefixedEntriesToRegistry(t *testing.T) { - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[ - {"name":"search_repos","description":"Search repos","inputSchema":{"type":"object"}} - ]}}`), - }, - } - c := newTestClient(f) - reg := tools.NewRegistry() - - n, err := c.RegisterTools(context.Background(), reg) - if err != nil { - t.Fatal(err) - } - if n != 1 { - t.Fatalf("RegisterTools registered %d tools, want 1", n) - } - - entries := reg.All() - if len(entries) != 1 { - t.Fatalf("registry has %d entries, want 1", len(entries)) - } - entry := entries[0] - if entry.Name != "mcp.test-server.search_repos" { - t.Errorf("registered name = %q, want mcp.test-server.search_repos", entry.Name) - } - if entry.Toolset != "mcp" { - t.Errorf("Toolset = %q, want mcp", entry.Toolset) - } - if entry.Handler == nil { - t.Fatal("registered entry has nil Handler") - } -} - -func TestRegisterToolsHandlerInvokesCallTool(t *testing.T) { - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"echo","inputSchema":{}}]}}`), - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"echoed"}]}}`), - }, - } - c := newTestClient(f) - reg := tools.NewRegistry() - - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - - entry, ok := reg.Get("mcp.test-server.echo") - if !ok { - t.Fatal("mcp.test-server.echo not found in registry") - } - out, err := entry.Handler(context.Background(), map[string]any{"msg": "hi"}) - if err != nil { - t.Fatal(err) - } - if out != "echoed" { - t.Errorf("handler output = %v, want \"echoed\"", out) - } -} - -func TestRegisterToolsHandlerSurfacesToolLevelError(t *testing.T) { - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"broken","inputSchema":{}}]}}`), - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"denied"}],"isError":true}}`), - }, - } - c := newTestClient(f) - reg := tools.NewRegistry() - - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - entry, _ := reg.Get("mcp.test-server.broken") - _, err := entry.Handler(context.Background(), nil) - if err == nil { - t.Fatal("expected handler to surface isError as a Go error") - } -} - func TestListToolsReturnsErrorOnMalformedResponse(t *testing.T) { f := &fakeSender{responses: map[string][]byte{"tools/list": []byte(`not json`)}} c := newTestClient(f) diff --git a/internal/tools/mcp/multimodal_test.go b/internal/tools/mcp/multimodal_test.go deleted file mode 100644 index fe9816008..000000000 --- a/internal/tools/mcp/multimodal_test.go +++ /dev/null @@ -1,374 +0,0 @@ -package mcp - -import ( - "context" - "encoding/base64" - "encoding/json" - "os" - "path/filepath" - "strings" - "sync" - "testing" - - "github.com/samcharles93/archie-core/internal/tools" -) - -// A 1x1 transparent PNG, base64-encoded -- small, valid binary fixture. -const tinyPNGBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=" - -func TestCallToolHandlerReturnsPlainStringForTextOnlyResult(t *testing.T) { - // Backward compatibility: a pure-text result stays a plain string, - // not a MultimodalResult -- existing callers/tests depend on this. - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"echo","inputSchema":{}}]}}`), - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"hello"}]}}`), - }, - } - c := newTestClient(f) - reg := tools.NewRegistry() - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - entry, _ := reg.Get("mcp.test-server.echo") - - out, err := entry.Handler(context.Background(), nil) - if err != nil { - t.Fatal(err) - } - if out != "hello" { - t.Errorf("out = %v (%T), want plain string \"hello\"", out, out) - } -} - -func TestCallToolHandlerWritesImageAndReturnsMultimodalResult(t *testing.T) { - dir := t.TempDir() - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"screenshot","inputSchema":{}}]}}`), - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[ - {"type":"text","text":"captured the screen"}, - {"type":"image","data":"` + tinyPNGBase64 + `","mimeType":"image/png"} - ]}}`), - }, - } - c := NewClient(f, "test-server", WithMediaDir(dir)) - reg := tools.NewRegistry() - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - entry, _ := reg.Get("mcp.test-server.screenshot") - - out, err := entry.Handler(context.Background(), nil) - if err != nil { - t.Fatal(err) - } - result, ok := out.(tools.MultimodalResult) - if !ok { - t.Fatalf("out type = %T, want tools.MultimodalResult", out) - } - if !result.IsMultimodal { - t.Error("IsMultimodal = false, want true") - } - if result.Summary != "captured the screen" { - t.Errorf("Summary = %q", result.Summary) - } - if result.SubdirHint == "" { - t.Fatal("SubdirHint is empty, want a written subdirectory") - } - if len(result.Files) != 1 { - t.Fatalf("Files = %v, want 1 entry", result.Files) - } - - written, err := os.ReadFile(result.Files[0]) - if err != nil { - t.Fatalf("written file not readable: %v", err) - } - wantBytes, _ := base64.StdEncoding.DecodeString(tinyPNGBase64) - if string(written) != string(wantBytes) { - t.Error("written file contents do not match the decoded image data") - } - if !filepath.IsAbs(result.Files[0]) { - t.Error("Files entries should be absolute paths") - } -} - -func TestCallToolHandlerWithoutMediaDirSkipsFilePersistence(t *testing.T) { - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"screenshot","inputSchema":{}}]}}`), - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[ - {"type":"image","data":"` + tinyPNGBase64 + `","mimeType":"image/png"} - ]}}`), - }, - } - c := newTestClient(f) // no WithMediaDir - reg := tools.NewRegistry() - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - entry, _ := reg.Get("mcp.test-server.screenshot") - - out, err := entry.Handler(context.Background(), nil) - if err != nil { - t.Fatal(err) - } - result, ok := out.(tools.MultimodalResult) - if !ok { - t.Fatalf("out type = %T, want tools.MultimodalResult", out) - } - if !result.IsMultimodal { - t.Error("IsMultimodal = false, want true") - } - if result.SubdirHint != "" || len(result.Files) != 0 { - t.Errorf("expected no persisted files without a media dir, got SubdirHint=%q Files=%v", result.SubdirHint, result.Files) - } -} - -func TestCallToolHandlerRejectsMalformedBase64(t *testing.T) { - dir := t.TempDir() - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"broken_image","inputSchema":{}}]}}`), - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"image","data":"not-valid-base64!!!","mimeType":"image/png"}]}}`), - }, - } - c := NewClient(f, "test-server", WithMediaDir(dir)) - reg := tools.NewRegistry() - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - entry, _ := reg.Get("mcp.test-server.broken_image") - - _, err := entry.Handler(context.Background(), nil) - if err == nil { - t.Fatal("expected error decoding malformed base64 image data") - } -} - -func TestCallToolHandlerHandlesResourceBlobAsMedia(t *testing.T) { - dir := t.TempDir() - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"export_pdf","inputSchema":{}}]}}`), - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[ - {"type":"resource","resource":{"uri":"file:///report.pdf","mimeType":"application/pdf","blob":"` + tinyPNGBase64 + `"}} - ]}}`), - }, - } - c := NewClient(f, "test-server", WithMediaDir(dir)) - reg := tools.NewRegistry() - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - entry, _ := reg.Get("mcp.test-server.export_pdf") - - out, err := entry.Handler(context.Background(), nil) - if err != nil { - t.Fatal(err) - } - result, ok := out.(tools.MultimodalResult) - if !ok { - t.Fatalf("out type = %T, want tools.MultimodalResult", out) - } - if len(result.Files) != 1 { - t.Fatalf("Files = %v, want 1 entry for the resource blob", result.Files) - } -} - -func TestCallToolHandlerTreatsResourceTextAsPlainText(t *testing.T) { - // A resource block with inline text (not a blob) is not media -- it - // should fold into Summary text, not produce a written file. - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"read_config","inputSchema":{}}]}}`), - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[ - {"type":"resource","resource":{"uri":"file:///config.yaml","mimeType":"text/yaml","text":"key: value"}} - ]}}`), - }, - } - c := newTestClient(f) - reg := tools.NewRegistry() - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - entry, _ := reg.Get("mcp.test-server.read_config") - - out, err := entry.Handler(context.Background(), nil) - if err != nil { - t.Fatal(err) - } - if out != "key: value" { - t.Errorf("out = %v (%T), want plain text \"key: value\"", out, out) - } -} - -func TestCallToolHandlerRejectsPathTraversalToolName(t *testing.T) { - dir := t.TempDir() - malicious := "../../../tmp/archie-mcp-traversal-test" - body, err := json.Marshal(map[string]any{ - "jsonrpc": "2.0", "id": 1, - "result": map[string]any{"tools": []map[string]any{{"name": malicious, "inputSchema": map[string]any{}}}}, - }) - if err != nil { - t.Fatal(err) - } - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": body, - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"image","data":"` + tinyPNGBase64 + `","mimeType":"image/png"}]}}`), - }, - } - c := NewClient(f, "test-server", WithMediaDir(dir)) - reg := tools.NewRegistry() - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - entry, ok := reg.Get("mcp." + "test-server" + "." + malicious) - if !ok { - t.Fatal("expected the tool to be registered even with a malicious name (rejection happens at call time)") - } - - _, err = entry.Handler(context.Background(), nil) - if err == nil { - t.Fatal("expected an error rejecting the path-traversal tool name, media would have escaped mediaDir") - } - if !strings.Contains(err.Error(), "not safe as a path component") { - t.Errorf("error = %v, want a path-traversal rejection message", err) - } -} - -func TestWriteMultimodalResultRejectsDotAndDotDotToolNames(t *testing.T) { - dir := t.TempDir() - for _, name := range []string{".", "..", "a/b", `a\b`} { - c := NewClient(&fakeSender{}, "test-server", WithMediaDir(dir)) - _, err := c.writeMultimodalResult(name, "", []ContentBlock{{Type: "image", Data: tinyPNGBase64, MimeType: "image/png"}}) - if err == nil { - t.Errorf("toolName %q: expected rejection, got no error", name) - } - } -} - -func TestWriteMultimodalResultRejectsOversizedMediaBlock(t *testing.T) { - dir := t.TempDir() - c := NewClient(&fakeSender{}, "test-server", WithMediaDir(dir)) - oversized := strings.Repeat("A", maxMediaBlockBase64Bytes+1) - - _, err := c.writeMultimodalResult("big_tool", "", []ContentBlock{{Type: "image", Data: oversized, MimeType: "image/png"}}) - if err == nil { - t.Fatal("expected an error for a media block exceeding the size cap") - } - if !strings.Contains(err.Error(), "exceeds") { - t.Errorf("error = %v, want a size-limit message", err) - } - - // The per-call subdirectory may have been created (MkdirAll happens - // before the size check), but no file should have been written - // anywhere under dir. - var foundFile string - if err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error { - if err != nil { - return err - } - if !d.IsDir() { - foundFile = path - } - return nil - }); err != nil { - t.Fatal(err) - } - if foundFile != "" { - t.Errorf("expected no files written under %s, found %s", dir, foundFile) - } -} - -func TestWriteMultimodalResultConcurrentCallsDoNotClobberFiles(t *testing.T) { - dir := t.TempDir() - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"snap","inputSchema":{}}]}}`), - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"image","data":"` + tinyPNGBase64 + `","mimeType":"image/png"}]}}`), - }, - } - c := NewClient(f, "test-server", WithMediaDir(dir)) - reg := tools.NewRegistry() - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - entry, _ := reg.Get("mcp.test-server.snap") - - const n = 25 - var wg sync.WaitGroup - results := make([]tools.MultimodalResult, n) - errs := make([]error, n) - for i := range n { - wg.Add(1) - go func(i int) { - defer wg.Done() - out, err := entry.Handler(context.Background(), nil) - if err != nil { - errs[i] = err - return - } - res, ok := out.(tools.MultimodalResult) - if !ok { - t.Errorf("call %d: out is %T, want tools.MultimodalResult", i, out) - return - } - results[i] = res - }(i) - } - wg.Wait() - - seen := make(map[string]bool, n) - for i, err := range errs { - if err != nil { - t.Fatalf("call %d: %v", i, err) - } - if len(results[i].Files) != 1 { - t.Fatalf("call %d: Files = %v, want 1 entry", i, results[i].Files) - } - path := results[i].Files[0] - if seen[path] { - t.Fatalf("concurrent calls both wrote to %q -- a file was clobbered", path) - } - seen[path] = true - } - if len(seen) != n { - t.Fatalf("expected %d distinct file paths, got %d", n, len(seen)) - } -} - -func TestCallToolHandlerMultipleImagesGetDistinctFiles(t *testing.T) { - dir := t.TempDir() - f := &fakeSender{ - responses: map[string][]byte{ - "tools/list": []byte(`{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"multi_shot","inputSchema":{}}]}}`), - "tools/call": []byte(`{"jsonrpc":"2.0","id":2,"result":{"content":[ - {"type":"image","data":"` + tinyPNGBase64 + `","mimeType":"image/png"}, - {"type":"image","data":"` + tinyPNGBase64 + `","mimeType":"image/png"} - ]}}`), - }, - } - c := NewClient(f, "test-server", WithMediaDir(dir)) - reg := tools.NewRegistry() - if _, err := c.RegisterTools(context.Background(), reg); err != nil { - t.Fatal(err) - } - entry, _ := reg.Get("mcp.test-server.multi_shot") - - out, err := entry.Handler(context.Background(), nil) - if err != nil { - t.Fatal(err) - } - result, ok := out.(tools.MultimodalResult) - if !ok { - t.Fatalf("out is %T, want tools.MultimodalResult", out) - } - if len(result.Files) != 2 { - t.Fatalf("Files = %v, want 2 distinct files", result.Files) - } - if result.Files[0] == result.Files[1] { - t.Error("both images wrote to the same file path") - } -} diff --git a/internal/tools/mcp/parallel_test.go b/internal/tools/mcp/parallel_test.go index fe1e9587f..8142963a4 100644 --- a/internal/tools/mcp/parallel_test.go +++ b/internal/tools/mcp/parallel_test.go @@ -70,32 +70,6 @@ func TestCallToolSerializesByDefault(t *testing.T) { wg.Wait() } -func TestCallToolParallelWhenOptedIn(t *testing.T) { - b := &blockingSender{release: make(chan struct{})} - c := NewClient(b, "test-server", WithParallelToolCalls(true)) - - const n = 5 - var wg sync.WaitGroup - for range n { - wg.Go(func() { - _, _ = c.CallTool(context.Background(), "slow", nil) - }) - } - - // All n calls should be able to reach Send concurrently since nothing - // serializes them. - deadline := time.Now().Add(2 * time.Second) - for b.MaxInFlight() < n && time.Now().Before(deadline) { - time.Sleep(5 * time.Millisecond) - } - if got := b.MaxInFlight(); got != n { - t.Errorf("MaxInFlight = %d, want %d (parallel opt-in)", got, n) - } - - close(b.release) - wg.Wait() -} - func TestCallToolSerializedStillCompletesAllCalls(t *testing.T) { b := &blockingSender{release: make(chan struct{})} close(b.release) // never actually blocks -- just verifying correctness, not timing