mcp-stata 1.18.0__cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of mcp-stata might be problematic. Click here for more details.
- mcp_stata/__init__.py +4 -0
- mcp_stata/_native_ops.cpython-312-aarch64-linux-gnu.so +0 -0
- mcp_stata/config.py +20 -0
- mcp_stata/discovery.py +550 -0
- mcp_stata/graph_detector.py +401 -0
- mcp_stata/models.py +62 -0
- mcp_stata/native_ops.py +87 -0
- mcp_stata/server.py +1130 -0
- mcp_stata/smcl/smcl2html.py +88 -0
- mcp_stata/stata_client.py +3692 -0
- mcp_stata/streaming_io.py +263 -0
- mcp_stata/test_stata.py +54 -0
- mcp_stata/ui_http.py +998 -0
- mcp_stata-1.18.0.dist-info/METADATA +471 -0
- mcp_stata-1.18.0.dist-info/RECORD +18 -0
- mcp_stata-1.18.0.dist-info/WHEEL +5 -0
- mcp_stata-1.18.0.dist-info/entry_points.txt +2 -0
- mcp_stata-1.18.0.dist-info/licenses/LICENSE +661 -0
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mcp-stata
|
|
3
|
+
Version: 1.18.0
|
|
4
|
+
Classifier: Development Status :: 4 - Beta
|
|
5
|
+
Classifier: Intended Audience :: Science/Research
|
|
6
|
+
Classifier: Intended Audience :: Developers
|
|
7
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
8
|
+
Classifier: License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
12
|
+
Requires-Dist: mcp[cli]>=1.0.0
|
|
13
|
+
Requires-Dist: pandas>=2.0.0
|
|
14
|
+
Requires-Dist: numpy>=1.26.0
|
|
15
|
+
Requires-Dist: pydantic>=2.0.0
|
|
16
|
+
Requires-Dist: pystata>=0.0.1
|
|
17
|
+
Requires-Dist: stata-setup>=0.1.0
|
|
18
|
+
Requires-Dist: httpx>=0.27.0,<0.28.0
|
|
19
|
+
Requires-Dist: pytest-asyncio>=1.3.0
|
|
20
|
+
Requires-Dist: pyarrow>=14.0.0
|
|
21
|
+
Requires-Dist: polars>=1.36.1
|
|
22
|
+
Requires-Dist: build>=1.3.0 ; extra == 'dev'
|
|
23
|
+
Requires-Dist: hatch>=1.16.2 ; extra == 'dev'
|
|
24
|
+
Requires-Dist: twine>=6.2.0 ; extra == 'dev'
|
|
25
|
+
Requires-Dist: ruff>=0.4.0 ; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest>=7.0.0 ; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest-cov>=4.0.0 ; extra == 'dev'
|
|
28
|
+
Requires-Dist: python-semantic-release>=9.8.0 ; extra == 'dev'
|
|
29
|
+
Requires-Dist: maturin>=1.11.5 ; extra == 'dev'
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
License-File: LICENSE
|
|
32
|
+
Summary: A lightweight Model Context Protocol (MCP) server for Stata. Execute commands, inspect data, retrieve stored results (`r()`/`e()`), and view graphs in your chat interface. Built for economists who want to integrate LLM assistance into their Stata workflow.
|
|
33
|
+
Keywords: mcp,stata,statistics,econometrics,ai,llm
|
|
34
|
+
Author-email: Thomas Monk <t.d.monk@lse.ac.uk>
|
|
35
|
+
License-Expression: AGPL-3.0-or-later
|
|
36
|
+
Requires-Python: >=3.12
|
|
37
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
38
|
+
Project-URL: Homepage, https://github.com/tmonk/mcp-stata
|
|
39
|
+
Project-URL: Issues, https://github.com/tmonk/mcp-stata/issues
|
|
40
|
+
Project-URL: Repository, https://github.com/tmonk/mcp-stata
|
|
41
|
+
|
|
42
|
+
# Stata MCP Server
|
|
43
|
+
|
|
44
|
+
<a href="https://cursor.com/en-US/install-mcp?name=mcp-stata&config=eyJjb21tYW5kIjogInV2eCAtLXJlZnJlc2ggLS1yZWZyZXNoLXBhY2thZ2UgbWNwLXN0YXRhIC0tZnJvbSBtY3Atc3RhdGFAbGF0ZXN0IG1jcC1zdGF0YSJ9"><img src="https://cursor.com/deeplink/mcp-install-dark.svg" alt="Install MCP Server" height="20"></a>
|
|
45
|
+
<a href="https://pypi.org/project/mcp-stata/"><img src="https://img.shields.io/pypi/v/mcp-stata?style=flat&color=black" alt="PyPI - Version" height="20"></a>
|
|
46
|
+
|
|
47
|
+
A [Model Context Protocol](https://github.com/modelcontextprotocol) (MCP) server that connects AI agents to a local Stata installation.
|
|
48
|
+
|
|
49
|
+
> If you'd like a fully integrated VS Code extension to run Stata code without leaving your IDE, and also allow AI agent interaction, check out my other project: [<img src="https://raw.githubusercontent.com/tmonk/stata-workbench/refs/heads/main/img/icon.png" height="12px"> Stata Workbench](https://github.com/tmonk/stata-workbench/).
|
|
50
|
+
|
|
51
|
+
Built by <a href="https://tdmonk.com">Thomas Monk</a>, London School of Economics.
|
|
52
|
+
<!-- mcp-name: io.github.tmonk/mcp-stata -->
|
|
53
|
+
|
|
54
|
+
This server enables LLMs to:
|
|
55
|
+
- **Execute Stata code**: run any Stata command (e.g. `sysuse auto`, `regress price mpg`).
|
|
56
|
+
- **Inspect data**: retrieve dataset summaries and variable codebooks.
|
|
57
|
+
- **Export graphics**: generate and view Stata graphs (histograms, scatterplots).
|
|
58
|
+
- **Streaming graph caching**: automatically cache graphs during command execution for instant exports.
|
|
59
|
+
- **Verify results**: programmatically check stored results (`r()`, `e()`) for accurate validation.
|
|
60
|
+
|
|
61
|
+
## Prerequisites
|
|
62
|
+
|
|
63
|
+
- **Stata 17+** (required for `pystata` integration)
|
|
64
|
+
- **Python 3.12+** (required)
|
|
65
|
+
- **uv** (recommended for install/run)
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
### Run as a published tool with `uvx`
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
uvx --refresh --refresh-package mcp-stata --from mcp-stata@latest mcp-stata
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`uvx` is an alias for `uv tool run` and runs the tool in an isolated, cached environment.
|
|
76
|
+
|
|
77
|
+
## Configuration
|
|
78
|
+
|
|
79
|
+
This server attempts to automatically discover your Stata installation (supporting standard paths and StataNow).
|
|
80
|
+
|
|
81
|
+
If auto-discovery fails, set the `STATA_PATH` environment variable to your Stata executable:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# macOS example
|
|
85
|
+
export STATA_PATH="/Applications/StataNow/StataMP.app/Contents/MacOS/stata-mp"
|
|
86
|
+
|
|
87
|
+
# Windows example (cmd.exe)
|
|
88
|
+
set STATA_PATH="C:\Program Files\Stata18\StataMP-64.exe"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
If you prefer, add `STATA_PATH` to your MCP config's `env` for any IDE shown below. It's optional and only needed when discovery cannot find Stata.
|
|
92
|
+
|
|
93
|
+
Optional `env` example (add inside your MCP server entry):
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
"env": {
|
|
97
|
+
"STATA_PATH": "/Applications/StataNow/StataMP.app/Contents/MacOS/stata-mp"
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## IDE Setup (MCP)
|
|
102
|
+
|
|
103
|
+
This MCP server uses the **stdio** transport (the IDE launches the process and communicates over stdin/stdout).
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
### Claude Desktop
|
|
108
|
+
|
|
109
|
+
Open Claude Desktop → **Settings** → **Developer** → **Edit Config**.
|
|
110
|
+
Config file locations include:
|
|
111
|
+
|
|
112
|
+
* macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
113
|
+
* Windows: `%APPDATA%\Claude\claude_desktop_config.json`
|
|
114
|
+
|
|
115
|
+
#### Published tool (uvx)
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"mcpServers": {
|
|
120
|
+
"mcp-stata": {
|
|
121
|
+
"command": "uvx",
|
|
122
|
+
"args": [
|
|
123
|
+
"--refresh",
|
|
124
|
+
"--refresh-package",
|
|
125
|
+
"mcp-stata",
|
|
126
|
+
"--from",
|
|
127
|
+
"mcp-stata@latest",
|
|
128
|
+
"mcp-stata"
|
|
129
|
+
]
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
After editing, fully quit and restart Claude Desktop to reload MCP servers.
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
### Cursor
|
|
140
|
+
|
|
141
|
+
Cursor supports MCP config at:
|
|
142
|
+
|
|
143
|
+
* Global: `~/.cursor/mcp.json`
|
|
144
|
+
* Project: `.cursor/mcp.json`
|
|
145
|
+
|
|
146
|
+
#### Published tool (uvx)
|
|
147
|
+
|
|
148
|
+
```json
|
|
149
|
+
{
|
|
150
|
+
"mcpServers": {
|
|
151
|
+
"mcp-stata": {
|
|
152
|
+
"command": "uvx",
|
|
153
|
+
"args": [
|
|
154
|
+
"--refresh",
|
|
155
|
+
"--refresh-package",
|
|
156
|
+
"mcp-stata",
|
|
157
|
+
"--from",
|
|
158
|
+
"mcp-stata@latest",
|
|
159
|
+
"mcp-stata"
|
|
160
|
+
]
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
### Windsurf
|
|
169
|
+
|
|
170
|
+
Windsurf supports MCP plugins and also allows manual editing of `mcp_config.json`. After adding/editing a server, use the UI’s refresh so it re-reads the config.
|
|
171
|
+
|
|
172
|
+
A common location is `~/.codeium/windsurf/mcp_config.json`.
|
|
173
|
+
#### Published tool (uvx)
|
|
174
|
+
|
|
175
|
+
```json
|
|
176
|
+
{
|
|
177
|
+
"mcpServers": {
|
|
178
|
+
"mcp-stata": {
|
|
179
|
+
"command": "uvx",
|
|
180
|
+
"args": [
|
|
181
|
+
"--refresh",
|
|
182
|
+
"--refresh-package",
|
|
183
|
+
"mcp-stata",
|
|
184
|
+
"--from",
|
|
185
|
+
"mcp-stata@latest",
|
|
186
|
+
"mcp-stata"
|
|
187
|
+
]
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
### Google Antigravity
|
|
196
|
+
|
|
197
|
+
In Antigravity, MCP servers are managed from the MCP store/menu; you can open **Manage MCP Servers** and then **View raw config** to edit `mcp_config.json`.
|
|
198
|
+
|
|
199
|
+
#### Published tool (uvx)
|
|
200
|
+
|
|
201
|
+
```json
|
|
202
|
+
{
|
|
203
|
+
"mcpServers": {
|
|
204
|
+
"mcp-stata": {
|
|
205
|
+
"command": "uvx",
|
|
206
|
+
"args": [
|
|
207
|
+
"--refresh",
|
|
208
|
+
"--refresh-package",
|
|
209
|
+
"mcp-stata",
|
|
210
|
+
"--from",
|
|
211
|
+
"mcp-stata@latest",
|
|
212
|
+
"mcp-stata"
|
|
213
|
+
]
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
### Visual Studio Code
|
|
222
|
+
|
|
223
|
+
VS Code supports MCP servers via a `.vscode/mcp.json` file. The top-level key is **`servers`** (not `mcpServers`).
|
|
224
|
+
|
|
225
|
+
Create `.vscode/mcp.json`:
|
|
226
|
+
|
|
227
|
+
#### Published tool (uvx)
|
|
228
|
+
|
|
229
|
+
```json
|
|
230
|
+
{
|
|
231
|
+
"servers": {
|
|
232
|
+
"mcp-stata": {
|
|
233
|
+
"type": "stdio",
|
|
234
|
+
"command": "uvx",
|
|
235
|
+
"args": [
|
|
236
|
+
"--refresh",
|
|
237
|
+
"--refresh-package",
|
|
238
|
+
"mcp-stata",
|
|
239
|
+
"--from",
|
|
240
|
+
"mcp-stata@latest",
|
|
241
|
+
"mcp-stata"
|
|
242
|
+
]
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
VS Code documents `.vscode/mcp.json` and the `servers` schema, including `type` and `command`/`args`.
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## Skills
|
|
253
|
+
|
|
254
|
+
- Skill file (for Claude/Codex): [skill/SKILL.md](skill/SKILL.md)
|
|
255
|
+
|
|
256
|
+
## Tools Available (from server.py)
|
|
257
|
+
|
|
258
|
+
* `run_command(code, echo=True, as_json=True, trace=False, raw=False, max_output_lines=None)`: Execute Stata syntax.
|
|
259
|
+
- Always writes output to a temporary log file and emits a single `notifications/logMessage` containing `{"event":"log_path","path":"..."}` so the client can tail it locally.
|
|
260
|
+
- May emit `notifications/progress` when the client provides a progress token/callback.
|
|
261
|
+
* `read_log(path, offset=0, max_bytes=65536)`: Read a slice of a previously-provided log file (JSON: `path`, `offset`, `next_offset`, `data`).
|
|
262
|
+
* `find_in_log(path, query, start_offset=0, max_bytes=5_000_000, before=2, after=2, case_sensitive=False, regex=False, max_matches=50)`: Search a log file for text and return context windows.
|
|
263
|
+
- Returns JSON with `matches` (context lines, line indices), `next_offset`, and `truncated` if `max_matches` is hit.
|
|
264
|
+
- Supports literal or regex search with bounded read window for large logs.
|
|
265
|
+
* `load_data(source, clear=True, as_json=True, raw=False, max_output_lines=None)`: Heuristic loader (sysuse/webuse/use/path/URL) with JSON envelope unless `raw=True`. Supports output truncation.
|
|
266
|
+
* `get_data(start=0, count=50)`: View dataset rows (JSON response, capped to 500 rows).
|
|
267
|
+
* `get_ui_channel()`: Return a short-lived localhost HTTP endpoint + bearer token for the UI-only data browser.
|
|
268
|
+
* `describe()`: View dataset structure via Stata `describe`.
|
|
269
|
+
* `list_graphs()`: See available graphs in memory (JSON list with an `active` flag).
|
|
270
|
+
* `export_graph(graph_name=None, format="pdf")`: Export a graph to a file path (default PDF; use `format="png"` for PNG).
|
|
271
|
+
* `export_graphs_all()`: Export all in-memory graphs. Returns file paths.
|
|
272
|
+
* `get_help(topic, plain_text=False)`: Markdown-rendered Stata help by default; `plain_text=True` strips formatting.
|
|
273
|
+
* `codebook(variable, as_json=True, trace=False, raw=False, max_output_lines=None)`: Variable-level metadata (JSON envelope by default; supports `trace=True` and output truncation).
|
|
274
|
+
* `run_do_file(path, echo=True, as_json=True, trace=False, raw=False, max_output_lines=None)`: Execute a .do file.
|
|
275
|
+
- Always writes output to a temporary log file and emits a single `notifications/logMessage` containing `{"event":"log_path","path":"..."}` so the client can tail it locally.
|
|
276
|
+
- Emits incremental `notifications/progress` when the client provides a progress token/callback.
|
|
277
|
+
* `get_stored_results()`: Get `r()` and `e()` scalars/macros as JSON.
|
|
278
|
+
* `get_variable_list()`: JSON list of variables and labels.
|
|
279
|
+
|
|
280
|
+
### Cancellation
|
|
281
|
+
|
|
282
|
+
- Clients may cancel an in-flight request by sending the MCP notification `notifications/cancelled` with `params.requestId` set to the original tool call ID.
|
|
283
|
+
- Client guidance:
|
|
284
|
+
1. Pass a `_meta.progressToken` when invoking the tool if you want progress updates (optional).
|
|
285
|
+
2. If you need to cancel, send `notifications/cancelled` with the same requestId. You may also stop tailing the log file path once you receive cancellation confirmation (the tool call will return an error indicating cancellation).
|
|
286
|
+
3. Be prepared for partial output in the log file; cancellation is best-effort and depends on Stata surfacing `BreakError`.
|
|
287
|
+
|
|
288
|
+
Resources exposed for MCP clients:
|
|
289
|
+
|
|
290
|
+
* `stata://data/summary` → `summarize`
|
|
291
|
+
* `stata://data/metadata` → `describe`
|
|
292
|
+
* `stata://graphs/list` → graph list (resource handler delegates to `list_graphs` tool)
|
|
293
|
+
* `stata://variables/list` → variable list (resource wrapper)
|
|
294
|
+
* `stata://results/stored` → stored r()/e() results
|
|
295
|
+
|
|
296
|
+
## UI-only Data Browser (Local HTTP API)
|
|
297
|
+
|
|
298
|
+
This server also hosts a **localhost-only HTTP API** intended for a VS Code extension UI to browse data at high volume (paging, filtering) without sending large payloads over MCP.
|
|
299
|
+
|
|
300
|
+
Important properties:
|
|
301
|
+
|
|
302
|
+
- **Loopback only**: binds to `127.0.0.1`.
|
|
303
|
+
- **Bearer auth**: every request requires an `Authorization: Bearer <token>` header.
|
|
304
|
+
- **Short-lived tokens**: clients should call `get_ui_channel()` to obtain a fresh token as needed.
|
|
305
|
+
- **No Stata dataset mutation** for browsing/filtering:
|
|
306
|
+
- No generated variables.
|
|
307
|
+
- Paging uses `sfi.Data.get`.
|
|
308
|
+
- Filtering is evaluated in Python over chunked reads.
|
|
309
|
+
|
|
310
|
+
### Discovery via MCP (`get_ui_channel`)
|
|
311
|
+
|
|
312
|
+
Call the MCP tool `get_ui_channel()` and parse the JSON:
|
|
313
|
+
|
|
314
|
+
```json
|
|
315
|
+
{
|
|
316
|
+
"baseUrl": "http://127.0.0.1:53741",
|
|
317
|
+
"token": "...",
|
|
318
|
+
"expiresAt": 1730000000,
|
|
319
|
+
"capabilities": {
|
|
320
|
+
"dataBrowser": true,
|
|
321
|
+
"filtering": true,
|
|
322
|
+
"sorting": true,
|
|
323
|
+
"arrowStream": true
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Server-enforced limits (current defaults):
|
|
329
|
+
|
|
330
|
+
- **maxLimit**: 500
|
|
331
|
+
- **maxVars**: 32,767
|
|
332
|
+
- **maxChars**: 500
|
|
333
|
+
- **maxRequestBytes**: 1,000,000
|
|
334
|
+
- **maxArrowLimit**: 1,000,000 (specific to `/v1/arrow`)
|
|
335
|
+
|
|
336
|
+
### Endpoints
|
|
337
|
+
|
|
338
|
+
All endpoints are under `baseUrl` and require the bearer token.
|
|
339
|
+
|
|
340
|
+
- `GET /v1/dataset`
|
|
341
|
+
- Returns dataset identity and basic state (`id`, `frame`, `n`, `k`).
|
|
342
|
+
- `GET /v1/vars`
|
|
343
|
+
- Returns variable metadata (`name`, `type`, `label`, `format`).
|
|
344
|
+
- `POST /v1/page`
|
|
345
|
+
- Returns a page of data for selected variables.
|
|
346
|
+
- `POST /v1/arrow`
|
|
347
|
+
- Returns a binary Arrow IPC stream (same input as `/v1/page`).
|
|
348
|
+
- `POST /v1/views`
|
|
349
|
+
- Creates a server-side filtered view (handle-based filtering).
|
|
350
|
+
- `POST /v1/views/:viewId/page`
|
|
351
|
+
- Pages within a filtered view.
|
|
352
|
+
- `POST /v1/views/:viewId/arrow`
|
|
353
|
+
- Returns a binary Arrow IPC stream from a filtered view.
|
|
354
|
+
- `DELETE /v1/views/:viewId`
|
|
355
|
+
- Deletes a view handle.
|
|
356
|
+
- `POST /v1/filters/validate`
|
|
357
|
+
- Validates a filter expression.
|
|
358
|
+
|
|
359
|
+
### Paging request example
|
|
360
|
+
|
|
361
|
+
```bash
|
|
362
|
+
curl -sS \
|
|
363
|
+
-H "Authorization: Bearer $TOKEN" \
|
|
364
|
+
-H "Content-Type: application/json" \
|
|
365
|
+
-d '{"datasetId":"...","frame":"default","offset":0,"limit":50,"vars":["price","mpg"],"includeObsNo":true,"maxChars":200}' \
|
|
366
|
+
"$BASE_URL/v1/page"
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
#### Sorting
|
|
370
|
+
|
|
371
|
+
The `/v1/page` and `/v1/views/:viewId/page` endpoints support sorting via the optional `sortBy` parameter:
|
|
372
|
+
|
|
373
|
+
```bash
|
|
374
|
+
# Sort by price ascending
|
|
375
|
+
curl -sS \
|
|
376
|
+
-H "Authorization: Bearer $TOKEN" \
|
|
377
|
+
-H "Content-Type: application/json" \
|
|
378
|
+
-d '{"datasetId":"...","offset":0,"limit":50,"vars":["price","mpg"],"sortBy":["price"]}' \
|
|
379
|
+
"$BASE_URL/v1/page"
|
|
380
|
+
|
|
381
|
+
# Sort by price descending
|
|
382
|
+
curl -sS \
|
|
383
|
+
-H "Authorization: Bearer $TOKEN" \
|
|
384
|
+
-H "Content-Type: application/json" \
|
|
385
|
+
-d '{"datasetId":"...","offset":0,"limit":50,"vars":["price","mpg"],"sortBy":["-price"]}' \
|
|
386
|
+
"$BASE_URL/v1/page"
|
|
387
|
+
|
|
388
|
+
# Multi-variable sort: foreign ascending, then price descending
|
|
389
|
+
curl -sS \
|
|
390
|
+
-H "Authorization: Bearer $TOKEN" \
|
|
391
|
+
-H "Content-Type: application/json" \
|
|
392
|
+
-d '{"datasetId":"...","offset":0,"limit":50,"vars":["foreign","price","mpg"],"sortBy":["foreign","-price"]}' \
|
|
393
|
+
"$BASE_URL/v1/page"
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
**Sort specification format:**
|
|
397
|
+
- `sortBy` is an array of strings (variable names with optional prefix)
|
|
398
|
+
- No prefix or `+` prefix = ascending order (e.g., `"price"` or `"+price"`)
|
|
399
|
+
- `-` prefix = descending order (e.g., `"-price"`)
|
|
400
|
+
- Multiple variables are supported for multi-level sorting
|
|
401
|
+
- Uses the native Rust sorter when available, with a Polars fallback
|
|
402
|
+
|
|
403
|
+
**Sorting with filtered views:**
|
|
404
|
+
- Sorting is fully supported with filtered views
|
|
405
|
+
- The sort is computed in-memory over the sort columns, then filtered indices are re-applied
|
|
406
|
+
- Example: Filter for `price < 5000`, then sort descending by price
|
|
407
|
+
|
|
408
|
+
```bash
|
|
409
|
+
# Create a filtered view
|
|
410
|
+
curl -sS \
|
|
411
|
+
-H "Authorization: Bearer $TOKEN" \
|
|
412
|
+
-H "Content-Type: application/json" \
|
|
413
|
+
-d '{"datasetId":"...","frame":"default","filterExpr":"price < 5000"}' \
|
|
414
|
+
"$BASE_URL/v1/views"
|
|
415
|
+
# Returns: {"view": {"id": "view_abc123", "filteredN": 37}}
|
|
416
|
+
|
|
417
|
+
# Get sorted page from filtered view
|
|
418
|
+
curl -sS \
|
|
419
|
+
-H "Authorization: Bearer $TOKEN" \
|
|
420
|
+
-H "Content-Type: application/json" \
|
|
421
|
+
-d '{"offset":0,"limit":50,"vars":["price","mpg"],"sortBy":["-price"]}' \
|
|
422
|
+
"$BASE_URL/v1/views/view_abc123/page"
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
Notes:
|
|
426
|
+
|
|
427
|
+
- `datasetId` is used for cache invalidation. If the dataset changes due to running Stata commands, the server will report a new dataset id and view handles become invalid.
|
|
428
|
+
- Filter expressions are evaluated in Python using values read from Stata via `sfi.Data.get`. Use boolean operators like `==`, `!=`, `<`, `>`, and `and`/`or` (Stata-style `&`/`|` are also accepted).
|
|
429
|
+
- Sorting does **not** mutate the dataset order in Stata; it computes sorted indices for the response and caches them for subsequent requests.
|
|
430
|
+
- The Rust sorter is the primary implementation; Polars is used only as a fallback when the native extension is unavailable.
|
|
431
|
+
|
|
432
|
+
## License
|
|
433
|
+
|
|
434
|
+
This project is licensed under the GNU Affero General Public License v3.0 or later.
|
|
435
|
+
See the LICENSE file for the full text.
|
|
436
|
+
|
|
437
|
+
## Error reporting
|
|
438
|
+
|
|
439
|
+
- All tools that execute Stata commands support JSON envelopes (`as_json=true`) carrying:
|
|
440
|
+
- `rc` (from r()/c(rc)), `stdout`, `stderr`, `message`, optional `line` (when Stata reports it), `command`, optional `log_path` (for log-file streaming), and a `snippet` excerpt of error output.
|
|
441
|
+
- Stata-specific cues are preserved:
|
|
442
|
+
- `r(XXX)` codes are parsed when present in output.
|
|
443
|
+
- “Red text” is captured via stderr where available.
|
|
444
|
+
- `trace=true` adds `set trace on` around the command/do-file to surface program-defined errors; the trace is turned off afterward.
|
|
445
|
+
|
|
446
|
+
## Logging
|
|
447
|
+
|
|
448
|
+
Set `MCP_STATA_LOGLEVEL` (e.g., `DEBUG`, `INFO`) to control server logging. Logs include discovery details (edition/path) and command-init traces for easier troubleshooting.
|
|
449
|
+
|
|
450
|
+
## Development & Contributing
|
|
451
|
+
|
|
452
|
+
For detailed information on building, testing, and contributing to this project, see [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
453
|
+
|
|
454
|
+
Quick setup:
|
|
455
|
+
|
|
456
|
+
```bash
|
|
457
|
+
# Install dependencies
|
|
458
|
+
uv sync --extra dev --no-install-project
|
|
459
|
+
|
|
460
|
+
# Run tests (requires Stata)
|
|
461
|
+
pytest
|
|
462
|
+
|
|
463
|
+
# Run tests without Stata
|
|
464
|
+
pytest -v -m "not requires_stata"
|
|
465
|
+
|
|
466
|
+
# Build the package
|
|
467
|
+
python -m build
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
[](https://lobehub.com/mcp/tmonk-mcp-stata)
|
|
471
|
+
[](https://github.com/tmonk/mcp-stata/actions/workflows/build-test.yml)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
mcp_stata/__init__.py,sha256=kJKKRn7lGuVCuS2-GaN5VoVcvnxtNlfuswW_VOlYqwg,98
|
|
2
|
+
mcp_stata/_native_ops.cpython-312-aarch64-linux-gnu.so,sha256=qT2L7kRqzlr5SrUVacLy7oOGzYhwxMVcU1wCJrVH_s4,2396384
|
|
3
|
+
mcp_stata/config.py,sha256=SfXltpwO_gROABca1sm0xXDhaeRmlRfQmXcnBiG4GYk,714
|
|
4
|
+
mcp_stata/discovery.py,sha256=16DQhO2O3klKq-HwBaK3pv5LpKGPyIVlo1VyW1p4Msg,20799
|
|
5
|
+
mcp_stata/graph_detector.py,sha256=6dc2M9Lr_0PVA0yIPdc4clmbTQsiOADnqtb0fOxYEQs,17370
|
|
6
|
+
mcp_stata/models.py,sha256=G30Km396RDgvHjTeg7uR5BKT8AK5mX3Zw8sUuBSbdpM,1272
|
|
7
|
+
mcp_stata/native_ops.py,sha256=m9wOB4qVbfWKM629XuJI6KmP0cQw_EfbuQAUrVxW1Dk,2221
|
|
8
|
+
mcp_stata/server.py,sha256=cEgoI2kT8-H2GAoqW440y8KuiqGVwSUzz6WxJO5WXfc,40396
|
|
9
|
+
mcp_stata/smcl/smcl2html.py,sha256=nrZIzmSSWD9AuDQHXJ80A0v1f8fkR7gbl4nCy_BHRIg,3005
|
|
10
|
+
mcp_stata/stata_client.py,sha256=7ojqm2B9-X7ZXFOXGklGUVqpL-2zi3cSiv6J4YPboeU,149343
|
|
11
|
+
mcp_stata/streaming_io.py,sha256=1WLStbdYEQFO1P3gCchmD7frHpmn5mHi6DuynodfiqM,7087
|
|
12
|
+
mcp_stata/test_stata.py,sha256=HF_P4IjvLGlihXggbt79MZZB6PbuYHpKI3S0R2dgCeE,1512
|
|
13
|
+
mcp_stata/ui_http.py,sha256=SeV_GzExft2AxaLIvjXTRvmm_serFKFMIEbAsCMTNQw,39831
|
|
14
|
+
mcp_stata-1.18.0.dist-info/METADATA,sha256=nEKkYc5MM-ycKDn20IFwiFZVmS-3tMnW-UnYG1YQNs4,17316
|
|
15
|
+
mcp_stata-1.18.0.dist-info/WHEEL,sha256=52Zi36pDPQlOrLthVvXF6f11TX-HRQ2sllWxsSJsrtQ,149
|
|
16
|
+
mcp_stata-1.18.0.dist-info/entry_points.txt,sha256=veUG0YD5rR8Kghyf39JasKMu3-XKptiXIwrQg1Xd2Es,50
|
|
17
|
+
mcp_stata-1.18.0.dist-info/licenses/LICENSE,sha256=DZak_2itbUtvHzD3E7GNUYSRK6jdOJ-GqncQ2weavLA,34523
|
|
18
|
+
mcp_stata-1.18.0.dist-info/RECORD,,
|