mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#3992
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/2343
Author: @dsvi
Created: 10/14/2026
Status: 🔄 Open
Base:
main← Head:mcp-image-results📝 Commits (3)
2d629cdMCP: a picture a tool returns reaches the model as a picture (#2130)0b195cfserve: read only a PNG's or a JPEG's magic bytes in a tool's text (#2343)e14b8ccserve: a reader left on the cleaned-away work folder is replaced, not only re-made📊 Changes
9 files changed (+619 additions, -47 deletions)
View changed files
📝
docs/DETAILS.md(+21 -1)📝
serve/mcp.py(+97 -23)📝
serve/mcp_fake_server.py(+22 -3)📝
serve/server.py(+97 -14)📝
serve/test_mcp.py(+86 -3)📝
serve/test_server.py(+126 -0)➕
serve/test_tool_text_images.py(+154 -0)📝
serve/web/app.css(+2 -0)📝
serve/web/app.js(+14 -3)📄 Description
Problem and result
This allows model to request images through MCP and analyze them with its visual part.
Closes #2130.
1. A picture an MCP tool returned never reached the model. An MCP tool result that carries an
imageblock reached the model as a sentence about a picture:serve/mcp.py'sresult_text()joined the content blocks into one string, so a server that reads image files, takes screenshots or renders charts could hand a picture back and the model never saw it. The model can already ask for the picture with an MCP call - that is what #2130 asks for - only the way back was missing. Now the picture goes to the vision encoder and into the prompt where the server put it.2. A client that runs the MCP server itself hands the picture back as text, and it blew the context. Chatbox (and the rest built on the AI SDK) never touches Strata's MCP path, and for
/v1/chat/completionsthe SDK JSON-stringifies a tool'scontentoutput, so the base64 of the picture lands inside the tool message. From a real Chatbox conversation against a 262144-token context:Before: a 400, and the conversation dies on the turn after the picture. After: the picture is taken out of the text and given to the encoder, and the turn answers.
3. A picture reader left running for long enough stopped reading any picture at all. The reader's work folder is made once, at start. A temp cleaner removes it while the server runs, and #3370 (fixed in
76186bb5) made the folder again - which covers a reader that was unloaded. A reader that is still running keeps its cwd on the deleted inode, so the folder being back does not help it: theENClines name files relative to the cwd (#480). Before: every picture after the loss fails. After: the reader is replaced and the picture is encoded.Related discussion
#2130 - the request itself.
#3370 (closed, fixed on
main) - the same folder, the unloaded case. This PR adds the running-reader case on top of_ensure_dir, it does not replace it.@qiuzan2001's review on this PR found a false positive in the text lifting (a 2-byte
BMread as a bitmap). Reproduced, fixed and covered by a test in0b195cfa; see Limits.Approach
1. Strata's own MCP path -
serve/mcp.py.result_text()becameresult_parts(): the same text as before, plus - only when the result carried pictures - that same content in the prompt's own part shape ({"type": "image", "source": "data:…"}, whichserve/frontend.py's_parts_ofandchat_template.jinjaalready consume).McpHub.callalso returnspartsandimages, andrun_with_mcpappendsr["parts"] or r["text"]. Block order and the newlines between blocks survive, so the picture sits where the server put it. A result with no pictures produces byte-identical prompt text:partsisNoneand the tool message stays the plain string it was.MAX_IMAGE_BYTESisserve/server.py'sIMAGE_URL_MAX, the chat's own image limit). Past a cap the picture is named in the text, as it always was.max_result_charscaps text only.resourceblock whoseblobhas animage/*mime counts as a picture (the same base64, another wrapper).audio,resource_linkand error results stay text, and Strata still never opens a path an MCP server names.[image omitted: …]in its place and the chat goes on - the existing_note_unreadable_tool_imagespath, not a new one.localStorage(it is megabytes).2. A picture a client hands back as text -
serve/server.py.Service._lift_pictures_from_tool_text, at the top ofprepare(): a base64 run of 1024 characters or more in a tool's string content that decodes to a PNG or JPEG magic (or a RIFF/WEBP header) is taken out of the text and given to the encoder as an image part, with[image (image/png, 1,528,438 bytes)]left in the text where it was. Same caps as one MCP result. Onlyrole: "tool"string content is read - a user who pastes base64 means the characters - and a client that sends the picture as a real image part (an Anthropictool_result, an OpenAIimage_url) never needed this and is untouched.3. The reader left on a deleted folder -
serve/server.py._ensure_dir()(#3370) now also takes a fresh scratch folder when the old path cannot be made again - its parent is gone, or it is not writable - instead of raising, and returns whether it remade anything.encode()uses that: when the folder had to be made again and a reader is alive, it is replaced, because its cwd is the inode that was deleted and not the folder just made.Compatibility: no C++ is touched, so no CUDA / HIP / SYCL / Metal rebuild.
/v1/messagesand/v1/responsesdo not run MCP, so nothing there changes;GET /mcpis unchanged.Validation
All of it on
e14b8cc7(this branch, rebased onmainat011c9e60), Linux, Python 3.14.8. The repository's CI does not run the Python suite, so these are local runs.Passed:
python -m unittest serve.test_tool_text_images serve.test_mcp->Ran 37 tests / OK(29 + 8; 36 of them are this PR's).python -m unittest serve.test_server.VisionWorkDirDeleted->Ran 6 tests / OK: the image after the loss is encoded again, the cache of.svefiles that went with the folder is dropped, a reader left on the deleted cwd is replaced, a stopped reader is not started by the recovery, starting the reader makes a missing folder (and the test asserts thecwdthe reader was actually handed), and a folder that cannot be remade falls back to a fresh one.origin/main(011c9e60) against a stand-in reader speaking theENC/OKprotocol:encode()across a removed folder fails there withthe image could not be read: Unable to open file b2917681b4f4fee3ef345ae33557fbe7.img: No such file or directoryand restarts nothing, although the folder exists again; on this commit the same call restarts the reader once and encodes.tools/strata_tokenizer.pyoverpacks/iq3_s/tokenizer): the tool message is 2,038,158 characters = 1,486,521 tokens as text; after the lift, 120 tokens of text + 1 picture, the base64 replaced by[image (image/png, 1,528,438 bytes)].lifted=1and the tool text becomesresult: [image (image/bmp, 3,016 bytes)]plus an image part; with the 2 it now has,lifted=0, kindNone, text byte for byte as the client sent it.python -m unittest discover -s . -p "test_*.py"->Ran 947 tests in 342.385s,FAILED (failures=2, skipped=18). Both failures -serve.test_keepawake.test_service_holds_for_the_whole_request_including_early_closeandserve.test_lifeline.test_the_portable_watcher_ends_the_child_when_the_server_is_killed- reproduce on plainorigin/main(011c9e60) in a separate worktree (Ran 12 tests / FAILED (failures=2)for those two modules), so they are not from this change.Failed:
test_keepawakeandtest_lifeline), both of which reproduce on plainmainon this box.Not run:
imgMCP server, the web app) on this revision. It was run on an earlier revision of this branch (ca1c1f7, onmainat296b59d) on an RTX 3090, 24 GB, Qwen3.8-Flash-Next IQ3_S + mmproj: theimgserver'sread_imageon a 4320x2432 JPEG (resized by that server to a 640x360 PNG, 455 KB) arrived through the hub as one picture with parts in the order['text', 'text', 'image'], and a red 160x120 PNG sent as an Anthropictool_resultimage block was answered "Red" at 142 input tokens. That evidence is forca1c1f7, note14b8cc7.strata-visionprocess: the reproduction above uses a stand-in reader, and the 53 log lines are from the real one on Linux.Limits
e14b8cc7) comes off into its own PR, it touches different lines and needs no rebase.base64(b"BMW service log " + b"x" * 3000)begins with a bitmap's 2-byteBM, and the log line was taken away from the model and handed to the encoder as a 3,016-byte "bitmap". Checking the BMP header would not make a 2-byte magic safe to guess at in ordinary text, and a GIF's 6 bytes are not worth extra rules either. A BMP or GIF a client sends as a real image part still reaches the encoder:Vision.normalizepasses both through to stb_image, as it always did.MAX_RESULT_IMAGES(8) andMAX_IMAGE_BYTES(32 MiB) are constants, not config settings.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.