Skip to Content
TutorialsEmbed an agent in a custom UI

Embed an agent in a custom UI

Sometimes the right place to talk to an agent isn’t the Harnesst chat app — it’s inside the tool where the work is already happening. A customer-support agent in your Zendesk console. A data assistant in a Google Sheet. A back-office helper in your admin dashboard.

This tutorial walks through the patterns for doing that. The specifics vary by host system, but the overall shape is the same every time.

The basic idea

Harnesst exposes one HTTP endpoint that starts a turn and streams the agent’s response as it is generated. Your custom UI:

  1. Captures what the user typed
  2. Sends it to your own backend, which forwards it to Harnesst with a personal access token
  3. Reads the streamed events as they arrive and renders them

The agent itself — its instructions, its tools, its MCP server bindings — lives in Harnesst. Your custom UI is just a thin chat surface on top.

Browser ──fetch──▶ Your backend (adds the PAT) ──▶ harnesst /api/backend/threads/{id}/runs/stream ◀──── SSE ────────────── piped through ◀──────────────── SSE ────

When this pattern makes sense

  • Customer support consoles — the agent drafts replies using the same data the support rep is looking at, without forcing them to switch tabs
  • Back-office dashboards — operations or finance team tools embed a “ask a question about this customer” panel
  • Google Sheets / Excel add-ons — users select a range, ask the agent a question about it, and the answer streams back into the sidebar
  • Internal wikis or docs sites — a floating “ask an expert” widget answers questions about a specific page’s topic
  • Product-specific help widgets — when the question is narrow and the agent’s knowledge is tailored, a widget beats a generic chatbot

What you’ll need

All paths below go through the Harnesst web app’s proxy, so they start with /api/backend. If you address the backend service directly, drop that prefix.

Why you need a proxy

Your browser code must not call Harnesst directly, for three reasons:

  • The token is a secret. A PAT acts as the user who owns it — their role, their agents, their connected MCP accounts. Anything shipped to a browser can be read from devtools.
  • Harnesst doesn’t know your users. Every request made with the PAT is that one Harnesst user. Your backend is the only place that knows which of your users is asking, so it is the one that must decide whether they may use a given thread.
  • Browsers would block it anyway. Harnesst only accepts cross-origin browser requests from its own web app.

So the proxy has exactly three jobs:

  1. Authenticate your user with whatever your app already uses (session cookie, JWT…)
  2. Check the thread belongs to them — store (your user id → thread id) when you create a thread, and refuse any thread id you didn’t hand to that user
  3. Forward the request with the PAT and pipe the response back untouched, without buffering it

The third point is where streaming usually breaks. The response is a long-lived text/event-stream: hand the body through as a stream, don’t await it as text or JSON, and make sure nothing in between (nginx, a CDN, a serverless platform) buffers or compresses it. If tokens arrive all at once at the end, something is buffering.

Here is a complete proxy as a Next.js route handler. The same thirty lines translate directly to Express, FastAPI, Rails, or anything else:

// app/api/chat/[threadId]/route.ts const HARNESST = process.env.HARNESST_URL; // https://your-harnesst-host/api/backend const PAT = process.env.HARNESST_PAT; // server-side only export async function POST( request: Request, { params }: { params: Promise<{ threadId: string }> }, ) { const { threadId } = await params; // 1 + 2: your auth, your ownership check. const user = await getSessionUser(request); if (!user) return new Response(null, { status: 401 }); if (!(await userOwnsThread(user.id, threadId))) { return new Response(null, { status: 403 }); } // 3: forward with the PAT. Only pass through the fields you intend to // expose — here, the user's text and nothing else. const { text } = await request.json(); const upstream = await fetch(`${HARNESST}/threads/${threadId}/runs/stream`, { method: "POST", headers: { Authorization: `Bearer ${PAT}`, "Content-Type": "application/json", }, body: JSON.stringify({ input: { messages: [{ type: "human", content: text }] }, }), // If the browser goes away, stop reading from harnesst too. signal: request.signal, }); // Refusals (401/403/404/409…) arrive as ordinary JSON responses with their // status; a started run arrives as an event stream. Either way: same // status, same body, piped through as a stream. return new Response(upstream.body, { status: upstream.status, headers: { "Content-Type": upstream.headers.get("Content-Type") ?? "application/json", "Cache-Control": "no-cache, no-transform", "X-Accel-Buffering": "no", "X-Run-Id": upstream.headers.get("X-Run-Id") ?? "", }, }); }

