merge: lane C (tools-core-7, tools-core-6)

This commit is contained in:
sam committed 2026-09-15 13:01:26 +10:00
commit d0395cebd5
6 files changed
+284 -748

No files matched your search

+10 -251
View File
@@ -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/<toolName>/.
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.<serverName>.<toolName>", 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/<sanitized toolName>/<call
// sequence>/, 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.
+1 -86
View File
@@ -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)
-374
View File
@@ -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")
}
}
-26
View File
@@ -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
+200 -11
View File
@@ -3,11 +3,16 @@ package mcp
import (
"context"
"encoding/base64"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/samcharles93/archie-core/internal/plugin"
@@ -33,6 +38,14 @@ type Provider struct {
segment string
transport LifecycleTransport
client *protocol.Client
// mediaDir is the root directory binary content blocks (images, audio,
// resource blobs) are written under, created lazily on the first
// multimodal tool result. It is removed by Stop.
mediaDir string
// mediaCallSeq disambiguates concurrent calls to the same tool so
// their written media files never share a directory.
mediaCallSeq atomic.Int64
}
// New creates an MCP tool provider.
@@ -163,11 +176,19 @@ func (p *Provider) Health(context.Context) plugin.Health {
}
}
// Stop stops the MCP transport.
// Stop stops the MCP transport and removes any media files written during
// this provider's lifetime.
func (p *Provider) Stop(ctx context.Context) error {
p.mu.Lock()
p.client = nil
mediaDir := p.mediaDir
p.mediaDir = ""
p.mu.Unlock()
if mediaDir != "" {
if err := os.RemoveAll(mediaDir); err != nil {
return fmt.Errorf("remove MCP media dir %q: %w", mediaDir, err)
}
}
if isNilTransport(p.transport) {
return nil
}
@@ -184,16 +205,23 @@ func (p *Provider) handlerFor(client *protocol.Client, originalName string) tool
return nil, err
}
var text strings.Builder
multimodal := false
var media []protocol.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; keep the text in the summary and still treat
// the blob as media.
if block.Resource.Text != "" {
text.WriteString(block.Resource.Text)
}
media = append(media, block)
case block.Type == "resource" && block.Resource != nil:
text.WriteString(block.Resource.Text)
multimodal = multimodal || block.Resource.Blob != ""
case block.Data != "":
media = append(media, block)
case block.Type == "" || block.Type == "text":
text.WriteString(block.Text)
case block.Data != "":
multimodal = true
default:
fmt.Fprintf(&text, "[unhandled content block type %q]", block.Type)
}
@@ -201,13 +229,174 @@ func (p *Provider) handlerFor(client *protocol.Client, originalName string) tool
if result.IsError {
return nil, fmt.Errorf("MCP tool %q reported an error: %s", originalName, text.String())
}
if multimodal {
return tools.MultimodalResult{
IsMultimodal: true,
Summary: text.String(),
}, nil
if len(media) == 0 {
return text.String(), nil
}
return text.String(), nil
return p.writeMultimodalResult(originalName, text.String(), 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. ~48MiB decoded (base64 is ~4/3 the size of the
// decoded payload).
const maxMediaBlockBase64Bytes = 64 << 20 // 64MiB
// writeMultimodalResult decodes and writes each media block to disk under
// p.mediaDir/<tool name>/<call sequence>/, returning an envelope whose URLs
// carry the local files for the channel to upload. Malformed base64 data
// is a hard error -- the server sent a content block it claimed was media
// but wasn't decodable.
func (p *Provider) writeMultimodalResult(toolName, summaryText string, media []protocol.ContentBlock) (tools.MultimodalResult, error) {
result := tools.MultimodalResult{IsMultimodal: true, Summary: summaryText}
root, err := p.mediaRoot()
if err != nil {
return tools.MultimodalResult{}, err
}
// 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 the media root. 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 never write to the same index-based filename and clobber or
// interleave each other's output.
callSeq := p.mediaCallSeq.Add(1)
subdir := filepath.Join(root, 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))
refs := make([]tools.MediaRef, 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)
}
ext := extensionForMimeType(mimeType)
name := fmt.Sprintf("%d%s", i, ext)
path := filepath.Join(subdir, name)
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)
refs = append(refs, tools.MediaRef{
Type: mediaTypeForMimeType(mimeType),
Path: path,
FileName: name,
})
}
result.SubdirHint = subdir
result.Files = files
result.URLs = refs
return result, nil
}
// mediaRoot returns the provider's media directory, creating it under the
// system temp directory on first use. Providers are long-lived relative to
// a turn (the daemon keeps them for its whole lifetime; a task worker keeps
// them for the task), so a lazily created root keeps text-only servers from
// creating directories they never need.
func (p *Provider) mediaRoot() (string, error) {
p.mu.Lock()
defer p.mu.Unlock()
if p.mediaDir != "" {
return p.mediaDir, nil
}
dir, err := os.MkdirTemp("", "archie-mcp-media-")
if err != nil {
return "", fmt.Errorf("MCP provider %q: create media root: %w", p.name, err)
}
p.mediaDir = dir
return dir, 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 ""
}
}
// mediaTypeForMimeType maps an MCP media MIME type onto the four-value
// vocabulary gateway.MediaAttachment.Type uses. Only the photo formats the
// Telegram photo endpoint accepts are classified "image": image/svg+xml and
// image/tiff are images that a photo endpoint rejects outright, so they fall
// through to "document" and still arrive as a file, mirroring
// sendfile.MediaType's photo-format list.
func mediaTypeForMimeType(mimeType string) string {
switch mimeType {
case "image/png", "image/jpeg", "image/gif", "image/webp":
return "image"
}
switch {
case strings.HasPrefix(mimeType, "video/"):
return "video"
case strings.HasPrefix(mimeType, "audio/"):
return "audio"
default:
return "document"
}
}
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"os"
"strings"
"sync"
"testing"
@@ -126,6 +127,78 @@ func TestProviderPreservesResourceTextAndMultimodalResults(t *testing.T) {
}
}
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)
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