ferock (Discord) asked to see calls to magpie's MCP tools, such as magpie-image's generate_image, generate_video and generation_result, in Routing > Requests. Each tools/call to magpie's own MCP servers (image, vision, web search, tools and the Library hub) is now a row of the request log. The row has Source and Kind "mcp", the tool as its model, the server as its host, how long the call took, and its error if it failed. The provider and model it shows are those of the model requests the call made, collected through the request's context in appendUsage. generation_result names the model of the job it answers. The stdio Magpie Image server reports each call it answered to the gateway as "magpie/toolCalled". That report carries the tool, ms, model and error, never the prompt. The arguments are never stored at all, so there is nothing to mask. The rows go down the probe lane: they are listed, but no total, host list, budget or key limit counts them. The model requests a call made are rows of their own. In the GUI the row reuses the Requests list. It gets a muted "MCP" badge with a tooltip, the server beside the provider, and a Succeeded/Failed status. The Purpose filter gains "MCP tool" in every language. There is no new page, no <select> and no left border. The core scripts went over their budget with the new purpose. genpurposes now writes purposes.js as compact JSON, which makes the core 448 bytes smaller than main in all, and coreScriptBudget is lowered to the new total, 1,538,975. No room is added. Run on mote, at the rebased head: - gofmt -l: clean. go vet -tags nogui ./... with GOOS darwin, linux and windows: ok. - go test -tags nogui -timeout 45m: ./internal/usage, ./internal/imagemcp, ./internal/usageapi and ./internal/gui ok. internal/budget has no test files. ./internal/gateway (805s): every test ok except five Antigravity and Codex/Grok tests. They fail the same on a clean origin/main3bebc3839and are filed as #4362. - GUI, the usage-ledger*, routing-request*, purpose*, i18n* and gui-zh/ja/de files (25). On mote, Chromium: 32 of 32 ok; WebKit could not launch (10 other sessions' suites on mote). On Linux (wsl-run), both engines: 335 of 355 ok. The 20 reds are routing-request-model, usage-ledger, -ttft, -chart and -open-settled, which fail the same at the parent1534474c8(#4120). usage-ledger-mcp, purpose-filter and i18n-keys are ok in both. The whole GUI suite runs after the push, and each red it finds gets an issue. - At the pushed head, rebased on0804f2d97: ./internal/gui (the budget and purpose asset tests among them), ./internal/usage, ./internal/imagemcp and ./internal/usageapi ok, and ./internal/gateway -run "MCP|Purpose|Probe" ok. - usage-ledger-mcp.test.cjs covers 5 languages, widths 1180 and 420, light and dark, with no sideways scroll. Broken by hand. Each break went red: - sawHost's MCP skip removed: TestMCPCallRowsListedNotCounted (the hosts list had magpie-image). - Row/Record.IsProbe's MCP case removed: the same test (3 calls counted), and TestMCPCallCountsInNoKeyLimit (Calls: 1). - The callMCPTool hook removed: TestMCPToolCallIsARequestRow and its siblings (no rows). - The appendUsage hook removed: the row had no provider or model. - imagemcp's report removed: TestToolCallsAreReported. - The gateway's magpie/toolCalled case removed: TestMCPCalledFromStdioIsARow. budget's in-memory counts() now skips these rows too. Breaking only that stays green, because budget.Of reads the file through usage.Visit, which already skips probes, and limits count tokens and cost. Not tried: - no real agent calling a real vendor's drawing; - no test run on Windows or Linux; - the page not checked in the real app or in WebKitGTK. A generate call that answers while its drawing still runs has no model on its row. The generation_result row that follows names it.
89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package imagemcp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
// reportGW is gw that keeps the tool calls the server reports.
|
|
type reportGW struct {
|
|
gw
|
|
mu sync.Mutex
|
|
calls []string
|
|
}
|
|
|
|
func (g *reportGW) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == gateway.ImageMCPPath {
|
|
b, _ := io.ReadAll(r.Body)
|
|
var m struct {
|
|
Method string `json:"method"`
|
|
Params json.RawMessage `json:"params"`
|
|
}
|
|
json.Unmarshal(b, &m)
|
|
if m.Method == gateway.MCPCalledMethod {
|
|
g.mu.Lock()
|
|
g.calls = append(g.calls, string(m.Params))
|
|
g.mu.Unlock()
|
|
io.WriteString(w, `{"jsonrpc":"2.0","id":1,"result":{}}`)
|
|
return
|
|
}
|
|
modelsMCP(w, b)
|
|
return
|
|
}
|
|
g.gw.ServeHTTP(w, r)
|
|
}
|
|
|
|
func (g *reportGW) reported(n int) []string {
|
|
for end := time.Now().Add(10 * time.Second); time.Now().Before(end); time.Sleep(5 * time.Millisecond) {
|
|
g.mu.Lock()
|
|
got := append([]string(nil), g.calls...)
|
|
g.mu.Unlock()
|
|
if len(got) >= n {
|
|
return got
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Each tool call the Magpie Image server answers is reported to the
|
|
// gateway (MCPCalledMethod), for its row in the request log: the tool,
|
|
// how long it took, the model the gateway named, and never the prompt.
|
|
func TestToolCallsAreReported(t *testing.T) {
|
|
g := &reportGW{}
|
|
project := t.TempDir()
|
|
c := start(t, g, project)
|
|
c.call("initialize", map[string]any{"protocolVersion": "2025-06-18", "capabilities": map[string]any{"roots": map[string]any{}}, "clientInfo": map[string]any{"name": "claude-code", "version": "2.1.0"}})
|
|
r := c.call("tools/call", map[string]any{"name": "generate_image", "arguments": map[string]any{"prompt": "secret-plum-branch"}})
|
|
if r["isError"] == true {
|
|
t.Fatalf("%v", r)
|
|
}
|
|
c.call("tools/call", map[string]any{"name": "generation_result", "arguments": map[string]any{"id": "img_nothing"}})
|
|
got := g.reported(2)
|
|
if len(got) != 2 {
|
|
t.Fatalf("reported %v", got)
|
|
}
|
|
by := map[string]gateway.MCPCalled{}
|
|
for _, x := range got {
|
|
var c gateway.MCPCalled
|
|
json.Unmarshal([]byte(x), &c)
|
|
by[c.Tool] = c
|
|
}
|
|
first, second := by["generate_image"], by["generation_result"]
|
|
if first.Tool != "generate_image" || first.Model != "art/gpt-image-1" || first.Error != "" || first.Millis < 0 {
|
|
t.Fatalf("reported %+v", first)
|
|
}
|
|
if second.Tool != "generation_result" || second.Error == "" {
|
|
t.Fatalf("reported %+v", second)
|
|
}
|
|
if strings.Contains(strings.Join(got, ""), "secret-plum") {
|
|
t.Fatalf("the prompt was reported: %v", got)
|
|
}
|
|
}
|