Creating the thread goes through your backend the same way — that’s where you record who owns it:

// app/api/chat/route.ts export async function POST(request: Request) { const user = await getSessionUser(request); if (!user) return new Response(null, { status: 401 }); const response = await fetch(`${HARNESST}/threads`, { method: "POST", headers: { Authorization: `Bearer ${PAT}`, "Content-Type": "application/json", }, body: JSON.stringify({ agent_id: process.env.HARNESST_AGENT_ID, model_id: "claude-haiku-4-5", }), }); // Refused (unknown agent, unavailable model…): pass the error on rather // than recording a thread that doesn't exist. if (!response.ok) { return new Response(await response.text(), { status: response.status, headers: { "Content-Type": "application/json" }, }); } const thread = await response.json(); await rememberThreadOwner(user.id, thread.id); return Response.json({ threadId: thread.id }); }

The streaming endpoint

POST /api/backend/threads/{thread_id}/runs/stream

It takes the same body as runs/invoke, starts the same kind of run, and answers with Server-Sent Events until the run ends, then closes the connection. One request per turn; context lives in the thread, so a follow-up message is simply another call on the same thread id.

curl -N -X POST https://your-harnesst-host/api/backend/threads/0b1c6f9e-…/runs/stream \ -H "Authorization: Bearer YOUR_PAT" \ -H "Content-Type: application/json" \ -d '{ "input": { "messages": [{ "type": "human", "content": "What did this customer last ask us about?" }] } }'
Body fieldDefaultMeaning
input—{ "messages": [{ "type": "human", "content": "…" }] } — starts a turn
command—{ "resume": … } — answers an approval request instead of starting a turn (see Approvals)
output_schema—A JSON Schema; the validated object comes back in the final frame’s structured_response
multitask_strategy"reject"What to do if the thread is still running: reject (400), enqueue (wait for it), or interrupt (stop it and start this one)
channels["messages", "tools", "lifecycle", "input"]Which kinds of events to send. An unknown name is a 422. Approval requests (input) are sent whatever you list here
subagentsfalseAlso send events from inside subagents. Their approval requests are sent either way

The response carries the run’s id in the X-Run-Id header.

What comes back

Each frame is one data: line holding a JSON object. Frames with "type": "event" are Agent Streaming Protocol  events — the same ones the Harnesst chat app renders from:

data: {"type":"event","method":"lifecycle","params":{"namespace":[],"data":{"event":"running"}}} data: {"type":"event","method":"messages","params":{"namespace":[],"data":{"event":"message-start","role":"ai","id":"lc_run--01a1…"}}} data: {"type":"event","method":"messages","params":{"namespace":[],"data":{"event":"content-block-delta","index":0,"delta":{"type":"text-delta","text":"Hello"}}}} data: {"type":"event","method":"messages","params":{"namespace":[],"data":{"event":"message-finish"}}} data: {"type":"event","method":"lifecycle","params":{"namespace":[],"data":{"event":"completed"}}} data: {"type":"result","run_id":"793da115-…","status":"success","content":"Hello! How are you today?","structured_response":null,"error":null}

(Frames also carry event_id, seq and a timestamp, left out here. Lines starting with : are keep-alive comments — ignore them.)

methoddata.eventWhat to do with it
messagesmessage-startA message begins. Check role: render ai; skip the others (see the note below)
messagescontent-block-deltaWith delta.type == "text-delta", append delta.text to the current message. Other delta types are reasoning or tool-call arguments
messagesmessage-finishThe message is complete
toolstool-started / tool-finished / tool-errorA tool call (tool_name, input, output) — show a “Searching HubSpot…” indicator
lifecyclestarted / running / completed / failed / interruptedThe run’s progress
input.requested—The agent is waiting for an approval (see Approvals)

