msw-cli
Skill81 starsChanged 5 months agoMITView on GitHub
Control MSW mocks at runtime via msw-cli (open session, add/update/remove handlers). Use when mocking APIs, changing mock responses, or debugging MSW without restarting the app.
In this repository4 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .cursor/rules/testing.mdcCursor rule
- msw-setupSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 16
When using this skill to change mocks:
- Very specific: line 32
Use msw-cli open --port 6789 when the app already hard-codes that port.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
msw-cli
Dynamically add, update, and remove MSW handlers while the app keeps running. Run commands from the project directory (or pass -s <name> for a named session).
Project not set up yet? Use the msw-setup skill or run msw-cli setup first.
Agent guidelines
When using this skill to change mocks:
- Do not edit local handler files (e.g.
handlers.ts,mocks/handlers).msw-cliappliesadd/update/remove/resetto the browser runtime over the WebSocket session. Changing on-disk handler modules is the wrong tool and can confuse the next dev reload. - Before
open, check whether a session is already running: runmsw-cli list(all open sessions) and, if a session for this project already exists,msw-cli status(orstatus -s <name>) to see connection and handlers. Only runopenwhen no suitable session is active—avoid duplicate daemons for the same work.
Quick start
msw-cli open # prints WebSocket URL + port
# If port ≠ app config, update MSW_WS_URL (or VITE_MSW_WS_URL / NEXT_PUBLIC_MSW_WS_URL) to match output
msw-cli add "http.get('/api/users', () => HttpResponse.json([{ id: 1 }]))"
msw-cli status # connected must be true for mocks to apply
msw-cli closeUse msw-cli open --port 6789 when the app already hard-codes that port. list works without a session. Handler commands (add, update, remove, reset, status) require an open session for that cwd (or -s) — otherwise you get "Session not open".
Workflow
list/status— confirm no session is already open (or reuse it). Thenopenonly if needed — start daemon; read Port and WebSocket from markdown output.- Sync app — if port auto-changed (6789 busy), set
ws://127.0.0.1:<port>inMSW_WS_URL/VITE_MSW_WS_URL/NEXT_PUBLIC_MSW_WS_URLenv. add/update/remove/reset— handler changes.status— confirm browser connected.closewhen finished.
Sessions: default name = current directory basename. State: ~/.msw-cli/sessions/.
Handler rules
- Valid JS strings;
http,graphql,HttpResponse,bypass,passthrough,delayare in scope (no imports). - Wrap handlers in double quotes; use single quotes inside JS.
- Return a
ResponseviaHttpResponse(e.g.HttpResponse.json({ ok: true })). - Passthrough / partial override — fetch the real response, then override fields. Always wrap the request in
bypass()to avoid an infinite intercept loop:
msw-cli add "http.get('*/api/user', async ({ request }) => { const res = await fetch(bypass(request)); const data = await res.json(); return HttpResponse.json({ ...data, role: 'admin' }) })"Pattern matching (remove / update)
status displays handlers as METHOD URL, e.g. GET */api/v1/users/*. But remove / update patterns match the handler URL only (substring or * glob) — not the method.
| Command | Pattern is… | Example |
status | Display only: METHOD + URL | GET */api/v1/users/* |
remove/update | URL substring or * glob | */api/v1/users/* |
- To filter by method, use
-m, --method <methods...>(e.g.remove "*/api/users" -m GET). - A leading method token in the pattern (e.g.
"GET */api/users", copied fromstatus) is auto-split into a method filter, so pasting a status line works — but prefer the URL-only pattern plus-mfor clarity. remove/updatereport how many handlers matched.Removed 0or0 matchedmeans the pattern matched nothing — fix the pattern, do not retry with anotherupdate(that just adds a duplicate).
Commands
| Command | Purpose |
open [--port N] [-s name] [--no-persist-handlers] [--persist-handlers N] [--single-client] | Start/reuse daemon (required first) |
add "<handler>" | Add handler(s) |
update "<pattern>" [-m METHOD...] -h "<handler>" | Replace handlers matching pattern |
remove "<pattern>" [-m METHOD...] | Remove handlers (reports match count) |
reset [handlers...] | Clear runtime handlers (optional new set) |
status | Connection + active handlers |
list | All open sessions |
close [name] / close-all | Stop daemon(s) |
Troubleshooting
- Session not open → run
msw-cli list; if no session for this project, runopen, then retry. - No browser clients connected → app not wired to the WebSocket URL from
open; fix port/env and reload. - Mock not firing → match request path exactly or use wildcards (
*/api/users/*). removesays "Removed 0" → the pattern matched no handler. Drop any HTTP method prefix (patterns match the URL only) and match the URL fromstatus, e.g.remove "*/api/v1/users/*". Use-m GETto filter by method.- Duplicate handlers after
update→ theupdatepattern matched nothing, so it added a second handler instead of replacing (the CLI warns0 matched).remove "*/api/..."the extras, thenupdate/addonce with a correct URL pattern. open --portfails → port in use; free it or useopenwithout--portand sync app to the new port.- Handler syntax errors → check shell quoting and JS validity.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

