MCP HubMCP Hub
Tsuchijo

matlab-mcp

by: Tsuchijo

Model Context Protocol server to let LLMs write and execute matlab scripts

13created 10/01/2025
Visit
LLM
MATLAB

📌Overview

Purpose: To provide a server solution for integrating MATLAB scripts and functions with MCP clients like Claude.

Overview: The MATLAB MCP Server is designed to facilitate the execution of MATLAB scripts and functions directly from MCP-compatible environments. By leveraging Python's capabilities, users can create, run, and manage MATLAB scripts efficiently, with automated setup for the necessary MATLAB Engine.

Key Features:

  • Create MATLAB Scripts: Users can generate new MATLAB script files stored in a designated directory, ensuring that file names adhere to valid MATLAB identifier conventions.

  • Create MATLAB Functions: Similar to scripts, this feature enables the creation of MATLAB functions, which are also saved in the same directory and require valid function definitions.

  • Execute MATLAB Scripts: This functionality allows users to run scripts and retrieve output, including text results, generated figures, and workspace variables, along with the ability to pass arguments.

  • Call MATLAB Functions: Users can invoke MATLAB functions with specified arguments and obtain outputs, along with any figures produced during execution.


MATLAB MCP Server

This Model Context Protocol (MCP) server provides integration with MATLAB, allowing you to create and execute MATLAB scripts and functions through Claude or other MCP clients.

Setup Requirements

  • Python 3.11 (Python 3.12 and 3.13 are not currently supported by MATLAB Engine)
  • MATLAB R2024a (or compatible version)
  • uv package manager

Installation

  1. Create and set up the Python environment:
# Pin Python version
uv python pin 3.11

# Create virtual environment
uv venv

# Activate virtual environment
source .venv/bin/activate

# Install MCP
uv add "mcp[cli]"
  1. Install MATLAB Engine
    The MATLAB Engine will be installed automatically the first time the server runs, using the MATLAB installation specified in the MATLAB_PATH environment variable.

Directory Structure

  • matlab_server.py: The main MCP server implementation
  • matlab_scripts/: Directory where MATLAB scripts and functions are saved (created automatically)
  • pyproject.toml: Python project configuration
  • .python-version: Specifies Python version for uv

Claude Desktop Integration

  1. Open your Claude Desktop configuration:
# On macOS
code ~/Library/Application\ Support/Claude/claude_desktop_config.json
  1. Add the MATLAB server configuration:
{
    "mcpServers": {
        "matlab": {
            "command": "uv",
            "args": [
                "--directory",
                "/absolute/path/to/matlab-mcp",
                "run",
                "matlab_server.py"
            ],
            "env": {
                "MATLAB_PATH": "/Applications/MATLAB_R2024a.app"
            }
        }
    }
}

Make sure to:

  • Replace /absolute/path/to/matlab-mcp with your project directory path
  • Verify MATLAB_PATH points to your MATLAB installation
  • Use absolute paths (not relative)

Features

The server provides several tools:

  1. create_matlab_script: Create a new MATLAB script file

    • Scripts saved in the matlab_scripts directory
    • File names must be valid MATLAB identifiers
  2. create_matlab_function: Create a new MATLAB function file

    • Functions saved in the matlab_scripts directory
    • Must include a valid function definition
  3. execute_matlab_script: Run a MATLAB script and get results

    • Returns output text, generated figures, and workspace variables
    • Arguments can be passed to scripts
  4. call_matlab_function: Call a MATLAB function with arguments

    • Returns function output and any generated figures

Testing

Test the server using the MCP Inspector:

# Activate your virtual environment
source .venv/bin/activate

# Run the inspector
MATLAB_PATH=/Applications/MATLAB_R2024a.app mcp dev matlab_server.py

Example MATLAB test script:

t = 0:0.01:2*pi;
y = sin(t);
plot(t, y);
title('Test Plot');
xlabel('Time');
ylabel('Amplitude');

Script Storage

  • All MATLAB scripts and functions are saved in the matlab_scripts directory
  • This directory is created automatically when the server starts
  • Files named <script_name>.m or <function_name>.m
  • Located in the same directory as matlab_server.py

Environment Variables

  • MATLAB_PATH: Path to your MATLAB installation (default: /Applications/MATLAB_R2024a.app)
  • Set this either in Claude Desktop config or before running the server

Troubleshooting

  1. MATLAB Engine Installation Fails

    • Verify MATLAB_PATH is correct
    • Try manual install:
      cd $MATLAB_PATH/extern/engines/python
      python setup.py install
      
  2. Python Version Issues

    • Use Python 3.11
    • Check version with python --version
    • Pin version with uv python pin 3.11 if needed
  3. Script Execution Errors

    • Ensure matlab_scripts directory exists
    • Verify script syntax is valid
    • Check MATLAB output for error messages

Updates and Maintenance

  • Keep MATLAB installation updated
  • Update Python packages as needed:
    uv pip install --upgrade mcp[cli]
    
  • Check MATLAB engine compatibility when updating Python