Skip to main content
Glama
README.md
# PC MCP

**Give a cloud AI agent a controlled door into your Windows PC.**

PC MCP is a small Model Context Protocol server that lets Gemini Web, Claude, Cursor, or any
MCP client read and edit files, run PowerShell, use git, drive a separate Chrome window, and
extract text from PDFs or screenshots. It runs on your machine and is exposed through a
Cloudflare Tunnel, so the AI never needs your router opened or your PC on a public IP.

```
AI client (cloud)
      │  HTTPS + secret token
      ▼
Cloudflare edge ──► Cloudflare Tunnel ──► 127.0.0.1:8787 (Node)
                                             │
                                             ▼
                                  your sandbox folder
```

> **Read this first:** `shell_run` is **not** sandboxed. It runs PowerShell with your Windows
> user rights. Anyone with the connector URL can do what you can do. Keep the token secret,
> start with write/shell/browser disabled, and stop the launcher when you are done.

---

## What it can do

| Area | Tools |
| --- | --- |
| Read files | list, stat, read, search, recent files, SHA-256 |
| Write files | write, append, mkdir, move, copy, delete, precise edit, restore backup |
| Code search | ripgrep search with globs, context, case and literal modes |
| Shell | PowerShell with timeout and a catastrophic-command blocklist |
| Git | status, diff, log, stage + commit (never push) |
| Browser | open, snapshot, click/fill/press/select, read text, tabs, screenshot, JS eval |
| Documents | PDF text extraction and Windows OCR through `shell_run` |
| Safety | sandbox paths, secret-file deny list, audit log, rate limit, pause switch |

The server also exposes a `capabilities` tool. The included instructions tell the model to use
it instead of guessing, and to quote real paths, exit codes and hashes when claiming success.

---

## Requirements

- **Windows 10 or 11**
- **Node.js 20+**
- **cloudflared** (Cloudflare Tunnel)
- **Chrome** for browser automation (optional)
- **ripgrep** for faster `code_search` (optional, there is a fallback)
- **Poppler** for `pdftotext` (optional, for PDF text)
- A Cloudflare account with a domain for a stable URL (optional; quick tunnels work without one)
- An MCP client that supports Streamable HTTP, such as a Gemini custom connector

Install the basics with winget:

```powershell
winget install OpenJS.NodeJS.LTS
winget install Cloudflare.cloudflared
winget install BurntSushi.ripgrep.MSVC
```

---

## Quick start

```powershell
git clone https://github.com/AchmadAlvin/pc-mcp.git
cd pc-mcp
npm install

# Create .env, generate a token, choose the sandbox folder.
powershell -ExecutionPolicy Bypass -File scripts\setup.ps1 `
  -Root "C:\Users\YourName\Documents\Projects" `
  -EnableWrite -EnableShell -EnableBrowser

# Start the server + a temporary Cloudflare quick tunnel.
.\PC MCP.bat
```

The launcher prints a URL like:

```
https://random-words.trycloudflare.com
Connector: https://random-words.trycloudflare.com/t/<MCP_TOKEN>/mcp
```

