Files
magpie/internal/agent/claude_toolsearch_test.go
T
yetone 5416fdb76b agent: Claude Code on magpie keeps its MCP tools deferred (ENABLE_TOOL_SEARCH)
cybercaius on Discord (#help): the MCP router's 245 tools took 124K
tokens of a 341K context ("Magpie ×245" in Usage › Context). Claude Code
before 2.1.304 keeps tool search off behind an ANTHROPIC_BASE_URL that
isn't Anthropic's, so every router tool's definition went on every
request. 2.1.304 (10-19) turned it on for gateways itself, but a
conversation begun without it stays held off.

While Claude Code is routed through magpie, magpie now writes
env.ENABLE_TOOL_SEARCH="true" into ~/.claude/settings.json: Claude
Code's own documented switch, no prompt or identity change. The gateway
carries deferred tools to every upstream already (#2660,
withoutDeferredTools). It follows the auto-mode review's ownership:
magpie's value is stashed (claude.tool_search), a value the user set,
an empty one too, is theirs, and magpie's goes in desktopOnly/unroute.
T3 Code's magpie instance gets the same "true" in its environment
unless the user gave it a value there.

Not changed, filed as #4755: agents with no tool search of their own
(OpenCode, Gemini CLI, ...) still get the router's whole list; a lazy
router mode for them needs an owner decision. Not changed: Codex, which
already sends tool_search on magpie's catalog (0.165.0 measured: 14
tools, no MCP ones), and newmax/claudescience, which run Claude Code
through the SDK and set no gateway env of this kind.

Run:
- Real Claude Code 2.1.290 on mote, sandbox HOME, sandbox magpie serve
  on 127.0.0.1:3563 built from this tree, a fake Anthropic upstream, a
  245-tool stdio MCP server in the Library behind the router, wired
  with `magpie claude up/fake-1`: first /v1/messages had 12 tools,
  27,443 bytes of tools JSON (+~9KB of deferred names in the messages).
  The same with magpie's ENABLE_TOOL_SEARCH taken out (the old
  wiring): 266 tools, 245 of them MCP, 242,346 bytes.
- Without magpie, against a capture server: 2.1.200 to 2.1.303 send
  every MCP tool (239-257KB); 2.1.304 to 2.1.316 defer them (~28KB);
  ENABLE_TOOL_SEARCH=true in settings.json env or the process env makes
  2.1.200, 2.1.260 and 2.1.290 defer them too.
- TestClaudeToolSearchOn, TestT3CodeToolSearchOn (new); #4755 filed.
- go test -tags nogui ./internal/agent on mote; GOOS=linux and
  GOOS=windows go vet ./internal/agent; gofmt -l.
Broken by hand: writeSearch stashing nothing and writing nothing, and
T3's entry left out: both new tests red. The user-value check skipped:
TestClaudeToolSearchOn red on the user's "false".
Not tried: a real model loading a deferred router tool through
ToolSearch and calling it via magpie (gateway tests cover deferred
tools); Claude Code on Windows/Linux; T3 Code's real app.
2026-11-01 16:15:27 +08:00

153 lines
4.8 KiB
Go