Two things to know when rendering:

  • Human messages are echoed. The stream includes message-start events with "role": "human" — the user’s message, and on later turns earlier ones too. You already rendered what the user typed, so only render messages whose message-start said "role": "ai". The deltas that follow a message-start belong to that message until its message-finish.
  • An agent turn can contain several AI messages — one before a tool call (“Let me check…”), one after. Start a new bubble, or keep appending, as you prefer.

The final frame

The last frame is always "type": "result". It is the one place to look to know how the run ended:

statusMeaningcontent
successThe agent answeredThe full final answer (and structured_response, if you sent output_schema)
interruptedWaiting for an approvalnull
cancelledStopped — by a cancel call, or by a newer message sent with multitask_strategy: "interrupt"null
error / timeoutThe run failed; error holds the reasonnull

Because the HTTP status is sent before the run starts, a run that fails midway is still a 200 — the failure is in this frame, not in the status code. Problems detected before the run starts are ordinary error responses with a JSON body:

StatusBodyMeaning
400{"detail": "This thread already has an active run."}The previous turn is still running — wait, or send multitask_strategy
401{"error": "oauth_required", "auth_url": "…"}An OAuth MCP server bound to the agent isn’t connected for the token’s user. Open auth_url once, signed in to Harnesst as that user
403 / 404{"detail": "…"}The thread belongs to another Harnesst user, or doesn’t exist
409{"error": "model_unavailable", …} / {"error": "sandbox_unavailable", …}The thread’s model or the agent’s sandbox can’t be used right now

Reading the stream in the browser

EventSource can’t send a POST, so read the response body with fetch. This is the whole client — it talks to the proxy above, never to Harnesst:

