vapor-chamber API reference - v1.16.0
    Preparing search index...

    Function createMcpHandler

    • Create a transport-agnostic MCP message handler for a schema command bus.

      Takes one parsed JSON-RPC 2.0 message, returns the reply object — or null for notifications (messages without an id), which MUST NOT be answered. Wire it to any transport: stdio (see serveMcpStdio), an HTTP POST body, a WebSocket frame, or a test harness.

      Protocol methods handled:

      • initialize — echoes the client's protocolVersion (or advertises '2025-06-18'), declares capabilities: { tools: {} }
      • notifications/initialized — notification, no reply
      • ping — replies {}
      • tools/list — whitelisted schema actions as McpTools
      • tools/call — dispatches { target, payload } from params.arguments through the bus; the CommandResult is serialized as a text content block (result.value as JSON on success; error.message with isError: true on failure — tool errors are results, not JSON-RPC errors)
      • anything else with an id — JSON-RPC error -32601 (method not found)

      Origin stamping: install agentOrigin on the bus (bus.use(agentOrigin(), { priority: 150 })) to stamp meta.origin='agent' on MCP-driven dispatches. See agentOrigin for the concurrency caveat on async buses.

      Parameters

      Returns (message: unknown) => Promise<object | null>

      const handle = createMcpHandler(bus, { actions: ['cartGet', 'cartAdd'] });
      const reply = await handle({ jsonrpc: '2.0', id: 1, method: 'tools/list' });
      // → { jsonrpc: '2.0', id: 1, result: { tools: [...] } }