Labsco
Tsuchijo logo

MATLAB

โ˜… 39

from Tsuchijo

Execute MATLAB scripts and functions via MCP clients. Requires a local MATLAB installation.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedFreeNeeds API keys

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.

Directory Structure

  • matlab_server.py: The main MCP server implementation
  • matlab_scripts/: Directory where all 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 the actual path to your project directory
  • Verify the 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 are saved in the matlab_scripts directory
    • File names must be valid MATLAB identifiers
  2. create_matlab_function: Create a new MATLAB function file

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

    • Returns output text, generated figures, and workspace variables
    • Can pass arguments to scripts
  4. call_matlab_function: Call a MATLAB function with arguments

    • Returns function output and any generated figures

Testing

You can test the server using the MCP Inspector:

# Make sure you're in your virtual environment
source .venv/bin/activate

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

Example 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 are named <script_name>.m or <function_name>.m
  • The directory is in the same location as matlab_server.py

Environment Variables

  • MATLAB_PATH: Path to your MATLAB installation
    • Default: /Applications/MATLAB_R2024a.app
    • Set in Claude Desktop config or when running directly

Updates and Maintenance

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