MATLAB MCP Integration
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.
Features
- Execute MATLAB Code: Run arbitrary MATLAB code snippets via the
runMatlabCode
tool. - Retrieve Variables: Get the value of variables from the MATLAB workspace using the
getVariable
tool. - Structured Communication: Tools return results and errors as structured JSON for easier programmatic use by clients.
- Non-Blocking Execution: MATLAB engine calls are run asynchronously using
asyncio.to_thread
to prevent blocking the server. - Standard Logging: Uses Python's standard
logging
module, outputting tostderr
for visibility in client logs. - Shared Session: Connects to an existing shared MATLAB session.
TODO:
- Add a
setVariable
tool to write data to the MATLAB workspace. - Add a
runScript
tool to execute.m
files directly. - Add tools for workspace management (e.g.,
clearWorkspace
,getWorkspaceVariables
). - Expand
matlab_to_python
helper to handle more complex data types (structs, cell arrays, objects). - Add support for interacting with Simulink models.
Requirements
- Python 3.12 or higher
- MATLAB (R2023a or higher recommended - check MATLAB Engine API for Python compatibility) with the MATLAB Engine API for Python installed.
numpy
Python package.
Installation
- Clone this repository:
- Set up a Python virtual environment (recommended):
- Install dependencies:
- 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:You can verify it's shared by running
matlab.engine.isEngineShared
in MATLAB (it should returntrue
or1
). The MCP server needs this shared engine to connect.
Configuration (for Claude Desktop)
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 themcpServers
section to include theMatlabMCP
configuration: - 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 viatail -f ~/Library/Logs/Claude/mcp-server-MatlabMCP.log
on macOS or checking%APPDATA%\Claude\logs\
on Windows).
Development
Project Structure:
Documentation
Check out Updates for detailed documentation on the server's features, usage, and development notes.
Contributing
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!
This server cannot be installed
local-only server
The server can only run on the client's local machine because it depends on local resources.
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.
Related MCP Servers
- -securityFlicense-qualityAllows interaction with MATLAB by creating and executing scripts and functions through Claude or other MCP clients, supporting script management and execution result retrieval with environment configuration capabilities.Last updated -10Python
- -securityAlicense-qualityThe server facilitates access to Julia documentation and source code through Claude Desktop, allowing users to retrieve information on Julia packages, modules, types, functions, and methods.Last updated -402JavaScriptMIT License
- AsecurityAlicenseAqualityIntegrates MATLAB with AI to execute code, generate scripts from natural language, and access MATLAB documentation seamlessly.Last updated -229JavaScriptApache 2.0
- AsecurityFlicenseAqualityA server that enables Claude Desktop users to access the Claude API directly, allowing them to bypass Professional Plan limitations and use advanced features like custom system prompts and conversation management.Last updated -15Python