matlab-simulink-mcp 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- matlab_simulink_mcp/__init__.py +0 -0
- matlab_simulink_mcp/__main__.py +13 -0
- matlab_simulink_mcp/data/__init__.py +0 -0
- matlab_simulink_mcp/data/blacklist.txt +39 -0
- matlab_simulink_mcp/data/simlib_db.json +20023 -0
- matlab_simulink_mcp/functions.py +194 -0
- matlab_simulink_mcp/installer/__init__.py +0 -0
- matlab_simulink_mcp/installer/installer.py +160 -0
- matlab_simulink_mcp/installer/launcher.py +45 -0
- matlab_simulink_mcp/installer/win_elevate.py +18 -0
- matlab_simulink_mcp/log_utils.py +112 -0
- matlab_simulink_mcp/security.py +56 -0
- matlab_simulink_mcp/server.py +54 -0
- matlab_simulink_mcp/state.py +106 -0
- matlab_simulink_mcp-0.1.0.dist-info/METADATA +169 -0
- matlab_simulink_mcp-0.1.0.dist-info/RECORD +19 -0
- matlab_simulink_mcp-0.1.0.dist-info/WHEEL +5 -0
- matlab_simulink_mcp-0.1.0.dist-info/entry_points.txt +2 -0
- matlab_simulink_mcp-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,106 @@
|
|
1
|
+
import os, types, json, sys
|
2
|
+
|
3
|
+
from pathlib import Path
|
4
|
+
from importlib import resources
|
5
|
+
from dataclasses import dataclass
|
6
|
+
from dotenv import load_dotenv
|
7
|
+
|
8
|
+
import matlab_simulink_mcp
|
9
|
+
from matlab_simulink_mcp import data
|
10
|
+
from matlab_simulink_mcp.log_utils import create_log_file, create_logger, create_console
|
11
|
+
|
12
|
+
|
13
|
+
def get_full_path(pkg: types.ModuleType, path: str | Path) -> Path:
|
14
|
+
if path is None:
|
15
|
+
return None
|
16
|
+
path = Path(path)
|
17
|
+
if path.is_absolute():
|
18
|
+
return path
|
19
|
+
return (Path(pkg.__file__).resolve().parent / path).resolve()
|
20
|
+
|
21
|
+
load_dotenv()
|
22
|
+
log_dir = get_full_path(matlab_simulink_mcp, os.getenv("LOG_DIR"))
|
23
|
+
|
24
|
+
log_file = create_log_file(filename=matlab_simulink_mcp.__name__, dir=log_dir)
|
25
|
+
log_console = create_console(log_file=log_file)
|
26
|
+
logger = create_logger(name=matlab_simulink_mcp.__name__, log_file=log_file)
|
27
|
+
|
28
|
+
|
29
|
+
@dataclass
|
30
|
+
class EngineState:
|
31
|
+
installed: int = 0
|
32
|
+
session: str | None = None
|
33
|
+
eng = None # Don't put typehint here since engine may not be installed
|
34
|
+
helpers: Path | None = None
|
35
|
+
simlib: dict | None = None
|
36
|
+
blacklist: set[str] | None = None
|
37
|
+
|
38
|
+
def initialize(self):
|
39
|
+
self.ensure_engine()
|
40
|
+
|
41
|
+
if self.installed == 1:
|
42
|
+
self.connect_engine()
|
43
|
+
self.load_data()
|
44
|
+
|
45
|
+
if self.eng is None:
|
46
|
+
logger.warning("Starting server without an engine. " \
|
47
|
+
"Run matlab.engine.shareEngine in MATLAB to share a session and access_matlab tool to reconnect.")
|
48
|
+
else:
|
49
|
+
logger.info(f"Connected to MATLAB session: {self.session}.")
|
50
|
+
logger.info(f"Logging to: {log_file}")
|
51
|
+
|
52
|
+
def load_data(self):
|
53
|
+
self.simlib = json.loads((resources.files(data) / "simlib_db.json").read_text())
|
54
|
+
self.blacklist = {
|
55
|
+
line.strip().lower()
|
56
|
+
for line in (resources.files(data) / "blacklist.txt").read_text().splitlines()
|
57
|
+
if line.strip() and not line.strip().startswith("#")
|
58
|
+
}
|
59
|
+
|
60
|
+
def connect_engine(self):
|
61
|
+
if self.installed == 1:
|
62
|
+
import matlab.engine
|
63
|
+
sessions = matlab.engine.find_matlab() or []
|
64
|
+
if sessions:
|
65
|
+
self.session = sessions[0]
|
66
|
+
self.eng = matlab.engine.connect_matlab(self.session)
|
67
|
+
self.helpers = self.add_helpers()
|
68
|
+
|
69
|
+
def add_helpers(self):
|
70
|
+
if getattr(sys, "frozen", False):
|
71
|
+
pth = Path(sys._MEIPASS) / "matlab_simulink_mcp" / "data/helpers"
|
72
|
+
else:
|
73
|
+
pth = get_full_path(matlab_simulink_mcp, "data/helpers")
|
74
|
+
self.eng.addpath(str(pth), nargout=0)
|
75
|
+
return Path(pth)
|
76
|
+
|
77
|
+
def ensure_engine(self):
|
78
|
+
try:
|
79
|
+
import matlab.engine
|
80
|
+
except ImportError:
|
81
|
+
logger.info("MATLAB Engine for Python package not found. Starting package installer...")
|
82
|
+
import matlab_simulink_mcp.installer.launcher as launcher
|
83
|
+
self.installed = launcher.run()
|
84
|
+
if self.installed == 1:
|
85
|
+
try:
|
86
|
+
import matlab.engine
|
87
|
+
logger.info("MATLAB engine package successfully installed.")
|
88
|
+
except ImportError:
|
89
|
+
raise ImportError("MATLAB Engine package not available even after attempted install.")
|
90
|
+
elif self.installed != -1:
|
91
|
+
raise RuntimeError("Failed to install MATLAB engine package. " \
|
92
|
+
"This server requires MATLAB engine package to work.. " \
|
93
|
+
"You can try installing it manually from PyPi or your MATLAB installation.")
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
@@ -0,0 +1,169 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: matlab-simulink-mcp
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: An MCP server for allowing LLMs to interact with MATLAB and Simulink
|
5
|
+
Author: Rohail
|
6
|
+
License: MIT
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
9
|
+
Classifier: Operating System :: OS Independent
|
10
|
+
Requires-Python: <3.13,>=3.10
|
11
|
+
Description-Content-Type: text/markdown
|
12
|
+
Requires-Dist: fastmcp>=2.12.2
|
13
|
+
Requires-Dist: platformdirs>=4.4.0
|
14
|
+
|
15
|
+
# MATLAB Simulink MCP Server
|
16
|
+
|
17
|
+
[](https://pypi.org/project/matlab-simulink-mcp/)
|
18
|
+
[](https://pypi.org/project/matlab-simulink-mcp/)
|
19
|
+
|
20
|
+
This [Model Context Protocol (MCP)](https://github.com/modelcontextprotocol) server allows MCP clients (such as Claude Desktop or other LLM-based agents) to interact with **MATLAB** and **Simulink** in real time. It runs locally and is built on top of the [FastMCP 2.0](https://gofastmcp.com/getting-started/welcome) library.
|
21
|
+
|
22
|
+
## Features
|
23
|
+
|
24
|
+
- Read, write, and run MATLAB code and scripts
|
25
|
+
- Parse and interact with Simulink files
|
26
|
+
- Access MATLAB workspace variables and outputs (including visualizations)
|
27
|
+
- Basic safety layer to prevent execution of unsafe commands (configurable)
|
28
|
+
- Non-blocking execution (async MATLAB engine calls)
|
29
|
+
- Automatic installation of MATLAB Engine package if unavailable
|
30
|
+
|
31
|
+
## Requirements
|
32
|
+
|
33
|
+
- **MATLAB** R2022b or later
|
34
|
+
- **Python** 3.10–3.12 (check MATLAB's [supported versions](https://www.mathworks.com/support/requirements/python-compatibility.html))
|
35
|
+
|
36
|
+
## Installation
|
37
|
+
|
38
|
+
### Option 1 — Download Binary (MATLAB R2025a only)
|
39
|
+
|
40
|
+
If you don’t want to interact with Python at all, you can download a prebuilt **exe/app** from the [Releases](../../releases) page and run it directly. In that case you can skip the steps below.
|
41
|
+
|
42
|
+
### Option 2 — From Source
|
43
|
+
|
44
|
+
1. Clone the repository:
|
45
|
+
|
46
|
+
```bash
|
47
|
+
git clone https://github.com/rohailamalik/matlab-simulink-mcp
|
48
|
+
cd matlab-simulink-mcp
|
49
|
+
|
50
|
+
2. Create a Python virtual environment (recommended: [uv](https://pypi.org/project/uv/0.1.32/)):
|
51
|
+
|
52
|
+
```bash
|
53
|
+
uv venv --python 3.12 # match Python to your MATLAB-supported version
|
54
|
+
source .venv/Scripts/activate # on macOS/Linux: source .venv/bin/activate
|
55
|
+
uv sync
|
56
|
+
```
|
57
|
+
|
58
|
+
Without uv, first download a Python version manually:
|
59
|
+
|
60
|
+
```bash
|
61
|
+
python3.12 -m venv .venv
|
62
|
+
source .venv/bin/activate
|
63
|
+
pip install -r requirements.txt
|
64
|
+
pip install .
|
65
|
+
```
|
66
|
+
|
67
|
+
3. Alternatively, skip step 1, create virtual environment and install directly via PyPI:
|
68
|
+
|
69
|
+
```bash
|
70
|
+
uv pip install matlab-simulink-mcp
|
71
|
+
# or
|
72
|
+
pip install matlab-simulink-mcp
|
73
|
+
```
|
74
|
+
|
75
|
+
4. On first run, if the MATLAB Engine is not installed, the server will open a console window and guide you through installation.
|
76
|
+
|
77
|
+
- This requires admin permission and the server will request for it.
|
78
|
+
- If you prefer to install manually, install a [matching PyPi version](https://pypi.org/project/matlabengine/#history) or from your [MATLAB installation](https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html).
|
79
|
+
|
80
|
+
## Configuration (Claude Desktop)
|
81
|
+
|
82
|
+
1. Install [Claude Desktop](https://claude.ai/download).
|
83
|
+
|
84
|
+
2. Open Settings → Developer → Edit Config.
|
85
|
+
|
86
|
+
3. In the `claude_desktop_config.json`, add or update:
|
87
|
+
|
88
|
+
```json
|
89
|
+
{
|
90
|
+
"mcpServers": {
|
91
|
+
"MATLAB_Simulink_MCP": {
|
92
|
+
"command": "C:/Data/Research/Doctoral/src/matlab-simulink-mcp/.venv/Scripts/python.exe",
|
93
|
+
"args": ["-m", "matlab_simulink_mcp"]
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
```
|
98
|
+
|
99
|
+
On macOS/Linux, use `absolute-path-to/.venv/bin/python` in `command`.
|
100
|
+
|
101
|
+
If using the standalone exe/app, just put absolute path to it in `command` and omit args.
|
102
|
+
|
103
|
+
**Note**: Only use `/` or `\\` in the paths, not `\`.
|
104
|
+
|
105
|
+
4. Save and restart Claude Desktop. (Ensure it is fully closed in Task Manager/Activity Monitor.)
|
106
|
+
|
107
|
+
5. On first launch, the server may open multiple consoles to install MATLAB Engine. Interact with one, complete installation, then restart Claude if needed.
|
108
|
+
|
109
|
+
6. Check running status in Settings → Developer, or click the equalizer button in the chat box.
|
110
|
+
|
111
|
+
7. The server logs outputs and errors to both Claude's and its own log file. To keep a log file tracking console open, add `--console` in Claude config args.
|
112
|
+
|
113
|
+
- Claude MCP logs (Windows): `%APPDATA%\Claude\logs\mcp-server-MatlabMCP.log`
|
114
|
+
- Claude MCP logs (macOS): `~/Library/Logs/Claude/mcp-server-MatlabMCP.log`
|
115
|
+
- Server logs: written to your user log directory (or configured via `.env`).
|
116
|
+
|
117
|
+
## Debugging
|
118
|
+
|
119
|
+
FastMCP 2.0 includes an MCP Inspector for manual testing without an LLM client. This launches a UI to send dummy requests directly to the server.
|
120
|
+
|
121
|
+
```bash
|
122
|
+
cd scripts
|
123
|
+
fastmcp dev debugger.py
|
124
|
+
```
|
125
|
+
|
126
|
+
## Repository Structure
|
127
|
+
|
128
|
+
```txt
|
129
|
+
|
130
|
+
matlab-simulink-mcp/
|
131
|
+
├─ scripts
|
132
|
+
│ ├─ debugger.py # Entry point for MCP inspector
|
133
|
+
├─ src/matlab_simulink_mcp/
|
134
|
+
│ ├─ data/ # Data files and resources
|
135
|
+
│ ├─ installer/ # MATLAB Engine installer
|
136
|
+
│ │ ├─ installer.py
|
137
|
+
│ │ ├─ launcher.py
|
138
|
+
│ │ └─ win_elevate.py
|
139
|
+
│ ├─ functions.py # Baseline MCP tool functions
|
140
|
+
│ ├─ log_utils.py # Logging utilities
|
141
|
+
│ ├─ security.py # Safety layer for code execution
|
142
|
+
│ ├─ server.py # Main server script
|
143
|
+
│ ├─ state.py # Server lifespan objects (logger, engine)
|
144
|
+
│ ├─ __main__.py # Main entry point
|
145
|
+
├─ .env # Optional config (e.g. LOG_DIR)
|
146
|
+
├─ pyproject.toml # Project metadata and dependencies
|
147
|
+
├─ requirements.txt # Package dependencies for pip
|
148
|
+
├─ uv.lock # Package dependencies for uv
|
149
|
+
└─ README.md # This file
|
150
|
+
|
151
|
+
```
|
152
|
+
|
153
|
+
## FAQ
|
154
|
+
|
155
|
+
Q: Which Python version should I install?
|
156
|
+
|
157
|
+
A: Match it to the highest Python version supported by your MATLAB release (see MathWorks docs).
|
158
|
+
|
159
|
+
Q: The console disappears too quickly!
|
160
|
+
|
161
|
+
A: Add `--console` in your Claude config args to keep the server console open.
|
162
|
+
|
163
|
+
Q: Multiple installer consoles opened on first run.
|
164
|
+
|
165
|
+
A: This is expected if Claude sends multiple startup requests. Complete one installation, then restart Claude Desktop.
|
166
|
+
|
167
|
+
## Contributing
|
168
|
+
|
169
|
+
Pull requests are welcome! Feel free to submit one or open an issue.
|
@@ -0,0 +1,19 @@
|
|
1
|
+
matlab_simulink_mcp/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
+
matlab_simulink_mcp/__main__.py,sha256=KpLGWl7NgBhLBVMIkOwETQoAsh-NXwO8OOVLjsBNBpE,332
|
3
|
+
matlab_simulink_mcp/functions.py,sha256=SDZUnISiG8GVQ6uDLdzIasMiZPXA49wkuHFGlMwXlBo,6348
|
4
|
+
matlab_simulink_mcp/log_utils.py,sha256=7fJWkTZdSzadPHmKNLzy6gmGWnOC8IGFYVYocuaZIZE,3450
|
5
|
+
matlab_simulink_mcp/security.py,sha256=bqEHujIW3-URUrOPVvBOUmhPMdkCgKLwTUr-7SPOTIQ,2136
|
6
|
+
matlab_simulink_mcp/server.py,sha256=nJhAxSa8MMxsXfGu5ygxm6fqunyyZk7rjhFOBkOOCrI,1287
|
7
|
+
matlab_simulink_mcp/state.py,sha256=bT8Ud2E3W2eWxcqgSuRcuvj43O9BYERWciBYh82ey24,3696
|
8
|
+
matlab_simulink_mcp/data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
9
|
+
matlab_simulink_mcp/data/blacklist.txt,sha256=YXWTjcjFM-uTb3gD_pblEpPPGoIKjSHMFgZJdWfmuMw,656
|
10
|
+
matlab_simulink_mcp/data/simlib_db.json,sha256=bni7DcGG6SofIcCMX5KSlaHOHOYsELT833reNhz4S5Q,538658
|
11
|
+
matlab_simulink_mcp/installer/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
12
|
+
matlab_simulink_mcp/installer/installer.py,sha256=0SHMTaqZjSvb9zKx4in67bS5xT4JcnsO2U2vYvmb968,5949
|
13
|
+
matlab_simulink_mcp/installer/launcher.py,sha256=aGuxkXnLcb1OZ4P6rHvXFi8KZsDUZVQlo0rNvjdmFqA,1438
|
14
|
+
matlab_simulink_mcp/installer/win_elevate.py,sha256=LyHHUh4vWmIlIXxrM9tOhQUvVeMxcVdXMzkC3vbGm3A,514
|
15
|
+
matlab_simulink_mcp-0.1.0.dist-info/METADATA,sha256=gYmQ8SGHTA3mk-sg9loIb8DwK8kCg2yDojsMUtk8h6I,6604
|
16
|
+
matlab_simulink_mcp-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
17
|
+
matlab_simulink_mcp-0.1.0.dist-info/entry_points.txt,sha256=0ba8PFlQcaWyAqKXRIVDfDvrifeD5LY60lV7i6ak9oE,74
|
18
|
+
matlab_simulink_mcp-0.1.0.dist-info/top_level.txt,sha256=90gv-RNCWWS7E-JMiqwUf2TjB_B8Zb2B-crAeXaABqU,20
|
19
|
+
matlab_simulink_mcp-0.1.0.dist-info/RECORD,,
|
@@ -0,0 +1 @@
|
|
1
|
+
matlab_simulink_mcp
|