Stata MCP Skill
Instructions
- Ensure the
stata MCP server is registered (see project README for config) and request it if not already active.
- When the user asks for Stata work:
- Use
run_command for ad-hoc syntax (trace=True for call stacks, raw=True for plain output).
- Use
load_data before analyses that require datasets.
- Use
get_data, describe, codebook, or get_variable_list to inspect data.
- Use
run_do_file for provided .do scripts.
- Use
export_graph/export_graphs_all for visualization requests.
- Use
get_help when the user wants Stata documentation.
- Use
get_stored_results to return r()/e() scalars/macros after commands for validation.
- Use
read_log to tail or retrieve output from long-running commands.
- Use
get_ui_channel to obtain a localhost HTTP endpoint for high-volume data browsing.
- Surface
rc/stderr info back to the user, referencing r()/e() codes.
- If Stata isn't auto-discovered, remind the user to set
STATA_PATH (examples in README).
Tool quick reference
Command Execution
run_command(code, echo=True, as_json=True, trace=False, raw=False, max_output_lines=None): Run Stata syntax.
code: The Stata command(s) to execute.
echo: Include the command itself in output (default: True).
as_json: Return JSON envelope with rc/stdout/stderr/error (default: True).
trace: Enable set trace on for deeper error diagnostics (default: False).
raw: Return plain stdout/error message instead of JSON (default: False).
max_output_lines: Truncate output to this many lines (default: None for no truncation).
- Note: Always writes output to a temporary log file and emits a
notifications/logMessage with {"event":"log_path","path":"..."} so the client can tail it locally.
run_do_file(path, echo=True, as_json=True, trace=False, raw=False, max_output_lines=None): Execute .do files.
path: Path to the .do file.
echo: Include commands in output (default: True).
as_json: Return JSON envelope (default: True).
trace: Enable trace mode for debugging (default: False).
raw: Return plain output instead of JSON (default: False).
max_output_lines: Truncate output to this many lines (default: None).
- Note: Always writes output to a temporary log file and emits incremental
notifications/progress when the client provides a progress token/callback.
read_log(path, offset=0, max_bytes=65536): Read a slice of a previously-provided log file.
path: Path to the log file (from notifications/logMessage).
offset: Byte offset to start reading from (default: 0).
max_bytes: Maximum bytes to read (default: 65536).
- Returns JSON:
path, offset, next_offset, data.
Data Loading & Inspection
load_data(source, clear=True, as_json=True, raw=False, max_output_lines=None): Load data using sysuse/webuse/use heuristics.
source: Dataset name, URL, or file path (e.g., "auto", "webuse nlsw88", "/path/to/file.dta").
clear: Append , clear to replace existing data (default: True).
as_json: Return JSON envelope (default: True).
raw: Return plain output (default: False).
max_output_lines: Truncate output to this many lines (default: None).
get_data(start=0, count=50): Retrieve a slice of the active dataset as JSON.
start: Zero-based index of first observation (default: 0).
count: Number of observations to retrieve (default: 50, max: 500).
describe(): Return variable descriptions, storage types, and labels.
get_variable_list(): Return JSON list of all variables with names, labels, and types.
codebook(variable, as_json=True, trace=False, raw=False, max_output_lines=None): Return codebook/summary for a specific variable.
variable: Variable name to describe.
as_json: Return JSON envelope (default: True).
trace: Enable trace mode (default: False).
raw: Return plain output (default: False).
max_output_lines: Truncate output to this many lines (default: None).
Graph Management
list_graphs(): List all graphs in Stata's memory with active graph marked.
export_graph(graph_name=None, format="pdf"): Export a stored graph to file.
graph_name: Name of graph to export (from list_graphs); if None, exports active graph.
format: Output format—"pdf" (default) or "png". Use "png" to view plots directly.
export_graphs_all(): Export all graphs in memory. Returns file paths by default.
Help & Results
get_help(topic, plain_text=False): Return Stata help text.
topic: Command or help topic (e.g., "regress", "graph").
plain_text: Return plain text instead of Markdown (default: False).
get_stored_results(): Return current r() and e() results as JSON after a command.
UI Data Browser
get_ui_channel(): Return a short-lived localhost HTTP endpoint + bearer token for the UI-only data browser.
- Returns JSON with
baseUrl, token, expiresAt, and capabilities.
- Intended for VS Code extension UI to browse data at high volume (paging, filtering, sorting) without sending large payloads over MCP.
- Loopback only (binds to
127.0.0.1), requires bearer auth.
Cancellation
- Clients may cancel an in-flight request by sending the MCP notification
notifications/cancelled with params.requestId set to the original tool call ID.
- Pass a
_meta.progressToken when invoking the tool if you want progress updates (optional).
- Cancellation is best-effort and depends on Stata surfacing
BreakError.
MCP Resources
The server exposes these resources for MCP clients:
stata://data/summary → summarize
stata://data/metadata → describe
stata://graphs/list → graph list
stata://variables/list → variable list
stata://results/stored → stored r()/e() results
Graph review workflow
- Call
list_graphs() to see available plots and identify the active graph.
- Use
export_graphs_all() to fetch file paths for every graph; view them directly in the client.
- For a single plot, call
export_graph(graph_name="GraphName", format="png") to get a viewable file.
- Compare the rendered PNGs to the user spec (titles, axes labels, legends, colors, filters); state whether the graph matches and what to change.
Examples
Run a regression
# Load sample data and run regression
load_data("auto")
run_command("regress price mpg")
get_stored_results() # Retrieve coefficients and statistics
Export a histogram
# Create and export a graph
run_command("histogram price")
list_graphs() # Confirm graph exists
export_graph(graph_name="Graph", format="png") # Export for viewing
Debug a do-file
run_do_file("/path/to/analysis.do", trace=True)
Inspect data structure
load_data("nlsw88", clear=True)
describe()
get_variable_list()
codebook("wage")
get_data(start=0, count=10)
Read log output from long-running command
# After run_command emits a log_path notification
read_log("/tmp/stata_log_abc123.log", offset=0)
# Continue reading with next_offset for incremental output
read_log("/tmp/stata_log_abc123.log", offset=4096)
1---2name: stata-mcp3description: Run or debug Stata workflows through the local io.github.tmonk/mcp-stata server. Use when users mention Stata commands, .do files, r()/e() results, dataset inspection, or Stata graph exports.4---5
6# Stata MCP Skill
7
8## Instructions
91. Ensure the `stata` MCP server is registered (see project README for config) and request it if not already active.
102. When the user asks for Stata work:
11 - Use `run_command` for ad-hoc syntax (`trace=True` for call stacks, `raw=True` for plain output).
12 - Use `load_data` before analyses that require datasets.
13 - Use `get_data`, `describe`, `codebook`, or `get_variable_list` to inspect data.
14 - Use `run_do_file` for provided `.do` scripts.
15 - Use `export_graph`/`export_graphs_all` for visualization requests.
16 - Use `get_help` when the user wants Stata documentation.
17 - Use `get_stored_results` to return `r()`/`e()` scalars/macros after commands for validation.
18 - Use `read_log` to tail or retrieve output from long-running commands.
19 - Use `get_ui_channel` to obtain a localhost HTTP endpoint for high-volume data browsing.
203. Surface `rc`/`stderr` info back to the user, referencing `r()`/`e()` codes.
214. If Stata isn't auto-discovered, remind the user to set `STATA_PATH` (examples in README).
22
23## Tool quick reference
24
25### Command Execution
26- `run_command(code, echo=True, as_json=True, trace=False, raw=False, max_output_lines=None)`: Run Stata syntax.
27 - `code`: The Stata command(s) to execute.
28 - `echo`: Include the command itself in output (default: True).
29 - `as_json`: Return JSON envelope with rc/stdout/stderr/error (default: True).
30 - `trace`: Enable `set trace on` for deeper error diagnostics (default: False).
31 - `raw`: Return plain stdout/error message instead of JSON (default: False).
32 - `max_output_lines`: Truncate output to this many lines (default: None for no truncation).
33 - Note: Always writes output to a temporary log file and emits a `notifications/logMessage` with `{"event":"log_path","path":"..."}` so the client can tail it locally.
34
35- `run_do_file(path, echo=True, as_json=True, trace=False, raw=False, max_output_lines=None)`: Execute .do files.
36 - `path`: Path to the .do file.
37 - `echo`: Include commands in output (default: True).
38 - `as_json`: Return JSON envelope (default: True).
39 - `trace`: Enable trace mode for debugging (default: False).
40 - `raw`: Return plain output instead of JSON (default: False).
41 - `max_output_lines`: Truncate output to this many lines (default: None).
42 - Note: Always writes output to a temporary log file and emits incremental `notifications/progress` when the client provides a progress token/callback.
43
44- `read_log(path, offset=0, max_bytes=65536)`: Read a slice of a previously-provided log file.
45 - `path`: Path to the log file (from `notifications/logMessage`).
46 - `offset`: Byte offset to start reading from (default: 0).
47 - `max_bytes`: Maximum bytes to read (default: 65536).
48 - Returns JSON: `path`, `offset`, `next_offset`, `data`.
49
50### Data Loading & Inspection
51- `load_data(source, clear=True, as_json=True, raw=False, max_output_lines=None)`: Load data using sysuse/webuse/use heuristics.
52 - `source`: Dataset name, URL, or file path (e.g., "auto", "webuse nlsw88", "/path/to/file.dta").
53 - `clear`: Append `, clear` to replace existing data (default: True).
54 - `as_json`: Return JSON envelope (default: True).
55 - `raw`: Return plain output (default: False).
56 - `max_output_lines`: Truncate output to this many lines (default: None).
57
58- `get_data(start=0, count=50)`: Retrieve a slice of the active dataset as JSON.
59 - `start`: Zero-based index of first observation (default: 0).
60 - `count`: Number of observations to retrieve (default: 50, max: 500).
61
62- `describe()`: Return variable descriptions, storage types, and labels.
63
64- `get_variable_list()`: Return JSON list of all variables with names, labels, and types.
65
66- `codebook(variable, as_json=True, trace=False, raw=False, max_output_lines=None)`: Return codebook/summary for a specific variable.
67 - `variable`: Variable name to describe.
68 - `as_json`: Return JSON envelope (default: True).
69 - `trace`: Enable trace mode (default: False).
70 - `raw`: Return plain output (default: False).
71 - `max_output_lines`: Truncate output to this many lines (default: None).
72
73### Graph Management
74- `list_graphs()`: List all graphs in Stata's memory with active graph marked.
75
76- `export_graph(graph_name=None, format="pdf")`: Export a stored graph to file.
77 - `graph_name`: Name of graph to export (from `list_graphs`); if None, exports active graph.
78 - `format`: Output format—"pdf" (default) or "png". Use "png" to view plots directly.
79
80- `export_graphs_all()`: Export all graphs in memory. Returns file paths by default.
81
82### Help & Results
83- `get_help(topic, plain_text=False)`: Return Stata help text.
84 - `topic`: Command or help topic (e.g., "regress", "graph").
85 - `plain_text`: Return plain text instead of Markdown (default: False).
86
87- `get_stored_results()`: Return current `r()` and `e()` results as JSON after a command.
88
89### UI Data Browser
90- `get_ui_channel()`: Return a short-lived localhost HTTP endpoint + bearer token for the UI-only data browser.
91 - Returns JSON with `baseUrl`, `token`, `expiresAt`, and `capabilities`.
92 - Intended for VS Code extension UI to browse data at high volume (paging, filtering, sorting) without sending large payloads over MCP.
93 - Loopback only (binds to `127.0.0.1`), requires bearer auth.
94
95## Cancellation
96- Clients may cancel an in-flight request by sending the MCP notification `notifications/cancelled` with `params.requestId` set to the original tool call ID.
97- Pass a `_meta.progressToken` when invoking the tool if you want progress updates (optional).
98- Cancellation is best-effort and depends on Stata surfacing `BreakError`.
99
100## MCP Resources
101The server exposes these resources for MCP clients:
102- `stata://data/summary` → `summarize`
103- `stata://data/metadata` → `describe`
104- `stata://graphs/list` → graph list
105- `stata://variables/list` → variable list
106- `stata://results/stored` → stored r()/e() results
107
108## Graph review workflow
1091. Call `list_graphs()` to see available plots and identify the active graph.
1102. Use `export_graphs_all()` to fetch file paths for every graph; view them directly in the client.
1113. For a single plot, call `export_graph(graph_name="GraphName", format="png")` to get a viewable file.
1124. Compare the rendered PNGs to the user spec (titles, axes labels, legends, colors, filters); state whether the graph matches and what to change.
113
114## Examples
115
116### Run a regression
117```
118# Load sample data and run regression
119load_data("auto")
120run_command("regress price mpg")
121get_stored_results() # Retrieve coefficients and statistics
122```
123
124### Export a histogram
125```
126# Create and export a graph
127run_command("histogram price")
128list_graphs() # Confirm graph exists
129export_graph(graph_name="Graph", format="png") # Export for viewing
130```
131
132### Debug a do-file
133```
134run_do_file("/path/to/analysis.do", trace=True)
135```
136
137### Inspect data structure
138```
139load_data("nlsw88", clear=True)
140describe()
141get_variable_list()
142codebook("wage")
143get_data(start=0, count=10)
144```
145
146### Read log output from long-running command
147```
148# After run_command emits a log_path notification
149read_log("/tmp/stata_log_abc123.log", offset=0)
150# Continue reading with next_offset for incremental output
151read_log("/tmp/stata_log_abc123.log", offset=4096)
152```