Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions docs/source/markdown/get_started/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ model and output parameters. Set its animation timing inside `options`:

- `frame_duration`: milliseconds for each intermediate frame (default `600`);
- `final_hold_duration`: milliseconds to hold the completed diagram (default `1500`); and
- `loop`: whether the GIF repeats (default `true`).
- `loop`: whether the GIF repeats forever (default `true`); `false` plays it once.

GIF stores frame delays in 10 ms steps, so durations are rounded to the nearest 10 ms. Durations
must be between `20` and `655350`: viewers slow shorter delays down to about 100 ms, and GIF cannot
store longer ones. Passing `null` for any of these controls selects its default.

### `visualtorch_capabilities`

Expand All @@ -114,9 +118,9 @@ filters the links to one style.
| `style` | string | no | `graph`, `flow`, or `lenet`; defaults to `graph`. |
| `model_expression` | string | no | Expression evaluated after `source`; defaults to `model`. |
| `output_path` | string | no | Output filename or path. Relative paths are resolved below `output_dir`. |
| `output_dir` | string | no | Base directory for generated output. |
| `output_dir` | string | no | Base directory for output; defaults to `~/visualtorch_outputs`. |
| `options` | object | no | Style-specific VisualTorch options advertised by capabilities. |
| `workdir` | string | no | Existing working directory made available to imports in `source`. |
| `workdir` | string | no | Existing absolute directory made available to imports in `source`. |
| `timeout_seconds` | integer | no | Worker timeout in seconds; defaults to `120` and must be between `1` and `600`. |

Use a flat array for one model input:
Expand Down Expand Up @@ -159,10 +163,25 @@ validates.

## Output paths and metadata

If `output_path` is omitted, the server creates a unique filename below `output_dir` (or its
default output directory). The server normalizes static output names to `.png` and animation output
names to `.gif`. It creates missing parent directories, rejects relative paths that escape
`output_dir`, and validates paths before launching the worker.
If `output_path` is omitted, the server creates a unique filename below `output_dir`. Without
`output_dir`, output goes to `visualtorch_outputs` in the user's home directory. Relative paths never
depend on the directory the MCP host started the server in, since hosts choose it themselves and it
is often read-only:

- a relative `output_path` is resolved below `output_dir`;
- a relative `output_dir` is resolved below `workdir` when one is given, and below
`~/visualtorch_outputs` otherwise;
- `workdir` must be an absolute path (a leading `~` is expanded); and
- Windows drive-relative (`C:images`) and root-relative (`\images`) paths are rejected.

Model source runs in `workdir` when one is given, and otherwise in the output file's directory.

Static outputs always end in `.png` and animations in `.gif`. An existing image extension is
replaced (`diagram.jpg` becomes `diagram.png`), while any other dotted name keeps its dots
(`resnet-1.5` becomes `resnet-1.5.png`). The server creates missing parent directories, rejects
relative paths that escape `output_dir`, and validates paths before launching the worker. The new
file replaces an existing one only after the render has succeeded, and it keeps the existing file's
permissions.

A successful tool result is structured data containing at least:

Expand Down Expand Up @@ -234,7 +253,8 @@ Requests are validated before a worker starts. Invalid styles, shapes, path trav
directories, options, and timeouts produce actionable MCP tool errors. Model import, evaluation,
tracing, and rendering happen in a separate subprocess with a timeout; a failed render therefore
does not terminate the long-running MCP server. Each tool call returns only after its output file
has been written and inspected for metadata.
has been written and inspected for metadata. If the MCP server itself exits while a render is
running, the worker notices and stops on its own.

## Security: trusted source only

Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def _read_requirements(file: str) -> list:
install_requires=_read_requirements("requirements.txt"),
extras_require={
"dev": _read_requirements("docs/requirements.txt") + _read_requirements("dev-requirements.txt"),
"mcp": ["mcp>=1.0.0,<2"],
# FastMCP accepts instructions= from 1.3.0, but releases before 1.22.0 import a pydantic
# internal that current pydantic no longer has, without capping pydantic, so a fresh
# install of them cannot start the server.
"mcp": ["mcp>=1.22.0,<2"],
},
entry_points={"console_scripts": ["visualtorch-mcp=visualtorch_mcp.server:main"]},
python_requires=">=3.10",
Expand Down
26 changes: 24 additions & 2 deletions tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import pytest
import torch
from PIL import Image, ImageSequence
from PIL import Image, ImageSequence, features
from torch import nn
from visualtorch import animate
from visualtorch.backend import extract_architecture
Expand Down Expand Up @@ -284,7 +284,29 @@ def test_loop_false_plays_once(style: str, sequential_model: nn.Module, tmp_path
out_path = tmp_path / "out.gif"
animate(sequential_model, (1, 3, 8, 8), style=style, to_file=str(out_path), loop=False)
with Image.open(out_path) as saved:
assert saved.info.get("loop") == 1
# No NETSCAPE extension at all: a loop count of 1 would make browsers play it twice.
assert "loop" not in saved.info


@pytest.mark.parametrize(
("suffix", "loop", "expected_loop"),
[(".png", False, 1), (".png", True, 0), (".webp", False, 1), (".webp", True, 0)],
)
def test_loop_for_other_animated_formats(
suffix: str,
loop: bool,
expected_loop: int,
sequential_model: nn.Module,
tmp_path: object,
) -> None:
"""APNG and WebP store a play count, so `loop=False` must write 1 rather than PIL's 0 default."""
if suffix == ".webp" and not features.check("webp"):
pytest.skip("Pillow was built without WebP support")
out_path = tmp_path / f"out{suffix}"
animate(sequential_model, (1, 3, 8, 8), style="graph", to_file=str(out_path), loop=loop)
with Image.open(out_path) as saved:
assert getattr(saved, "n_frames", 1) > 1
assert saved.info.get("loop") == expected_loop


@pytest.mark.parametrize("style", _STYLES)
Expand Down
Loading