baltamatica-mcp 0.2.0__tar.gz
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.
- baltamatica_mcp-0.2.0/.github/workflows/ci.yml +36 -0
- baltamatica_mcp-0.2.0/.gitignore +49 -0
- baltamatica_mcp-0.2.0/LICENSE +21 -0
- baltamatica_mcp-0.2.0/PKG-INFO +425 -0
- baltamatica_mcp-0.2.0/README.md +400 -0
- baltamatica_mcp-0.2.0/bex/CMakeLists.txt +17 -0
- baltamatica_mcp-0.2.0/bex/bex_plot_probe.c +150 -0
- baltamatica_mcp-0.2.0/bex/mcp_bridge.c +2059 -0
- baltamatica_mcp-0.2.0/bex/mcp_protocol.h +29 -0
- baltamatica_mcp-0.2.0/docs/bex-bridge.md +201 -0
- baltamatica_mcp-0.2.0/docs/bex-plot-probe.md +49 -0
- baltamatica_mcp-0.2.0/docs/bex-plugin.md +126 -0
- baltamatica_mcp-0.2.0/docs/bex-protocol.md +306 -0
- baltamatica_mcp-0.2.0/docs/contributing.md +48 -0
- baltamatica_mcp-0.2.0/docs/pr-plan.md +252 -0
- baltamatica_mcp-0.2.0/docs/releasing.md +74 -0
- baltamatica_mcp-0.2.0/examples/artifact_export_demo.m +20 -0
- baltamatica_mcp-0.2.0/examples/bex_plot_probe_demo.m +12 -0
- baltamatica_mcp-0.2.0/examples/monte_carlo_pi.m +17 -0
- baltamatica_mcp-0.2.0/examples/numerical_pipeline_demo.m +55 -0
- baltamatica_mcp-0.2.0/pyproject.toml +53 -0
- baltamatica_mcp-0.2.0/scripts/build_bex.sh +64 -0
- baltamatica_mcp-0.2.0/src/baltamatica_mcp/__init__.py +3 -0
- baltamatica_mcp-0.2.0/src/baltamatica_mcp/__main__.py +6 -0
- baltamatica_mcp-0.2.0/src/baltamatica_mcp/backend_bex.py +316 -0
- baltamatica_mcp-0.2.0/src/baltamatica_mcp/backend_cli.py +323 -0
- baltamatica_mcp-0.2.0/src/baltamatica_mcp/bex_shutdown.py +158 -0
- baltamatica_mcp-0.2.0/src/baltamatica_mcp/engine.py +177 -0
- baltamatica_mcp-0.2.0/src/baltamatica_mcp/serializer.py +320 -0
- baltamatica_mcp-0.2.0/src/baltamatica_mcp/server.py +213 -0
- baltamatica_mcp-0.2.0/tests/__init__.py +1 -0
- baltamatica_mcp-0.2.0/tests/fixtures/sample_script.m +4 -0
- baltamatica_mcp-0.2.0/tests/test_backend_bex.py +392 -0
- baltamatica_mcp-0.2.0/tests/test_backend_cli.py +294 -0
- baltamatica_mcp-0.2.0/tests/test_bex_shutdown.py +122 -0
- baltamatica_mcp-0.2.0/tests/test_bex_sources.py +95 -0
- baltamatica_mcp-0.2.0/tests/test_integration_bex.py +97 -0
- baltamatica_mcp-0.2.0/tests/test_integration_cli.py +332 -0
- baltamatica_mcp-0.2.0/tests/test_serializer.py +232 -0
- baltamatica_mcp-0.2.0/tests/test_server.py +250 -0
- baltamatica_mcp-0.2.0/undefined_0.m +0 -0
- baltamatica_mcp-0.2.0/undefined_0.masv +5 -0
- baltamatica_mcp-0.2.0//345/274/200/345/217/221/346/226/207/346/241/243.pdf +0 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
push:
|
|
6
|
+
branches:
|
|
7
|
+
- main
|
|
8
|
+
- develop
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
test:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
strategy:
|
|
14
|
+
matrix:
|
|
15
|
+
python-version: ["3.10", "3.11", "3.12"]
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- name: Check out repository
|
|
19
|
+
uses: actions/checkout@v4
|
|
20
|
+
|
|
21
|
+
- name: Set up Python
|
|
22
|
+
uses: actions/setup-python@v5
|
|
23
|
+
with:
|
|
24
|
+
python-version: ${{ matrix.python-version }}
|
|
25
|
+
|
|
26
|
+
- name: Install package
|
|
27
|
+
run: python -m pip install -e ".[dev]"
|
|
28
|
+
|
|
29
|
+
- name: Run unit tests
|
|
30
|
+
run: pytest -q -m "not integration"
|
|
31
|
+
|
|
32
|
+
- name: Compile sources
|
|
33
|
+
run: python -m compileall -q src tests
|
|
34
|
+
|
|
35
|
+
- name: Lint
|
|
36
|
+
run: ruff check src tests
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
*.egg-info/
|
|
7
|
+
dist/
|
|
8
|
+
build/
|
|
9
|
+
.eggs/
|
|
10
|
+
*.egg
|
|
11
|
+
|
|
12
|
+
# Virtual Environment
|
|
13
|
+
.venv/
|
|
14
|
+
venv/
|
|
15
|
+
ENV/
|
|
16
|
+
|
|
17
|
+
# IDE
|
|
18
|
+
.vscode/
|
|
19
|
+
.idea/
|
|
20
|
+
*.swp
|
|
21
|
+
*.swo
|
|
22
|
+
*~
|
|
23
|
+
|
|
24
|
+
# OS
|
|
25
|
+
.DS_Store
|
|
26
|
+
Thumbs.db
|
|
27
|
+
|
|
28
|
+
# BEX compiled files
|
|
29
|
+
*.bexw64
|
|
30
|
+
*.bexa64
|
|
31
|
+
*.bexmaci64
|
|
32
|
+
*.o
|
|
33
|
+
*.obj
|
|
34
|
+
*.dll
|
|
35
|
+
|
|
36
|
+
# CMake
|
|
37
|
+
bex/build/
|
|
38
|
+
CMakeFiles/
|
|
39
|
+
CMakeCache.txt
|
|
40
|
+
cmake_install.cmake
|
|
41
|
+
|
|
42
|
+
# Environment variables
|
|
43
|
+
.env
|
|
44
|
+
.env.local
|
|
45
|
+
|
|
46
|
+
# Test artifacts
|
|
47
|
+
.pytest_cache/
|
|
48
|
+
htmlcov/
|
|
49
|
+
.coverage
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wzlwww
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: baltamatica-mcp
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: MCP server for Baltamatica (北太天元) scientific computing engine
|
|
5
|
+
Project-URL: Homepage, https://github.com/wzlwww/baltamatica.mcp
|
|
6
|
+
Project-URL: Repository, https://github.com/wzlwww/baltamatica.mcp
|
|
7
|
+
Project-URL: Issues, https://github.com/wzlwww/baltamatica.mcp/issues
|
|
8
|
+
Author: wzlwww
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: ai-agent,baltamatica,mcp,scientific-computing
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Requires-Dist: mcp>=1.0.0
|
|
19
|
+
Requires-Dist: pydantic>=2.0.0
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
23
|
+
Requires-Dist: ruff>=0.1.0; extra == 'dev'
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# baltamatica.mcp
|
|
27
|
+
|
|
28
|
+
[](LICENSE)
|
|
29
|
+
[](https://modelcontextprotocol.io)
|
|
30
|
+
[](https://www.baltamatica.com)
|
|
31
|
+
|
|
32
|
+
> 🔗 让 AI 代理(Claude Code / Cursor / Codex)直接驱动国产科学计算软件 **北太天元(Baltamatica)** 进行交互式数值计算。
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## 📖 项目简介
|
|
37
|
+
|
|
38
|
+
**baltamatica.mcp** 是一个基于 [Model Context Protocol (MCP)](https://modelcontextprotocol.io) 的开源服务,为大语言模型提供与北太天元科学计算内核之间的双向通信桥梁。
|
|
39
|
+
|
|
40
|
+
通过本项目,AI 代理可以:
|
|
41
|
+
|
|
42
|
+
- 🧮 **执行代码**:直接在北太天元中运行 `.m` 脚本或单行表达式,并取回控制台输出
|
|
43
|
+
- 📊 **读取变量**:获取工作区中矩阵、向量、结构体、元胞等变量的值(BEX 后端二进制全保真)
|
|
44
|
+
- ✍️ **写入变量**:把标量/向量/矩阵注入工作区(`set_variable`)
|
|
45
|
+
- 🔍 **查询状态**:列出当前工作区中所有变量的名称、类型和维度
|
|
46
|
+
- 📂 **管理脚本**:运行本地 `.m` 脚本文件
|
|
47
|
+
- 🧹 **清空工作区**:重置计算环境
|
|
48
|
+
|
|
49
|
+
**当前状态**:两套后端均可用。
|
|
50
|
+
|
|
51
|
+
- **CLI 后端**:通过北太天元命令行入口执行代码,用 `.mat` 状态文件在多次 MCP 调用之间保持工作区变量。支持全部 6 个工具。
|
|
52
|
+
- **BEX 后端**:在北太天元 GUI 进程内运行的 C 桥接(JSON-over-TCP),支持 `execute_code`、`run_script`、`list_variables`、`get_variable`、`set_variable`、`clear_workspace`:
|
|
53
|
+
- `get_variable` 对数值/逻辑数组(实数**和复数**、任意大小)走 base64 二进制全保真回传,对字符/字符串/结构体/元胞走结构化 JSON;
|
|
54
|
+
- `set_variable` 用 `bxAddVariable` 把标量/向量/矩阵注入工作区;
|
|
55
|
+
- `execute_code` / `run_script` 用 `evalc` 捕获控制台输出并解析 `BALTAMATICA_ARTIFACT=` 文件产物;
|
|
56
|
+
- 桥接生命周期健壮(可靠 `stop`、Ctrl-C 恢复、`background` 模式),并可触发 GUI Figure 弹窗(但北太天元本身无图形导出函数,见下)。
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## 🏗️ 系统架构
|
|
61
|
+
|
|
62
|
+
项目提供两套后端。**CLI 后端**通过北太天元命令行入口执行代码,用 `.mat` 状态文件保持工作区变量,无需编译,适合快速上手。**BEX 后端**是一个在北太天元 GUI 进程内运行的 C 桥接,走 **JSON-over-TCP**,提供进程内低延迟长连接、二进制变量传输和变量注入。两者暴露相同的 MCP 工具集。
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
┌──────────────────────────────────────────────────────────┐
|
|
66
|
+
│ AI Agent (Claude Code / Cursor / Codex) │
|
|
67
|
+
└─────────────────────────┬────────────────────────────────┘
|
|
68
|
+
│ MCP Protocol (stdio)
|
|
69
|
+
▼
|
|
70
|
+
┌──────────────────────────────────────────────────────────┐
|
|
71
|
+
│ Python MCP Server (baltamatica-mcp) │
|
|
72
|
+
│ execute_code / run_script / list_variables / │
|
|
73
|
+
│ get_variable / set_variable / clear_workspace │
|
|
74
|
+
│ Engine Dispatcher │
|
|
75
|
+
└───────────────┬──────────────────────────┬───────────────┘
|
|
76
|
+
│ (--backend cli) │ (--backend bex)
|
|
77
|
+
▼ ▼
|
|
78
|
+
┌────────────────────┐ ┌──────────────────────────┐
|
|
79
|
+
│ CLI Backend │ │ BEX Backend │
|
|
80
|
+
│ subprocess + .mat │ │ JSON-over-TCP client │
|
|
81
|
+
└─────────┬──────────┘ └────────────┬─────────────┘
|
|
82
|
+
▼ ▼
|
|
83
|
+
baltamatica -nodesktop -s "…" mcp_bridge (BEX in GUI, TCP 31415)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 后端状态
|
|
87
|
+
|
|
88
|
+
| 特性 | CLI 后端 | BEX 后端 |
|
|
89
|
+
|:---|:---:|:---:|
|
|
90
|
+
| `execute_code` | ✅ | ✅ `evalc` 捕获输出 |
|
|
91
|
+
| `run_script` | ✅ | ✅ `evalc` 捕获输出 |
|
|
92
|
+
| `list_variables` | ✅ `whos` 解析 | ✅ SDK 变量枚举 |
|
|
93
|
+
| `get_variable` | ✅ `disp()` 文本 | ✅ 二进制全保真 + 结构化 JSON |
|
|
94
|
+
| `set_variable` | ✅ 字面量代码 | ✅ `bxAddVariable`(整数/浮点/复数/bool) |
|
|
95
|
+
| `clear_workspace` | ✅ | ✅ `clear` |
|
|
96
|
+
| 工作区状态保持 | ✅ `.mat` 状态文件 | ✅ BEX 进程长连接 |
|
|
97
|
+
| 文件产物反馈 | ✅ artifact marker | ✅ 从捕获输出解析 marker |
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## 🚀 快速开始
|
|
102
|
+
|
|
103
|
+
### 前置条件
|
|
104
|
+
|
|
105
|
+
- Python 3.10+
|
|
106
|
+
- [北太天元 2025](https://www.baltamatica.com/download.html)(社区版即可)
|
|
107
|
+
- 北太天元命令行入口(macOS 通常是 `/Applications/Baltamatica.app/Contents/MacOS/baltamatica`)
|
|
108
|
+
- C 编译器(仅使用 BEX 后端时需要;北太天元自带 `bex` 编译器会调用系统 clang/gcc)
|
|
109
|
+
|
|
110
|
+
### 安装
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
git clone https://github.com/wzlwww/baltamatica.mcp.git
|
|
114
|
+
cd baltamatica.mcp
|
|
115
|
+
python3 -m venv .venv
|
|
116
|
+
source .venv/bin/activate
|
|
117
|
+
pip install -e .
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### BEX 插件状态
|
|
121
|
+
|
|
122
|
+
BEX JSON 协议见 [docs/bex-protocol.md](docs/bex-protocol.md),桥接使用与生命周期见
|
|
123
|
+
[docs/bex-plugin.md](docs/bex-plugin.md) 与 [docs/bex-bridge.md](docs/bex-bridge.md)。
|
|
124
|
+
当前 BEX 桥接已实现 `execute_code`、`run_script`、`clear_workspace`、`list_variables`
|
|
125
|
+
和 `get_variable`,返回结构化成功/错误结果。
|
|
126
|
+
|
|
127
|
+
**编译并加载**(在北太天元 GUI 命令行):
|
|
128
|
+
|
|
129
|
+
```matlab
|
|
130
|
+
clear mcp_bridge
|
|
131
|
+
cd '/path/to/baltamatica.mcp/bex'
|
|
132
|
+
bex 'mcp_bridge.c' % 生成 mcp_bridge.bexmaci64 / .bexa64 / .bexw64
|
|
133
|
+
addpath('/path/to/baltamatica.mcp/bex'); savepath % 让 mcp_bridge 常驻搜索路径
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**两种运行模式**:
|
|
137
|
+
|
|
138
|
+
```matlab
|
|
139
|
+
mcp_bridge() % 前台:阻塞命令行,直到被停止
|
|
140
|
+
mcp_bridge('background') % 后台:立即返回,命令行空着(推荐,可从同一 GUI 控制)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
启动后让 Python MCP server 连接同一端口:
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
python -m baltamatica_mcp --backend bex --bex-host 127.0.0.1 --bex-port 31415
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**停止**:
|
|
150
|
+
|
|
151
|
+
```matlab
|
|
152
|
+
mcp_bridge('stop') % 可靠停止;前台被 Ctrl-C 打断后也能释放端口
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
桥接把监听 socket 记在进程全局态里:`stop` 能在前台被 Ctrl-C 打断后直接关闭 socket 释放端口,
|
|
156
|
+
重跑 `mcp_bridge()` 会自动回收泄漏的 socket 而不是 bind 失败。若 `mcp_bridge` 不在搜索路径上
|
|
157
|
+
(例如重启后 `addpath` 丢失),可用纯 TCP 的兜底工具停止,完全不依赖路径:
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
PYTHONPATH=src python -m baltamatica_mcp.bex_shutdown
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
**已知限制**:
|
|
164
|
+
|
|
165
|
+
- `get_variable`:数值/逻辑数组(实数**和复数**、任意大小)走 base64 二进制全保真回传;字符/字符串/结构体/元胞走结构化 JSON。极大的结构体/元胞会按上限截断(见 `truncated` 字段);结构体/元胞里嵌套的数值目前是列主序扁平数组。
|
|
166
|
+
- `set_variable`:支持整数(`int8`..`uint64`)、浮点(`float32`/`float64`)、复数(`complex64`/`complex128`,`data={"real":...,"imag":...}`)和 `bool`,二维以内。请求走缓冲读取 + 堆分配缓冲区,单条请求上限约 16 MB(约 100 万个 double)。
|
|
167
|
+
- 屏幕绘图可用(`figure`/`plot` 等会在 GUI 弹窗),但**图形导出到文件不可用**——遍查北太天元全部 3736 个文档函数,均无 `saveas`/`print`/`exportgraphics`/`getframe`/`imwrite` 等图形导出函数(这是北太天元本身的能力缺失)。替代方案:用 `get_variable` 取回绘图数据由 AI 端渲染,或用 `writematrix`/`writetable`/`save` 把数据导出成文件 + `BALTAMATICA_ARTIFACT=` 标记回传(见下)。
|
|
168
|
+
- CLI 后端无需编译、跨平台,适合快速上手;BEX 后端功能更全(二进制传输、变量注入、输出捕获)。
|
|
169
|
+
|
|
170
|
+
**文件产物反馈(BEX 也支持)**:`execute_code` / `run_script` 现在捕获控制台输出,脚本用
|
|
171
|
+
`fprintf('BALTAMATICA_ARTIFACT=text/csv:/tmp/x.csv\n')` 声明的文件会被解析进返回的
|
|
172
|
+
`artifacts` 列表(路径、MIME、是否存在、大小)。
|
|
173
|
+
|
|
174
|
+
### 在 Claude Desktop 中配置
|
|
175
|
+
|
|
176
|
+
编辑 `~/Library/Application Support/Claude/claude_desktop_config.json`:
|
|
177
|
+
|
|
178
|
+
```json
|
|
179
|
+
{
|
|
180
|
+
"mcpServers": {
|
|
181
|
+
"baltamatica": {
|
|
182
|
+
"command": "/path/to/baltamatica.mcp/.venv/bin/python",
|
|
183
|
+
"args": ["-m", "baltamatica_mcp"]
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### 在 Claude Code 中配置
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
claude mcp add baltamatica -- /path/to/baltamatica.mcp/.venv/bin/python -m baltamatica_mcp
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### CLI Fallback 模式(无需编译)
|
|
196
|
+
|
|
197
|
+
如果暂时没有 C 编译环境,可以使用 CLI 后端启动:
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
BALTAMATICA_CLI=/path/to/baltamatica python -m baltamatica_mcp --backend cli
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
macOS 社区版通常使用真实可执行文件,而不是 `baltamaticaC.sh` 包装脚本:
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
/Applications/Baltamatica.app/Contents/MacOS/baltamatica
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
也可以直接通过参数指定北太天元命令行入口和单次执行超时:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
python -m baltamatica_mcp --backend cli --cli-executable /path/to/baltamatica --timeout 30
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
CLI 后端会用 `.mat` 状态文件在多次 MCP 调用之间保存工作区变量。默认使用临时状态文件,
|
|
216
|
+
也可以显式指定:
|
|
217
|
+
|
|
218
|
+
```bash
|
|
219
|
+
python -m baltamatica_mcp --backend cli --state-file /tmp/baltamatica-mcp-state.mat
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### 在 Codex 中配置
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
codex mcp add baltamatica \
|
|
226
|
+
--env PYTHONPATH=/path/to/baltamatica.mcp/src \
|
|
227
|
+
--env BALTAMATICA_CLI=/Applications/Baltamatica.app/Contents/MacOS/baltamatica \
|
|
228
|
+
-- python3 -m baltamatica_mcp --backend cli --timeout 30
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
配置后可通过 MCP 工具调用 `execute_code`、`run_script`、`list_variables`、
|
|
232
|
+
`get_variable`、`set_variable` 和 `clear_workspace`。
|
|
233
|
+
|
|
234
|
+
### 开发测试
|
|
235
|
+
|
|
236
|
+
运行不依赖北太天元安装的单元测试:
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
PYTHONPATH=src pytest -q -m "not integration"
|
|
240
|
+
PYTHONPATH=src python3 -m compileall -q src tests
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
如果本机已安装北太天元 CLI,可以启用真实集成测试:
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
BALTAMATICA_CLI=/Applications/Baltamatica.app/Contents/MacOS/baltamatica \
|
|
247
|
+
PYTHONPATH=src pytest -q -m integration
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## 📁 项目结构
|
|
253
|
+
|
|
254
|
+
```
|
|
255
|
+
baltamatica.mcp/
|
|
256
|
+
├── README.md
|
|
257
|
+
├── LICENSE
|
|
258
|
+
├── pyproject.toml
|
|
259
|
+
├── .github/workflows/ci.yml
|
|
260
|
+
├── src/baltamatica_mcp/
|
|
261
|
+
│ ├── __init__.py
|
|
262
|
+
│ ├── __main__.py
|
|
263
|
+
│ ├── server.py # MCP Server & Tool 注册
|
|
264
|
+
│ ├── engine.py # 后端协议与结果类型
|
|
265
|
+
│ ├── backend_cli.py # CLI 后端:subprocess + .mat 状态文件
|
|
266
|
+
│ ├── backend_bex.py # BEX JSON-over-TCP 客户端
|
|
267
|
+
│ ├── serializer.py # 变量二进制解码/编码与结构化呈现
|
|
268
|
+
│ └── bex_shutdown.py # 纯 TCP 关闭 BEX 桥接的兜底工具
|
|
269
|
+
├── bex/
|
|
270
|
+
│ ├── CMakeLists.txt # BEX CMake 构建配置
|
|
271
|
+
│ ├── mcp_protocol.h # BEX JSON 协议常量
|
|
272
|
+
│ ├── mcp_bridge.c # BEX TCP 桥接源码(主体)
|
|
273
|
+
│ └── bex_plot_probe.c # 绘图能力探针
|
|
274
|
+
├── tests/
|
|
275
|
+
│ ├── test_backend_cli.py
|
|
276
|
+
│ ├── test_backend_bex.py
|
|
277
|
+
│ ├── test_serializer.py
|
|
278
|
+
│ ├── test_bex_sources.py
|
|
279
|
+
│ ├── test_bex_shutdown.py
|
|
280
|
+
│ ├── test_server.py
|
|
281
|
+
│ ├── test_integration_cli.py # 标记 integration
|
|
282
|
+
│ ├── test_integration_bex.py # 标记 integration
|
|
283
|
+
│ └── fixtures/sample_script.m
|
|
284
|
+
├── docs/
|
|
285
|
+
│ ├── contributing.md
|
|
286
|
+
│ ├── bex-protocol.md
|
|
287
|
+
│ ├── bex-plugin.md
|
|
288
|
+
│ ├── bex-bridge.md
|
|
289
|
+
│ ├── bex-plot-probe.md
|
|
290
|
+
│ ├── releasing.md
|
|
291
|
+
│ └── pr-plan.md
|
|
292
|
+
├── scripts/
|
|
293
|
+
│ └── build_bex.sh # 用北太天元 bex 编译器编译本平台桥接二进制
|
|
294
|
+
└── examples/
|
|
295
|
+
├── monte_carlo_pi.m
|
|
296
|
+
├── numerical_pipeline_demo.m
|
|
297
|
+
├── artifact_export_demo.m
|
|
298
|
+
└── bex_plot_probe_demo.m
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
---
|
|
302
|
+
|
|
303
|
+
## 🛠️ MCP Tools(暴露给 AI 的工具接口)
|
|
304
|
+
|
|
305
|
+
| Tool 名称 | 参数 | 描述 | CLI 后端 | BEX 后端 |
|
|
306
|
+
|:---|:---|:---|:---:|:---:|
|
|
307
|
+
| `execute_code` | `code: string` | 执行代码并返回控制台输出 | ✅ | ✅ `evalc` 捕获输出 |
|
|
308
|
+
| `run_script` | `file_path: string` | 运行 `.m` 脚本文件 | ✅ | ✅ `evalc` 捕获输出 |
|
|
309
|
+
| `list_variables` | — | 列出工作区所有变量(名称、类型、维度) | ✅ `whos` 解析 | ✅ SDK 变量枚举 |
|
|
310
|
+
| `get_variable` | `name: string` | 获取变量值 | ✅ `disp()` 文本 | ✅ 二进制全保真 + 结构化 JSON |
|
|
311
|
+
| `set_variable` | `name, data, dtype?` | 创建/覆盖工作区变量 | ✅ 字面量代码 | ✅ `bxAddVariable`(整数/浮点/复数/bool) |
|
|
312
|
+
| `clear_workspace` | — | 清空工作区状态 | ✅ | ✅ `clear` |
|
|
313
|
+
|
|
314
|
+
### 文件产物反馈
|
|
315
|
+
|
|
316
|
+
脚本可以通过标准输出声明生成的文件产物:
|
|
317
|
+
|
|
318
|
+
```matlab
|
|
319
|
+
fprintf('BALTAMATICA_ARTIFACT=text/csv:/tmp/result.csv\n');
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
MCP 返回值会包含 `artifacts` 列表,记录文件路径、MIME 类型、是否存在和文件大小。若省略 MIME 类型:
|
|
323
|
+
|
|
324
|
+
```matlab
|
|
325
|
+
fprintf('BALTAMATICA_ARTIFACT=/tmp/plot.png\n');
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
服务会根据扩展名推断常见类型,例如 `image/png`、`application/pdf`、`text/csv`。示例见
|
|
329
|
+
`examples/artifact_export_demo.m`。
|
|
330
|
+
|
|
331
|
+
---
|
|
332
|
+
|
|
333
|
+
## 🗺️ 开发路线图 (Roadmap)
|
|
334
|
+
|
|
335
|
+
详细 PR 拆分和实现方案见 [docs/pr-plan.md](docs/pr-plan.md)。
|
|
336
|
+
|
|
337
|
+
### 已完成
|
|
338
|
+
|
|
339
|
+
- [x] MCP Server 骨架(`FastMCP`)
|
|
340
|
+
- [x] CLI 后端(`subprocess` + `baltamatica -nodesktop -s`)
|
|
341
|
+
- [x] `execute_code` / `run_script`
|
|
342
|
+
- [x] `.mat` 状态文件保持 CLI 工作区
|
|
343
|
+
- [x] `list_variables` / `get_variable` / `clear_workspace`
|
|
344
|
+
- [x] 可选真实集成测试与 GitHub Actions CI
|
|
345
|
+
- [x] 数值计算示例:`examples/numerical_pipeline_demo.m`
|
|
346
|
+
- [x] 文件产物反馈:`BALTAMATICA_ARTIFACT=...`
|
|
347
|
+
- [x] BEX JSON 协议设计与 Python TCP 客户端骨架
|
|
348
|
+
- [x] BEX 插件最小可用版
|
|
349
|
+
- [x] BEX `list_variables` / `get_variable` 文本变量读取
|
|
350
|
+
- [x] BEX 小型实数数值/逻辑数组结构化 JSON 读取
|
|
351
|
+
- [x] BEX 生命周期健壮化:可靠 `stop` / Ctrl-C 恢复 / 自愈重绑 / `background` 模式 / 状态返回值
|
|
352
|
+
- [x] BEX `get_variable` 存在性预检查(避免不存在变量在 GUI 里回显 `evalin` 错误)
|
|
353
|
+
- [x] BEX 数值/逻辑二进制全保真传输(含复数)+ 字符/字符串/结构体/元胞结构化序列化
|
|
354
|
+
- [x] BEX `set_variable`:从标量/向量/矩阵注入工作区变量(`bxAddVariable`)
|
|
355
|
+
- [x] BEX `execute_code` / `run_script` 控制台输出捕获(`evalc` + `try/catch`,成功返回 stdout、失败返回错误信息)
|
|
356
|
+
- [x] BEX 文件产物反馈:从捕获输出解析 `BALTAMATICA_ARTIFACT=` 标记
|
|
357
|
+
- [x] 绘图导出调研:确认北太天元无任何图形导出函数(3736 个函数全查),改走数据侧回传
|
|
358
|
+
- [x] `background` 模式线程安全评估:桥接串行化所有请求(800 顺序 + 并发双连接零错误);唯一风险是后台模式下手动同时操作 GUI(见 [docs/bex-bridge.md](docs/bex-bridge.md) 线程与并发一节)
|
|
359
|
+
- [x] 自动化 BEX 集成测试(`bex` 编译校验 + 运行中桥接往返,标记 `integration`)
|
|
360
|
+
- [x] 发布体验:`baltamatica-mcp` 控制台入口、项目 URL、`set_variable` 工具与文档
|
|
361
|
+
|
|
362
|
+
- [x] BEX `set_variable` 扩展:整数(int8..uint64)/浮点(float32/64)/复数(complex64/128)类型 + 缓冲读取大 payload(上限约 16 MB)
|
|
363
|
+
- [x] 打包就绪:`python -m build` 可出 sdist/wheel、控制台入口、`scripts/build_bex.sh` 编译桥接二进制、发布流程见 [docs/releasing.md](docs/releasing.md)
|
|
364
|
+
|
|
365
|
+
### 下一步
|
|
366
|
+
|
|
367
|
+
- [ ] BEX 图形导出到文件:受限于北太天元本身缺少 `saveas`/`print`/`exportgraphics` 等函数,需厂商侧支持
|
|
368
|
+
- [ ] 实际发布到 PyPI 并附带预编译 BEX 二进制的 GitHub Release(需维护者执行 `twine upload` + 各平台编译)
|
|
369
|
+
|
|
370
|
+
---
|
|
371
|
+
|
|
372
|
+
## 🔧 核心依赖的北太天元 SDK API
|
|
373
|
+
|
|
374
|
+
本项目依赖北太天元 BEX SDK (`bex.h`, API v3.9) 提供的以下关键接口:
|
|
375
|
+
|
|
376
|
+
| API 函数 | 功能 |
|
|
377
|
+
|:---|:---|
|
|
378
|
+
| `bxEvalString(expr)` | 执行一条字符串命令 |
|
|
379
|
+
| `bxEvalIn(ws, expr, plhs)` | 在指定工作区执行表达式并捕获返回值 |
|
|
380
|
+
| `bxCallBaltamatica(...)` | 通过函数名调用北太天元内置/自定义函数 |
|
|
381
|
+
| `bxGetVariableNames(result, num)` | 获取当前工作区所有变量名 |
|
|
382
|
+
| `bxAddVariable(name, value, mode)` | 向工作区注入变量 |
|
|
383
|
+
| `bxRemoveVariable(name)` | 删除工作区变量 |
|
|
384
|
+
| `bxGetDoublesRO/bxGetComplexDoublesRO/...(ba)` | 只读获取各类型矩阵数据指针(二进制回传) |
|
|
385
|
+
| `bxCreateDoubleMatrix/bxCreateLogicalMatrix(...)` | 创建矩阵(`set_variable` 注入) |
|
|
386
|
+
| `bxGetDoublesRW/bxGetLogicalsRW(ba)` | 读写数据指针(写入注入的数据) |
|
|
387
|
+
| `bxGetString / bxGetFieldByNumberRO / bxGetCellRO` | 读取字符串/结构体字段/元胞元素(结构化序列化) |
|
|
388
|
+
| `bxGetDimensions(ba)` | 获取数组维度信息 |
|
|
389
|
+
| `bxGetClassID(ba)` | 获取变量的数据类型 |
|
|
390
|
+
| `bxArrayToCStr(ba, ...)` / `bxAsCStr(ba, ...)` | 将变量格式化/转换为字符串 |
|
|
391
|
+
| `bxPrintf(...)` | 向命令行窗口输出(桥接状态信息) |
|
|
392
|
+
|
|
393
|
+
`execute_code` / `run_script` 通过 `bxCallBaltamatica` 调用内置的 `evalc` 函数捕获控制台输出。
|
|
394
|
+
|
|
395
|
+
---
|
|
396
|
+
|
|
397
|
+
## 🤝 贡献指南
|
|
398
|
+
|
|
399
|
+
欢迎贡献代码!请参阅 [CONTRIBUTING.md](docs/contributing.md)。
|
|
400
|
+
|
|
401
|
+
### 分支模型
|
|
402
|
+
|
|
403
|
+
- `main`:稳定发布分支,始终保持可用
|
|
404
|
+
- `develop`:开发主分支,所有 feature 合入此处
|
|
405
|
+
- `feature/<name>`:功能分支,从 `develop` 拉出
|
|
406
|
+
- `fix/<name>`:Bug 修复分支
|
|
407
|
+
|
|
408
|
+
### 提交规范
|
|
409
|
+
|
|
410
|
+
使用 [Conventional Commits](https://www.conventionalcommits.org/) 规范:
|
|
411
|
+
|
|
412
|
+
```
|
|
413
|
+
feat: 添加 execute_code 工具实现
|
|
414
|
+
fix: 修复控制台输出截断问题
|
|
415
|
+
docs: 更新 README 安装说明
|
|
416
|
+
refactor: 重构引擎通信模块
|
|
417
|
+
test: 添加变量序列化单元测试
|
|
418
|
+
chore: 更新 CI 配置
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
---
|
|
422
|
+
|
|
423
|
+
## 📄 License
|
|
424
|
+
|
|
425
|
+
MIT License © 2026 [wzlwww](https://github.com/wzlwww)
|