image mcp: generate_image and generate_video take the model to use (XYenon)

XYenon on Discord asked whether an agent can pick the model magpie's
image/video MCP draws with. Both tools now take an optional `model`,
as provider/model:

- The schema lists the models that may be asked (the Settings default
  first, then each on provider's OfferedDrawers or Videomakers), only
  those the calling gateway key may use. Up to 40 of them form an enum;
  past that, the description names three.
- Left out, the Settings default is used, as before.
- A bare id only one provider has, a ChatGPT account's other GPT Image
  name (ListedDrawer), or a newer family model (drawsFamily,
  grokVideoFamily) is accepted. Anything else is a tool error naming the
  models the key may use, and nothing is drawn. It never falls back.
- The chosen model goes into the images/videos request's `model`, so
  the APIs' own key check (keyHolds, keyModelError, #882) refuses one
  the key may not use: the same check /v1/images makes.
- The stdio Magpie Image server copies the gateway's `model` property
  from its tools/list, and asks the hidden JSON-RPC method
  magpie/media_model which model a call means. An older gateway
  (-32601) passes the model through as named.
- `magpie tool generate_image --model ...` works through the schema;
  the usage text says so.

Docs: gateway-routing.md (image MCP section), reference.md.

Run (mote, via mote-run, at this tree):
- gofmt -l: clean.
- go vet ./... for darwin, GOOS=linux -tags nogui, GOOS=windows: ok.
- go test -count=1 -timeout 45m ./internal/gateway/ ./internal/imagemcp/
  ./internal/cli/: ok.
Broken by hand: with `req["model"] = ref` dropped in imageMCPDraw and
imageMCPFilm, TestImageMCPDrawsWithTheModelAsked,
TestImageMCPModelHeldToTheKey and TestImageMCPVideoModelAsked go red
(the default was drawn instead); restored, they pass.
Not tried: a real vendor account. The fakes are easeled's art provider
and the fake Grok. GUI files not run: no GUI file changed.
This commit is contained in:
yetone
2026-10-29 22:16:56 +08:00
parent 6aac51ac11
commit fd5c631cbe
10 changed files with 636 additions and 15 deletions
+4
View File
@@ -562,6 +562,10 @@ as an HTTP MCP server; from another computer send a gateway key as
`Authorization: Bearer <key>`. An image comes back in the tool's answer and
at a URL on the gateway kept for an hour (the same key reads it), a video at
`/v1/videos/<id>/content`. With Image generation Off the tool says so.
`generate_image` and `generate_video` take an optional `model`
(provider/model, one of those the tool's schema lists, for `magpie tool`
`--model`). Left out, the one in Settings is used. One that isn't listed, or
that a gateway key may not use, is refused with the models that may be used.
WorkBuddy (China)'s daily check-in (签到) is pressed from the app's Usage card
(*Check in now*), `magpie accounts checkin`, or `c` on the TUI's Usage page,
+24
View File
@@ -1004,6 +1004,30 @@ model, checked before the arguments. Nothing is written into the agent's
project. Settings › Models shows the address (`#imageMcpRow`, below
`#visionMcpRow`).
`generate_image` and `generate_video` take an optional `model` (XYenon on
Discord), in [`image_mcp_models.go`](../../internal/gateway/image_mcp_models.go).
`mediaModels` lists what may be asked, as provider/model: Settings' default
first (`Drawer`, `videomaker`), then each provider's `OfferedDrawers` (a
model the user unticked is left out) or `Videomakers`, from providers that
are on and not decide-only. `mediaModelsFor` keeps only those the calling
key may use (`refAllowed`: `allows`, or `groupAllowed` for a group ref). The
schema's `model` is an `enum` of them, or, past 40 (`mediaEnumMost`), a
description naming three. `pickMediaModel` takes a listed ref (a ChatGPT
account's other GPT Image name through `ListedDrawer`), a bare id only one
provider has, or a newer model of a family a provider serves (`drawsFamily`,
`grokVideoFamily`). Anything else is a tool error that names the models
the key may use, and nothing is drawn. Left out, Settings' default is used,
as before. The model chosen is put in the request's `model`, so the images
and videos APIs' own key check (`keyHolds`, `keyModelError`) refuses one
the key may not use, the same check `/v1/images` makes. The stdio Magpie
Image server can't read providers (WSL, a gateway elsewhere), so it copies
the gateway's `model` property from this server's `tools/list` and asks the
JSON-RPC method `magpie/media_model` (`MediaModelMethod`; not a tool) which
model one asked for stands for. An older gateway's -32601 passes the model
through as named. Covered by `TestImageMCPDrawsWithTheModelAsked`,
`TestImageMCPModelHeldToTheKey`, `TestImageMCPVideoModelAsked` and
imagemcp's `TestGenerateTakesTheModel`.
Each answer also carries `_meta` for a client that acts on it rather than
reads it: `usemagpie.ai/pending` is the id to wait on (`PendingMeta`, beside
the text that says to call `generation_result`), and `usemagpie.ai/files`
+2 -1
View File
@@ -44,7 +44,8 @@ const toolUsage = `usage:
the tool's answer as it came and saves nothing.
An unknown parameter, or a missing required one, is refused before anything is
called. The models are the ones magpie's Settings pick (Images › Image generation,
Image recognition, Web search). It asks the gateway at the port in Settings' Gateway
Image recognition, Web search); generate_image and generate_video take --model
provider/model for another one set up (magpie tools generate_image lists them). It asks the gateway at the port in Settings' Gateway
address (or MAGPIE_ADDR; MAGPIE_GATEWAY=<url> for another).
e.g. magpie tool generate_image "a magpie on a branch, watercolour" --size 1536x1024 -o hero.png
+12 -3
View File
@@ -91,6 +91,7 @@ func (f *fakeTools) ServeHTTP(w http.ResponseWriter, r *http.Request) {
"n": map[string]any{"type": "integer", "minimum": 1, "maximum": 4},
"quality": map[string]any{"type": "string", "enum": []string{"low", "medium", "high", "auto"}},
"reference_images": map[string]any{"type": "array", "items": map[string]any{"type": "string"}, "description": "Images to edit: file paths or URLs"},
"model": map[string]any{"type": "string", "enum": []string{"art/gpt-image-1", "art/painter"}, "description": "The image model to use, as provider/model."},
}}},
map[string]any{"name": gateway.GenerationResultTool, "description": "Waits.", "inputSchema": map[string]any{
"type": "object", "required": []string{"id"}, "properties": map[string]any{"id": map[string]any{"type": "string"}}}},
@@ -197,7 +198,8 @@ func TestToolReadsParameters(t *testing.T) {
func TestToolRefusesWhatItDoesntKnowAndCallsNothing(t *testing.T) {
f, dir := toolHere(t)
for _, args := range [][]string{
{"tool", "generate_image", "--model", "gpt-image-1", "a magpie"},
{"tool", "generate_image", "--style", "vivid", "a magpie"},
{"tool", "generate_image", "--model", "art/nope", "a magpie"},
{"tool", "generate_image", "--n", "2"},
{"tool", "generate_image", "--n", "two", "a magpie"},
{"tool", "generate_image", "--quality", "ultra", "a magpie"},
@@ -219,8 +221,8 @@ func TestToolRefusesWhatItDoesntKnowAndCallsNothing(t *testing.T) {
if got := filesIn(t, dir); len(got) != 0 {
t.Fatalf("a refused call wrote %v", got)
}
_, err := runOut(t, "tool", "generate_image", "--model", "x", "a magpie")
if err == nil || !strings.Contains(err.Error(), "takes no --model") || !strings.Contains(err.Error(), "--prompt") || !strings.Contains(err.Error(), "nothing was called") {
_, err := runOut(t, "tool", "generate_image", "--style", "x", "a magpie")
if err == nil || !strings.Contains(err.Error(), "takes no --style") || !strings.Contains(err.Error(), "--prompt") || !strings.Contains(err.Error(), "nothing was called") {
t.Fatalf("an unknown parameter: %v", err)
}
}
@@ -287,6 +289,13 @@ func TestToolSavesTheImage(t *testing.T) {
if err != nil || !strings.Contains(out, gateway.FilesMeta) || len(filesIn(t, dir)) != 3 {
t.Fatalf("--json: %v %q", err, out)
}
// --model goes to the tool as its model argument
if _, err := runOut(t, "tool", "generate_image", "--model", "art/painter", "--json", "a magpie"); err != nil {
t.Fatal(err)
}
if c := f.toolCalls(); !strings.Contains(c[len(c)-1], `"model":"art/painter"`) {
t.Fatalf("--model: %v", c)
}
}
// One still being made is waited on with generation_result, and one that
+43 -7
View File
@@ -125,27 +125,31 @@ const (
const imageMCPFrom = "an http(s) URL or a data URL (data:image/png;base64,...); an absolute file path only for an agent on the computer magpie runs on"
func imageMCPTools() []any {
ro := func(props map[string]any) map[string]any {
// imageMCPTools are the server's tools as r's caller is given them: the
// models each generate tool may be asked for are those its key may use.
func imageMCPTools(r *http.Request) []any {
ro := func(props map[string]any, video bool) map[string]any {
_, shown, def := mediaModelsFor(r, video)
props["model"] = mediaModelProp(video, shown, def)
return map[string]any{"type": "object", "properties": props, "required": []string{"prompt"}}
}
return []any{
map[string]any{
"name": GenerateImageTool,
"title": "Generate image",
"description": "Generate an image from a text prompt, or edit/combine images given as reference_images, with the image model set in Magpie (Settings → Images → Image generation). " +
"description": "Generate an image from a text prompt, or edit/combine images given as reference_images, with the image model set in Magpie (Settings → Images → Image generation) or the one model names. " +
"The image comes back in the answer and at a URL Magpie keeps it at for an hour (from another computer, download it with the same Authorization header this server is called with); nothing is saved in your project, so save it there yourself if you need it. " +
"When it is not ready within about 20 seconds, the answer is an id to wait on with " + GenerationResultTool + " instead. Write a detailed prompt: subject, style, composition, colours, any text to render exactly.",
"inputSchema": ro(ImageToolProps(imageMCPFrom + "s")),
"inputSchema": ro(ImageToolProps(imageMCPFrom+"s"), false),
"annotations": map[string]any{"readOnlyHint": false, "destructiveHint": false, "openWorldHint": true},
},
map[string]any{
"name": GenerateVideoTool,
"title": "Generate video",
"description": "Generate a short video from a text prompt, optionally starting from an image to animate, with a video model set up in Magpie. It takes from ten seconds to a few minutes; " +
"description": "Generate a short video from a text prompt, optionally starting from an image to animate, with a video model set up in Magpie (the one model names, else the default). It takes from ten seconds to a few minutes; " +
"the answer is the URL to download the mp4 from (with the same Authorization header this server is called with), or, when it is not ready within about 20 seconds, an id to wait on with " + GenerationResultTool + ". " +
"Nothing is saved in your project. Describe the subject, the motion and the camera: what moves, how, and what the shot is.",
"inputSchema": ro(VideoToolProps(imageMCPFrom, imageMCPFrom+"s")),
"inputSchema": ro(VideoToolProps(imageMCPFrom, imageMCPFrom+"s"), true),
"annotations": map[string]any{"readOnlyHint": false, "destructiveHint": false, "openWorldHint": true},
},
map[string]any{
@@ -202,7 +206,21 @@ func (s *Server) imageMCPCall(r *http.Request, m mcpMessage) (any, map[string]an
case "ping":
return map[string]any{}, nil
case "tools/list":
return map[string]any{"tools": imageMCPTools()}, nil
return map[string]any{"tools": imageMCPTools(r)}, nil
case MediaModelMethod:
var p struct {
Tool string `json:"tool"`
Model string `json:"model"`
}
json.Unmarshal(m.Params, &p)
if p.Tool != GenerateImageTool && p.Tool != GenerateVideoTool {
return nil, map[string]any{"code": -32602, "message": "no tool " + p.Tool + " takes a model"}
}
ref, err := mediaModelFor(r, p.Tool, p.Model)
if err != nil {
return nil, map[string]any{"code": -32602, "message": err.Error()}
}
return map[string]any{"model": ref}, nil
case "tools/call":
var p struct {
Name string `json:"name"`
@@ -245,6 +263,7 @@ func (s *Server) imageMCPDraw(r *http.Request, raw json.RawMessage) map[string]a
}
var a struct {
Prompt string `json:"prompt"`
Model string `json:"model"`
Size string `json:"size"`
N int `json:"n"`
References []string `json:"reference_images"`
@@ -258,6 +277,15 @@ func (s *Server) imageMCPDraw(r *http.Request, raw json.RawMessage) map[string]a
return toolResult(GenerateImageTool+" needs a prompt", true)
}
req := map[string]any{"prompt": a.Prompt, "n": max(1, min(a.N, maxDrawings)), "response_format": "b64_json"}
// a model named goes to the images API named, so a gateway key's
// models hold it there (#882)
if strings.TrimSpace(a.Model) != "" {
ref, err := mediaModelFor(r, GenerateImageTool, a.Model)
if err != nil {
return toolResult(err.Error(), true)
}
req["model"] = ref
}
for k, v := range map[string]string{"size": a.Size, "quality": a.Quality, "background": a.Background} {
if v != "" {
req[k] = v
@@ -400,6 +428,7 @@ func (s *Server) imageMCPFilm(r *http.Request, raw json.RawMessage) map[string]a
}
var a struct {
Prompt string `json:"prompt"`
Model string `json:"model"`
Seconds any `json:"seconds"`
Size string `json:"size"`
Image string `json:"image"`
@@ -412,6 +441,13 @@ func (s *Server) imageMCPFilm(r *http.Request, raw json.RawMessage) map[string]a
return toolResult(GenerateVideoTool+" needs a prompt", true)
}
req := map[string]any{"prompt": a.Prompt}
if strings.TrimSpace(a.Model) != "" {
ref, err := mediaModelFor(r, GenerateVideoTool, a.Model)
if err != nil {
return toolResult(err.Error(), true)
}
req["model"] = ref
}
switch v := a.Seconds.(type) {
case nil:
case float64:
+180
View File
@@ -0,0 +1,180 @@
package gateway
import (
"fmt"
"net/http"
"slices"
"strconv"
"strings"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
)
// generate_image and generate_video take the model to use (XYenon on
// Discord): one of those set up for it, as provider/model, else the one
// Settings › Images picks. The tool's schema lists those the caller may use
// (mediaModelsFor), and a model none of them is, is a tool error naming
// them, never a drawing with another. The model chosen is named to the
// images and videos APIs, so a gateway key's models hold it there as they
// hold one named at /v1/images (keyHolds, #882): that check is the API's,
// on every route to it.
// MediaModelMethod is the image MCP server's JSON-RPC method, not a tool,
// that the stdio Magpie Image server asks which model a call's model
// argument stands for: {"tool": …, "model": …} → {"model": provider/model},
// or an error naming those it may be.
const MediaModelMethod = "magpie/media_model"
// mediaEnumMost is the most models a tool's schema lists as its model's
// enum; more are described instead, and named in full by the error an
// unknown one gets.
const mediaEnumMost = 40
// mediaModels are the models generate_image (video false) or
// generate_video (true) may be asked for, as provider/model, the default
// first, and the default itself: "" when image generation is off or
// nothing makes them. Image models are each provider's OfferedDrawers (one
// the user unticked is left out, as Automatic leaves it), videos its
// Videomakers.
func mediaModels(video bool) (all []string, def string) {
if video {
def, _ = videomaker()
} else {
def = Drawer()
}
if def == "" {
return nil, ""
}
all = []string{def}
for _, p := range provider.All() {
if !p.On() || p.DecideOnly() {
continue
}
ms := Videomakers(p)
if !video {
ms = OfferedDrawers(p)
}
for _, m := range ms {
if ref := p.ID + "/" + m.ID; !slices.Contains(all, ref) {
all = append(all, ref)
}
}
}
return all, def
}
// mediaModelsFor is mediaModels less those r's gateway key may not use.
func mediaModelsFor(r *http.Request, video bool) (all, shown []string, def string) {
all, def = mediaModels(video)
who, held := keyHolds(r)
if !held {
return all, all, def
}
for _, ref := range all {
if refAllowed(who, ref) {
shown = append(shown, ref)
}
}
return all, shown, def
}
// refAllowed says the key may use ref, a model or a routing group.
func refAllowed(who access.Identity, ref string) bool {
if provider.IsGroupRef(ref) {
g, ms, ok := provider.FindGroup(ref)
return ok && groupAllowed(who, g, ms)
}
return allows(who, ref)
}
// mediaModelProp is the model argument's schema: the models shown, as an
// enum while they are few.
func mediaModelProp(video bool, shown []string, def string) map[string]any {
what := "image"
if video {
what = "video"
}
d := "The " + what + " model to use, as provider/model."
switch {
case def == "":
d += " Leave it out: none is set up in Magpie."
case video:
d += " Leave it out to use the video model set up in Magpie (now " + def + ")."
default:
d += " Leave it out to use the one set in Magpie (Settings → Images → Image generation; now " + def + ")."
}
prop := map[string]any{"type": "string"}
switch n := len(shown); {
case n > mediaEnumMost:
d += fmt.Sprintf(" One of the %d %s models set up in Magpie, such as %s; an unknown one is refused with the full list.", n, what, strings.Join(shown[:3], ", "))
case n > 0:
prop["enum"] = slices.Clone(shown)
case def != "":
d += " No other can be picked here."
}
prop["description"] = d
return prop
}
// pickMediaModel is the model asked stands for: one of all, by its whole
// id or by a bare id only one of them has; a ChatGPT account's other GPT
// Image names as its one (ListedDrawer); or a newer model of a family a
// subscription serves before magpie lists it (drawsFamily,
// grokVideoFamily), as the APIs take it. Else an error that names those
// shown.
func pickMediaModel(tool, asked string, video bool, all, shown []string, def string) (string, error) {
asked = strings.TrimSpace(asked)
if !video && strings.Contains(asked, "/") {
if l := ListedDrawer(asked); slices.Contains(all, l) {
asked = l
}
}
if slices.Contains(all, asked) {
return asked, nil
}
if !strings.Contains(asked, "/") && asked != "" {
var same []string
for _, ref := range all {
if _, id, _ := strings.Cut(ref, "/"); id == asked && !provider.IsGroupRef(ref) {
same = append(same, ref)
}
}
switch len(same) {
case 1:
return same[0], nil
case 0:
default:
return "", fmt.Errorf("%s: more than one provider has %q; name it with its provider: %s", tool, asked, strings.Join(same, ", "))
}
}
if p, id, ok := provider.Resolve(asked); ok && strings.Contains(asked, "/") && p.On() && !p.DecideOnly() {
if video && drawsGrok(p) && strings.HasPrefix(id, grokVideoFamily) || !video && drawsFamily(p, id) {
return asked, nil
}
}
what, a := "image", "an image"
if video {
what, a = "video", "a video"
}
if def == "" {
return "", fmt.Errorf("%s: no %s model is set up in Magpie", tool, what)
}
if len(shown) == 0 {
return "", fmt.Errorf("%s: %q isn't %s model that can be used here; leave model out to use the default", tool, asked, a)
}
list := shown
more := ""
if len(list) > 100 {
list, more = list[:100], ", and "+strconv.Itoa(len(shown)-100)+" more"
}
return "", fmt.Errorf("%s: %q isn't %s model that can be used here. Pick one of: %s%s; or leave model out to use the default (%s)", tool, asked, a, strings.Join(list, ", "), more, def)
}
// mediaModelFor is the model r's call of tool asked for (pickMediaModel),
// among those r's key may use.
func mediaModelFor(r *http.Request, tool, asked string) (string, error) {
video := tool == GenerateVideoTool
all, shown, def := mediaModelsFor(r, video)
return pickMediaModel(tool, asked, video, all, shown, def)
}
+183
View File
@@ -0,0 +1,183 @@
package gateway
import (
"encoding/json"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/access"
)
// mcpModelProp is the model argument tools/list gives tool, for the key
// in hdr.
func mcpModelProp(t *testing.T, h http.Handler, addr, tool string, hdr ...string) (enum []string, desc string) {
t.Helper()
var list struct {
Result struct {
Tools []struct {
Name string
InputSchema struct {
Properties map[string]struct {
Enum []string
Description string
}
}
}
}
}
w := imagePost(h, addr, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, hdr...)
json.Unmarshal(w.Body.Bytes(), &list)
for _, tl := range list.Result.Tools {
if tl.Name == tool {
p, ok := tl.InputSchema.Properties["model"]
if !ok {
t.Fatalf("%s takes no model: %s", tool, w.Body.String())
}
return p.Enum, p.Description
}
}
t.Fatalf("no %s: %s", tool, w.Body.String())
return nil, ""
}
// generate_image takes the model to draw with (XYenon on Discord): one the
// schema lists, by its whole id or a bare one, is the one drawn with; left
// out, Settings' is; one it doesn't list is an error naming those it
// does, and nothing is drawn.
func TestImageMCPDrawsWithTheModelAsked(t *testing.T) {
s, e := easeled(t)
t.Cleanup(s.imageStore.running.Wait)
h := lanGuard(s.Handler())
const here = "127.0.0.1:5000"
enum, desc := mcpModelProp(t, h, here, GenerateImageTool)
if !slices.Contains(enum, "art/gpt-image-1") || !slices.Contains(enum, "art/painter-image-preview") || slices.Contains(enum, "art/text") || enum[0] != Drawer() {
t.Fatalf("enum %v (default %s)", enum, Drawer())
}
if !strings.Contains(desc, "now "+Drawer()) {
t.Fatalf("description %q", desc)
}
// left out: the default, at the images API
m := readImageReply(t, imagePost(h, here, imageToolCall(GenerateImageTool, map[string]any{"prompt": "a magpie"})))
if m.Result.IsError || len(e.got("/v1/images/generations")) != 1 || len(e.got("/v1/chat/completions")) != 0 {
t.Fatalf("default: %+v %v", m, e.got("/v1/chat/completions"))
}
// asked by its whole id, then by its bare one: drawn with it
for i, model := range []string{"art/painter-image-preview", "painter-image-preview"} {
m = readImageReply(t, imagePost(h, here, imageToolCall(GenerateImageTool, map[string]any{"prompt": "a magpie", "model": model})))
if sent := e.got("/v1/chat/completions"); m.Result.IsError || len(sent) != i+1 || !strings.Contains(sent[i], "painter-image-preview") || !strings.Contains(m.Result.Content[0].Text, "art/painter-image-preview") {
t.Fatalf("%s: %+v, sent %v", model, m, sent)
}
}
if n := len(e.got("/v1/images/generations")); n != 1 {
t.Fatalf("the default drew %d times", n)
}
// unknown, or not an image model: an error naming those there are
for _, model := range []string{"art/nope", "art/text", "nope"} {
m = readImageReply(t, imagePost(h, here, imageToolCall(GenerateImageTool, map[string]any{"prompt": "a magpie", "model": model})))
if txt := m.Result.Content[0].Text; !m.Result.IsError || !strings.Contains(txt, `"`+model+`"`) || !strings.Contains(txt, "art/gpt-image-1") || !strings.Contains(txt, "art/painter-image-preview") {
t.Fatalf("%s: %+v", model, m)
}
}
if len(e.got("/v1/images/generations")) != 1 || len(e.got("/v1/chat/completions")) != 2 {
t.Fatalf("an unknown model drew: %v %v", e.got("/v1/images/generations"), e.got("/v1/chat/completions"))
}
// the stdio server asks the same
ask := func(model string) mediaReply {
b, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": 9, "method": MediaModelMethod, "params": map[string]any{"tool": GenerateImageTool, "model": model}})
return readMCPRaw(t, imagePost(h, here, string(b)))
}
if r := ask("painter-image-preview"); r.Error != nil || !strings.Contains(string(r.Result), `"art/painter-image-preview"`) {
t.Fatalf("media_model: %+v", r)
}
if r := ask("art/nope"); r.Error == nil || !strings.Contains(r.Error.Message, "art/gpt-image-1") {
t.Fatalf("media_model, unknown: %+v", r)
}
}
type mediaReply struct {
Result json.RawMessage
Error *struct {
Code int
Message string
}
}
func readMCPRaw(t *testing.T, w *httptest.ResponseRecorder) mediaReply {
t.Helper()
var r mediaReply
res := w.Result()
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
t.Fatal(err)
}
return r
}
// A gateway key held to some models is held to them here too: the schema
// lists only those, one it may not use is refused by the images API's own
// check, and nothing is drawn.
func TestImageMCPModelHeldToTheKey(t *testing.T) {
s, e := easeled(t)
t.Cleanup(s.imageStore.running.Wait)
keys, secrets := newCaller(t, "Narrow")
if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{Drawer()}}); err != nil {
t.Fatal(err)
}
h := lanGuard(s.Handler())
const there = "192.0.2.7:4000"
auth := []string{"Authorization", "Bearer " + secrets[0]}
if enum, _ := mcpModelProp(t, h, there, GenerateImageTool, auth...); len(enum) != 1 || enum[0] != Drawer() {
t.Fatalf("the key's enum %v", enum)
}
m := readImageReply(t, imagePost(h, there, imageToolCall(GenerateImageTool, map[string]any{"prompt": "a magpie", "model": "art/painter-image-preview"}), auth...))
if txt := m.Result.Content[0].Text; !m.Result.IsError || !strings.Contains(txt, "may not use") || !strings.Contains(txt, `"Narrow"`) {
t.Fatalf("a model the key may not use: %+v", m)
}
m = readImageReply(t, imagePost(h, there, imageToolCall(GenerateImageTool, map[string]any{"prompt": "a magpie", "model": "art/nope"}), auth...))
if txt := m.Result.Content[0].Text; !m.Result.IsError || !strings.Contains(txt, Drawer()) || strings.Contains(txt, "painter") {
t.Fatalf("unknown, for the key: %+v", m)
}
if n := len(e.got("/v1/chat/completions")) + len(e.got("/v1/images/generations")); n != 0 {
t.Fatalf("drew %d times", n)
}
// the one it may use draws
m = readImageReply(t, imagePost(h, there, imageToolCall(GenerateImageTool, map[string]any{"prompt": "a magpie", "model": Drawer()}), auth...))
if m.Result.IsError || len(e.got("/v1/images/generations")) != 1 {
t.Fatalf("the key's model: %+v", m)
}
}
// generate_video takes the model too: one listed reaches the vendor, one
// that isn't is an error naming those that are.
func TestImageMCPVideoModelAsked(t *testing.T) {
grokSignedIn(t)
g := newGrokMedia(t)
s := New()
h := lanGuard(s.Handler())
was, wasPoll := imageMCPWithin, imageMCPPoll
t.Cleanup(func() { imageMCPWithin, imageMCPPoll = was, wasPoll })
imageMCPWithin, imageMCPPoll = time.Minute, time.Millisecond
const here = "127.0.0.1:5000"
enum, _ := mcpModelProp(t, h, here, GenerateVideoTool)
if len(enum) == 0 || !strings.HasPrefix(enum[0], "grok/") {
t.Fatalf("video enum %v", enum)
}
m := readImageReply(t, imagePost(h, here, imageToolCall(GenerateVideoTool, map[string]any{"prompt": "a kite", "model": "grok/nope-video"})))
if txt := m.Result.Content[0].Text; !m.Result.IsError || !strings.Contains(txt, enum[0]) || g.body("/v1/videos/generations", 0) != "" {
t.Fatalf("unknown video model: %+v", m)
}
m = readImageReply(t, imagePost(h, here, imageToolCall(GenerateVideoTool, map[string]any{"prompt": "a kite", "model": enum[0]})))
_, id, _ := strings.Cut(enum[0], "/")
if sent := g.body("/v1/videos/generations", 0); m.Result.IsError || !strings.Contains(sent, `"model":"`+id+`"`) {
t.Fatalf("video %s: %+v, sent %s", enum[0], m, sent)
}
// a newer model of the family Grok serves, before magpie lists it
m = readImageReply(t, imagePost(h, here, imageToolCall(GenerateVideoTool, map[string]any{"prompt": "a kite", "model": "grok/grok-imagine-video-9"})))
if sent := g.body("/v1/videos/generations", 1); m.Result.IsError || !strings.Contains(sent, `"model":"grok-imagine-video-9"`) {
t.Fatalf("a newer video model: %+v, sent %s", m, sent)
}
}
+1 -1
View File
@@ -120,7 +120,7 @@ func TestImageMCPDrawsAndServesTheImage(t *testing.T) {
t.Fatalf("%s takes a path the gateway can't write to", tl.Name)
}
}
if strings.Join(names, ",") != "generate_image,generate_video,generation_result" || len(list.Result.Tools[0].InputSchema.Properties) != len(ImageToolProps("")) {
if strings.Join(names, ",") != "generate_image,generate_video,generation_result" || len(list.Result.Tools[0].InputSchema.Properties) != len(ImageToolProps(""))+1 {
t.Fatalf("tools: %s", w.Body.String())
}
m := readImageReply(t, imagePost(h, "127.0.0.1:5000", imageToolCall(GenerateImageTool, map[string]any{"prompt": "a magpie", "size": "1024x1024"})))
+128 -2
View File
@@ -1,7 +1,8 @@
// Package imagemcp is `magpie mcp image`: a stdio MCP server an agent is
// given, when it is picked for it in the Library, to make images with the
// model the Settings' Image generation names, and videos with the first
// video model the gateway has (gateway.AutoVideomaker). It asks the
// video model the gateway has (gateway.AutoVideomaker), or with the model
// a call names, among those the gateway lists for it. It asks the
// gateway, which knows the providers, and saves what comes back in the
// project.
package imagemcp
@@ -15,6 +16,7 @@ import (
"errors"
"fmt"
"io"
"maps"
"mime"
"net/http"
"net/url"
@@ -314,7 +316,8 @@ func (s *server) handle(req rpc) {
case "ping":
result = map[string]any{}
case "tools/list":
result = map[string]any{"tools": []any{schema, videoSchema, resultSchema}}
props := s.modelProps()
result = map[string]any{"tools": []any{withModel(schema, props[tool]), withModel(videoSchema, props[videoTool]), resultSchema}}
case "tools/call":
var p struct {
Name string `json:"name"`
@@ -370,6 +373,7 @@ func (s *server) handle(req rpc) {
type args struct {
Prompt string `json:"prompt"`
Model string `json:"model"`
Path string `json:"path"`
Size string `json:"size"`
N int `json:"n"`
@@ -394,6 +398,13 @@ func (s *server) generate(raw json.RawMessage) (string, error) {
// vendor keeps through the vendor's own proxy, the way it just reached
// it (#1576)
req := map[string]any{"prompt": a.Prompt, "n": a.N, "response_format": "b64_json"}
if strings.TrimSpace(a.Model) != "" {
m, err := s.mediaModel(tool, a.Model)
if err != nil {
return "", err
}
req["model"] = m
}
for k, v := range map[string]string{"size": a.Size, "quality": a.Quality, "background": a.Background} {
if v != "" {
req[k] = v
@@ -710,6 +721,7 @@ func extOf(mt string) string {
type filmArgs struct {
Prompt string `json:"prompt"`
Model string `json:"model"`
Path string `json:"path"`
Seconds any `json:"seconds"`
Size string `json:"size"`
@@ -788,6 +800,13 @@ func (s *server) film(raw json.RawMessage) (string, error) {
}
project := s.project()
req := map[string]any{"prompt": a.Prompt}
if strings.TrimSpace(a.Model) != "" {
m, err := s.mediaModel(videoTool, a.Model)
if err != nil {
return "", err
}
req["model"] = m
}
if secs > 0 {
req["seconds"] = strconv.Itoa(secs)
}
@@ -887,3 +906,110 @@ func (s *server) film(raw json.RawMessage) (string, error) {
}
return fmt.Sprintf("Generated a %svideo with %s, saved to:\n- %s\n", length, v.Model, shownPath(path)), nil
}
// The model a generate tool takes is the gateway's to say: it knows the
// providers, which this process may not read (an agent in WSL, a gateway
// elsewhere), and the models its caller may use. Its image MCP server's
// tools list them (gateway.MediaModelMethod checks one).
// modelAsk bounds asking the gateway for the models.
const modelAsk = 10 * time.Second
// ask asks the gateway's image MCP server method with params: its result,
// or its JSON-RPC error's code and message.
func (s *server) ask(method string, params any) (json.RawMessage, int, error) {
body, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": 1, "method": method, "params": params})
ctx, cancel := context.WithTimeout(context.Background(), modelAsk)
defer cancel()
r, _ := http.NewRequestWithContext(ctx, http.MethodPost, s.gateway+gateway.ImageMCPPath, bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json, text/event-stream")
r.Header.Set("Authorization", "Bearer "+gateway.Token)
s.mu.Lock()
r.Header.Set("User-Agent", s.agent)
s.mu.Unlock()
res, err := s.client.Do(r)
if err != nil {
return nil, 0, fmt.Errorf("magpie isn't answering at %s (is it running?): %w", s.gateway, err)
}
defer res.Body.Close()
data, _ := io.ReadAll(io.LimitReader(res.Body, 4<<20))
if res.StatusCode != http.StatusOK {
return nil, 0, fmt.Errorf("magpie at %s answered %d: %s", s.gateway, res.StatusCode, gatewaySaid(data))
}
var m struct {
Result json.RawMessage `json:"result"`
Error *struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
if err := json.Unmarshal(data, &m); err != nil {
return nil, 0, fmt.Errorf("magpie at %s answered what isn't JSON-RPC: %w", s.gateway, err)
}
if m.Error != nil {
return nil, m.Error.Code, errors.New(m.Error.Message)
}
return m.Result, 0, nil
}
// modelProps are the model argument of each generate tool, by its name, as
// the gateway's image MCP server gives it; a plain one where it can't be
// asked.
func (s *server) modelProps() map[string]any {
out := map[string]any{
tool: map[string]any{"type": "string", "description": "The image model to use, as provider/model. Leave it out to use the one set in Magpie (Settings → Images → Image generation)."},
videoTool: map[string]any{"type": "string", "description": "The video model to use, as provider/model. Leave it out to use the video model set up in Magpie."},
}
res, _, err := s.ask("tools/list", map[string]any{})
if err != nil {
return out
}
var l struct {
Tools []struct {
Name string `json:"name"`
InputSchema struct {
Properties map[string]json.RawMessage `json:"properties"`
} `json:"inputSchema"`
} `json:"tools"`
}
json.Unmarshal(res, &l)
for _, t := range l.Tools {
var prop map[string]any
if _, ok := out[t.Name]; ok && json.Unmarshal(t.InputSchema.Properties["model"], &prop) == nil && prop != nil {
out[t.Name] = prop
}
}
return out
}
// withModel is sc, a tool's schema, with prop as its model argument.
func withModel(sc map[string]any, prop any) map[string]any {
out := maps.Clone(sc)
in := maps.Clone(sc["inputSchema"].(map[string]any))
props := maps.Clone(in["properties"].(map[string]any))
props["model"] = prop
in["properties"] = props
out["inputSchema"] = in
return out
}
// mediaModel is the model of the gateway's that asked stands for in a call
// of name, or why there is none.
func (s *server) mediaModel(name, asked string) (string, error) {
res, code, err := s.ask(gateway.MediaModelMethod, map[string]any{"tool": name, "model": asked})
if code == -32601 {
// a magpie older than this one: it takes the model as named
return strings.TrimSpace(asked), nil
}
if err != nil {
return "", err
}
var m struct {
Model string `json:"model"`
}
if json.Unmarshal(res, &m) != nil || m.Model == "" {
return "", fmt.Errorf("magpie at %s didn't say which model %q is", s.gateway, asked)
}
return m.Model, nil
}
+59 -1
View File
@@ -16,6 +16,8 @@ import (
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/gateway"
)
var png = []byte("\x89PNG\r\n\x1a\n fake")
@@ -30,6 +32,10 @@ type gw struct {
func (g *gw) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
if r.URL.Path == gateway.ImageMCPPath {
modelsMCP(w, b)
return
}
g.mu.Lock()
g.path, g.body, g.ua = append(g.path, r.URL.Path), append(g.body, string(b)), append(g.ua, r.Header.Get("User-Agent"))
g.mu.Unlock()
@@ -48,6 +54,33 @@ func (g *gw) ServeHTTP(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]any{"model": "art/gpt-image-1", "data": data})
}
// modelsMCP is the gateway's image MCP server as the stdio one asks it:
// the model argument tools/list gives, and which model one asked is.
func modelsMCP(w http.ResponseWriter, b []byte) {
var m struct {
ID any `json:"id"`
Method string `json:"method"`
Params struct {
Model string `json:"model"`
} `json:"params"`
}
json.Unmarshal(b, &m)
out := map[string]any{"jsonrpc": "2.0", "id": m.ID}
switch m.Method {
case "tools/list":
out["result"] = map[string]any{"tools": []any{map[string]any{"name": "generate_image", "inputSchema": map[string]any{"properties": map[string]any{
"model": map[string]any{"type": "string", "enum": []string{"art/gpt-image-1", "art/painter"}, "description": "The image model to use."}}}}}}
case gateway.MediaModelMethod:
switch m.Params.Model {
case "art/painter", "painter":
out["result"] = map[string]any{"model": "art/painter"}
default:
out["error"] = map[string]any{"code": -32602, "message": "generate_image: \"" + m.Params.Model + "\" isn't an image model that can be used here. Pick one of: art/gpt-image-1, art/painter"}
}
}
json.NewEncoder(w).Encode(out)
}
// client talks to a server over pipes, answering roots/list with root, and
// keeps the notifications it is sent.
type client struct {
@@ -125,7 +158,7 @@ func TestGenerateSavesInTheProject(t *testing.T) {
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"}})
tools := c.call("tools/list", map[string]any{})
if b, _ := json.Marshal(tools); !strings.Contains(string(b), `"generate_image"`) {
if b, _ := json.Marshal(tools); !strings.Contains(string(b), `"generate_image"`) || !strings.Contains(string(b), `"enum":["art/gpt-image-1","art/painter"]`) {
t.Fatalf("tools %s", b)
}
r := c.call("tools/call", map[string]any{"name": "generate_image", "arguments": map[string]any{"prompt": "A Magpie, on a branch!", "n": 2}})
@@ -207,6 +240,10 @@ type vgw struct {
func (g *vgw) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
if r.URL.Path == gateway.ImageMCPPath {
http.NotFound(w, r)
return
}
g.mu.Lock()
defer g.mu.Unlock()
g.path, g.body = append(g.path, r.Method+" "+r.URL.Path), append(g.body, string(b))
@@ -491,3 +528,24 @@ func TestSlowImageAnswersBeforeTheAgentGivesUp(t *testing.T) {
t.Fatalf("an unknown id: %v", r)
}
}
// The model asked is the one the gateway says it is, named to the images
// API; one it doesn't know is a tool error and nothing is drawn.
func TestGenerateTakesTheModel(t *testing.T) {
g := &gw{}
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": "a magpie", "model": "painter"}})
if r["isError"] == true || len(g.body) != 1 || !strings.Contains(g.body[0], `"model":"art/painter"`) {
t.Fatalf("%v: asked %v", r, g.body)
}
r = c.call("tools/call", map[string]any{"name": "generate_image", "arguments": map[string]any{"prompt": "a magpie", "model": "art/nope"}})
if r["isError"] != true || !strings.Contains(text(r), "art/gpt-image-1, art/painter") || len(g.body) != 1 {
t.Fatalf("%v: asked %v", r, g.body)
}
r = c.call("tools/call", map[string]any{"name": "generate_image", "arguments": map[string]any{"prompt": "a magpie"}})
if r["isError"] == true || len(g.body) != 2 || strings.Contains(g.body[1], `"model"`) {
t.Fatalf("%v: asked %v", r, g.body)
}
}