Posted on

How to Integrate AI Into Stata Using External Code Editors (2026 Guide)

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

TaskClassic Stata Do-file EditorEditor + MCP
Write a regressionYou type itAssistant drafts it, you review
Read the errorYou read the logAssistant reads the log and patches the code
Run a selectionCtrl+D in StataCtrl+Shift+Enter in the editor
Produce a reportManual exportAssistant can chain estimation, graphs, and LaTeX export
Version controlBolted onNative 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

  1. Open VS Code, Cursor, or Antigravity.
  2. Open the Extensions view with Ctrl+Shift+X (Cmd+Shift+X on macOS).
  3. Search for Stata MCP and click Install.
  4. Wait. First-time dependency setup can take up to two minutes; later launches are instant.
  5. 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

ActionWindows / LinuxmacOS
Run selectionCtrl+Shift+EnterCmd+Shift+Enter
Run whole .do fileCtrl+Shift+DCmd+Shift+D
Stop executionCtrl+Shift+CCmd+Shift+C
Restart sessionToolbar 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

SettingDefaultWhy you would change it
stata-vscode.stataPathAuto-detectedNon-standard install location
stata-vscode.stataEditionmpYou license SE or BE
stata-vscode.mcpServerPort4000Port already taken
stata-vscode.workingDirectorydofileSet to parent when scripts live in a code/ subfolder
stata-vscode.runFileTimeout600 secondsLong bootstraps or simulations
stata-vscode.resultDisplayModecompactSwitch to full when debugging loop output
stata-vscode.maxOutputTokens10000Large outputs get written to file and the path is returned instead
stata-vscode.maxSessions100License 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

ProjectForm factorLicense
hanlulong/stata-mcpEditor extension with bundled MCP serverMIT
tmonk/mcp-stata + Stata WorkbenchStandalone server plus companion extensionAGPL-3.0 (server)
SepineTam’s stata-mcp (PyPI)CLI installer via uvx stata-mcp install -c cursorSee 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

SymptomLikely causeFix
Status bar has no “Stata”Server did not startCheck autoStartServer and the Stata MCP output panel
Tools missing in chatClient cached the old tool listFully restart the IDE
Server unreachablePort 4000 in useChange mcpServerPort or set forcePort
Wrong Stata edition launchesDefault is MPSet stataEdition to se or be
File-not-found in do-filesWorking directory mismatchSet workingDirectory to parent
Runs time out600-second capRaise 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.

Frequently asked questions

1
Do I need a separate Stata license to use AI in an external editor?
No. The extension drives your existing local Stata installation, which must be version 17 or higher. Multi-session mode launches multiple instances, so confirm your license permits concurrent use.
2
Does my data get uploaded to the AI provider?
The Stata server runs locally on port 4000, so the dataset stays on your machine. However, output returned to the assistant — regression tables, summary statistics, error messages — is sent to your AI provider as context. Compact mode and the 10,000-token cap limit how much.
3
Which editor works best?
VS Code with Copilot is the most conservative choice and requires version 1.102 or later for MCP support. Cursor and Antigravity install the same extension from Open VSX and expose the same tools.
4
Why don’t the Stata tools appear in my chat window?
MCP clients read the tool list at startup and do not refresh mid-session. Restart the IDE completely after adding the server config.
5
Can the AI edit or delete my files?
Yes, if you grant it that scope. The tools execute real Stata code. Commit your work to version control before handing an agent write access.
6
Is this an official StataCorp product?
No. These are community projects. StataCorp lists several of them in its Stata News “Community corner” feature on AI tools, but does not maintain or support them.