Using with an MCP server

An MCP App has two halves: a server that registers a tool plus a ui:// resource, and a React view that the host renders in a sandboxed iframe. MCP Blocks covers the view.

1.Install the SDKs

$npm install @modelcontextprotocol/ext-apps @modelcontextprotocol/server @modelcontextprotocol/node zod react react-dom

Package names and APIs follow ext-apps 2.x and may change. Check the ext-apps docs before copying. ext-apps also needs @modelcontextprotocol/client and zod 4.2 or newer as peers; npm installs them for you, pnpm and yarn may ask you to add them.

2.Register a tool and its UI resource

The tool points at a ui:// resource. The resource returns your bundled view as a single HTML file.

import fs from "node:fs/promises"
import { McpServer } from "@modelcontextprotocol/server"
import { registerAppResource, registerAppTool, RESOURCE_MIME_TYPE } from "@modelcontextprotocol/ext-apps/server"
import { z } from "zod/v4"

const server = new McpServer({ name: "deals", version: "1.0.0" })
const viewUri = "ui://deals/summary.html"

registerAppTool(
  server,
  "show-deal",
  {
    description: "Show a deal as a card",
    inputSchema: z.object({ id: z.string() }),
    _meta: { ui: { resourceUri: viewUri } },
  },
  async ({ id }) => {
    const deal = await getDeal(id)
    return {
      content: [{ type: "text", text: `${deal.name}: ${deal.stage}` }],
      structuredContent: deal,
    }
  }
)

registerAppResource(server, "Deal summary", viewUri, { mimeType: RESOURCE_MIME_TYPE }, async () => ({
  contents: [
    {
      uri: viewUri,
      mimeType: RESOURCE_MIME_TYPE,
      text: await fs.readFile("dist/view.html", "utf-8"),
    },
  ],
}))

Serve it over Streamable HTTP, then add the URL as a custom connector in Claude. On any runtime with web standard Request and Response (Cloudflare Workers, Deno, Bun) that is one line with createMcpHandler. On Node use NodeStreamableHTTPServerTransport from @modelcontextprotocol/node, or stdio for local use.

import { createMcpHandler } from "@modelcontextprotocol/server"

// Wrap your server setup above in a function so each request gets a fresh server.
const handler = createMcpHandler(() => createServer())

export default {
  fetch: (request: Request) => handler.fetch(request),
}

This site runs exactly that: the demo server at https://mcpblocks.dev/mcp is a Cloudflare Worker next to the static docs. Its source is in examples/deal-app.

3.Add the components to the view

$npx shadcn@latest add https://mcpblocks.dev/r/inline-card.json

Bundle the view into one HTML file (for example with vite-plugin-singlefile), because the host loads it as a single resource. See Installation for what the CLI adds.

4.Connect the view to the host

useApp performs the handshake. useHostStyles applies the CSS variables Claude sends (colors, fonts, radii), and applyHostContext passes theme, display mode and safe-area insets on to the components. Without useHostStyles the components fall back to the built-in token values.

import { useApp, useHostStyles } from "@modelcontextprotocol/ext-apps/react"
import { useEffect, useState } from "react"

import { InlineCard } from "@/components/inline-card"
import { applyHostContext } from "@/lib/host"

type Deal = { name: string; owner: string; stage: string }

export function DealView() {
  const [deal, setDeal] = useState<Deal>()

  const { app } = useApp({
    appInfo: { name: "Deal summary", version: "1.0.0" },
    capabilities: {},
    onAppCreated: (app) => {
      app.ontoolresult = (result) => setDeal(result.structuredContent as Deal)
      app.onhostcontextchanged = (ctx) => applyHostContext(ctx)
    },
  })

  // Applies the colors, fonts and theme the host sends, which the host-* utilities read.
  useHostStyles(app, app?.getHostContext())
  // First paint: apply the initial context once connected.
  useEffect(() => {
    if (app) applyHostContext(app.getHostContext())
  }, [app])

  if (!deal) return null

  return (
    <InlineCard
      title={deal.name}
      fields={[
        { label: "Owner", value: deal.owner },
        { label: "Stage", value: deal.stage },
      ]}
      actions={[
        { label: "Open record", onClick: () => app?.requestDisplayMode({ mode: "fullscreen" }) },
        {
          label: "Draft follow-up",
          onClick: () =>
            app?.sendMessage({ role: "user", content: [{ type: "text", text: "Draft a follow-up" }] }),
        },
      ]}
    />
  )
}

The initial host context is available from app.getHostContext() once connected, which is why the effect above applies it too.

5.Talk back to the host

  • app.callServerTool runs another tool on your server, for example on a confirm button.
  • app.sendMessage adds a message to the chat. Prefer this over in-widget forms for edits.
  • app.requestDisplayMode asks for fullscreen or pip.

6.Test it

  • Run the server and add it as a custom connector in Claude (Settings, Connectors, Add custom connector, then paste the /mcp URL), or use the basic host from the ext-apps repo. To try ours first, add https://mcpblocks.dev/mcp and ask Claude to show your open deals.
  • Check light and dark, inline and fullscreen, and a phone-sized window.
  • Watch the console for [mcpblocks] warnings. They flag guideline limits such as more than two inline actions.