A plugin's OpenCode `tool` hook ({tool: {name: tool({description, args,
execute})}}) used to reach no agent. Now the plugin host collects each
loaded plugin's tools, turns their zod args (v3/v4 shape or object; a
raw JSON Schema is kept) into JSON Schema, and runs one on a `tool`
call: arguments checked against the tool's own zod first (defaults
filled, a wrong one named), execute raced against ToolTimeout (55s,
ctx.abort fires), the answer as text.
The gateway serves them as an MCP server at /mcp/magpie/tools
(tools_mcp.go, guarded by serveMCP as the search one, 4 MiB body):
tools/list names each <plugin short name>_<tool>, at most 64 chars,
a shared name numbered _2; tools/call holds the call to the calling
key's limit through reserveFor and runs it with the calling agent.
The built-in plugin `tools` ("Plugin tools", Essential) serves
gwtool.PluginTools; off, nothing is listed, a call says the plugin is
off and the host isn't asked. InUse counts plugins that are on and
gave tools, from plugin-tools.json, which the host writes after each
load (whole, only on change), so it never starts Bun; the gateway's
KeepFresh watches that note too. Tool answers go to the calling
agent only, so masking doesn't apply (said in the docs).
The Plugins page lists an on plugin's tools by their MCP names, with
descriptions on hover and a button copying the server's address; a
plugin of tools alone has no Provider chip and isn't said to sign in
to nothing. Not done: a Library featured opt-in entry; wiring is the
copied address.
Docs: docs/subsystems/plugin-tools.md (new), plugin-runtime.md's new
"P4: community plugins at every point" section with the tool row, the
README index, usemagpie.ai/docs/plugins#tools in en, zh, ja with an
example. Strings in zh, zh-TW, ja, de. Example plugin:
internal/plugin/testdata/tools.
Run at this commit's tree (base 4df1ba0c6), each package alone, -p 2:
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows
over internal/plugin, gwtool, gateway, plugins/..., gui, pluginrt: ok
- go test ./internal/plugin -run Tool; ./plugins/...; ./internal/pluginrt;
./internal/gui -run 'Builtin|Plugins'; ./internal/gateway -timeout 45m
-run 'ToolsMCP|ServingGates|OwnMCPRejects|SearchOnAndOff|KeyLimitsOnAndOff':
all ok
- GUI, Chromium and WebKit: every plugin-*.test.cjs, i18n-keys,
builtins: 461/461 pass; plugin-tools.test.cjs (new) covers en, zh,
zh-TW, ja, de at 980 and 420 px
Broken by hand, each red, then restored:
- tools/list ignoring the gwtool gate -> TestToolsMCPOff
- tools/call ignoring the gate -> TestToolsMCPOff
- reserveFor removed -> TestToolsMCPHeldToKeyLimit (tool ran)
- collision suffix dropped -> TestToolsMCPServesPluginTools
- ToolsOn ignoring Off -> TestPluginTools
- host arg check removed -> TestPluginTools
- host timeout race not listening -> TestPluginTools (answered at
5.3s by Go's backstop; the test now bounds it at 4s, and the first
try at this break was a syntax error that proved nothing)
- GUI listing an off plugin's tools -> plugin-tools.test.cjs
Real use: a built magpie in a sandbox HOME (MAGPIE_ADDR 127.0.0.1:3491,
real Bun), the example plugin added with `magpie plugin add`. curl over
MCP: initialize, tools/list (4 tools, schemas), shout "HI HI (for
curl)", a missing argument as isError naming it, add {"sum":6.5}, fail
"it broke", a foreign Origin 403; plugin-tools.json written. Claude
Code (sandbox CLAUDE_CONFIG_DIR) `claude mcp add --transport http` +
`mcp list`: Connected. Not tried: a model calling a tool through a
real agent turn (no model in the sandbox). Binary and HOME removed.
155 lines
6.2 KiB
Go
155 lines
6.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/gwtool"
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
)
|
|
|
|
// fakePluginTools has the gateway's plugin tools be pts and record the
|
|
// calls run, with no plugin host.
|
|
func fakePluginTools(t *testing.T, pts []plugin.PluginTools) *[]string {
|
|
t.Helper()
|
|
var ran []string
|
|
listed := 0
|
|
oldList, oldRun, oldOn := pluginTools, runPluginTool, anyPluginOn
|
|
pluginTools = func(context.Context) ([]plugin.PluginTools, error) { listed++; return pts, nil }
|
|
runPluginTool = func(_ context.Context, spec, name string, args json.RawMessage, agent string) (string, error) {
|
|
ran = append(ran, spec+" "+name+" "+string(args)+" "+agent)
|
|
return "ran " + name, nil
|
|
}
|
|
anyPluginOn = func() bool { return true }
|
|
t.Cleanup(func() { pluginTools, runPluginTool, anyPluginOn = oldList, oldRun, oldOn })
|
|
return &ran
|
|
}
|
|
|
|
func toolsPost(h http.Handler, body string, hdr ...string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(http.MethodPost, ToolsMCPPath, strings.NewReader(body))
|
|
r.RemoteAddr, r.Host = "127.0.0.1:50000", "127.0.0.1:3425"
|
|
r.Header.Set("Content-Type", "application/json")
|
|
for i := 0; i+1 < len(hdr); i += 2 {
|
|
r.Header.Set(hdr[i], hdr[i+1])
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
|
|
const toolsList = `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`
|
|
|
|
func toolsCall(name, args string) string {
|
|
return `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"` + name + `","arguments":` + args + `}}`
|
|
}
|
|
|
|
// The plugin tools MCP server lists the tools of the plugins that are on,
|
|
// each as <plugin>_<tool> with its JSON Schema, two of one name told
|
|
// apart, and runs one in the plugin that gives it with the agent's
|
|
// arguments, naming the agent. A name no plugin gives is an error, and a
|
|
// web page is turned away.
|
|
func TestToolsMCPServesPluginTools(t *testing.T) {
|
|
fresh(t)
|
|
ran := fakePluginTools(t, []plugin.PluginTools{
|
|
{Spec: "@acme/opencode-weather-tools@1.2.0", Tools: []plugin.Tool{
|
|
{Name: "now", Description: "The weather now.", Schema: json.RawMessage(`{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}`)},
|
|
}},
|
|
{Spec: "/Users/me/weather-tools", Tools: []plugin.Tool{{Name: "now", Description: "Mine."}}},
|
|
})
|
|
h := lanGuard(New().Handler())
|
|
if m := readMCP(t, toolsPost(h, `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26"}}`)); m.Result.ServerInfo.Name != "magpie-tools" || m.Result.ProtocolVersion != "2025-03-26" {
|
|
t.Fatalf("initialize = %+v", m)
|
|
}
|
|
m := readMCP(t, toolsPost(h, toolsList))
|
|
var names []string
|
|
for _, x := range m.Result.Tools {
|
|
names = append(names, x.Name)
|
|
}
|
|
if strings.Join(names, " ") != "weather-tools_now weather-tools_now_2" {
|
|
t.Fatalf("tools = %v", names)
|
|
}
|
|
if r := m.Result.Tools[0].InputSchema.Required; len(r) != 1 || r[0] != "city" {
|
|
t.Errorf("schema required = %v", r)
|
|
}
|
|
m = readMCP(t, toolsPost(h, toolsCall("weather-tools_now_2", `{"city":"Oslo"}`), "User-Agent", "claude-cli/2.1.0"))
|
|
if m.Result.IsError || len(m.Result.Content) != 1 || m.Result.Content[0].Text != "ran now" {
|
|
t.Fatalf("call = %+v", m)
|
|
}
|
|
if len(*ran) != 1 || !strings.HasPrefix((*ran)[0], `/Users/me/weather-tools now {"city":"Oslo"} `) {
|
|
t.Errorf("ran %q", *ran)
|
|
}
|
|
if m := readMCP(t, toolsPost(h, toolsCall("nope_now", `{}`))); m.Error == nil || m.Error.Code != -32602 {
|
|
t.Errorf("an unknown tool: %+v", m)
|
|
}
|
|
if w := toolsPost(h, toolsList, "Origin", "https://evil.example"); w.Code != http.StatusForbidden {
|
|
t.Errorf("a web page: %d", w.Code)
|
|
}
|
|
if len(*ran) != 1 {
|
|
t.Errorf("ran %q", *ran)
|
|
}
|
|
}
|
|
|
|
// Off (plugins/tools not serving the tool point in a gateway that gates
|
|
// it), the server lists no tool and runs none, and says the plugin is off;
|
|
// the plugin host isn't asked. With no plugin on at all, it isn't started
|
|
// to list nothing.
|
|
func TestToolsMCPOff(t *testing.T) {
|
|
fresh(t)
|
|
ran := fakePluginTools(t, []plugin.PluginTools{{Spec: "opencode-x", Tools: []plugin.Tool{{Name: "y"}}}})
|
|
asked := 0
|
|
pluginTools = func(context.Context) ([]plugin.PluginTools, error) {
|
|
asked++
|
|
return []plugin.PluginTools{{Spec: "opencode-x", Tools: []plugin.Tool{{Name: "y"}}}}, nil
|
|
}
|
|
h := lanGuard(New().Handler())
|
|
stop := gwtool.Gate()
|
|
defer stop()
|
|
if m := readMCP(t, toolsPost(h, toolsList)); len(m.Result.Tools) != 0 || m.Error != nil {
|
|
t.Errorf("off, listed %+v", m)
|
|
}
|
|
m := readMCP(t, toolsPost(h, toolsCall("x_y", `{}`)))
|
|
if !m.Result.IsError || len(m.Result.Content) == 0 || !strings.Contains(m.Result.Content[0].Text, "Plugin tools plugin is off") {
|
|
t.Errorf("off, a call said %+v", m)
|
|
}
|
|
if len(*ran) != 0 || asked != 0 {
|
|
t.Errorf("off: ran %q, the host asked %d times", *ran, asked)
|
|
}
|
|
on := gwtool.Serve(gwtool.PluginTools)
|
|
if m := readMCP(t, toolsPost(h, toolsList)); len(m.Result.Tools) != 1 {
|
|
t.Errorf("on again, listed %+v", m)
|
|
}
|
|
anyPluginOn = func() bool { return false }
|
|
asked = 0
|
|
if m := readMCP(t, toolsPost(h, toolsList)); len(m.Result.Tools) != 0 || asked != 0 {
|
|
t.Errorf("no plugin on: listed %+v, the host asked %d times", m, asked)
|
|
}
|
|
on()
|
|
}
|
|
|
|
// A gateway key's limit holds each tool call: a key held to a limit
|
|
// nothing serves (the Key limits plugin off) is refused, and the tool
|
|
// isn't run.
|
|
func TestToolsMCPHeldToKeyLimit(t *testing.T) {
|
|
fresh(t)
|
|
ran := fakePluginTools(t, []plugin.PluginTools{{Spec: "opencode-x", Tools: []plugin.Tool{{Name: "y"}}}})
|
|
withoutLimits(t)
|
|
keys, _ := newCaller(t, "Capped")
|
|
setLimit(t, keys[0].ID, &access.Limit{Period: "day", Tokens: 700})
|
|
capped := access.Identity{KeyID: keys[0].ID, KeyName: "Capped", Limit: &access.Limit{Period: "day", Tokens: 700}}
|
|
s := New()
|
|
r := httptest.NewRequest("POST", ToolsMCPPath, nil).WithContext(access.WithIdentity(t.Context(), capped))
|
|
res, rpcErr := s.toolsMCPCall(r, mcpMessage{ID: json.RawMessage("1"), Method: "tools/call", Params: json.RawMessage(`{"name":"x_y","arguments":{}}`)})
|
|
b, _ := json.Marshal(res)
|
|
if rpcErr != nil || !strings.Contains(string(b), `"isError":true`) || !strings.Contains(string(b), "Key limits plugin") {
|
|
t.Errorf("a limited key with limits off: %s %v", b, rpcErr)
|
|
}
|
|
if len(*ran) != 0 {
|
|
t.Errorf("ran %q", *ran)
|
|
}
|
|
}
|