AgentBlit
Chat

Custom Chat UI

Build a custom chat client against AgentBlit chat v2 (AI SDK useChat)

Build your own chat UI against AgentBlit chat v2. The server speaks the standard AI SDK UI message stream. Prefer useChat + DefaultChatTransport — do not invent a custom protocol.

Reference implementation

Clone and run the public examples first. Copy from them; do not re-derive the contract.

Repo: github.com/agentblit/agentblit-chat-examples

FolderUse when
simplest/Minimal text chat + stop + tool approvals
full/Threads, files, markdown, tool activity, elicitation, MCP Apps
git clone https://github.com/agentblit/agentblit-chat-examples.git
cd agentblit-chat-examples/simplest
cp .env.example .env.local
# set NEXT_PUBLIC_AGENTBLIT_AGENT_ID
# optional: NEXT_PUBLIC_AGENTBLIT_API_BASE=http://localhost:3000
pnpm install && pnpm dev

Start with simplest/. Pull pieces from full/ only when you need them.


Contract

Endpoints

Base: {API_BASE}/api/v2/agents/{agentId}

MethodPathPurpose
POST/chatSend / resume a turn (UI message stream)
GET/threadsList threads
GET/threads/{threadId}/messagesLoad UI messages for a thread
DELETE/threads/{threadId}Delete a thread
POST/files/uploadPresign uploads → then PUT to each uploadUrl
POST/threads/{threadId}/apps/bridgeMCP App iframe host (full/ only)

Chat request

POST {API_BASE}/api/v2/agents/{agentId}/chat
Content-Type: application/json

{
  "id": "<thread-id>",
  "messages": [ /* AI SDK UIMessage[] */ ]
}

Response: AI SDK UI message stream (same shape useChat expects).

Rules (do not break these)

  1. Server owns LLM history. Client sends id + messages for the UI / interrupt decisions. Do not maintain a parallel OpenAI-style history for the model.
  2. Reuse one threadId for the conversation (useChat({ id: threadId })). Generate with generateId() or use a URL segment.
  3. Approvals and elicitation resume the same assistant message. After the user decides, let useChat POST again with updated tool parts — do not start a new user turn.
  4. Enable auto-resume with:
sendAutomaticallyWhen: ({ messages }) =>
  lastAssistantMessageIsCompleteWithApprovalResponses({ messages }) ||
  lastAssistantMessageIsCompleteWithToolCalls({ messages }),
  1. Optional header X-Agentblit-User: <end-user-id> scopes threads per end user. CORS is open for custom UIs today.

Step 1 — Minimal chat (required)

Dependencies: ai, @ai-sdk/react.

const chatUrl = `${API_BASE}/api/v2/agents/${agentId}/chat`;
"use client";

import { useChat } from "@ai-sdk/react";
import {
  DefaultChatTransport,
  generateId,
  lastAssistantMessageIsCompleteWithApprovalResponses,
  lastAssistantMessageIsCompleteWithToolCalls,
} from "ai";
import { useMemo } from "react";

export function Chat({ chatUrl }: { chatUrl: string }) {
  const threadId = useMemo(() => generateId(), []);
  const transport = useMemo(
    () => new DefaultChatTransport({ api: chatUrl }),
    [chatUrl],
  );

  const { messages, sendMessage, status, stop, addToolApprovalResponse, error } =
    useChat({
      id: threadId,
      transport,
      sendAutomaticallyWhen: ({ messages }) =>
        lastAssistantMessageIsCompleteWithApprovalResponses({ messages }) ||
        lastAssistantMessageIsCompleteWithToolCalls({ messages }),
    });

  // Render messages[].parts (text + tool parts).
  // Send: await sendMessage({ text })
  // Stop: stop()
  // Approve: addToolApprovalResponse({ id: approvalId, approved: true|false })
}

Exact UI wiring: simplest/src/components/chat.tsx.

Tool approvals

When a tool part has state === "approval-requested" and approval.isAutomatic === false, show Allow / Deny:

addToolApprovalResponse({ id: part.approval.id, approved: true });
// or approved: false

sendAutomaticallyWhen continues the run after the response.


Step 2 — Threads (optional)

Use when the UI needs history across reloads.

CallClient helper in examples
GET .../threadslistThreads
GET .../threads/{id}/messagesloadThreadMessages → pass as useChat({ messages: initial })
DELETE .../threads/{id}deleteThread

Put threadId in the URL (see full/ /c/[threadId]). Remount the chat panel with key={threadId} when switching threads.

Source: full/src/lib/api.ts.


Step 3 — File attachments (optional)

  1. POST .../files/upload with { files: [{ filename, mediaType, size }] }.
  2. PUT each file to the returned uploadUrl.
  3. Send AI SDK file parts:
await sendMessage({
  text,
  files: [
    {
      type: "file",
      mediaType,
      filename,
      url, // download URL from the upload ticket
    },
  ],
});

Limits (server-enforced): up to 5 files, 10 MB each. Images, PDF, and text-like types are supported.

Source: full/src/lib/upload.ts.

Computer screenshots from tools arrive as normal assistant file parts when the tool sets share_with_user. Render part.type === "file" like any other attachment. Ignore markdown images that use a fake attachment: URL.


Step 4 — Elicitation (optional)

Some tools pause for structured user input. Detect elicitation on a tool part via toolMetadata.elicitation while state === "input-available". Submit with:

addToolOutput({
  tool: toolName,
  toolCallId,
  output: {
    action: "accept" | "decline" | "cancel",
    content?: Record<string, string>, // field values when accept
  },
});

Source: full/src/components/message.tsx + submitElicitation in chat-panel.tsx.


Step 5 — MCP Apps (optional)

  1. Listen for stream data parts of type data-mcp-app.
  2. Host the app iframe through:
POST {API_BASE}/api/v2/agents/{agentId}/threads/{threadId}/apps/bridge

Bridge methods used by the iframe: tools/call, resources/read, ui/update-model-context.

See the full/ README for the intended host shape. Skip this until you need interactive MCP App UIs.


Checklist for implementers / AI agents

Copy this order:

  1. Clone agentblit-chat-examples and run simplest/.
  2. Wire DefaultChatTransport → POST /api/v2/agents/{agentId}/chat.
  3. Set useChat id + sendAutomaticallyWhen as above.
  4. Render message.parts (text, tool parts, later file).
  5. Implement approval Allow / Deny via addToolApprovalResponse.
  6. Only then add threads, uploads, elicitation, or MCP Apps from full/.

Do not:

  • Call chat v1 (/api/agents/.../chat) for new UIs.
  • Send OpenAI messages / tool_calls payloads to v2.
  • Rebuild LLM memory on the client.
  • Skip sendAutomaticallyWhen if you support approvals or elicitation.

On this page