MCP integration#
VisualTorch ships an optional, client-neutral Model Context Protocol (MCP) server. Any MCP host that supports local stdio servers can use it to inspect VisualTorch’s capabilities and generate static PNG diagrams or animated GIF reveals from PyTorch model source. The integration is part of VisualTorch itself; it does not depend on a client-specific plugin or extension.
Installation#
Install VisualTorch and its MCP dependency in the Python environment that should perform renders:
python -m pip install "visualtorch[mcp]"
For a source checkout, use an editable install:
python -m pip install -e ".[mcp]"
Both commands install the visualtorch-mcp console script. Confirm that the same environment is
active when starting it:
visualtorch-mcp
The process communicates only through MCP messages on standard input and standard output. It does
not start an HTTP server. Diagnostic and model-generated output is kept off the protocol stream.
The equivalent module entry point is python -m visualtorch_mcp.
Connect an MCP host#
Register visualtorch-mcp as a stdio server in your MCP host. Configuration formats vary, but a
typical entry has this shape:
{
"mcpServers": {
"visualtorch": {
"command": "visualtorch-mcp"
}
}
}
If the console script is not on the host’s PATH, launch the module with the exact interpreter
where VisualTorch is installed:
{
"mcpServers": {
"visualtorch": {
"command": "/absolute/path/to/python",
"args": ["-m", "visualtorch_mcp"]
}
}
}
On Windows, the interpreter commonly ends in Scripts/python.exe; on POSIX systems it commonly
ends in bin/python.
Discover capabilities first#
Call visualtorch_capabilities before constructing a request when the host can do so. It returns a
machine-readable description of:
the MCP schema version, installed VisualTorch version, stdio transport, and subprocess model;
accepted
input_shapeforms;canonical styles, aliases, and the options supported by each style;
named palettes;
static and animation output formats; and
the available tools and the trusted-code requirement.
Pass an optional style or alias to limit the response to one canonical style. The same data is
available as JSON text from the visualtorch://capabilities resource.
Tools#
visualize_model#
Render a static PyTorch architecture diagram as a PNG.
animate_model#
Render an animated GIF that reveals the architecture one column at a time. It accepts the shared
model and output parameters. Set its animation timing inside options:
frame_duration: milliseconds for each intermediate frame (default600);final_hold_duration: milliseconds to hold the completed diagram (default1500); andloop: whether the GIF repeats (defaulttrue).
visualtorch_capabilities#
Return the structured capabilities described above. Its optional style argument accepts a
canonical name or compatibility alias.
visualtorch_reference#
Return links to the upstream VisualTorch API reference and examples. Its optional style argument
filters the links to one style.
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.
A successful tool result is structured data containing at least:
Field |
Meaning |
|---|---|
|
Absolute path to the generated file. |
|
|
|
Canonical style used for the render. |
|
MIME type ( |
|
Pixel dimensions. |
|
Generated file size. |
Static results also report the Pillow mode when available. Animation results additionally report
frame_count, durations_ms, and loop, read back from the generated GIF. Hosts should use the
returned metadata instead of inferring format, dimensions, or timing from the request.
Resources#
URI |
Contents |
|---|---|
|
Complete machine-readable capabilities as JSON text. |
|
Links to VisualTorch documentation and examples. |
|
Backward-compatible API reference links. |
|
Installed VisualTorch package version and MCP API schema version. |
Examples#
Static graph request:
{
"source": "import torch\nmodel = torch.nn.Sequential(torch.nn.Flatten(), torch.nn.Linear(4, 2))",
"input_shape": [1, 1, 2, 2],
"style": "graph",
"output_path": "architecture.png",
"options": { "show_neurons": false, "palette": "okabe_ito" }
}
Animated flow request:
{
"source": "import torch\nmodel = torch.nn.Sequential(torch.nn.Flatten(), torch.nn.Linear(4, 2))",
"input_shape": [1, 1, 2, 2],
"style": "flow",
"output_path": "architecture.gif",
"options": {
"frame_duration": 300,
"final_hold_duration": 1000,
"loop": true
}
}
model_expression can name a factory or constructor instead of a pre-built variable:
{
"source": "import torch\nclass Net(torch.nn.Module):\n def forward(self, x):\n return x.relu()",
"model_expression": "Net()",
"input_shape": [1, 4]
}
Errors and process isolation#
Requests are validated before a worker starts. Invalid styles, shapes, path traversal, working 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.
Security: trusted source only#
The server intentionally executes source and evaluates model_expression as Python code. The
worker subprocess is a reliability boundary, not a security sandbox: supplied code can read or
modify files, access the network, start processes, and use the current user’s permissions. Connect
the server only to MCP hosts you trust, review generated tool arguments when the host requests
approval, and render only source you trust. For stronger isolation, run the entire MCP server in a
separately secured operating-system account, container, or virtual machine with limited filesystem
and network access.
VisualTorch deliberately provides this portable stdio MCP surface without bundling configuration, authentication, branding, or packaging for any particular MCP host.