You can integrate AI into Stata using external code editors today, and the setup takes about ten minutes. The trick is not a new Stata command — it is the Model Context Protocol (MCP), which lets an AI assistant inside VS Code, Cursor, or Antigravity actually run your .do files and read the output. The most widely used bridge is the open-source Stata MCP extension, released under an MIT license. This guide walks through installation, client configuration, token control, and the guardrails you should keep in place.
How to integrate AI into Stata using external code editors
Stata has no built-in chat assistant. It does, however, run happily as a background process that another program can drive. That gap is what the current wave of tooling fills: a small local server exposes Stata as a set of callable tools, and your editor’s AI assistant calls them.
MCP is the standard that makes this portable. It is an open protocol built on JSON-RPC that lets AI applications connect to external tools and data sources through one interface instead of a dozen bespoke integrations, as described in the official MCP specification. Write one Stata server, and Copilot, Claude Code, Cursor, and Codex can all use it.
StataCorp itself has acknowledged the trend. Its Stata News “Community corner” feature on AI tools for Stata lists several community MCP projects by name, including Thomas Monk’s mcp-stata server and the Stata Workbench extension it powers. These are community-led, not StataCorp products — a distinction worth remembering before you route confidential microdata through one.
What actually changes in your workflow
| Task | Classic Stata Do-file Editor | Editor + MCP |
|---|---|---|
| Write a regression | You type it | Assistant drafts it, you review |
| Read the error | You read the log | Assistant reads the log and patches the code |
| Run a selection | Ctrl+D in Stata | Ctrl+Shift+Enter in the editor |
| Produce a report | Manual export | Assistant can chain estimation, graphs, and LaTeX export |
| Version control | Bolted on | Native Git in the editor |
The honest summary: you are not getting a smarter econometrician. You are getting a very fast research assistant who never gets bored of fixing varlist typos.
Requirements before you start
- Stata 17 or higher, licensed and installed locally — the extension’s stated minimum.
- VS Code, Cursor, or Antigravity as the editor.
- The uv package manager, which the extension installs automatically if missing. It has bundled uv since version 0.1.8 to speed up Python environment setup.
- An AI client: GitHub Copilot, Claude Code, Cursor’s agent, Cline, or Codex.
If you are still on Stata 16, this route is closed. Stata 19 shipped on 8 April 2025 with H2O-based machine learning, conditional average treatment effects, and high-dimensional fixed effects, per StataCorp’s release announcement, so an upgrade buys you more than MCP compatibility.
Step-by-step: install the Stata MCP extension
- Open VS Code, Cursor, or Antigravity.
- Open the Extensions view with
Ctrl+Shift+X(Cmd+Shift+Xon macOS). - Search for Stata MCP and click Install.
- Wait. First-time dependency setup can take up to two minutes; later launches are instant.
- Confirm the status bar shows Stata. That means the local MCP server auto-started.
Prefer the command line? The publisher ID is DeepEcon.stata-mcp:
code --install-extension DeepEcon.stata-mcp
cursor --install-extension DeepEcon.stata-mcp
antigravity --install-extension DeepEcon.stata-mcp
Cursor and Antigravity pull from the Open VSX Registry rather than the VS Code Marketplace. Offline machines can install a downloaded .vsix from the project’s releases page instead.
Verify the server is alive
The server listens on port 4000 and exposes two endpoints: http://localhost:4000/mcp-streamable for Streamable HTTP (preferred) and http://localhost:4000/mcp for legacy SSE. A one-line check:
curl -s http://localhost:4000/health
A {"status":"ok"} response means Stata is reachable. Anything else usually means a port collision or an undetected Stata path.
Connect your AI assistant
GitHub Copilot
Copilot supports MCP starting with VS Code 1.102. Create .vscode/mcp.json in your project root, or use the Command Palette entry MCP: Open User Configuration for a global setup:
{
"servers": {
"stata-mcp": {
"type": "http",
"url": "http://localhost:4000/mcp-streamable"
}
}
}
Reload the window, open Copilot Chat, and type @mcp. You should see stata_run_selection and stata_run_file. On older VS Code builds without Streamable HTTP, switch to "type": "sse" and the /mcp endpoint.
Claude Code
claude mcp add --transport http stata-mcp http://localhost:4000/mcp-streamable --scope user
Restart the IDE afterwards. MCP tool lists do not refresh mid-session, which is the single most common reason people conclude the setup “didn’t work.”
Claude Desktop
Add it as a custom connector under Settings → Connectors → Add custom connector, pointing at the streamable URL. If you prefer editing claude_desktop_config.json, you need a local stdio wrapper such as mcp-remote, because that config file does not accept remote URLs directly.
Running code day to day
| Action | Windows / Linux | macOS |
|---|---|---|
| Run selection | Ctrl+Shift+Enter | Cmd+Shift+Enter |
| Run whole .do file | Ctrl+Shift+D | Cmd+Shift+D |
| Stop execution | Ctrl+Shift+C | Cmd+Shift+C |
| Restart session | Toolbar button or “Stata: Restart Session” | |
Restarting clears in-memory data, globals, and programs — the equivalent of closing and reopening Stata. The extension also ships a data viewer that accepts Stata if conditions (for example price > 5000 & mpg < 30) and displays graphs either in an editor webview or an external browser.
Cursor and Antigravity hide the toolbar icons by default. Use the … menu in the editor title bar, then Configure Icon Visibility, and switch them on.
Settings that matter most
| Setting | Default | Why you would change it |
|---|---|---|
stata-vscode.stataPath | Auto-detected | Non-standard install location |
stata-vscode.stataEdition | mp | You license SE or BE |
stata-vscode.mcpServerPort | 4000 | Port already taken |
stata-vscode.workingDirectory | dofile | Set to parent when scripts live in a code/ subfolder |
stata-vscode.runFileTimeout | 600 seconds | Long bootstraps or simulations |
stata-vscode.resultDisplayMode | compact | Switch to full when debugging loop output |
stata-vscode.maxOutputTokens | 10000 | Large outputs get written to file and the path is returned instead |
stata-vscode.maxSessions | 100 | License limits on concurrent instances |
Compact mode and your token bill
Stata is verbose. Compact mode strips loop code echoes, program and Mata block definitions, command echoes, and chatter like “(N missing values generated)” before the output reaches the model. Keep it on unless you are diagnosing a loop, in which case the noise is the point.
Multi-session execution
Multi-session mode is on by default and gives each session isolated data, variables, and macros, with idle sessions destroyed after 3,600 seconds. Budget roughly 200–300 MB of RAM per session, and check your Stata license for concurrent-instance limits before you let an agent spawn a dozen of them.
Alternatives worth knowing
| Project | Form factor | License |
|---|---|---|
| hanlulong/stata-mcp | Editor extension with bundled MCP server | MIT |
| tmonk/mcp-stata + Stata Workbench | Standalone server plus companion extension | AGPL-3.0 (server) |
| SepineTam’s stata-mcp (PyPI) | CLI installer via uvx stata-mcp install -c cursor | See package page |
The AGPL on mcp-stata matters if you plan to embed it in a hosted service; MIT is the permissive option. Check with your institution’s software policy rather than guessing.
Risks, and how to keep this defensible
The MCP specification is blunt about this: the protocol enables arbitrary data access and code execution, so implementors are expected to build in explicit user consent and access controls. An agent with stata_run_file can also run erase.
- Keep everything local. The server binds to localhost; do not expose port 4000 to a network.
- Version control before you delegate. Commit, then let the agent edit.
- Read the do-file, not just the chat summary. Sample restrictions and cluster levels are where silent errors hide.
- Check restricted-data agreements. Model outputs may leave your machine even when the data does not.
- Re-run final results in a clean session. Multi-session state makes reproducibility easy to fake.
An assistant that produces a beautifully formatted table from the wrong sample is worse than no assistant at all, because the formatting buys it credibility it has not earned.
Troubleshooting checklist
| Symptom | Likely cause | Fix |
|---|---|---|
| Status bar has no “Stata” | Server did not start | Check autoStartServer and the Stata MCP output panel |
| Tools missing in chat | Client cached the old tool list | Fully restart the IDE |
| Server unreachable | Port 4000 in use | Change mcpServerPort or set forcePort |
| Wrong Stata edition launches | Default is MP | Set stataEdition to se or be |
| File-not-found in do-files | Working directory mismatch | Set workingDirectory to parent |
| Runs time out | 600-second cap | Raise runFileTimeout |
Turning on debugMode adds detailed diagnostics to the output panel — the first thing maintainers will ask for if you file an issue.
Is it worth the setup?
For anyone who writes .do files weekly, yes. Ten minutes of configuration replaces a large share of the copy-paste-between-windows ritual that has defined Stata work since the 1990s. The extension has drawn roughly 490 GitHub stars since its March 2025 launch, and StataCorp now points users toward this category of tool in its own newsletter — a reasonable signal that it is more than a weekend experiment.
What it will not do is take responsibility. Your name goes on the paper, the audit file, or the board memo; the model’s does not. Set it up, use it hard, and read every line before it counts.
