MatlabMCP Deployment Patterns: Persistent Server vs. On-Demand Execution
MatlabMCP supports two distinct deployment patterns—a persistent server that maintains a long-running MATLAB session for high-throughput workloads, and an on-demand invocation that launches the server only when needed to conserve license and memory resources.
The jigarbhoye04/matlabmcp repository implements a Model Context Protocol (MCP) server that bridges large language models with MATLAB. Understanding the correct MatlabMCP deployment patterns is essential for optimizing resource utilization, whether you need continuous availability for automated pipelines or occasional access for desktop assistance.
Understanding the Two Deployment Patterns
Persistent Server Mode
In persistent server mode, main.py runs continuously as a background process, maintaining an active connection to a shared MATLAB engine. This pattern eliminates startup latency for successive requests because the MATLAB session remains alive between calls.
You initiate this pattern by running python main.py or uv run main.py directly, or by using a process manager like systemd, supervisord, or Docker to handle restarts and boot-time initialization. This approach suits high-throughput environments where multiple LLM agents repeatedly query MATLAB, such as CI/CD pipelines or shared research servers.
On-Demand Invocation Mode
In on-demand mode, the server process starts only when a client requires MATLAB functionality and terminates immediately after request completion. This pattern conserves MATLAB licenses and system RAM by releasing resources when idle.
Configure this pattern by pointing your MCP client (such as Claude Desktop) to invoke uv run main.py for each MATLAB-related query. The client launches the process, handles the request, and exits, making this ideal for desktop users who occasionally need MATLAB assistance or environments where continuous MATLAB processes would waste resources.
Technical Differences Between Patterns
Engine Connection Lifecycle
The connection logic in main.py (lines 25-44) determines how the server attaches to MATLAB. The script calls matlab.engine.find_matlab() to locate shared sessions before establishing the link.
- Persistent: You start MATLAB once using
matlab.engine.shareEngineand keep it running indefinitely. The server connects to this existing session at startup and maintains the binding throughout its lifecycle. - On-demand: Each invocation spawns a fresh MATLAB process. The server connects to a new engine instance for every request, then shuts down both the MATLAB process and the Python server when the client disconnects.
State Retention Characteristics
Workspace persistence differs significantly between the two patterns.
A persistent server retains the MATLAB workspace across multiple requests. Variables created during one tool invocation remain accessible to subsequent calls, enabling stateful workflows where previous computations inform future queries.
An on-demand deployment creates a fresh workspace for every invocation. Variables defined in one request do not persist to the next, ensuring clean state isolation but requiring scripts to be self-contained.
Resource and License Implications
The deployment choice directly impacts resource consumption and licensing.
Persistent mode consumes RAM and holds a MATLAB license continuously, even during idle periods. The trade-off is zero startup latency for incoming requests, typically saving several seconds per call.
On-demand mode releases the MATLAB license and frees memory when not actively processing requests. However, each invocation incurs a startup penalty (approximately seconds) while MATLAB initializes and the engine connection establishes.
Implementation Examples
Persistent Server with systemd
For Linux servers, configure a systemd service to manage the persistent process.
Create /etc/systemd/system/matlab-mcp.service:
[Unit]
Description=MATLAB MCP persistent server
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/matlab-mcp
ExecStart=/usr/bin/uv run main.py
Restart=on-failure
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl start matlab-mcp
sudo systemctl enable matlab-mcp
The service keeps main.py running indefinitely, preserving the MATLAB engine connection across system reboots and process failures.
Persistent Server with Docker
Containerize the application for consistent deployment across environments.
FROM python:3.12-slim
WORKDIR /app
COPY . /app
RUN apt-get update && apt-get install -y wget && \
pip install uv && \
uv pip sync
CMD ["uv", "run", "main.py"]
Build and run the container:
docker build -t matlab-mcp .
docker run -d --name matlab-mcp \
-v /usr/local/MATLAB/R2023a:/usr/local/MATLAB/R2023a \
matlab-mcp
The container maintains the persistent server, assuming the host MATLAB installation is mounted at the appropriate path.
On-Demand Configuration for Claude Desktop
Configure Claude Desktop to invoke MatlabMCP only when needed by modifying the MCP server configuration:
{
"mcpServers": {
"MatlabMCP": {
"command": "C:\\Users\\username\\.local\\bin\\uv.exe",
"args": [
"--directory",
"C:\\Users\\username\\Desktop\\MatlabMCP\\",
"run",
"main.py"
]
}
}
}
Claude Desktop launches this command exclusively when processing MATLAB-related queries, terminating the process immediately after completion.
Ad-Hoc On-Demand Shell Script
Create a wrapper script for single-request invocations from the command line:
#!/usr/bin/env bash
# run-matlab-mcp.sh – start, handle one request, then exit
cd "$(dirname "$0")"
uv run main.py
Execute with a piped request:
echo '{"tool":"runMatlabCode","args":{"code":"disp('Hello')"}}' | ./run-matlab-mcp.sh
The script starts the server, processes the single JSON-RPC request, and exits, releasing all MATLAB resources.
Summary
- Persistent deployment runs
main.pycontinuously via process managers or containers, maintaining an active MATLAB engine connection for high-frequency requests and stateful workspace persistence. - On-demand deployment launches
main.pyper request through MCP client configurations or shell scripts, conserving licenses and memory for infrequent usage at the cost of startup latency. - The engine connection logic in
main.py(lines 25-44) usesmatlab.engine.find_matlab()to attach to shared sessions, with behavior varying based on whether MATLAB is already running. - Resource trade-offs involve choosing between continuous license/RAM consumption with instant response times versus on-demand resource release with initialization overhead.
Frequently Asked Questions
How do I choose between persistent and on-demand deployment?
Select persistent deployment when running automated pipelines or serving multiple users who need rapid, repeated MATLAB access throughout the day. Choose on-demand deployment for personal desktop use where MATLAB assistance is sporadic, or in environments with strict license constraints that prohibit idle MATLAB sessions.
Can I switch between patterns without modifying the source code?
Yes. Both patterns use the same main.py entry point. To switch from on-demand to persistent, simply run uv run main.py directly or configure a process manager instead of having your MCP client launch the command. The code automatically detects existing shared MATLAB sessions via matlab.engine.find_matlab() regardless of how the server was started.
What happens to MATLAB variables when using on-demand mode?
Variables are not retained between requests in on-demand mode. Each invocation creates a fresh MATLAB process with an empty workspace. If your workflow requires variable persistence across multiple tool calls (such as using getVariable after runMatlabCode), you must use persistent deployment to maintain the workspace state.
Does persistent mode require a dedicated MATLAB license?
Yes. Persistent mode holds a MATLAB license continuously while the server runs, even during idle periods between requests. This is necessary to keep the shared engine session active for instant response. On-demand mode releases the license immediately after each request completes, making it more suitable for environments with floating license pools or strict usage limits.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →