# redux-webmcp [![npm](https://img.shields.io/npm/v/redux-webmcp.svg)](https://www.npmjs.com/package/redux-webmcp) Published on npm: https://www.npmjs.com/package/redux-webmcp Give an AI browser agent a developer's view of your Redux Toolkit app. It can operate the app through real actions and read the state and transition history behind each one, so it can test and debug the frontend, not just click it. > **Alpha, and not reviewed by a human yet.** The package is tested, linted, type > checked, and validated as an npm artifact on supported runtimes, but the code has not > had a manual human review. Read it before you rely on it. Its public API may still > change before `1.0`, so pin an exact version and review release notes before upgrading. An agent needs two things to debug a frontend: a way to act, and a way to see what its actions did. redux-webmcp gives both over [WebMCP](https://webmachinelearning.github.io/webmcp/): - **Act.** Operate the app through tools, either semantic actions you declare or the raw Redux actions themselves. - **Inspect.** Read the state and the sanitized transition history to confirm what each action changed. Redux DevTools lets you inspect state as a human. redux-webmcp lets an agent operate and inspect, since that extension is human only and a web agent cannot read it. Live demo, one storefront driven by a human and an agent: https://agent-workflow-lab-consult-bf3cd622be26.herokuapp.com/ ## Install ```bash npm install redux-webmcp @reduxjs/toolkit redux ``` Supported runtimes are Node 20, 22, and 24. The peer range covers Redux Toolkit `>=1.9.7 <3` and Redux `>=4.2.1 <6`, including both the Redux 4 generation and the current Redux 5 generation. WebMCP works only in secure contexts (HTTPS or `localhost`). ## Quickstart A complete, copy and paste example. It exposes the whole state for reading, lets the agent dispatch any of the slice's actions, and asks for human approval before the one destructive action. ```ts import { configureStore, createSlice } from '@reduxjs/toolkit'; import { connectReduxWebMCP } from 'redux-webmcp'; const cart = createSlice({ name: 'cart', initialState: { screen: 'catalog', items: [] as string[] }, reducers: { itemAdded(state, action: { payload: { productId: string } }) { state.items.push(action.payload.productId); }, cartCleared(state) { state.items = []; }, }, }); const bridge = connectReduxWebMCP({ appName: 'My shop', // Turn the agent surface on in dev, off in a normal production build. mode: import.meta.env.DEV ? 'developer' : 'off', // Let the agent dispatch any of these actions by type. reducerActions: cart.actions, allowRawDispatch: true, // Require human approval before this one runs. confirm: ['cart/cartCleared'], }); export const store = configureStore({ reducer: cart.reducer, middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(bridge.middleware), }); void bridge.attach(store); ``` The whole integration is two lines against your store: `bridge.middleware` in the middleware chain, and `bridge.attach(store)` once. ## Two ways to expose actions **Raw dispatch, least setup.** Hand over `reducerActions`, set `allowRawDispatch`, and the agent gets one `redux_dispatch` tool limited to your action types. It infers the payload from the state and the transition history. Guard destructive types with `confirm`. This is the Quickstart above. **Semantic actions, most control.** Declare each action the agent may use, with an input schema, an optional precondition, and optional human confirmation: ```ts const addToCart = { id: 'add_to_cart', title: 'Add to cart', description: 'Add a valid catalog item to the cart.', inputSchema: { type: 'object', properties: { product_id: { type: 'string' }, quantity: { type: 'integer', minimum: 1, maximum: 5 }, }, required: ['product_id', 'quantity'], additionalProperties: false, }, toReduxAction: (input) => cart.actions.itemAdded({ productId: String(input.product_id) }), }; // then pass it to the bridge: // connectReduxWebMCP({ ...rest, actions: [addToCart] }) ``` You can mix both: raw dispatch for most actions, a declared semantic action for the few that need a tight schema or a human gate. ## Configuration | Option | What it does | | --- | --- | | `mode` | `'developer'` registers tools, `'off'` registers nothing. Gate this by environment. | | `reducerActions` | Slice action creators the agent may dispatch by type. | | `allowRawDispatch` | Expose the single `redux_dispatch` tool for those types. | | `confirm` | Action types that require human approval, raw dispatch included. | | `describe` | Optional per type `{ description, inputSchema }`, validated before dispatch. | | `actions` | Declared semantic actions, one agent tool each. | | `selectSnapshot` | What state the agent can read. Defaults to the whole state. | | `redactPaths` | State paths to redact, on top of the default private key redaction. | | `applicationMap` | Screens, fields, and navigation, so the agent understands structure. | | `observeUI` | Returns rendered values so the bridge can flag UI versus state drift. | | `webMCPTools` | Your own hand written WebMCP tools, registered alongside the Redux ones. | | `scenarioResetActionId` | The reset action that makes deterministic replay possible. | ## Schema dialect The bridge intentionally supports a small, dependency-free JSON Schema subset: `type`, `properties`, `required`, `enum`, `items`, `minimum`, `maximum`, `minLength`, `maxLength`, `minItems`, `maxItems`, `additionalProperties`, and `description`. Schemas are checked recursively when the bridge is created. Unsupported keywords, invalid values, and keywords combined with the wrong type fail initialization with the exact schema path. They are never silently ignored. ## Tools the agent gets - `redux_initialize_agent`. The first call: purpose, capabilities, current context, and recommended next tools. - **Read.** App description, UI schema, navigation, action and field inspection, state snapshot, transition history, and UI to state comparison. - **Act.** One tool per semantic action, an optional `redux_dispatch` tool for raw action types, plus run scenario, assert state, and export or replay. - `AbortSignal` based disposal for hot reload. `redux_get_transitions` uses a forward cursor. A cursor means "return records after this sequence." Pages contain the earliest matching records, `hasMore`, and a `nextCursor` only when another page exists. This allows an agent to consume history without skipping transitions. Custom tools receive `execute(input, signal?)`. Semantic actions accept `ExecuteActionOptions.signal`, while scenarios and replay accept `{ signal }`. Cancellation is checked before dispatch and before every scenario step. Pass the signal to `fetch` or other cancellable work inside a custom tool. ## Relationship to WebMCP redux-webmcp sits on top of the raw WebMCP API. Every tool it creates is registered through the same `registerTool` call, so the agent sees them like any hand written tool. You can still declare your own WebMCP tools directly, and pass extra ones through the `webMCPTools` option to register them next to the Redux tools. ## Safety - No arbitrary state setter. Raw dispatch is opt in and limited to your slice's own action types. - Private keys are redacted (`token`, `password`, `secret`, and so on). Add more with `redactPaths`. - Inputs are schema validated. Off screen actions are rejected with a reason. - **Confirmation required actions reject the agent until a human approves them**, for raw dispatch too. - History and tool output are length bounded. Replay needs an explicit reset action. Before replay changes state, the complete scenario is statically checked. Empty, oversized, unknown, schema-invalid, reset-containing, non-replayable, and confirmation-required steps are rejected without resetting or dispatching. The configured reset action must be global, accept `{}`, have no precondition or availability gate, require no confirmation, and not itself be part of the scenario. A later dynamic precondition can still fail after earlier replay steps have run. In that case the store remains at the reached replay state so the transition history truthfully represents what happened. `auditAgentArchitecture()` validates the whole contract at startup, so a misconfigured setup fails loudly instead of exposing a broken surface to an agent. ## Production posture Set `mode: import.meta.env.DEV ? 'developer' : 'off'`. Off registers no tools, so the agent sees nothing, the same idea as Redux `devTools: false`. Turn it on only where you want the agent surface, such as local dev and a hosted demo. For zero footprint in a real production bundle, import the package behind a dev only dynamic import. ## Adding this with an AI coding agent If you ask an AI coding assistant to add redux-webmcp, point it at this README and at [AGENTS.md](./AGENTS.md), which is a short, imperative integration playbook. The short version: install the three packages, create the bridge, add `bridge.middleware` to the store, call `bridge.attach(store)` once, choose raw dispatch or semantic actions, and list destructive actions in `confirm`. ## More - [ARCHITECTURE.md](./ARCHITECTURE.md) for how it works and why. - [AGENTS.md](./AGENTS.md) for AI coding agents. - [CHANGELOG.md](./CHANGELOG.md) for what changed. - [CONTRIBUTING.md](./CONTRIBUTING.md) to contribute. ## License [MIT](./LICENSE)