Skip to main content
Glama
Scratch Agent MCP

Scratch Agent MCP

by yoericwsg
README.md
# Scratch Agent MCP

A local-first MCP server and Chrome extension that let an AI agent inspect and make **explicitly permitted changes** in an open Scratch or TurboWarp editor. The extension is the bridge between MCP (stdio) and the editor tab; it does not automate clicks or send your Scratch login to the agent.

> **Status:** early prototype (0.2.0). Scratch and TurboWarp can change internal editor APIs. Support is best-effort; the included tools fail with an explanatory error if the current editor cannot be inspected safely.

> **Fastest path:** run `master-setup.bat`, choose the integrations you actually
> use, then follow the prompts. It is intentionally marked **ADVANCED / RISKY**.

## Start here

| Goal | Read/run |
| --- | --- |
| Guided setup | [`master-setup.bat`](master-setup.bat) or [`ADVANCED-SETUP.md`](ADVANCED-SETUP.md) |
| Manual setup | [Install and pair](#install-and-pair) |
| Add Cursor/OpenCode/Codex/Claude | [`INTEGRATIONS.md`](INTEGRATIONS.md) |
| Contribute code | [`CONTRIBUTING.md`](CONTRIBUTING.md) |
| Report a security issue | [`SECURITY.md`](SECURITY.md) |

## What it can do

| MCP tool | Capability switch | Behavior |
| --- | --- | --- |
| `scratch_status` | Read workspace | Report editor, project ID when visible in the URL, selected target, sprites, block count, and supported controls. |
| `scratch_inspect_workspace` | Read workspace | Read a bounded list of block opcodes/fields and the current target's variables. |
| `scratch_create_variable` | Create variables & lists | Create a scalar variable or list on the current workspace. Existing matching variables are left alone. |
| `scratch_build_script` | Build scripts | Add a new green-flag script from a small, validated block allowlist. Variable blocks use an existing scalar variable. |
| `scratch_start_project` / `scratch_stop_project` | Control playback | Start the green flag or stop all running scripts. |
| `scratch_set_turbo_mode` | Toggle TurboWarp mode | Change TurboWarp VM turbo mode where the editor exposes that feature. |

Block scripts currently support `event_whenflagclicked`, a few Motion blocks, `looks_say`, `looks_sayforsecs`, `looks_think`, `looks_show`, `looks_hide`, `control_wait`, and the set/change variable blocks. New blocks are validated on both the MCP side and in the extension before changing the workspace. Scripts are limited to 32 blocks and text inputs to 500 characters.

The agent operates on the **currently selected target** in the **active editor tab** (or the first matching editor tab if none is active). It does not save, publish, share, delete, or download projects. It does not read account credentials, project contents from Scratch's cloud, or private reasoning from the model.

## Requirements

- Node.js 20 or later (no npm packages are required by the server).
- Chrome or another Chromium browser supporting Manifest V3.
- A Scratch project editor or TurboWarp editor tab.
- An MCP-compatible AI client.

## Install and pair

### 1. Generate your local pairing token

From the cloned/downloaded project directory, run this in PowerShell, Terminal, or your shell:

```sh
node ./scripts/setup.mjs
```

`setup.mjs` prints a randomly generated pairing token and stores it in `%LOCALAPPDATA%\ScratchAgentMCP\config.json` plus `%LOCALAPPDATA%\ScratchAgentMCP\pairing-token.txt` on Windows, or `~/.config/ScratchAgentMCP/` on macOS/Linux. Keep this token private. Your MCP client launches the server process when it connects; you normally do **not** need to run `npm start` separately. The server listens on `127.0.0.1:4317`, not your network interfaces.

To rotate a token that may have leaked, run `node ./scripts/setup.mjs --rotate`,
then re-pair the extension. Clients using the device-local setup directory do
not need a config edit. If you explicitly set `SCRATCH_MCP_TOKEN` in a client,
replace that private value too.

### 2. Add the MCP server to your AI client

MCP clients launch this server as a **local stdio process**. Replace the example path with the absolute path to your clone.

#### Generic `mcpServers` configuration (Claude Desktop and compatible clients)

```json
{
  "mcpServers": {
    "scratch-agent": {
      "command": "node",
      "args": ["C:/path/to/scratch-agent-mcp/src/server.mjs"]
    }
  }
}
```

The server reads the token from the device-local setup directory, so the token
does not need to be copied into client config. `SCRATCH_MCP_TOKEN` remains
available as an optional environment override for isolated/test setups.

For Claude Desktop, this is usually in `%APPDATA%\Claude\claude_desktop_config.json`. Restart Claude Desktop after editing the config.

#### OpenCode example

Add this to `opencode.json` in your OpenCode config directory or project root.
Use forward slashes in Windows paths. The server reads the device-local token,
so no token belongs in this file:

```json
{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "servers": {
      "scratch-agent": {
        "type": "local",
        "command": ["node", "C:/path/to/scratch-agent-mcp/src/server.mjs"],
        "protocol": "legacy"
      }
    }
  }
}
```

For clients that do not use the local config path, set `SCRATCH_MCP_TOKEN`
through a private machine-local environment setting rather than committing a
token. Anyone with the token and access to your browser extension could issue
calls to the local bridge.

### 3. Load the extension

1. Configure and open your MCP client so it starts the local server process.
2. Open `chrome://extensions` and turn on **Developer mode**.
3. Choose **Load unpacked** and select this repository's `extension` folder.
4. Open Scratch Agent Link, then choose **Open connection settings**.
5. Paste the setup token and press **Save & connect**. The page checks the local bridge before storing the token.
6. Open a project in `https://scratch.mit.edu/` or `https://turbowarp.org/`. Select **Scripts** / **Code** so the block workspace is loaded.

The Chrome extension host permissions are limited to those two editor sites and the local bridge. It uses Chrome's `scripting` API to run an allowlisted operation in the selected tab; it does not install an editor script or add blocks to Scratch's extension catalog.

## Advanced master setup

If you want a guided setup that detects installed clients and asks what to do,
run the clearly labelled **ADVANCED / RISKY** command:

```cmd
master-setup.bat
```

It can run pairing setup, offer Codex CLI, OpenCode, Claude Code, Claude
Desktop, Cursor, Antigravity IDE, generic MCP output, and the browser extension
manual-load steps. It asks before config changes and process launch, backs up
existing JSON under `%LOCALAPPDATA%\ScratchAgentMCP\backups`, and never puts the
pairing token into generated client config.

Preview detection without writing, opening, or launching anything:

```cmd
master-setup.bat --dry-run
```

Read [ADVANCED-SETUP.md](ADVANCED-SETUP.md) and
[INTEGRATIONS.md](INTEGRATIONS.md) first. Browser extensions cannot be silently
installed by a local script; **Load unpacked** remains a manual Chrome/Edge
step.

## Settings and safety

The extension settings page has individual switches for:

- Read workspace (on by default)
- Create variables and lists (on by default)
- Build scripts (off by default)
- Control playback (off by default)
- TurboWarp mode (off by default)
- Ask before edits (on by default; one-time confirmation for variables and scripts)

Permissions are enforced in the extension service worker, not just hidden from the UI. The browser page cannot invoke the main-world execution function directly. The bridge checks a bearer token with a timing-safe comparison, only listens on the IPv4 loopback interface, limits CORS to Chrome extension origins, and bounds request sizes and tool inputs. Never paste the token in a public issue, repo, or chat.

Editing blocks changes the open project in the browser. Review it and use the editor's own save controls if you want to save. A browser tab may already be authenticated to Scratch; the agent itself is not given your Scratch password or session cookies.

## Troubleshooting

- **“MCP server is offline”** — make sure your MCP client is connected and has launched the server process; verify the extension token matches `%LOCALAPPDATA%\ScratchAgentMCP\config.json`. For standalone pairing tests, stop the MCP client first, then run `node .\src\server.mjs` from the repo.
- **“Open a Scratch project editor…”** — navigate to a project editor, not the Scratch homepage or a shared-project player.
- **Workspace not ready** — choose Scripts/Code, wait for blocks to appear, then retry. Reloading the editor may help after Scratch updates.
- **Permission is off** — turn on the corresponding capability in Settings. Script and variable edits can additionally prompt once for approval.
- **Client does not discover tools** — restart the MCP client after editing its config and confirm its local process can run `node`.
- **Port 4317 is busy** — edit the `port` in `%LOCALAPPDATA%\ScratchAgentMCP\config.json` and update `BRIDGE_URL` in `extension/service-worker.js` plus the extension host permission before loading the extension. (Configurable port UI is not included yet.)

## Development and checks

This project has no third-party runtime dependencies.

```powershell
node --test
node --check .\src\server.mjs
node --check .\src\core.mjs
node --check .\scripts\setup.mjs
node --check .\scripts\master-setup.mjs
node --check .\scripts\preview.mjs
node --check .\extension\service-worker.js
node --check .\extension\content.js
node --check .\extension\options.js
node --check .\extension\popup.js
```

To preview the settings screen outside Chrome without connecting it:

```powershell
node .\scripts\preview.mjs
```

Then open `http://127.0.0.1:4173/options.html`. This is only a visual preview; connection and storage actions require the unpacked extension.

The page-to-extension `postMessage` bridge is intentionally not used: it would allow the page to fake operation results or invoke editor actions. The service worker runs a self-contained function through `chrome.scripting.executeScript` only after permission checks (and, for edits, user approval).

## GitHub repository

This project is published publicly at <https://github.com/yoericwsg/scratch-agent-mcp>. Clone it with:

```powershell
git clone https://github.com/yoericwsg/scratch-agent-mcp.git
cd scratch-agent-mcp
```

Then follow the install steps above. For your own version, use GitHub's **Fork** button or create a separate repository under your account and push your clone there. Never commit your local pairing token.

## License and project relationship

This project is MIT-licensed (see `LICENSE`). Scratch is a project of the Scratch Foundation, and TurboWarp is a separate project. This is an independent community tool, not affiliated with or endorsed by either project. Review each project's own trademarks and policies before distribution.