Harness

Vercel AI SDK

Guard AI SDK v7 tools with run.tools, route verification and confirmation through toolApproval, and hide tools outside the task.

TypeScript only. The adapter targets the Vercel AI SDK v7 and has no dependency on the ai package; it works on the shapes the SDK passes.

app/api/chat/route.tsts
import { generateText, isStepCount, tool, jsonSchema } from "ai";import { z } from "zod";import { createHarness, guards } from "@scute/harness";const harness = createHarness({  agent: "support-bot",  guards: [guards.approval(), guards.grounding(), guards.budget({ calls: 20 }), guards.permissions()],  tools: { refund_invoice: { tier: "high" } },});const refund_invoice = tool({  description: "Refund an invoice",  inputSchema: z.object({ invoice_id: z.string(), amount: z.number() }),  execute: async ({ invoice_id, amount }) => ({ refunded: invoice_id, amount }),});export async function POST(req: Request) {  const { chatId, userId, messages } = await req.json();  const run = harness.run({ id: chatId, actsFor: userId, task: { actions: ["invoice:read", "invoice:refund"] } });  const result = await generateText({    model,    messages,    tools: { ...run.tools({ refund_invoice }), ...run.humanTools(jsonSchema) },    toolApproval: run.toolApproval,    prepareStep: run.prepareStep,    stopWhen: [isStepCount(20), run.budgetExceeded],  });  return Response.json(result.response.messages);}

model is your provider's model. Reuse the chat id as the run id so a confirmation or verification from one request carries into the next.

run.tools(tools)#

Wraps each tool that has an execute. Tools without execute pass through unchanged.

  • Before execute, the call is checked with the SDK's toolCallId and messages, so grounding sees the conversation and approval sees tool approval responses.
  • When the call does not run, execute returns { error, decision, reason } to the model instead of calling your function. error is the verdict's message for the model.
  • When it runs, execute gets the arguments after any transform, and the result goes through the after-guards before the model reads it.
  • When toolApproval already checked the same call (same id, tool and arguments) and allowed it, execute reuses that verdict instead of checking again.

run.toolApproval#

Pass it as toolApproval. The harness checks the call and answers the SDK:

VerdictAnswer to the SDK
proceed, transformNo approval needed (or user-approval when the tool's own needsApproval says so)
approve by the personuser-approval, with the confirmation text as the reason
approve by a reviewerdenied, with the message saying the request is filed
verifyuser-approval with the verification message. With run.humanTools() offered: denied, so the model verifies the person itself with scute_verify_person
guide, redirect, denydenied, with the message for the model

A user-approval shows up as a tool approval request in your chat UI. When the person approves, the SDK sends a tool-approval-response, and the next check of that call sees approvedByUser and passes approval. The exported userApproved(messages, toolCallId) does that lookup if you need it yourself.

A tool's own needsApproval (deprecated in v7) is still honored. A call that waits on the person's approval is not counted toward budgets until it runs. The harness's human tools are never sent for approval.

run.prepareStep#

Pass it as prepareStep. It sets activeTools to the tools the task could ever use: tools whose permission is in the task's ceiling (from whoami), tools with no permission, and the human tools. The model never sees tools it can never use.

run.budgetExceeded#

Pass it in stopWhen. It returns true once a budget run limit (calls or usdPerRun) is used up.

Streaming tools#

The SDK streams a tool's output when execute is an async generator. The harness keeps it a generator:

  • Each yielded part is a whole preliminary output, so each goes through the after-guards as a result of its own (redacted where needed).
  • When an enforced after-guard withholds a part, the stream ends there with the withheld notice as the final output, and the tool's generator is closed.
  • An execute that returns a promise of an async iterable cannot stream: the model gets the last part, guarded like every part before it.

Human tools#

run.humanTools(jsonSchema) returns scute_verify_person, scute_submit_code, scute_check_verification, scute_approval_status and scute_whoami as AI SDK tools. Pass jsonSchema from ai. See human tools.