package agent
import (
"encoding/json"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// TestClaudeToolSearchOn: Claude Code before 2.1.304 keeps tool search off
// behind an ANTHROPIC_BASE_URL other than Anthropic's, so every MCP tool's
// definition went on each request: magpie's MCP router's 245 tools were
// 124K tokens of a 341K context (cybercaius on Discord). While Claude Code
// is on magpie, magpie sets ENABLE_TOOL_SEARCH=true; it goes when Claude
// Code is taken off magpie, and a value of the user's, an empty one
// among them, is theirs through a switch, Sync and reset.
func TestClaudeToolSearchOn(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
if err := provider.Save(provider.Provider{ID: "v", Name: "V", Chat: "http://127.0.0.1:1/v1", Key: "k", Models: []string{"gpt-x"}}); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".claude", "settings.json")
writeFile(t, path, `{"theme":"dark"}`)
a := claude(home)
search := func() (string, bool) { return edit.GetJSON(path, "env."+claudeSearchEnv) }
set := func(key, v string) {
t.Helper()
if err := a.Field(key).Set(v); err != nil {
t.Fatal(err)
}
}
sync := func() {
t.Helper()
if err := a.Sync(); err != nil {
t.Fatal(err)
}
}
set("model", "v/gpt-x")
if v, _ := search(); v != "true" {
t.Fatalf("on magpie: %q, want true", v)
}
sync()
if v, _ := search(); v != "true" {
t.Fatalf("Sync: %q", v)
}
// off magpie, onto Claude Code's own model, and reset: magpie's goes
set("model", "claude-opus-4-8")
if v, has := search(); has {
t.Fatalf("Claude's own model kept magpie's value: %q", v)
}
set("model", "v/gpt-x")
if v, _ := search(); v != "true" {
t.Fatalf("back on magpie: %q", v)
}
set("model", "")
if v, has := search(); has {
t.Fatalf("reset kept magpie's value: %q", v)
}
// the user's own value is theirs
for _, mine := range []string{"false", "auto:5", ""} {
if err := edit.SetJSON(path, edit.KV{Path: "env." + claudeSearchEnv, Value: mine}); err != nil {
t.Fatal(err)
}
set("model", "v/gpt-x")
sync()
if v, has := search(); v != mine || !has {
t.Fatalf("the user's %q became %q (%v)", mine, v, has)
}
set("model", "")
if v, has := search(); v != mine || !has {
t.Fatalf("reset took the user's %q away: %q (%v)", mine, v, has)
}
if err := edit.DelJSON(path, "env."+claudeSearchEnv); err != nil {
t.Fatal(err)
}
}
// magpie's "true" taken out by the user, then a value of theirs: magpie
// doesn't write over it
set("model", "v/gpt-x")
if err := edit.SetJSON(path, edit.KV{Path: "env." + claudeSearchEnv, Value: "false"}); err != nil {
t.Fatal(err)
}
sync()
if v, _ := search(); v != "false" {
t.Fatalf("the user's false after magpie's true became %q", v)
}
if v, _ := edit.GetJSON(path, "theme"); v != "dark" {
t.Fatal("theme lost")
}
}
// TestT3CodeToolSearchOn: T3 Code's magpie instance runs Claude Code on the
// gateway through its own environment, which may be a Claude Code from
// before 2.1.304: it is given ENABLE_TOOL_SEARCH=true too, and a value the
// user gave the instance stays.
func TestT3CodeToolSearchOn(t *testing.T) {
home := syncHome(t)
t.Setenv("T3CODE_HOME", "")
path := filepath.Join(home, ".t3", "userdata", "settings.json")
writeFile(t, path, `{}`)
a := t3code(home)
if err := a.Field("provider").Set(magpieID); err != nil {
t.Fatal(err)
}
search := func() []string {
t.Helper()
var file map[string]map[string]json.RawMessage
if err := json.Unmarshal([]byte(readFile(path)), &file); err != nil {
t.Fatal(err)
}
var vs []string
for _, v := range t3Format(t, file["providerInstances"][magpieID]).Environment {
if v.Name == claudeSearchEnv {
vs = append(vs, v.Value)
}
}
return vs
}
if vs := search(); len(vs) != 1 || vs[0] != "true" {
t.Fatalf("magpie's instance: %q, want [true]", vs)
}
// the user's own value on the instance
var file map[string]any
if err := json.Unmarshal([]byte(readFile(path)), &file); err != nil {
t.Fatal(err)
}
inst := file["providerInstances"].(map[string]any)[magpieID].(map[string]any)
env := inst["environment"].([]any)
for i, v := range env {
if v.(map[string]any)["name"] == claudeSearchEnv {
env[i] = map[string]any{"name": claudeSearchEnv, "value": "false", "sensitive": false}
}
}
b, _ := json.Marshal(file)
writeFile(t, path, string(b))
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if vs := search(); len(vs) != 1 || vs[0] != "false" {
t.Fatalf("the user's false: %q", vs)
}
}