Files
magpie/internal/gateway/vision_mcp.go
T
yetone 6c1c0a7472 gateway, gui: magpie's image recognition is an MCP server of its own, /mcp/magpie/vision, with one analyze_image tool (JonahFu on X)
What: beside /mcp/magpie/web-search, the gateway serves the model that
describes images (Settings › Models › Image recognition, else magpie's
automatic pick) as a Streamable HTTP MCP server. analyze_image takes an
image (absolute file path, http(s) URL or data:image URL) and an optional
prompt: without one it is described as a model's image is (same cache and
system text), with one the prompt is asked beside it. The JSON-RPC serving,
guard and Origin check are shared with the web search server (serveMCP).
A file path is read only for a local, unproxied caller, up to 20 MB, and
only when its bytes are an image; another computer sends a URL or data
URL with a gateway key. Each call holds the key's limit (budget.Reserve)
and reads in Routing as the agent's analyze_image call. Settings › Models
shows the address under Image generation (#visionMcpRow), in every
language.

Run:
- go test -tags nogui ./... (no failures); gofmt -l clean; GOOS=linux and
  GOOS=windows go vet ./internal/gateway.
- New TestVisionMCPAnalyzesImage / WithNoSeer / Guarded / HeldToKeyLimit.
  Broke the local() guard by hand: TestVisionMCPGuarded went red on the
  other computer's file path being read.
- GUI (chromium + webkit, en/zh/zh-TW/ja/de, 1100 and 520px):
  vision-mcp, search-mcp, public-url-default, images-missing,
  settings-sections, i18n-keys, routing-vision-why: 95 pass. The row was
  first put between the two pickers and images-missing went red (it finds
  them by position), so it sits last.
- Real use: this build under a sandbox HOME holding only the owner's
  OpenRouter provider, Image recognition set to openai/gpt-6-luna.
  initialize, then tools/call with docs/screenshots/font-settings.png as a
  file path and a prompt: the model listed every settings row and its
  value correctly.
Not tried: a real agent (Claude Code, Cursor) mounting it; its MCP
exchange is the same as the web search server's.
2026-10-12 08:03:24 +08:00

201 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/budget"
"github.com/yetone/magpie/internal/provider"
)
// magpie's image recognition, the model that describes images to a model
// that can't see them (seer: Settings › Models › Image recognition, else
// AutoVision's), is also an MCP server of its own beside the web search's
// (JonahFu on X), so an agent on a model that can't see, or any agent at
// all, can mount it as an analyze_image tool: Streamable HTTP at
// VisionMCPPath, served and guarded as SearchMCPPath is (serveMCP), a
// gateway key's limit holding each look (budget.Reserve).
// VisionMCPPath is where the image recognition MCP server is served.
const VisionMCPPath = "/mcp/magpie/vision"
const visionMCPTool = "analyze_image"
// visionMCPBody is how long a message to the server may be: an image is
// sent in it as a data URL.
const visionMCPBody = 32 << 20
// visionFileMax is the largest image file the tool reads from a path.
const visionFileMax = 20 << 20
// visionAskSystem is what the model is told when the agent asks something
// of the image, in place of visionSystem's full description.
const visionAskSystem = `You look at an image for an AI agent that cannot see it, and answer what it asks about the image from what the image shows.
- Transcribe text exactly as written wherever the answer needs it: code, terminal output, error messages, UI labels, numbers.
- Say so when what is asked can't be read or isn't in the image; don't guess.
No preamble.`
var visionMCPSchema = map[string]any{
"type": "object",
"properties": map[string]any{
"image": map[string]any{
"type": "string",
"description": "The image: an absolute path to an image file on this computer (PNG, JPEG, GIF or WebP), an http(s) URL, or a data URL (data:image/png;base64,...).",
},
"prompt": map[string]any{
"type": "string",
"description": "What to find out about the image. Without it the image is described in full: its text transcribed, its layout, charts and diagrams.",
},
},
"required": []string{"image"},
}
func (s *Server) visionMCP(w http.ResponseWriter, r *http.Request) {
serveMCP(w, r, "magpie's image recognition", visionMCPBody, s.visionMCPCall)
}
// visionMCPCall answers one request: its result, or its JSON-RPC error.
func (s *Server) visionMCPCall(r *http.Request, m mcpMessage) (any, map[string]any) {
switch m.Method {
case "initialize":
return map[string]any{
"protocolVersion": mcpVersion(m),
"capabilities": map[string]any{"tools": map[string]any{}},
"serverInfo": map[string]any{"name": "magpie-vision", "title": "Magpie Image Recognition", "version": Version},
"instructions": "analyze_image looks at an image with the model magpie describes images with (Settings → Models → Image recognition) and answers in text: for a model or agent that can't see images, or to read a screenshot, chart or diagram.",
}, nil
case "ping":
return map[string]any{}, nil
case "tools/list":
return map[string]any{"tools": []any{map[string]any{
"name": visionMCPTool,
"title": "Analyze image",
"description": "Looks at an image and answers in text: what the prompt asks of it, or, with no prompt, a full description with all its text transcribed. Use it to read screenshots, error dialogs, charts, diagrams and photos when you can't see images yourself.",
"inputSchema": visionMCPSchema,
"annotations": map[string]any{"readOnlyHint": true, "openWorldHint": false},
}}}, nil
case "tools/call":
var p struct {
Name string `json:"name"`
Arguments struct {
Image string `json:"image"`
Prompt string `json:"prompt"`
} `json:"arguments"`
}
if err := json.Unmarshal(m.Params, &p); err != nil {
return nil, map[string]any{"code": -32602, "message": "invalid params: " + err.Error()}
}
if p.Name != visionMCPTool {
return nil, map[string]any{"code": -32602, "message": "unknown tool " + p.Name + ": this server has " + visionMCPTool}
}
said, err := s.visionMCPRun(r, strings.TrimSpace(p.Arguments.Image), strings.TrimSpace(p.Arguments.Prompt))
if err != nil {
return toolResult(err.Error(), true), nil
}
return toolResult(said, false), nil
}
return nil, map[string]any{"code": -32601, "message": "method not found: " + m.Method}
}
// visionMCPRun has the image looked at as an image of a model's request
// would be, held to the calling gateway key's limit, and named in the
// Routing view as the agent's MCP call.
func (s *Server) visionMCPRun(r *http.Request, image, prompt string) (string, error) {
if image == "" {
return "", searchMCPError("analyze_image needs an image: a file path, an http(s) URL or a data URL")
}
src, err := visionSrc(r, image)
if err != nil {
return "", err
}
model, ok := seer()
if !ok {
if provider.HeldSettings().Vision == "off" {
return "", searchMCPError("Image recognition is off in magpie: pick a model for it in Settings → Models → Image recognition")
}
return "", searchMCPError("magpie has no model that sees images: add a provider with one, or pick one in Settings → Models → Image recognition")
}
release, refused := budget.Reserve(access.Caller(r.Context()), int64(len(src)+len(prompt)), "", limitClock())
if refused != nil {
return "", refused
}
defer release()
ctx := context.WithValue(r.Context(), describeForKey{}, describeFor{call: &CallFor{Agent: callerOf(r).agent, Missing: VisionMissing(), MCP: true}})
var said string
if prompt == "" {
said, err = s.describe(ctx, model, src)
} else {
said, err = s.askVisionWith(ctx, model, src, visionAskSystem, prompt)
}
if err != nil {
if r.Context().Err() == nil {
log.Printf("vision MCP: %v", err)
}
return "", searchMCPError(fmt.Sprintf("magpie couldn't look at the image with %s: %v%s", model, err, missingSeerNote()))
}
return said, nil
}
// visionSrc is the image as the describer is sent it: a URL or data URL as
// it is, a file read into a data URL. A file is read only for an agent on
// this computer: another machine's path names none of its own here, and a
// shared magpie's files aren't its callers'.
func visionSrc(r *http.Request, image string) (string, error) {
low := strings.ToLower(image)
switch {
case strings.HasPrefix(low, "data:"):
if !strings.HasPrefix(low, "data:image/") || !strings.Contains(low[:min(len(low), 64)], ";base64,") {
return "", searchMCPError("analyze_image takes a data URL of an image in base64 (data:image/png;base64,...)")
}
return image, nil
case strings.HasPrefix(low, "http://"), strings.HasPrefix(low, "https://"):
return image, nil
}
path := image
if strings.HasPrefix(low, "file://") {
path = image[len("file://"):]
}
if strings.HasPrefix(path, "~/") {
if home, err := os.UserHomeDir(); err == nil {
path = filepath.Join(home, path[2:])
}
}
if !filepath.IsAbs(path) {
return "", searchMCPError("analyze_image takes an absolute file path (the gateway doesn't know your agent's working folder), an http(s) URL or a data URL: " + image + " is none of them")
}
if !local(r) {
return "", searchMCPError("analyze_image reads a file only for an agent on the computer magpie runs on: from another computer, send the image as a data URL (data:image/png;base64,...) or an http(s) URL")
}
f, err := os.Open(path)
if err != nil {
return "", searchMCPError("analyze_image couldn't read " + path + ": " + err.Error())
}
defer f.Close()
if st, err := f.Stat(); err == nil && st.IsDir() {
return "", searchMCPError(path + " is a folder, not an image")
} else if err == nil && st.Size() > visionFileMax {
return "", searchMCPError(fmt.Sprintf("%s is %d MB: analyze_image reads images up to %d MB", path, st.Size()>>20, visionFileMax>>20))
}
b, err := io.ReadAll(io.LimitReader(f, visionFileMax+1))
if err != nil {
return "", searchMCPError("analyze_image couldn't read " + path + ": " + err.Error())
}
if len(b) > visionFileMax {
return "", searchMCPError(fmt.Sprintf("%s is over %d MB: analyze_image reads images up to %d MB", path, visionFileMax>>20, visionFileMax>>20))
}
mime := http.DetectContentType(b)
if !strings.HasPrefix(mime, "image/") {
return "", searchMCPError(path + " isn't an image analyze_image can read (PNG, JPEG, GIF or WebP): it reads as " + mime)
}
return "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(b), nil
}