Skip to content

Add approved image attachment inputs for MCP tools - #10871

Merged
danielhanchen merged 73 commits into
unslothai:mainfrom
Etherll:codex/inline-mcp-image-attachments-c90948005cc9
Oct 1, 2026
Merged

danielhanchen merged 73 commits into
unslothai:mainfrom
Etherll:codex/inline-mcp-image-attachments-c90948005cc9

Conversation

@Etherll

@Etherll Etherll commented Sep 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Lets an MCP tool receive the image a user attaches in chat, without the image bytes ever entering the model's context. Typical use: a reverse image search server (for example trace.moe) driven by a text-only model.

How it works:

  1. Opt in per server. The MCP server edit form has a "Send attached images to tools" switch. With it on, pick a tool and one of its top-level string fields, and whether that field takes raw base64 or a data URL. Stored as image_input_mappings on the server row (new nullable column, nothing to migrate).
  2. Attach. While MCP is on for the chat and an enabled server has a mapping, a newly attached PNG, JPEG or WebP image (10 MB max, one at a time, HEIC/AVIF/BMP/TIFF converted first) is marked tool-only. It stays in the thread, is never sent to the model as image input, and goes to the backend as mcp_image on the request.
  3. Model sees a placeholder. In the tool list the mapped field becomes {"type": "string", "enum": ["attached_image"]}. A mapping whose field is no longer a top-level string after a server update is ignored, so the tool keeps its own schema.
  4. Every send is approved. A call with "attached_image" in the mapped field always shows an approval card ("Share image once" / "Deny") in all three tool loops (GGUF, safetensors and external providers), whatever the permission mode, bypass included. The card names the server, tool, size and destination (host, or "local command " for stdio; never URL credentials or arguments). "Always allow" and the keyboard shortcut are not offered on that card.
  5. Inserted at dispatch. Only after that approval does execute_tool swap the encoded image into a copy of the arguments, and only for the server configuration the card named: the row is re-read right before sending, and a server edited while the card was open (URL, headers, OAuth, mapping, or a dropped tool cache) gets an error instead. The fingerprint of that configuration never leaves the backend. An exact base64 or data URL echo of the image in the server's reply is replaced with [attached image]; a re-encoded copy (url-safe, hex, line-wrapped, JSON-escaped, percent-encoded, partial) withholds the whole reply from the model. Other results pass on as usual. Without an approved image the call returns an error asking the user to attach one.

Calls without the placeholder, servers without mappings, and requests without mcp_image take the existing paths unchanged. mcp_image needs stream=true plus X-Unsloth-Events: 1, because the user has to be able to answer the approval card. Durable chat runs refuse it like other media, so the image is never persisted in a run's request. If the browser cannot read the MCP server list, the image is refused rather than sent to the model.

Changes

  • core/inference/mcp_image.py: data URL validation (type, size, Pillow decode, format match), mapping lookup, schema rewrite, echo redaction.
  • tools.py: rewritten schemas in the MCP listing and schema lookups, mcp_image_share() for the loops, insertion and redaction in execute_tool.
  • Tool loops, orchestrator and routes: thread mcp_image through and force the approval card.
  • MCP server API: image_input_mappings on create/update/response, plus GET /api/mcp/servers/{id}/tools (cached tool schemas) for the field picker.
  • Frontend: mapping editor in the server form, tool-only marking in the image adapter, modelVisibleMessage filtering wherever request messages are built, mcp_image on the request, approval card copy.

Tests

  • tests/test_mcp_image.py: parsing, schema rewrite, insertion only with an approved image, redaction, ordinary path unchanged, safetensors loop asks even under bypass (allow and deny), route rejects non-interactive requests, mapping round trip, GGUF loop wiring.
  • tests/test_studio_tool_loop.py: provider loop asks even under bypass and passes the image only for that call.
  • frontend/tests/mcp-image.test.ts: tool-only filtering, mcp_image extraction, mapping candidates.

UI

MCP server edit form, merge base vs this PR. The same server is created on both sides with an image_input_mappings entry; the base drops the field, this PR stores it and shows the switch with the mapping row.

MCP server edit form before and after

Live run

Real Studio with unsloth/Qwen3-8B-GGUF (Q4_K_M) and a local MCP server whose lookup(image) tool records what it receives. Permission mode was "Approve for me", and the card still asked. Nothing reached the server before the click; afterwards it received the exact attached bytes (sha256 match), and the model answered from the tool result.

Approval card and answer in a live chat

Same setup over the API: Deny sends nothing; an echoing tool returns you sent [attached image]; editing the server while the card is open returns "the MCP server changed after the image was approved" and sends nothing; a call without mcp_image gets "no approved image to send".

chatgpt-codex-connector[bot]

This comment was marked as outdated.

@Etherll Etherll changed the title Studio: Add approved image attachment inputs for MCP tools Add approved image attachment inputs for MCP tools Sep 13, 2026
chatgpt-codex-connector[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants