Skip to content

nsgo mcp: app-defined tools (automation.Register) cannot return an image; their PNG reaches the agent as base64 text #2343

Backlog

Description

@houko

Context

automation.Register (#1285) lets an app or library add its own tools to nsgo mcp, but every result such a tool returns reaches the agent as text. On main (a34e7e7f):

  • automation/automation.go:11-14: an automation.Tool handler "returns a JSON-encodable result or an error"; core/automationtool_on.go:80-85 runs it and json.Marshals the value.
  • cmd/nsgo/mcp_apptool.go:95-104: the MCP handler for an app tool forwards that JSON to mcpResult(tool.Name, result).
  • cmd/nsgo/mcp.go:83-119: mcpResult switches on the method name. Only the built-in automation.Screenshot becomes &mcp.ImageContent{Data: r.PNG, MIMEType: "image/png"} (mcp.go:88-95); an app tool's name never matches a case, so its result falls through to indented JSON in a single mcp.TextContent (mcp.go:115-118).

So a handler that returns PNG bytes (a []byte, or a struct shaped like automation.ScreenshotResult) is marshalled by encoding/json into a base64 string, and the agent receives that string as text instead of an image. There is no type or field an app tool can return to get ImageContent (or any non-text MCP content).

docs/public/en/debugging.md:231-258 ("App-defined MCP tools") and docs/architecture/tool.md:57 describe the result only as "a JSON-encodable value" and say nothing about images either way.

Why it matters

The pixels an app most wants to show an agent are often not the window. A game layer built on NSGo (nsgo-dev/game) wants tools such as "render this camera / this render target / this sprite atlas / this texture at full resolution" or "capture the game viewport without the editor chrome around it". These are exactly what the built-in screenshot cannot give, and what an agent needs to judge a visual change. Today such a tool can only hand back base64 text: a multimodal client does not look at it, it costs many times the tokens an image block would, and large captures can push the response past client limits. The same applies to any app that renders charts, documents, canvases or camera frames off screen.

Proposal

Give automation an explicit result type that nsgo mcp maps to MCP content, rather than guessing from the JSON shape. For example:

// Image is an image in a tool's result, shown to an agent as MCP image content.
type Image struct {
    Data     []byte // encoded bytes
    MIMEType string // "image/png", "image/jpeg", ...
}

// Result is a tool result with content beyond JSON. A handler may return it,
// *Result, or any other JSON-encodable value as today.
type Result struct {
    Text   string
    Images []Image
}

core.CallAutomationTool would marshal a Result into a tagged form of the internal protocol (which is unversioned, docs/architecture/tool.md:51), and mcpAppTools.add would turn it into TextContent plus one ImageContent per image instead of calling mcpResult with the app tool's name. Any other value keeps today's behaviour. automation.Call in tests could keep returning the JSON, or a decoder (automation.DecodeResult) could give the Result back.

Acceptance

  • An app tool whose handler returns an image result is listed and, when called through go tool nsgo mcp, answers with an MCP image content block carrying the bytes and MIME type, plus a text block when Text is set.
  • A handler returning any other value behaves exactly as now (indented JSON text).
  • An app tool cannot spoof the built-in screenshot shape by accident: a plain struct with a png field still comes back as JSON text.
  • TestAutomationEndToEnd (or a unit test on mcpAppTools) covers a fixture tool returning a PNG.
  • docs/public/*/debugging.md ("App-defined MCP tools") and docs/architecture/tool.md describe the image result.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions