Skip to main content
Glama
README.md
# Stata Econ MCP

**English** | [简体中文](README.zh-CN.md)

Let Claude (Claude Desktop chat or Claude Code) drive **your own local Stata** to run regressions.

- Uses the official pystata engine that ships with Stata (`<Stata install dir>/utilities/pystata`), so it runs your
  own Stata, license, and installed community packages (reghdfe, csdid, ...).
- Stata runs in a separate worker process with a persistent session: data, `estimates store` results and locals
  survive between calls. If Stata hangs, `stata_restart` kills and restarts it.
- Ships with an applied-econometrics playbook (panel FE, DiD, staggered DiD, DDD, event studies, LaTeX tables,
  referee standards) that Claude reads through `stata_guide`.

## Requirements
- Stata 17 or later (MP / SE / BE), installed and licensed on the same machine
- [uv](https://docs.astral.sh/uv/) (or Python 3.11+)
- Tested on macOS (Apple Silicon) with Stata 18 MP. Linux and Windows should work in principle but are untested;
  set `STATA_DIR` there.

## Installation
```bash
git clone https://github.com/shanyuzhang/stata-econ-mcp.git
cd stata-econ-mcp
uv sync
```
The server executable is `<repo path>/.venv/bin/stata-econ-mcp` (called `<SERVER>` below; use the absolute path).

### Claude Code
```bash
claude mcp add stata --scope user -- "<SERVER>"
```

### Claude Desktop
Edit `~/Library/Application Support/Claude/claude_desktop_config.json`
(Windows: `%APPDATA%\Claude\claude_desktop_config.json`) and add under `mcpServers`:
```json
{
  "mcpServers": {
    "stata": {
      "command": "<SERVER>",
      "args": []
    }
  }
}
```
Save, then fully quit and reopen Claude Desktop.

If Stata is not in a default location (on macOS the server searches `/Applications/Stata`,
`/Applications/StataNow`, ...), add environment variables to the entry:
```json
"env": { "STATA_DIR": "/path/to/Stata", "STATA_EDITION": "mp" }
```

## Tools
| Tool | Purpose |
|---|---|
| `stata_run` | Run a block of Stata code (quick checks) |
| `stata_do` | Save code as a do-file and run it with a log (for real analysis) |
| `stata_wait` / `stata_restart` | Keep waiting for a long job / restart Stata |
| `stata_data` | Inspect data: overview, describe, summarize, xtdescribe, missing values, ... |
| `stata_results` | Structured coefficients, SEs, p-values, N, clusters, FE |
| `stata_table` | Publication LaTeX regression tables (esttab, booktabs) |
| `stata_graph` | Export graphs (PNG shown in chat + PDF for LaTeX) |
| `stata_help` | Read help files of official and community commands |
| `stata_guide` | The econometrics playbook |
| `stata_status` / `stata_set_workdir` | Status and installed-package check / switch project folder |

## Where outputs go
`<project folder>/stata_output/{do,logs,tables,figures}`

- Claude Code (or the Code tab in Claude Desktop): the project folder is the session's folder.
- Claude Desktop chat (no folder): defaults to `~/Documents/StataMCP/`. You can also tell Claude
  "put the project in <folder>" and it will call `stata_set_workdir`.
- Every command is appended to `logs/history_<date>.do` for reproducibility.

## Environment variables (optional)
| Variable | Default |
|---|---|
| `STATA_DIR` | auto-detected |
| `STATA_EDITION` | inferred from the install (mp / se / be) |
| `STATA_MCP_WORKDIR` | auto (session folder, then the default folder) |
| `STATA_MCP_DEFAULT_DIR` | `~/Documents/StataMCP` |
| `STATA_MCP_OUTPUT_SUBDIR` | `stata_output` |
| `STATA_MCP_WAIT_SECONDS` | `240` (after this the job keeps running in the background; collect it with `stata_wait`) |

## Recommended community packages
```stata
ssc install reghdfe
ssc install ftools
ssc install estout
ssc install coefplot
ssc install csdid
ssc install drdid
ssc install did_imputation
ssc install eventstudyinteract
ssc install event_plot
ssc install boottest
```
`stata_status` lists which ones are installed.

## Known issues
- If your `profile.do` contains GUI-only commands (such as `window menu`), it stops early in headless mode.
  The server re-applies `more off`, `type double`, `maxvar` and `linesize` after startup.
- Stata's embedded Python bridge cannot handle interpreter paths that contain spaces. If the repo lives in such
  a folder, the worker creates a space-free symlink (`~/.cache/stata-econ-mcp/python`) automatically.
- The safety rules (no `shell` / `!`, no deleting user files, ask before installing packages) are instructions to
  Claude, not enforced in code. The server runs your full local Stata, so only use it with clients you trust.

## License
MIT

TDQS

A3.7/5.0

Scored across 12 tools

Disambiguation4/5

Most tools target clearly distinct facets of the Stata workflow (run, wait, restart, workdir, status, data, results, graph, table, help, guide). The main risk is stata_run vs stata_do, which both execute code, but the descriptions explicitly differentiate quick exploration from saved do-files with logs, and stata_help vs stata_guide are separated by 'official help file' vs 'econometrics playbook'.

Naming Consistency4/5

All names share a uniform snake_case `stata_` prefix, making the set instantly recognizable. There is a minor stylistic split between verb-led names (stata_run, stata_do, stata_wait, stata_restart, stata_set_workdir) and noun-led names (stata_data, stata_results, stata_graph, stata_table), but it stays readable and predictable.

Tool Count5/5

Twelve tools is a well-scoped set for a persistent-session Stata server: session control, execution, inspection, export, and reference docs. Each tool maps to a real step in an econometric workflow with no filler.

Completeness4/5

The surface covers the full lifecycle: start/run/wait/restart the session, set the project folder, inspect data, extract structured estimates, export figures and LaTeX tables, plus help and a playbook. Minor gaps exist around browsing or cleaning previously produced output files (figures/tables/logs) and non-destructive interrupt, but stata_run is a general escape hatch that mitigates most of these.