loading…
Search for a command to run...
loading…
Controls the Stata GUI through Windows Stata Automation COM, enabling do-file management, command execution, and data analysis within natural language workflows
Controls the Stata GUI through Windows Stata Automation COM, enabling do-file management, command execution, and data analysis within natural language workflows.
通过 Windows Stata Automation COM 让 Claude Code 控制 Stata GUI 的 MCP 服务器。
A Claude Code MCP server that controls the Stata GUI through Windows Stata Automation COM.

D:/Stata18/mcp/
├── README.md # 项目文档
├── pyproject.toml # Python 项目配置
├── .gitignore
├── stata_mcp.py # 兼容启动器(推荐配置路径)
├── src/
│ └── stata_mcp/
│ ├── __init__.py
│ └── server.py # MCP 服务器主程序
└── assets/ # 项目资源
# 运行时自动创建的项目本地缓存(每个项目目录)
project-dir/.stata-mcp/
├── cache/
│ └── task_registry.json # Session/do/log 关系记录
└── dofiles/ # 生成的 do 文件
| 要求 | 版本 | 备注 |
|---|---|---|
| 操作系统 | Windows | 必须。依赖 Windows COM 和 pywin32 |
| Stata | 18 MP | 须已注册 Automation COM 接口 |
| Python | 3.10+ | 推荐 3.11+ |
| 包 | mcp, pywin32 | 通过 pip 安装 |
| Claude 编辑器 | Latest | 已安装 Claude Code 扩展 |
推荐放在 Stata 安装目录下的 mcp 文件夹:
# 方式一:克隆到 Stata 目录
cd D:\Stata18
git clone https://github.com/shichengg/stata-mcp mcp
# 方式二:手动下载并解压
# 下载 ZIP 后解压到 D:\Stata18\mcp
高级用户也可放在任意稳定目录,但后续 Claude Code 配置需指向实际 stata_mcp.py 路径。
推荐方案(使用虚拟环境):
cd D:\Stata18\mcp
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install mcp pywin32
快速方案(全局安装):
pip install mcp pywin32
必须以 管理员身份 运行 PowerShell,执行:
Start-Process -FilePath "D:\Stata18\StataMP-64.exe" -ArgumentList "/Register" -Wait
⚠️ 注意:
- 替换
D:\Stata18\StataMP-64.exe为你的 Stata 安装路径- 不要在 Git Bash 中运行此命令,因为 Git Bash 可能改写
/Register- Windows 7/8 用户可能需要禁用 UAC 或使用
runas命令
运行以下命令验证 Stata Automation 是否已正确注册:
python -c "import win32com.client; s = win32com.client.Dispatch('stata.StataOLEApp'); s.DoCommand('display 12345')"
预期结果:Stata GUI 打开或被连接,并在结果窗口显示 12345。
推荐方案(使用 claude mcp add 命令):
在 PowerShell 中运行以下命令。首先移除旧配置(如有),然后添加新配置:
# 移除旧配置(如果存在)
claude mcp remove stata --scope user
# 标准 Python 环境
claude mcp add stata --scope user -- python D:\Stata18\mcp\stata_mcp.py
如果使用 Conda 环境:
# 移除旧配置
claude mcp remove stata --scope user
# 使用 Conda Python
claude mcp add stata --scope user -- D:/anaconda/python.exe D:\Stata18\mcp\stata_mcp.py
💡 提示:
- 将
D:/anaconda替换为你的 Conda 安装路径- 如果 Stata 安装在其他路径,相应调整
D:\Stata18\mcp\stata_mcp.py- 也可在 Claude Code 中直接运行此命令
配置后重启 Claude Code,确认工具列表中出现 stata MCP。
替代方案(手动编辑 Claude Code 配置):
如果 claude mcp add 不可用,可手动编辑 Claude Code 的 MCP 配置文件,添加:
"stata": {
"command": "python",
"args": ["D:\\Stata18\\mcp\\stata_mcp.py"]
}
💡 配置文件位置:通常在
%APPDATA%\Code\User\settings.json或通过 Claude Code 的设置界面配置
通常用户无需设置。如果有特殊需求(如非标准安装路径),可在启动 MCP 前设置以下环境变量:
| 环境变量 | 默认值 | 说明 |
|---|---|---|
STATA_MCP_DIR |
自动检测项目根目录 | MCP 项目目录 |
STATA_DIR |
MCP 上一级目录 | Stata 安装目录 |
STATA_EXE |
%STATA_DIR%/StataMP-64.exe |
Stata 可执行文件路径 |
STATA_COM_PROG_ID |
stata.StataOLEApp |
Stata Automation COM ProgID |
示例:
$env:STATA_DIR = "C:\Program Files\Stata18"
$env:STATA_COM_PROG_ID = "stata.StataOLEApp"
python D:\Stata18\mcp\stata_mcp.py
Stata MCP 服务器提供以下工具供 Claude Code 调用:
stata_run - 在最近 Session 中执行命令在最近使用的 Stata 会话中执行一条或多条命令。如果还没有运行过 do 文件,会提示先运行一个。
参数:
commands (string, required): 要执行的 Stata 命令,多行用换行符分隔stata_run_dofile - 运行 Do 文件(Session 管理)运行一个 do 文件,并按 do 路径或 session_id 维护独立的 Stata 窗口。
参数:
path (string, required): do 文件的绝对路径session_id (string, optional): 自定义 session IDrole (string, optional): do 在 session 中的角色 (entry/source/auxiliary),默认 entrylog_mode (string, optional): 日志模式 (replace/append),默认 replacestata_session - 管理 Stata 会话列出、查询、销毁或切换 Stata 会话。
参数:
action (string, required): list/get/destroy/set_recentsession_id (string, optional): session IDstata_write_dofile - 写入 Do 文件生成并保存 Stata do 文件到磁盘。
参数:
content (string, required): do 文件内容filename (string, optional): 文件名或完整路径stata_read_dofile - 读取 Do 文件读取 do 文件内容。
参数:
path (string, required): do 文件的完整路径stata_append_dofile - 追加 Do 文件内容向已有 do 文件追加代码。
参数:
path (string, required): do 文件的完整路径content (string, required): 要追加的代码stata_read_log - 读取 Stata Log读取 Stata text log 文件。
参数:
path (string, optional): log 文件路径,留空则读取最近 session 的日志tail_lines (integer, optional): 只读取最后 N 行output_format (string, optional): 输出格式 (full/core/dict),默认 fullstata_install_package - 安装包在 Stata GUI 中安装外部包(如 estout、ivreg2 等)。
参数:
package (string, required): 包名称source (string, optional): 安装来源 (ssc/net),默认 sscstata_get_results - 获取存储结果显示 r() 或 e() 存储结果。
参数:
type (string, required): 结果类型 (r/e)stata_get_data_info - 查看数据信息显示当前数据集的基本信息。
stata_get_data_schema - 获取数据结构和样本读取当前数据集结构、缺失摘要和样本预览。
参数:
sample_rows (integer, optional): 样本预览行数,默认 20include_codebook (boolean, optional): 是否包含 codebook,默认 trueinclude_sample (boolean, optional): 是否包含样本预览,默认 trueinclude_missing (boolean, optional): 是否包含缺失值摘要,默认 truestata_status - 检查服务器状态查看 MCP 服务器状态和活跃会话。
# 1. 加载数据
stata_run_dofile(path="D:/project/load_data.do", session_id="explore")
# 2. 查看数据结构
stata_get_data_schema(sample_rows=50)
# 3. 运行探索性分析
stata_run(commands="summarize\ncorrelation")
# 1. 数据清洗
stata_run_dofile(path="D:/analysis/01_clean.do", session_id="proj_v1", role="source")
# 2. 主分析
stata_run_dofile(path="D:/analysis/02_model.do", session_id="proj_v1", role="entry")
# 3. 获取系数
stata_get_results(type="e")
# 首次运行
stata_run_dofile(path="D:/project/analysis.do", session_id="debug_v1")
# 检查结果
stata_read_log(output_format="dict")
# 修改代码
stata_append_dofile(path="D:/project/analysis.do", content="* Fixed: ...")
# 重新运行(同一 session,append 模式)
stata_run_dofile(path="D:/project/analysis.do", session_id="debug_v1", log_mode="append")
pywin32 不可用pip install --upgrade pywin32
以 管理员身份 重新注册 Stata Automation:
Start-Process -FilePath "D:\Stata18\StataMP-64.exe" -ArgumentList "/Register" -Wait
stata_run 提示没有 do 文件会话先运行一个 do 文件:
stata_run_dofile(path="D:/project/analysis.do")
之后无路径命令会发送到这个最近会话。
Claude 不能读取屏幕内容。使用以下方案之一:
stata_read_log 读取stata_get_data_schema() 读取当前数据结构快照stata_get_results() 获取存储结果
D:/Stata18/mcp/
├── README.md # Documentation
├── pyproject.toml # Python configuration
├── .gitignore
├── stata_mcp.py # Compatibility launcher
├── src/
│ └── stata_mcp/
│ ├── __init__.py
│ └── server.py # Main MCP server
└── assets/ # Resources
project-dir/.stata-mcp/
├── cache/
│ └── task_registry.json
└── dofiles/
| Requirement | Version | Notes |
|---|---|---|
| OS | Windows | Required for Windows COM |
| Stata | 18 MP | Must have Automation COM registered |
| Python | 3.10+ | Recommended 3.11+ |
| Packages | mcp, pywin32 | Install via pip |
| Claude Editor | Latest | With Claude Code extension |
cd D:\Stata18
git clone https://github.com/shichengg/stata-mcp mcp
cd D:\Stata18\mcp
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install mcp pywin32
Run PowerShell as Administrator:
Start-Process -FilePath "D:\Stata18\StataMP-64.exe" -ArgumentList "/Register" -Wait
python -c "import win32com.client; s = win32com.client.Dispatch('stata.StataOLEApp'); s.DoCommand('display 12345')"
Run the following commands in PowerShell. First remove old configuration (if exists), then add new configuration:
# Remove old configuration (if it exists)
claude mcp remove stata --scope user
# Standard Python environment
claude mcp add stata --scope user -- python D:\Stata18\mcp\stata_mcp.py
If using Conda environment:
# Remove old configuration
claude mcp remove stata --scope user
# Use Conda Python
claude mcp add stata --scope user -- D:/anaconda/python.exe D:\Stata18\mcp\stata_mcp.py
💡 Tips:
- Replace
D:/anacondawith your Conda installation path- If Stata is installed elsewhere, adjust the path accordingly
- You can also run this command directly in Claude Code
Restart Claude Code and verify stata appears in the MCP tools list.
| Variable | Default | Purpose |
|---|---|---|
STATA_MCP_DIR |
Auto-detect | MCP project directory |
STATA_DIR |
Parent of MCP | Stata installation directory |
STATA_EXE |
Default path | Stata executable path |
STATA_COM_PROG_ID |
stata.StataOLEApp | COM ProgID |
Stata MCP provides 12 main tools:
stata_run_dofile(path="D:/project/load_data.do", session_id="explore")
stata_get_data_schema(sample_rows=50)
stata_run(commands="summarize\ncorrelation")
stata_run_dofile(path="D:/analysis/01_clean.do", session_id="proj_v1", role="source")
stata_run_dofile(path="D:/analysis/02_model.do", session_id="proj_v1", role="entry")
stata_get_results(type="e")
stata_run_dofile(path="D:/project/analysis.do", session_id="debug_v1")
stata_read_log(output_format="dict")
stata_append_dofile(path="D:/project/analysis.do", content="* Fixed: ...")
stata_run_dofile(path="D:/project/analysis.do", session_id="debug_v1", log_mode="append")
pywin32 not availablepip install --upgrade pywin32
Re-register Stata Automation as Administrator:
Start-Process -FilePath "D:\Stata18\StataMP-64.exe" -ArgumentList "/Register" -Wait
Run a do file first:
stata_run_dofile(path="D:/project/analysis.do")
Use stata_read_log() or stata_get_data_schema():
stata_read_log(output_format="dict")
stata_get_data_schema()
Open source. See LICENSE file for details.
Contributions welcome! Please open issues and pull requests on GitHub.
Repository: github.com/shichengg/stata-mcp
Expected result: the Stata GUI opens or is connected, and displays 12345.
Use claude mcp add:
claude mcp add stata -- python D:\Stata18\mcp\stata_mcp.py
If you installed the project somewhere else, replace the path with your actual path. Example:
claude mcp add stata -- python "C:\Program Files\Stata18\mcp\stata_mcp.py"
Restart Claude Code after adding the MCP server, then confirm that the stata tools are available.
If you do not use claude mcp add, you can configure MCP manually with JSON:
"stata": {
"command": "python",
"args": ["D:\\Stata18\\mcp\\stata_mcp.py"]
}
You can also point directly to the main server file:
"stata": {
"command": "python",
"args": ["D:\\Stata18\\mcp\\src\\stata_mcp\\server.py"]
}
The compatibility launcher stata_mcp.py is usually recommended because it keeps your Claude Code configuration stable if the internal project layout changes later.
Most users do not need these. If your installation path or COM ProgID is unusual, you can configure:
| Variable | Default | Purpose |
|---|---|---|
STATA_MCP_DIR |
auto-detected project root | MCP project directory |
STATA_DIR |
parent of MCP directory | Stata installation directory |
STATA_EXE |
%STATA_DIR%/StataMP-64.exe |
used in README registration examples |
STATA_COM_PROG_ID |
stata.StataOLEApp |
Stata Automation ProgID |
| Tool | Purpose |
|---|---|
stata_run |
Run commands in the most recent do-file session |
stata_run_dofile |
Run/reuse a Stata session by absolute do-file path |
stata_write_dofile |
Write a do file; simple names go to runtime/dofiles, absolute paths write to that location |
stata_read_dofile |
Read a do file |
stata_append_dofile |
Append content to a do file |
stata_read_log |
Read a Stata text log |
stata_install_package |
Install external packages in the Stata GUI |
stata_get_results |
Display return list or ereturn list in the Stata GUI |
stata_get_data_info |
Display describe in the Stata GUI |
stata_get_data_schema |
Return current data structure, missing summary, and sample rows to Claude |
stata_status |
Show MCP and Stata session status |
stata_run_dofile(path) uses the absolute do-file path as the session key.stata_run, stata_get_results, stata_get_data_info, and stata_get_data_schema are sent to the most recent do-file session by default.stata_get_data_schema creates a text log snapshot in the most recent do-file session and returns it to Claude. By default, it includes:
describecodebook, compactmisstable summarizelist in 1/20, abbreviate(20)noteslabel dirOptions:
| Option | Default | Description |
|---|---|---|
sample_rows |
20 |
Number of sample rows, max 1000 |
include_codebook |
true |
Include compact codebook |
include_sample |
true |
Include sample preview |
include_missing |
true |
Include missing-value summary |
This lets Claude see not only variable names, types, and labels, but also a small real-data sample, which helps it write better Stata code.
stata_get_data_schema overwrites its own schema snapshot log because it represents the current data state.stata_read_log to read it.pywin32 is unavailableRun:
pip install pywin32
Register Stata Automation again:
Start-Process -FilePath "D:\Stata18\StataMP-64.exe" -ArgumentList "/Register" -Wait
Do not run the /Register command directly in Git Bash, because Git Bash may rewrite /Register as a path.
stata_run says there is no do-file sessionRun a do file first:
stata_run_dofile(path="D:/Stata18/mcp/examples/browse_test.do")
After that, pathless commands are sent to the most recent session.
Claude cannot read screen contents directly. To let Claude analyze Stata output, write a Stata text log and read it with stata_read_log; or use stata_get_data_schema for a current data snapshot.
Run in your terminal:
claude mcp add stata-mcp -- npx Not sure what to pick?
Find your stack in 60 seconds
Author?
Embed badge for your README
Browse similar
All development MCPs