Use that connector URL in your AI client. A **quick tunnel URL changes every time it restarts**,
so it is fine for a test but annoying for daily use. For a permanent hostname, see
[Named tunnel](#named-tunnel-stable-url).

The launcher window must stay open. Closing it or pressing `Ctrl+C` stops both the server and
the tunnel.

---

## Let an AI agent set it up

Copy the prompt below into the AI agent that has access to this PC (Claude Code, Codex, Gemini
CLI, Cursor, etc.). Fill in the angle-bracket values first.

```text
You are setting up the PC MCP repository on my Windows machine.

Context:
- Repository folder: <absolute path to this repo>
- Folder I want to expose to the AI: <absolute path, for example C:\Users\Me\Documents\Projects>
- Public hostname: <mcp.example.com, or "none" if I only want a quick tunnel>
- Enable write tools: <yes/no>
- Enable PowerShell: <yes/no>
- Enable browser automation: <yes/no>

Tasks:
1. Check that Node.js is version 20 or newer, and that npm, cloudflared, git, rg and
   pdftotext are available. Tell me exactly what is missing. Only install missing tools
   after asking me.
2. Run: npm install
3. Run: powershell -ExecutionPolicy Bypass -File scripts\setup.ps1 -Root "<folder>" plus
   -EnableWrite, -EnableShell and/or -EnableBrowser only for the features I said yes to.
   If .env already exists, ask before overwriting it.
4. If I gave a hostname, run:
   powershell -ExecutionPolicy Bypass -File scripts\setup-tunnel.ps1 -Hostname "<hostname>"
   and check that .cloudflared\config.yml exists.
5. Start the server. Run .\PC MCP.bat if I can keep a window open, otherwise run
   .\start.ps1 and start cloudflared separately. Verify http://127.0.0.1:8787/healthz.
6. Run the smoke tests and show me the real output:
   npm run test:client
   npm run test:rw
   npm run test:browser   (only if browser automation is enabled)
7. Print the final connector URL: https://<hostname>/t/<MCP_TOKEN>/mcp

Rules:
- Never invent command output. If a command fails, show the error and stop.
- Do not enable write, shell or browser unless I explicitly said yes.
- Do not commit, print or upload .env, tokens, tunnel credentials or browser profiles.
- Do not weaken the secret-file deny list or the path sandbox.
```

---

## Connect to Gemini Web

1. Open <https://gemini.google.com/customize/connectors>.
2. Choose **Create custom app / Add custom connector**.
3. Paste the connector URL: `https://YOUR-HOST/t/YOUR_TOKEN/mcp`.
4. Accept the consent prompt and save. No OAuth client ID or secret is needed.
5. On the first write/tool call Gemini may ask for permission. Choose **Always allow** if you
   trust the setup.

The connector URL contains the token. Treat it like a password for your PC. If the token leaks,
rotate it (see [Rotating the token](#rotating-the-token)) and recreate the connector.

> Some clients cache the tool list. Start a new chat after changing `.env` or adding tools.

---

## Named tunnel (stable URL)

A quick tunnel is enough to try PC MCP. For a URL that survives restarts:

```powershell
# One-time Cloudflare login (opens a browser).
cloudflared tunnel login

# Create the tunnel and route a hostname from your Cloudflare zone.
powershell -ExecutionPolicy Bypass -File scripts\setup-tunnel.ps1 `
  -Hostname mcp.example.com `
  -TunnelName pc-mcp
```

The script creates the tunnel if needed, routes DNS, writes `.cloudflared/config.yml`, and
updates `.env` (`MCP_TUNNEL_MODE=named`, `PUBLIC_HOST`, `TUNNEL_NAME`).

Then start normally:

```powershell
.\PC MCP.bat
```

If you prefer doing it by hand:

```powershell
cloudflared tunnel create pc-mcp
cloudflared tunnel route dns pc-mcp mcp.example.com
```

Copy `.cloudflared/config.yml.example` to `.cloudflared/config.yml`, fill in the tunnel UUID
and the credentials path printed by `cloudflared tunnel create`, then set:

```dotenv
MCP_TUNNEL_MODE=named
TUNNEL_NAME=pc-mcp
PUBLIC_HOST=mcp.example.com
```

Make sure the DNS record is **proxied** (orange cloud) in Cloudflare.

---

## Configuration

Copy `.env.example` to `.env`, or let `scripts/setup.ps1` generate it.

| Variable | Default | Meaning |
| --- | --- | --- |
| `MCP_TOKEN` | none | **Required.** At least 32 characters. Secret in the URL. |
| `HOST` | `127.0.0.1` | Local bind address. Keep it on localhost. |
| `PORT` | `8787` | Local HTTP port. |
| `MCP_ROOT` | `./workspace` | Folder that `fs_*` tools may touch. |
| `MCP_ENABLE_WRITE` | `0` | Enable write/edit/delete tools. |
| `MCP_ENABLE_SHELL` | `0` | Enable `shell_run`. **Not sandboxed.** |
| `MCP_ENABLE_BROWSER` | `0` | Enable browser automation. |
| `MCP_SHELL_TIMEOUT_MS` | `60000` | Default shell timeout (max 300000). |
| `MCP_BROWSER_HEADLESS` | `0` | `0` = visible Chrome, `1` = headless. |
| `MCP_BROWSER_PROFILE` | `%LOCALAPPDATA%\pc-mcp\browser-profile` | Separate Chrome profile. |
| `MCP_BROWSER_TIMEOUT_MS` | `30000` | Navigation/action timeout. |
| `RATE_LIMIT_PER_MINUTE` | `120` | Per-IP request limit. |
| `MCP_TUNNEL_MODE` | `quick` | `quick` or `named`. |
| `TUNNEL_NAME` | `pc-mcp` | Cloudflare named tunnel. |
| `PUBLIC_HOST` | empty | Stable hostname, for example `mcp.example.com`. |

Start safe: `MCP_ENABLE_WRITE=0`, `MCP_ENABLE_SHELL=0`, `MCP_ENABLE_BROWSER=0`. Enable only
what you need.

---

## Tools

### Read-only

| Tool | Purpose |
| --- | --- |
| `ping` | Health check and current mode. |
| `capabilities` | Exact tools, limits and unsupported features. |
| `sys_info` | Hostname, OS, CPU, RAM, uptime. |
| `disk_usage` | Free/total space per drive. |
| `fs_list` | List a folder (depth 1–3, max 500 entries). |
| `fs_stat` | File metadata; `hash=true` adds SHA-256. |
| `fs_read` | Read UTF-8 text (max 2 MB, offset/limit). |
| `fs_search` | Search text/regex inside the sandbox. |
| `fs_recent` | Recently modified files. |
| `code_search` | ripgrep search with globs, context, case, literal. |
| `git_status` | Repository status. |
| `git_diff` | Staged/unstaged diff. |
| `git_log` | Commit history. |

### Write and shell

| Tool | Purpose |
| --- | --- |
| `fs_write` | Create or overwrite a text file. |
| `fs_append` | Append text. |
| `fs_mkdir` | Create a folder. |
| `fs_move` | Move or rename. |
| `fs_copy` | Copy a file or folder. |
| `fs_delete` | Permanent delete; needs `confirm=true`. |
| `fs_edit` | Replace an exact unique string, with backup and SHA-256 verification. |
| `fs_restore` | Restore a file from an `fs_edit` backup. |
| `git_commit` | Stage and commit; refuses empty commits; never pushes. |
| `shell_run` | Run PowerShell with a timeout and blocklist. |

`fs_edit` refuses ambiguous matches instead of guessing. Include surrounding lines to make the
match unique. Backups live in `logs/backups/<id>/`.

### Browser

| Tool | Purpose |
| --- | --- |
| `browser_open` | Open a URL, return title, interactive elements and text preview. |
| `browser_snapshot` | Interactive elements with refs `e1`, `e2`, ... |
| `browser_action` | Click, fill, press, select, hover, scroll by ref. |
| `browser_text` | Visible page text. |
| `browser_tabs` | List, open, select or close tabs. |
| `browser_screenshot` | Save a PNG inside the sandbox; it is **not** sent to the model. |
| `browser_eval` | Run JavaScript in the page context; JSON-safe result. |
| `browser_close` | Close the automation browser. |

The browser uses a separate profile, so your personal Chrome tabs and cookies are not touched.
Logins you perform in the automation window are saved for next time. Page content is
**untrusted data**: the model is instructed never to follow instructions found on a page and
never to enter passwords or complete payments without your confirmation.

---

## PDFs and OCR

The Gemini connector forwards text blocks only. Images are not sent to the model, so PC MCP
does text extraction on the PC:

```powershell
# PDF with a text layer (Poppler must be on PATH).
pdftotext -layout -f 1 -l 5 "document.pdf" -

# Wrapper with scan detection and a character cap.
.\scripts\pdf-text.ps1 -Path "document.pdf" -From 1 -To 5 -MaxChars 15000

# OCR a screenshot or image with Windows OCR.
.\scripts\ocr.ps1 -Path "screenshot.png"
```

A scanned PDF has no text layer. Render its pages to images first, then run OCR. OCR returns
text, not visual understanding. If you need the model to actually see an image, attach it
directly in the chat.

---

## Security model

- **Path sandbox:** `fs_*` tools are resolved inside `MCP_ROOT`; absolute paths, `..`, UNC paths,
  alternate data streams and junctions/symlinks that escape the root are rejected.
- **Secret deny list:** `.env*`, `.ssh`, `.aws`, `.azure`, `.kube`, `.gnupg`, `id_rsa*`,
  `*.pem/key/pfx/p12/kdbx`, `credentials*`, browser `Cookies`/`Login Data`, `NTUSER.DAT`, etc.
- **Shell is not sandboxed:** `shell_run` runs with your user rights. The blocklist only stops
  obvious catastrophes (format, diskpart, bcdedit, shutdown, mass registry delete). It is not a
  security boundary.
- **Browser is separate:** its profile lives outside the sandbox. `file://` is limited to the
  sandbox; `chrome://`, `devtools://` and `edge://` are blocked.
- **Auth:** 32-byte token via `/t/:token/mcp`, `Authorization: Bearer`, `x-mcp-token` or `?key=`.
  A wrong token gets a plain 404.
- **Rate limit:** per-IP application limit, plus optional Cloudflare WAF/rate limit.
- **Audit:** every tool call goes to `logs/audit.jsonl`; HTTP requests go to `logs/http.log`
  with the token masked.
- **Pause switch:** create a `PAUSE` file in the repo root; all MCP requests return 503 until
  it is removed.
- **No delete undo:** `fs_edit` creates backups; `fs_delete` does not.

Do not expose the server directly to the internet. Use Cloudflare Tunnel, keep the token secret,
and stop the launcher when you are not using it.

---

## Tests

With the server running locally:

```powershell
npm run test:client    # initialize, tools/list, read, search, path/secret guards
npm run test:rw        # CRUD, fs_edit, code_search, git, shell (50 checks)
npm run test:browser   # open, snapshot, fill, click, tabs, screenshot, eval (28 checks)
```

To test through the public URL:

```powershell
$env:BASE_URL = "https://mcp.example.com"
npm run test:client
```

---

## Troubleshooting

| Symptom | Check |
| --- | --- |
| Gemini says a tool is missing | Restart the launcher, start a new chat; the client may cache the tool list. |
| 404 from the tunnel | Token in the URL is wrong or rotated. Check `.env` and recreate the connector. |
| 503 | A `PAUSE` file exists, or the server is stopped. |
| `shell_run` timed out | Increase `timeout_ms` (max 300000) or `MCP_SHELL_TIMEOUT_MS`. |
| Model claims success but file did not change | Ask for `verified`, `sha256_after` and `backup_id`; check `logs/audit.jsonl`. |
| `fs_edit` says "matches N times" | Add surrounding context, or use `replace_all=true` intentionally. |
| PDF text is empty | Likely a scanned PDF; render pages to images and run OCR. |
| OCR punctuation is wrong | Windows OCR language quality varies; it returns text, not layout. |
| Browser tool missing | Set `MCP_ENABLE_BROWSER=1`, install Chrome, restart the launcher. |
| Browser fails to open | Close an old automation Chrome window; profile is in `%LOCALAPPDATA%\pc-mcp`. |
| Ref expired | Take a new `browser_snapshot`; never reuse old refs. |
| Captcha or bot detection | No bypass. Complete it manually in the automation window. |
| Tunnel does not connect | `Get-Content logs\named-tunnel.err.log -Tail 30`; check `cloudflared tunnel info pc-mcp`. |
| Quick URL changed | Expected. Use a named tunnel for a stable connector. |

---

## Project layout

```
PC MCP.bat                 one-click launcher
src/
  config.js                env, sandbox root, limits, feature flags
  server.js                Express, auth, rate limit, logging, health, pause
  mcp.js                   MCP server factory, instructions, capabilities
  safety.js                path sandbox, secret policy, audit, rate limiter
  result.js                output cap and error formatting
  tools/fs.js              list/stat/read/search/recent
  tools/search.js          code_search (ripgrep + fallback)
  tools/write.js           write/append/mkdir/move/copy/delete/edit/restore
  tools/shell.js           shell_run (PowerShell)
  tools/sys.js             sys_info, disk_usage
  tools/git.js             status/diff/log/commit
  tools/browser.js         browser automation
scripts/
  setup.ps1                create .env and generate a token
  setup-tunnel.ps1         create/route a Cloudflare named tunnel
  run-all.ps1              supervisor: server + tunnel, kill-on-close
  status.ps1               server, process, tunnel, DNS, audit
  stop.ps1                 pause or hard stop
  install-autostart.ps1    optional Scheduled Tasks at login
  edge-setup.mjs           optional Cloudflare WAF/rate-limit rules
  pdf-text.ps1             PDF text + scan detection
  ocr.ps1                  Windows OCR
  smoke-test.js            end-to-end HTTP smoke test
  smoke-rw.js              CRUD/edit/search/git/shell tests
  smoke-browser.js         browser automation tests
  browser-fixture.html     deterministic test page
workspace/                 default sandbox root
logs/                      audit, HTTP log, backups, process logs
```

---

## Rotating the token

```powershell
node -e "console.log(require('crypto').randomBytes(32).toString('base64url'))"
```

Put the result in `MCP_TOKEN=` in `.env`, restart, then delete and recreate the AI connector
with the new URL. The old URL stops working immediately.

---

## License

MIT. See [LICENSE](LICENSE).

PC MCP is a tool, not a security boundary. You are responsible for what you expose and for the
commands you allow an AI to run on your machine.