Protocol hooks

Connect a React view to its MCP host and handle tool calls, streaming results, and prepared writes.

1.Connect the view

npx shadcn@latest add https://mcpblocks.dev/r/hooks.json
import { McpHostProvider, useHost } from "@/hooks/use-host"
import { useToolResult } from "@/hooks/use-tool-result"

export function App() {
  return (
    <McpHostProvider appInfo={{ name: "My view", version: "1.0.0" }}>
      <View />
    </McpHostProvider>
  )
}

function View() {
  const { app, error, context } = useHost()
  const { partialInput, input, result } = useToolResult()
  if (error) return <p role="alert">{error.message}</p>
  if (!app) return <p>Connecting…</p>
  return <pre>{JSON.stringify(result ?? input ?? partialInput ?? context)}</pre>
}

Mount one provider per view. It applies the host theme, CSS variables, font styles and safe areas, and reports the view size. requestDisplayMode(mode) returns the mode the host grants. Use context.displayMode to lay out the view.

2.Call a tool

const { call, available } = useToolCall("show_deal")
const outcome = await call({ id: "d1" }, { timeoutMs: 5000 })

A missing tool capability yields unavailable. A reply marked as an error yields error with the server result attached. Transport failures also yield error, with no result. A request deadline yields timeout. A timeout or a transport failure after sending a write can leave its outcome unknown.

3.Handle a prepared write

idle → preparing → preview → confirming
            ↓                     ├─→ saved
          blocked                 ├─→ refused
                                  └─→ unknown ⇄ checking
                                                    ├─→ saved
                                                    └─→ refused

prepare returns a preview or a reason that blocks the proposal. confirm calls your execute function once with the preview and its idempotency key. A write with an uncertain outcome enters unknown. checkAgain calls your read-only reconcile function with the original key. Use canClose to lock the enclosing UI during the request and while the outcome remains unknown.

On saved, the hook reports your result summary through tellModel. Set fallbackToMessage: true if you want a visible chat message when the host lacks text model-context updates. Your server should store the key and the write result together so it can return the same receipt on a later connection.

The deal app uses usePreparedWrite to drive ConfirmAction. See the prepared writes guide for the confirmation UI and server responsibilities.

4.Test in a fake host

npx shadcn@latest add https://mcpblocks.dev/r/host-harness.json

Wrap a Storybook view with FakeHost in place of McpHostProvider. Pass host context, capabilities, and a tool map. Use toolStub to simulate success, refusals, slow responses, errors and timeouts. onRequest receives each message sent by the view.

For Vitest, createFakeHost returns a bridge, a view transport and a request recorder. Supply the transport to McpHostProvider, then use the bridge to deliver tool inputs and results. The repository's Storybook prepared-write stories also let you change theme, mode, container width, safe areas and capabilities while testing the flow.