Allows execution of MATLAB code from Python using the MATLAB Engine API, enabling a shared MATLAB session across multiple requests for seamless integration with Claude Desktop.
This is an implementation of a Model Context Protocol (MCP) server for MATLAB. It allows MCP clients (like LLM agents or Claude Desktop) to interact with a shared MATLAB session using the MATLAB Engine API for Python.
runMatlabCode
tool.getVariable
tool.asyncio.to_thread
to prevent blocking the server.logging
module, outputting to stderr
for visibility in client logs.setVariable
tool to write data to the MATLAB workspace.runScript
tool to execute .m
files directly.clearWorkspace
, getWorkspaceVariables
).matlab_to_python
helper to handle more complex data types (structs, cell arrays, objects).numpy
Python package.Clone this repository:
git clone https://github.com/jigarbhoye04/MatlabMCP.git cd MatlabMCP
Set up a Python virtual environment (recommended):
# Install uv if you haven't already: https://github.com/astral-sh/uv uv init uv venv source .venv/bin/activate # On Windows use: .venv\Scripts\activate
Install dependencies:
uv pip sync
Ensure MATLAB is installed and the MATLAB Engine API for Python is configured for your Python environment. See MATLAB Documentation.
Start MATLAB and share its engine: Run the following command in the MATLAB Command Window:
matlab.engine.shareEngine
You can verify it's shared by running matlab.engine.isEngineShared
in MATLAB (it should return true
or 1
). The MCP server needs this shared engine to connect.
To use this server with Claude Desktop:
Go to Claude Desktop -> Settings -> Developer -> Edit Config.
This will open claude_desktop_config.json
. Add or modify the mcpServers
section to include the MatlabMCP
configuration:
{ "mcpServers": { "MatlabMCP": { "command": "C:\\Users\\username\\.local\\bin\\uv.exe", // Path to your uv executable "args": [ "--directory", "C:\\Users\\username\\Desktop\\MatlabMCP", // ABSOLUTE path to the cloned repository directory "run", "main.py" ] // Optional: Add environment variables if needed // "env": { // "MY_VAR": "value" // } } // Add other MCP servers here if you have them } }
IMPORTANT: Replace C:\\Users\\username\\...
paths with the correct absolute paths for your system.
Save the file and restart Claude Desktop.
Logging: Server logs (from Python's logging
module) will appear in Claude Desktop's MCP log files (accessible via tail -f ~/Library/Logs/Claude/mcp-server-MatlabMCP.log
on macOS or checking %APPDATA%\Claude\logs\
on Windows).
Project Structure:
MatlabMCP/
├── .venv/ # Virtual environment created by uv
├── Docs/
│ └── Images/
│ └── Updates.md # Documentation for updates and changes
├── main.py # The MCP server script
├── pyproject.toml # Project metadata and dependencies
├── README.md # This file
└── uv.lock # Lock file for dependencies
Check out Updates for detailed documentation on the server's features, usage, and development notes.
Contributions are welcome! If you have any suggestions or improvements, feel free to open an issue or submit a pull request.
Let's make this even better together!
Discover shared experiences
Shared threads will appear here, showcasing real-world applications and insights from the community. Check back soon for updates!