type Handlers = { onText: (text: string) => void; // append to the current answer onTool?: (name: string, running: boolean) => void; onApproval?: (interruptId: string, payload: unknown) => void; }; export async function sendMessage( threadId: string, text: string, handlers: Handlers, signal?: AbortSignal, ) { const response = await fetch(`/api/chat/${threadId}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text }), signal, }); // Refused before the run started: a normal JSON error. if (!response.ok || !response.body) { throw new Error((await response.json().catch(() => null))?.detail ?? "Request failed"); } const reader = response.body.pipeThrough(new TextDecoderStream()).getReader(); let buffer = ""; let fromAgent = false; // is the message being streamed the agent's? let result = null; for (;;) { const { value, done } = await reader.read(); if (done) break; buffer += value; // Frames are separated by a blank line. let end: number; while ((end = buffer.indexOf("\n\n")) !== -1) { const frame = buffer.slice(0, end); buffer = buffer.slice(end + 2); const line = frame.split("\n").find((l) => l.startsWith("data: ")); if (!line) continue; // keep-alive comment const message = JSON.parse(line.slice(6)); if (message.type === "result") { result = message; continue; } const data = message.params.data; if (message.method === "messages") { if (data.event === "message-start") fromAgent = data.role === "ai"; if ( fromAgent && data.event === "content-block-delta" && data.delta?.type === "text-delta" ) { handlers.onText(data.delta.text); } } else if (message.method === "tools") { // tool-started carries tool_name; the others end the indicator. handlers.onTool?.(data.tool_name ?? "", data.event === "tool-started"); } else if (message.method === "input.requested") { handlers.onApproval?.(data.interrupt_id, data.payload); } } } // { status, content, structured_response, error } — null only if the // connection dropped before the run finished. return result; }

When it returns, branch on result.status: success (you can replace the streamed text with result.content, they are the same answer), interrupted (show the approval UI), error / timeout (show result.error), cancelled. A null result means the connection dropped mid-run — see the next section.

Reloads, dropped connections, and Stop

A run belongs to Harnesst, not to the HTTP request that started it. Closing the connection does not stop the agent — it keeps working and saves its answer to the thread. That gives you three building blocks, each one more route on your proxy:

  • Show a conversation again (page reload, returning user, a stream that dropped): GET /api/backend/threads/{thread_id}/state returns the saved conversation as values.messages — a list of { "type": "human" | "ai" | "tool", "content": … }. An AI message’s content is either a string or a list of blocks; render the blocks whose type is "text". If next is non-empty the thread is still running or waiting for an approval.
  • A Stop button: POST /api/backend/threads/{thread_id}/runs/{run_id}/cancel, with the run id from the X-Run-Id response header. Aborting the browser’s fetch alone only stops watching.
  • The user sends a new message while the agent is still answering: either disable the input until the result frame arrives, or send the new message with "multitask_strategy": "interrupt" — the previous run is stopped (its stream ends with status: "cancelled") and the new one starts.

Approvals

If the agent calls a tool set to needs approval, the run pauses: the stream delivers an input.requested event describing the tool calls, then ends with status: "interrupted".

{ "type": "event", "method": "input.requested", "params": { "namespace": [], "data": { "interrupt_id": "5f0c…", "payload": { "action_requests": [ { "name": "send_email", "args": { "to": "jane@example.com", "subject": "…" } } ] } } } }

To continue, call runs/stream again on the same thread with a command instead of an input — one decision per action request, in the same order:

{ "command": { "resume": { "decisions": [{ "type": "approve" }] } } }

Use { "type": "reject", "message": "Not this recipient" } to refuse; the agent is told why and carries on. The resumed run streams exactly like a normal turn.

You have three ways to deal with this, in increasing order of effort:

  • Only expose agents whose tools are on always allow — no approvals possible, simplest UX. Review the agent’s tool settings.
  • Route approvals back to the Harnesst web app — the thread shows up there with the pending approval; your UI just says “waiting for approval”.
  • Build approval UI into your custom app — render the action_requests, send the decision through your proxy.

A typical integration plan

  1. Add a chat widget to your host UI (textarea + message list + submit button)
  2. Store the PAT on your backend, never in the browser
  3. Add the proxy routes: create a thread, send a message (streaming), and — when you need them — load the state, cancel a run
  4. Record which of your users owns which thread, and check it on every route
  5. Render the stream: AI text deltas, optionally tool indicators, then the result frame
  6. Decide how to handle approvals (see above) before you pick the agent

Using the LangChain SDK instead

Harnesst also serves the full Agent Streaming Protocol session the @langchain/react / @langchain/langgraph-sdk clients consume — POST /threads/{thread_id}/commands to start and resume runs, and POST /threads/{thread_id}/stream/events, a long-lived SSE session that follows every run of the thread and re-attaches to a run in progress. It is what the Harnesst chat app itself uses. Reach for it if you are building a full chat client in React and want useStream to manage state, optimistic updates and reconnection for you; point its apiUrl at your proxy, which then has to forward those paths (plus /state and /history). For everything else, runs/stream is the simpler contract.

For one-shot, non-streaming calls (useful for slower-moving surfaces like spreadsheet cells), use POST /api/backend/threads/{thread_id}/runs/invoke, which blocks and returns the final answer — see Trigger agents over HTTP.

Example setups

Rather than prescribe code, here’s the shape of a few concrete integrations teams have built:

  • Google Sheets add-on — an Apps Script sidebar with a text input. User selects a range, types a question, the add-on serializes the range as CSV, calls Harnesst via Apps Script’s URL fetch, streams the answer into the sidebar. Good for ad-hoc analysis.
  • Zendesk side panel — a Zendesk app that, for the currently open ticket, posts the ticket context + the rep’s question to the Harnesst agent and displays the streamed response. The agent has HubSpot and product-docs MCP servers so it can speak to the customer in context.
  • Internal React dashboard — a floating chat widget that talks to a “data assistant” agent with BigQuery access. The widget remembers the thread ID in localStorage so the user can keep a single conversation across sessions.

When you build one of these, consider sharing it in Case Studies — it helps other teams figure out the right shape for their own integration.

Last updated on