monaco-mcp
# monaco-mcp
An MCP server that **reads and writes code in web-based code editors from your AI assistant** — bypassing paste blocking.
You know the scenario: you ask your assistant to solve a problem, it writes the solution, and then... you can't get it into the Monaco editor on the page because right-click → Paste is disabled, Ctrl+V is swallowed, and drag-and-drop is off.
This server fixes that. It drives a real browser and writes into the editor through the editor's own API, so the code simply *appears* in the editor — no paste event is ever fired.
## How it works
The paste blocker only listens for the `paste` event. We never send one.
`set_code` tries a chain of strategies, in order, and verifies the result by reading the editor back:
| # | Strategy | When it works |
|---|----------|---------------|
| 1 | `monaco.editor.getEditors()` / `getModels()` → `setValue()` | Monaco's public API is on `window` |
| 2 | DOM expando properties + React prop/fiber walk to find a hidden editor instance | Monaco is bundled (no global), e.g. `@monaco-editor/react` |
| 3 | Ace → `ace.edit(node).setValue()` | Ace editors |
| 4 | CodeMirror 6 → `view.dispatch()` | CM6 editors |
| 5 | Focus editor → `Ctrl+A` → `keyboard.insertText()` | Universal fallback. This is **IME-style typing, not a paste**, so paste blockers don't fire |
The result reports `verified: "exact"` only after the code was read back out and matched. A silent failure can never look like a success.
## Requirements
- Node.js ≥ 18.17
- Google Chrome or Microsoft Edge (auto-detected), or any Chromium via `MONACO_MCP_EXECUTABLE`
## Install
```bash
git clone https://github.com/siddubakka/monaco-mcp.git
cd monaco-mcp
npm install
```
Run the test suite (opens a browser, loads a fixture with paste blocked, injects code, asserts nothing was pasted):
```bash
npm test
```
## Configure
The server speaks MCP over stdio. Point your client at `src/index.js`.
### OpenCode
`~/.config/opencode/opencode.json` (or a project-level `opencode.json`):
```json
{
"mcp": {
"servers": {
"monaco": {
"type": "local",
"command": ["node", "/absolute/path/to/monaco-mcp/src/index.js"],
"timeout": 120000
}
}
}
}
```
> The default MCP timeout is 5000 ms, which is too short for a cold browser launch. Raise `timeout`.
### Claude Desktop
`claude_desktop_config.json`:
```json
{
"mcpServers": {
"monaco": {
"command": "node",
"args": ["/absolute/path/to/monaco-mcp/src/index.js"]
}
}
}
```
### Cursor / Windsurf
```json
{
"mcpServers": {
"monaco": {
"command": "node",
"args": ["/absolute/path/to/monaco-mcp/src/index.js"]
}
}
}
```
> **Windows:** use forward slashes (`C:/Users/you/...`) so JSON needs no escaping.
Once published to npm you can use `npx -y monaco-mcp` instead of `node path/to/src/index.js`.
## Configuration (environment variables)
| Variable | Default | Purpose |
|----------|---------|---------|
| `MONACO_MCP_PROFILE_DIR` | `~/.monaco-mcp/profile` | Persistent browser profile. Log in once, sessions survive restarts. |
| `MONACO_MCP_CDP_URL` | *(unset)* | Attach to a browser you started yourself instead of launching one. |
| `MONACO_MCP_CHANNEL` | auto (`chrome`, `msedge`, `chromium`) | Which browser to launch. |
| `MONACO_MCP_EXECUTABLE` | *(unset)* | Explicit path to a Chromium-family binary. |
| `MONACO_MCP_HEADLESS` | `1` to enable | Run without a window (some sites refuse headless). |
| `MONACO_MCP_ARGS` | *(unset)* | Extra CLI args, comma separated. |
**Attach to your own Chrome** (keeps the logins you already have):
```json
{
"mcp": {
"servers": {
"monaco": {
"type": "local",
"command": ["node", "/path/to/monaco-mcp/src/index.js"],
"environment": { "MONACO_MCP_CDP_URL": "http://127.0.0.1:9222" },
"timeout": 120000
}
}
}
}
```
Start Chrome with debugging enabled first (fully quit Chrome first, or the flag is ignored):
```
chrome.exe --remote-debugging-port=9222
```
## Tools
| Tool | Purpose |
|------|---------|
| `open_page` | Open or navigate to a URL. |
| `list_pages` | List open tabs. |
| `get_content` | Read the page's visible text (problem statement, output panel, verdict). |
| `get_code` | Read the current editor contents. |
| `set_code` | **Write code into the editor**, bypassing paste blocking. Supports `replace` / `append`. |
| `inspect_editor` | Diagnostics: which editors were detected and how. Use when `set_code` fails. |
| `click_element` | Click a button or link by its visible text (`Run Code`, `Submit`, …). |
| `wait_for` | Wait for text to appear — e.g. a verdict like `Accepted`. |
| `screenshot` | Capture the page as an image. |
| `close_browser` | Close the managed browser (logins are kept in the profile). |
## A typical session
```
user: solve the problem in the open tab
assistant: open_page(...) # or use the tab already open
get_content() # read the problem statement
set_code(code=...) # code lands in the editor, verified exact
click_element(text="Run") # run the sample tests
wait_for(text="Accepted")
screenshot() # show the result
```
## Notes
- The browser starts **lazily** on the first tool call, so the MCP server starts instantly.
- Only one "current" page is targeted — the most recently used one. `open_page` selects it.
- `set_code` overwrites the editor (`mode: "append"` if you want to add instead).
## License
MIT
TDQS
Scored across 10 tools
The core intents are clearly separated: navigate (open_page), read page text (get_content), read editor code (get_code), write code (set_code), and interact (click_element, wait_for). Minor overlap exists between open_page and click_element (both can change the page) and between get_content/screenshot/get_code as ways to 'see' state, but descriptions steer the agent correctly. inspect_editor is well-differentiated as a fallback diagnostic for set_code failures.
Nine of ten tools follow a clean snake_case verb_noun or verb_noun-ish pattern (open_page, list_pages, get_content, set_code, click_element, wait_for, close_browser, inspect_editor). The lone outlier is 'screenshot', which is a bare noun rather than an action phrase like capture_screenshot. Otherwise highly predictable.
Ten tools is well-scoped for a browser-plus-Monaco-editor automation server; each tool maps to a distinct step in the solve loop (navigate, read, edit, run, wait, verify). No redundant or filler tools, and no obvious need to split or merge any of them.
The read/edit/run/verify loop is fully covered, including a diagnostic fallback (inspect_editor) and session persistence. However, there is no general text-input tool for non-editor fields (login forms, search boxes, custom test-case inputs), which is a real dead end despite the note that logins survive via the profile. Scrolling/selector-based clicking beyond visible text is also absent.