# autohotkey-mcp
> π **[Installation Guide](INSTALL.md)** β quick start, manual setup, and troubleshooting
**Run and manage AutoHotkey v2 scriptlets from your AI assistant** β list whatβs installed, launch or stop scripts, peek at source, and (optionally) draft new snippets in a safe sandbox. A small web app gives you help, a chat-style generator, and a live view of whatβs running.
| | |
|--:|--|
| **You might use this ifβ¦** | You already keep scriptlets in a depot (e.g. [autohotkey-tools](https://github.com/sandraschi/autohotkey-tools)), use Cursor or another MCP client, and want one place to drive hotkey utilities from conversation without juggling files by hand. |
| **What it connects to** | Your script folder on disk and, when you want it, **ScriptletCOMBridge** on port **10764** for the same list/run/stop behavior as your dashboard. |
**Repo:** [github.com/sandraschi/autohotkey-mcp](https://github.com/sandraschi/autohotkey-mcp) Β· standalone (not under RoboFang `hands/`). **Glama:** [`glama.json`](glama.json) at repo root for marketplace metadata.
## Prerequisites
- **Script depot:** clone [autohotkey-tools](https://github.com/sandraschi/autohotkey-tools) as a sibling of this repo (`../autohotkey-tools`) and it's found automatically; otherwise set `AUTOHOTKEY_SCRIPT_DEPOT` to its path explicitly.
- **AutoHotkey v2** installed (for direct run when bridge is not used). Set `AUTOHOTKEY_EXE` if not in default install path.
- **Optional:** ScriptletCOMBridge (autohotkey-tools) on `http://127.0.0.1:10764` β when running, list/run/stop use the bridge; when not, MCP scans the depot and runs AHK directly.
## Config
| Env | Default | Description |
|-----|--------|-------------|
| `AUTOHOTKEY_BRIDGE_URL` | `http://127.0.0.1:10764` | ScriptletCOMBridge base URL (optional; fallback: direct depot + AHK run) |
| `AUTOHOTKEY_SCRIPT_DEPOT` | auto-detected sibling `../autohotkey-tools` | Path to repo containing `scriptlets/` |
| `AUTOHOTKEY_EXE` | (auto-detect) | Path to AutoHotkey v2 exe when using direct run (no bridge) |
| `AUTOHOTKEY_MCP_HTTP` | (auto) | Set to `0` to force stdio-only. When unset, stdio-only if stdin is a pipe (Cursor/IDE). |
| `PORT` | `10746` | Backend port (fleet 10700β10800) |
| `AUTOHOTKEY_LLM_BASE_URL` | `http://127.0.0.1:11434/v1` | OpenAI-compatible chat URL (localhost only β SSRF-safe). Use Ollama or LM Studio. |
| `AUTOHOTKEY_LLM_MODEL` | `llama3.2` | Model id for `/chat/completions`. |
| `AUTOHOTKEY_LLM_API_KEY` | (unset) | Optional Bearer token for local servers that require it. `OPENAI_API_KEY` is also read. |
| `AUTOHOTKEY_LLM_TIMEOUT` | `120` | HTTP completion timeout (seconds). |
**LLM architecture:** **FastMCP 3.4 sampling** (`Context.sample`) is the **primary** path for `generate_scriptlet` / `refine_ahk_prompt` inside MCP hosts (e.g. Cursor). **Local HTTP** (`AUTOHOTKEY_LLM_*`, Ollama/LM Studio) is the **fallback** when sampling is unavailable, and powers the **web SPA** Chat + generate form (browsers have no MCP context). No file is written if both paths fail or validation fails.
**MCP resources (agents):** Preset prompt catalog is exposed as read-only resources: `ahk://prompts/catalog` (full JSON list), `ahk://prompts/categories`, `ahk://prompts/{prompt_id}` (one entry; JSON error if id unknown).
**Chat streaming (web):** `POST /api/chat` with `"stream": true` returns **SSE** (`text/event-stream`) with OpenAI-style chunked deltas from Ollama/LM Studio. The SPA Chat page enables this by default (toggle βStreamβ).
## Tools
9 MCP tools total (consolidated from 23 in the 2026-09-22 portmanteau refactor β fleet TOOL_DESIGN_STANDARDS.md mandates this above ~20 tools):
**`scriptlet_ops(operation, ...)`** β scriptlet lifecycle:
- `list` β List scriptlets (from bridge `/scriptlets` or depot scan); id, name, description, category, running.
- `run` β Run a scriptlet by id (e.g. `quick_notes` β bridge `/run/quick_notes.ahk`).
- `stop` β Stop by id; optional `pid` to stop one instance when several copies of the same script are running (direct).
- `list_running` β Running instances with depot metadata (`@hotkeys`, `@description`); PIDs for MCP direct runs.
- `get_source` β Read full source from depot `scriptlets/.ahk`.
- `get_metadata` β Read header metadata (@description, @version, etc.) from depot.
- `promote` β Move a script from `ai_generated/` into the live depot + register in `metadata.json`. Auto-fixes the silent-exit persistence bug, blocks on hotkey collisions and real lint errors (both overridable with `force=True`).
**`ahk_dev_ops(operation, ...)`** β AI-assisted authoring & help:
- `generate` β *(Sandbox)* Generate AHK v2 from a natural-language prompt via **MCP sampling** (when the client supports it) or **localhost OpenAI-compatible HTTP** (`AUTOHOTKEY_LLM_*`). Writes only to `scriptlets/ai_generated/` after validation; **no file on failure**. Review before promoting.
- `list_prompts` β Preset prompt library (categories, tags) for ideation.
- `refine_prompt` β Refine a vague idea into a clear prompt (sampling-first, same as generate).
- `help` β Multilevel help (markdown). `level`: `quick` | `reference` | `language` | `usage` | `tools` | `mcp_server`.
- `show_help` β Returns URLs for mini-help (`/help`) and full webapp (10747).
**`macro_ops(operation, ...)`** β `//trigger` text-expansion macros (`sop/macros.md`, read by `macro_expander.ahk` in the depot):
- `list` / `get` / `upsert` / `delete`
Plus 6 individual `show_*_card()` Prefab UI tools (rich cards in Claude Desktop/Cursor) β call `ahk_dev_ops(operation="help", level="tools")` for the full reference with examples.
## Webapp (fleet-standard SPA)
- **Frontend:** Vite + React on **10747** β Overview, Help, **Chat** (personas, presets, refine), **Scriptlets**, **Running** (live list: hotkeys, description, kill), Status.
- **Backend:** FastAPI on **10746** β `/health`, `/api/help`, `/api/scriptlets`, **`GET /api/running`**, **`POST /api/stop_scriptlet`**, `/api/personas`, `/api/prompts`, `POST /api/chat`, `POST /api/refine_prompt`, `POST /api/generate_scriptlet`, `/status`, `POST /tool`.
- **Launch:** `.\web_sota\start.ps1` β starts backend, then frontend, opens `http://127.0.0.1:10747/`.
- **Mini-help:** Backend-only alternative: run the server and open `http://127.0.0.1:10746/help` β single server-rendered HTML page (no SPA, no npm). See [docs/MINI_HELP_PATTERN.md](docs/MINI_HELP_PATTERN.md).
## Install
```powershell
git clone https://github.com/sandraschi/autohotkey-mcp.git
Set-Location autohotkey-mcp
uv sync
```
Commands below assume the **repository root** unless noted.
## Run
**Single process (FastMCP 3.4 dual transport):** stdio for Cursor/Claude and HTTP on 10746.
```powershell
uv sync
uv run autohotkey-mcp
```
Or: `just run` / `just server`.
**Cursor:** When stdin is a pipe (Cursor, Claude Desktop, etc.), the server runs **stdio-only** automatically (no HTTP, no port bind). So you don't need to set any env. If 10746 is already in use, Cursor's process won't try to bind it. To force stdio-only when running from a terminal, set `AUTOHOTKEY_MCP_HTTP=0`.
**Full webapp:** `.\web_sota\start.ps1` or `just web`.
**Tasks:** `just --list` β lint, format, install, clean, health.
## Related MCP servers
| Tool | What it does | When to use |
|------|-------------|-------------|
| **[autohotkey-mcp]** (this) | AHK scriptlet manager β list, run, stop, generate `.ahk` scripts | You have a **depot of AHK scripts** and want to fire them from chat. Best for **legacy automation**, **raw input recording/replay**, **low-level mouse/keyboard macros**, and apps that don't expose UIA. |
| **[pywinauto-mcp]** | Native Windows GUI automation β UIA element tree, screenshots, OCR, mouse/keyboard portmanteau | You need to **inspect controls, click buttons, read text** from modern Windows apps with accessibility trees. Best for **structured UI automation** where element IDs exist. |
**Overlap:** both control Windows input. **Use AHK** for raw low-level recording/replay (use `autohotkey-tools`'s built-in recorder). **Use pywinauto** when you need UIA element tree access, OCR, or structured window state capture.
**Together:** you can run both. `autohotkey-mcp` throws up a **"CUA at work" HUD** (blinking red overlay + e-stop button) while any scriptlet is running.
[autohotkey-mcp]: https://github.com/sandraschi/autohotkey-mcp
[pywinauto-mcp]: https://github.com/sandraschi/pywinauto-mcp
## Fleet
- **Ports:** Backend 10746, frontend 10747 (user-facing). Register in robofang `docs/standards/WEBAPP_PORTS.md` if using the Hub.
- **Health:** `GET http://127.0.0.1:10746/health` (frontend proxies `/health` to backend).
- **Tool invoke:** `POST /tool` with `{"name": "...", "arguments": {...}}` (RoboFang bridge style).
## π‘οΈ Industrial Quality Stack
This project adheres to **SOTA 14.1** industrial standards for high-fidelity agentic orchestration:
- **Python (Core)**: [Ruff](https://astral.sh/ruff) for linting and formatting. Zero-tolerance for `print` statements in core handlers (`T201`).
- **Webapp (UI)**: [Biome](https://biomejs.dev/) for sub-millisecond linting. Strict `noConsoleLog` enforcement.
- **Protocol Compliance**: Hardened `stdout/stderr` isolation to ensure crash-resistant JSON-RPC communication.
- **Automation**: [Justfile](./justfile) recipes for all fleet operations (`just lint`, `just fix`, `just dev`).
- **Security**: Automated audits via `bandit` and `safety`.