Repository navigation
Conversation
Address the code-review findings for the MCP server: - Resolve the default output directory per call under the home directory (~/visualtorch_outputs) instead of freezing the host's cwd at import time. Relative output_dir resolves against workdir (or the default directory), and workdir must be absolute. - Swap only real image extensions for .png/.gif so dotted names such as "resnet-1.5" or "model_v1.1" are no longer truncated, and keep the case of a matching extension. - Only validate output_dir when an output path actually uses it. - Create staging files with mode 0666 so the umask applies (no more 0600 artifacts), and keep an existing artifact's mode when replacing it. - Move the staging file into place in the server only after a successful, parsed worker result; the worker exits with os._exit() right after writing its result so lingering threads or atexit hooks cannot stall it. - Give the worker a watchdog that stops it (and, on POSIX, its process group) and removes its staging file when the server dies or the deadline passes. - Start the worker in a private empty directory so modules in the host's cwd cannot shadow installed packages. - Bound every wait in the timeout/cancel cleanup path. - Report SystemExit/KeyboardInterrupt from model source clearly, and hide the worker's argv from model source. - Treat null animation controls as "use the default", require durations of at least 20 ms, and round them to GIF's 10 ms resolution. - Write no NETSCAPE loop extension for loop=False so the GIF plays once (Pillow's loop=1 meant "repeat once"), and report loop accordingly. - Keep the render() page when visualtorch_reference filters by style. - Raise the mcp extra's floor to 1.3.0, the first release whose FastMCP accepts instructions=.
Follow-up to the review of the previous commit: - Wait for the job thread when an MCP call is cancelled, so a job that had already passed its last cancellation check cannot replace the output after the cancellation was acknowledged. - Start a replacement's staging file with the existing file's mode, so a private artifact is never readable while it is staged. - Terminate daemonic multiprocessing children before os._exit(), as normal interpreter shutdown would. - Only let the watchdog delete files named like reserved staging files that are not the output path; mark server-published jobs with an explicit parent_publishes flag so direct invocations that pass staging_path still publish their artifact. - Treat a Windows parent that no longer exists as gone, and stop the worker's whole process tree with taskkill when it aborts itself on Windows. - Run model source in the output directory when no workdir is given, so side files persist and no child process pins the scratch directory. - Resolve relative PYTHONPATH entries against the server's directory. - Reject Windows drive-relative and root-relative paths, validate output_dir only when it is used (including blank strings), and recognise more image extensions. - Report the size of the file this job wrote, and do not let a failed staging cleanup replace the job's real error. - Cap animation durations at 655350 ms, the largest GIF delay. - Keep loop=False playing once for APNG and WebP, whose PIL default loops. - Raise the mcp floor to 1.22.0: earlier releases import a pydantic internal that current pydantic no longer has.
Tariolle
marked this pull request as ready for review
December 2, 2026 15:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This fixes all 14 findings from a code review of
visualtorch_mcp/: 8 important, 2 pre-existing and 4 nits. Each one has a regression test.Output paths
~/visualtorch_outputs, so it is never/visualtorch_outputs(read-only on macOS GUI hosts) or inside an app's install folder. A relativeoutput_dirresolves againstworkdirif one is given, otherwise against~/visualtorch_outputs. A relativeworkdiris rejected with a clear error.diagram.jpgbecomesdiagram.png,resnet-1.5becomesresnet-1.5.png, andmodel_v1.1andmodel_v1.2stay separate files. A matching extension keeps its case (Diagram.PNG).output_diris validated only when it is used (Update README.md #14). An absoluteoutput_pathignores a staleoutput_dir.0666, so the umask applies, instead ofmkstemp's0600. When a file is replaced, it keeps the existing file's mode. New code is invisualtorch_mcp/_files.py.Worker lifecycle
A failed job can no longer overwrite the user's file (Fix incorrect module and parameter #4). The worker now leaves the verified artifact in staging. The server moves it into place only after it has parsed a successful result and checked for cancellation. The worker also calls
os._exit()right after writing its result, so non-daemon threads oratexithooks left by model source cannot stall it until the timeout.An orphaned worker stops itself (Update README.md and Change Default Parameters #3). The payload now carries
parent_pidandtimeout_seconds, and a watchdog thread in the worker stops it when either check fails:getppid()toparent_pid. Windows holds aSYNCHRONIZEhandle on the server process, so a reused PID cannot fool the check.On POSIX the worker also kills its own process group, which takes model-spawned children with it, and it removes its staging file.
PR_SET_PDEATHSIGwas ruled out because it fires when the spawning thread exits, and anyio worker threads are recycled.The worker can no longer import code from the host's cwd (Add unit tests #6). It starts in a private, empty temp directory, which also holds the payload. A
json.py,inspect.pyorvisualtorch.pyin the host's cwd therefore cannot shadow installed packages. The worker stillchdirs intoworkdirwhen one is given.Timeout and cancel cleanup is bounded (Update python version #8). After
kill(), the final wait has a timeout and the error is raised regardless. On POSIX the protocol pipe is also closed. It is not closed on Windows, because acommunicate()reader thread may still hold the buffer lock there.sys.exit()in model source gets a clear error (Update version to 0.1.1 #12).SystemExitandKeyboardInterruptnow produce an explicit error instead ofinvalid JSON: ''.sys.argvis reset before model source runs, so a module-levelargparsecall works.Animation options
nullanimation controls select the default (Add more usage examples #7). This coversframe_duration,final_hold_durationandloop.minimum: 20,maximum: 655350andresolution: 10.loop=falsenow plays once (Add a simple unit test and pytest workflow #9, upstreamvisualtorch/_animation.py). The GIF is written without a NETSCAPE extension. Pillow'sloop=1meant "repeat once", so the reveal played twice. APNG and WebP, which direct Python callers can request, still getloop=1: they store a play count, and Pillow's default for them is to loop forever. Worker metadata now reportsloop: falsefor such GIFs.Other
visualtorch_reference(style=...)keeps the Render page (Add badges #13). The index and examples pages are kept as well.mcpextra floor is raised to>=1.22.0(Add publish workflow #10). 1.0.x and 1.1.x have nomcp.server.fastmcp, and 1.2.x'sFastMCPdoes not acceptinstructions=(1.3.0 is the first that does). But every release before 1.22.0 imports a pydantic internal (eval_type_backport) that current pydantic no longer has, and none of them cap pydantic. A fresh install of any of them therefore cannot start the server. I checked each release against pydantic 2.14.4.get_started/mcp.md) are updated for the new path rules, the duration limits and the null handling.Behavior changes to be aware of
output_dir, output now goes to~/visualtorch_outputsinstead of<server cwd>/visualtorch_outputs.workdiris now rejected.frame_durationandfinal_hold_durationbelow 20 are now rejected (previously minimum 1). The tests that used1now use20.workdiris given, model source runs in the output file's directory instead of the host's cwd.C:images) and root-relative (\images)output_dir/output_pathvalues are rejected.mcpextra now needsmcp>=1.22.0.Review follow-up (second commit)
An independent review of the first commit found these issues. All of them are fixed, with regression tests that fail on the first commit:
0600artifact staged the new content as0644. A replacement's staging file now starts with the existing file's mode.os._exit()skipped multiprocessing's shutdown, so daemonic children of model source survived successful jobs. They are now terminated first.staging_paththat the worker had not validated yet. It now deletes only files named like reserved staging files, never the output path. Server-published jobs are marked with an explicitparent_publishesflag, so a direct invocation that passesstaging_pathstill publishes its artifact.taskkill /T).workdir, or in the output directory when there is none.PYTHONPATHentries. These resolved against the scratch directory. They are now made absolute against the server's directory, so the worker imports the same code as the server.output_diris ignored when it is unused. More image extensions are recognised (.avif,.heic,.jxl, …).bytesnow reports the file this job wrote, even if another writer replaces the path right afterwards.mcpfloor was raised as described above.Not changed:
chmod. That attacker could already replace the output itself.Testing
sleep 300. The worker and the grandchild were both gone within 0.5 s and the staging file was removed. A real render underumask 022produced a0644PNG.mcp==1.22.0+ pydantic 2.14.4:tests/test_mcp.pypasses (88 passed, 3 skipped), including the stdio end-to-end and cancellation tests.ruff@0.15.22check/format andmypy@2.3.0are clean. markdownlint and prettier pass on the docs, checked with LF line endings.