-
Notifications
You must be signed in to change notification settings - Fork 0
FAQ
The MATLAB MCP Server is a Python-based Model Context Protocol (MCP) server that bridges AI coding agents (Claude, Cursor, Copilot) to MATLAB. It lets agents execute MATLAB code, discover toolboxes, check code quality, view interactive plots, and manage files — all through the MCP protocol. See the README for a full overview.
- MATLAB: R2022b and later with the Engine API for Python installed
- Python: 3.10, 3.11, 3.12
- Platforms: Windows 10/11, macOS 11+, Linux (Ubuntu 20.04+)
This project is licensed under the MIT License. See LICENSE for details.
# Navigate to your MATLAB installation's Python engine directory
cd /Applications/MATLAB_R2024a.app/extern/engines/python # macOS
# cd "C:\Program Files\MATLAB\R2024a\extern\engines\python" # Windows
# Install into your Python environment
pip install .On Windows, the install.bat one-click installer handles this automatically.
| Transport | Use Case | Setup |
|---|---|---|
| stdio | Single user, development, local testing | Simplest — agent launches server directly |
| streamablehttp | Multiple users, remote agents, Codex CLI | Requires reverse proxy for production; recommended for new deployments |
| SSE | Multiple users, backward compat (deprecated) | Works but being phased out in favor of streamablehttp |
Recommendation: Use streamablehttp for new setups. It's the modern standard and required for Codex CLI support.
Use the one-click installer:
git clone https://github.com/HanSur94/matlab-mcp-server-python.git
cd matlab-mcp-server-python
install.batThe installer creates a virtual environment, installs everything from bundled wheels (fully offline), and auto-detects your MATLAB installation. No admin rights needed. See docs/windows-deployment.md for details.
Yes. The project includes Dockerfile and docker-compose.yml. The Docker image does not include MATLAB — you must mount your local MATLAB installation as a volume:
docker-compose upSee Installation for Docker configuration examples.
Run the server with the --generate-token flag to print a random 64-character hex token:
matlab-mcp --generate-token
# Output:
# Bearer token (64 hex chars):
# 3f7a9c2b1e8d5f4a6c9b2e1a7d3f9c5b8a2d4e6f7c9a1b3e5f7d9a2c4b6e8
#
# Export on POSIX (bash/zsh/fish):
# export MATLAB_MCP_AUTH_TOKEN=3f7a9c2b1e8d5f4a6c9b2e1a7d3f9c5b8a2d4e6f7c9a1b3e5f7d9a2c4b6e8
#
# Export on Windows (cmd.exe):
# set MATLAB_MCP_AUTH_TOKEN=3f7a9c2b1e8d5f4a6c9b2e1a7d3f9c5b8a2d4e6f7c9a1b3e5f7d9a2c4b6e8
#
# Export on Windows (PowerShell):
# $env:MATLAB_MCP_AUTH_TOKEN='3f7a9c2b1e8d5f4a6c9b2e1a7d3f9c5b8a2d4e6f7c9a1b3e5f7d9a2c4b6e8'Then set the environment variable and start the server:
export MATLAB_MCP_AUTH_TOKEN=3f7a9c2b1e...
matlab-mcp --transport streamablehttp| User Count | Recommended Pool |
|---|---|
| Personal (1 user) | 1–2 engines |
| Small team (2–5 users) | 2–4 engines |
| Larger team (5+ users) | Scale to license limit; typically 4–8 |
Configure via config.yaml:
pool:
min_engines: 2 # Start with 2
max_engines: 10 # Scale up to 10 if neededOr via environment variable:
export MATLAB_MCP_POOL_MIN_ENGINES=2
export MATLAB_MCP_POOL_MAX_ENGINES=10- Memory: 500MB–2GB per engine (varies by loaded toolboxes)
- CPU: Minimal idle; full core usage during execution
- Startup time: 10–30 seconds per new engine (first startup is slower)
- New requests queue up (default
queue_max_size: 50) - If the pool hasn't reached
max_engines, a new engine starts automatically - Requests are served FIFO as engines become available
- If queue is full, requests receive an error
Adjust via config:
pool:
max_engines: 15 # Higher limit for scaling
queue_max_size: 100 # Larger queueLong jobs (>30s) are auto-promoted to async execution. Tune these settings:
execution:
sync_timeout: 30 # Threshold for auto-async promotion
max_execution_time: 86400 # Max job runtime (24 hours)
job_retention_seconds: 86400 # Keep results for 24 hoursAgents can poll job status with get_job_status while it's running. For progress updates, use mcp_progress() in your MATLAB code. See Async Jobs for details.
Add to ~/Library/Application Support/Claude/claude_desktop_config.json (macOS) or %APPDATA%\Claude\claude_desktop_config.json (Windows):
{
"mcpServers": {
"matlab": {
"command": "matlab-mcp"
}
}
}Then restart Claude Desktop. See Installation for full setup.
claude mcp add matlabFor custom settings (e.g., different transport), use:
claude mcp set matlab --command "matlab-mcp --transport streamablehttp"Add to .cursor/rules or your Cursor config:
{
"mcpServers": {
"matlab": {
"command": "matlab-mcp",
"args": ["--transport", "streamablehttp"]
}
}
}Codex CLI requires streamable HTTP transport. See docs/agent-onboarding.md for the exact connection config with bearer token authentication.
Yes. The server supports stdio mode (default) which is CI-friendly:
# In your CI step
export MATLAB_MCP_POOL_MIN_ENGINES=1
export MATLAB_MCP_EXECUTION_SYNC_TIMEOUT=120
matlab-mcp --transport stdioOr containerize it:
docker build -t matlab-mcp .
docker run -e MATLAB_MCP_AUTH_TOKEN=... matlab-mcpThe server exposes three monitoring tools via MCP:
-
get_server_health— health status, uptime, issues -
get_server_metrics— pool utilization, job counts, error rates, system stats -
get_error_log— recent error events with 24-hour error rate
Plus HTTP endpoints for dashboard:
-
/health— JSON health status -
/metrics— JSON metrics snapshot -
/dashboard— Web UI (if monitoring enabled)
Configure via:
monitoring:
enabled: true # Enable metrics collection
sqlite_db_path: ./monitoring.db # Metrics database
dashboard_enabled: true # Enable web dashboard
dashboard_port: 8766 # Dashboard portQ: "MATLAB Engine startup failed"
- Verify MATLAB is installed:
matlab -version - Verify Engine API is installed:
python -c "import matlab.engine; print(matlab.engine.start_matlab)" - Check that MATLAB_ROOT env var is set if MATLAB is in a non-standard location
See Troubleshooting for more.
Q: "macOS MATLAB Engine limit exceeded"
On macOS, MATLAB limits you to ~4 concurrent engines. Either:
- Reduce
pool.max_enginesto 4 - Switch to a Linux/Windows machine with higher limits
Q: "MATLAB hangs on startup"
On Windows with network drives, MATLAB can hang during startup. Workaround:
- Map network drives as persistent before starting MATLAB
- Or use a local drive for the workspace
Q: "Blocked function error"
The security validator blocks dangerous functions (system, eval, etc.) by default. Either:
- Rewrite your code to avoid the blocked function
- Disable the block for that specific function in
config.yaml:
security:
blocked_functions:
- system
- eval
# Remove the function you need hereQ: "Code is timing out"
Code exceeding sync_timeout (default 30s) auto-promotes to async. Either:
- Increase
execution.sync_timeoutin config - Let it run async and poll with
get_job_status - Optimize the MATLAB code
Q: "Upload failed — file too large"
Default limit is 100MB. Increase via:
security:
max_upload_size_mb: 500Q: "Path traversal error"
Filenames are sanitized to prevent path traversal. Use simple filenames without ../, /, or absolute paths.
Q: "Plotly conversion failed"
Some complex custom plots may not convert. The server falls back to a static PNG. This is expected behavior.
Q: "Plot doesn't show colors/styles"
The conversion supports common plot types (line, scatter, bar, surface). Custom properties may be lost. Check the static PNG fallback.
For HTTP/SSE transports:
- Always put behind an authenticating reverse proxy (nginx, Apache)
- Set
require_proxy_auth: truein config - Bind to
127.0.0.1if the proxy is local - Use bearer tokens via environment variables only — never in config files
No. The security validator blocks 11 dangerous functions by default:
-
system,unix,dos,!(shell escapes) -
eval,evalc,evalin(code execution) -
feval(function indirection) -
assignin(workspace manipulation) -
perl,python(shell bridges)
You can customize the blocklist in config.yaml. See Security for details.
Yes. Each session gets:
-
Isolated workspace —
clear all; clear global; clear functionsbetween sessions - Private temp directory — files can't leak between sessions
-
Session timeout — idle sessions are cleaned up after
session_timeout(default 1 hour)
For detailed architecture, data flows, and design decisions, see the Wiki:
graph TD
Agent["AI Agent<br/>(Claude, Cursor, etc)"]
MCP["MCP Protocol<br/>(stdio/HTTP)"]
FastMCP["FastMCP 3.x<br/>Server"]
Auth["Bearer Token<br/>Middleware"]
SessionMgr["Session Manager<br/>(per-user workspaces)"]
Pool["Engine Pool<br/>(elastic scaling)"]
Engines["MATLAB Engines<br/>(R2022b+)"]
Agent -->|"MCP Messages"| MCP
MCP --> Auth
Auth --> FastMCP
FastMCP --> SessionMgr
FastMCP --> Pool
Pool --> Engines
SessionMgr --> Engines
Key components:
- FastMCP: MCP server framework (v3.x)
- SessionManager: Per-user workspace isolation and temp directories
- EnginePoolManager: Elastic MATLAB engine scaling (min → max engines)
- JobExecutor: Sync/async job orchestration with auto-promotion
- SecurityValidator: Blocklist-based code safety checks
- Monitoring: Real-time metrics, health, and error logs
pip install -e ".[dev]"
pytest tests/ -vTests use a mock MATLAB engine — no MATLAB installation needed for testing.
- Create implementation in
src/matlab_mcp/tools/ - Register with
@mcp.toolinserver.py - Add tests in
tests/test_tools_*.py
- Fork the repo
- Create a feature branch:
git checkout -b feature/my-feature - Write tests and code
- Run tests:
pytest tests/ -v - Run linter:
ruff check . && ruff format . - Open a PR
See CONTRIBUTING for guidelines.
For more help:
- Installation — detailed setup instructions
- Configuration — all config options with examples
- MCP-Tools-Reference — complete tool reference
- Async Jobs — long-running jobs and progress reporting
- Custom Tools — how to expose your MATLAB functions
- Security — security model and best practices
- Troubleshooting — common issues and solutions
- GitHub Issues — report bugs or ask questions