Tools
Define one canonical tool and use it for agents and MCP.
A tool is defined once, with the app's tool base. The definition carries the tool's policy (its
kind, confirmation and permission) and the base's rules, so every caller gets the same checks:
an agent in the chat, a run outside it, or an MCP app.
The tool base
toolBuilder() creates the function the app defines its tools with. The base holds the rules every
tool follows, whoever calls it:
// server/ai/tool.ts
import { toolBuilder } from "@tulip-systems/ai/tools/server";
import type { Permission } from "@tulip-systems/auth/permissions";
import type { AccessControl } from "@/server/auth/permissions";
export const tool = toolBuilder({
authorize: async (user, permission: Permission<AccessControl>) => {
const { success } = await auth.api.userHasPermission({
body: { userId: user.id, permissions: permission },
});
return success;
},
verifyProof: verifyApprovalPasskey,
context: ({ principal, trigger, run }) => ({
actor:
principal.type === "user"
? { id: principal.user.id, name: principal.user.name }
: { id: principal.agent, name: principal.title },
}),
});| Option | What it does |
|---|---|
authorize | Checks a tool's permission for a user, like the app's API does. The type of its permission parameter types every tool's permission. |
verifyProof | Checks the proof of an approval, such as a passkey assertion. Without it, proofs are refused. |
context | What execute gets as context, per principal, trigger and run. Its return type types it. |
Create one base per app. Because tools carry it, Agents, Chat and Mcp need no authorization
config of their own (see Services).
Define a tool
import { z } from "zod";
import { tool } from "@/server/ai/tool";
export const createProspect = tool({
name: "create_prospect",
title: "Prospect aanmaken",
description: "Create a prospect after duplicate checks.",
inputSchema: z.object({
prospect: z.object({
name: z.string().min(1),
email: z.string().email().optional(),
website: z.string().url().optional(),
}),
}),
outputSchema: z.object({
prospectId: z.string(),
}),
kind: "write",
confirmation: "approval",
permission: { prospect: ["create"] },
async execute(input, { context }) {
return createProspectFor(context.actor, input.prospect);
},
});Give the definition to defineAgent() or
Mcp.init() as it is; they resolve it. For a tool with a confirmation, the agent adds "The user approves every call."
to the description, so don't write that yourself.
The title is what users see: on the tool's card in the chat, on the Agents page and in MCP
clients. Write it in the app's language.
Every tool declares three things, none of them with a default, so a tool reads as it behaves:
kind: what the tool does with app data,"read","write"or"delete". MCP clients see reads as read-only and deletes as destructive.confirmation: how the user confirms a call before it runs."none"runs it right away,"approval"waits for Approve in the chat,"passkey"waits for an approval with a passkey (see Approvals). It does not follow fromkind: a write that sends an invoice can ask for a passkey, a delete of an empty draft can ask for a plain approval.permission: what the user must be allowed to do. It is required, so a tool cannot forget it; set it tofalseonly for tools that touch no app data.
Summary
A person reads a tool call before approving it, in the chat and in the approvals inbox. By default
they see the input with its field names in words and without ids. Give a tool a summary to
describe the call in their words, with names instead of ids:
export const createProject = tool({
name: "create_project",
// ...
summary: async (input, { context }) => {
const customer = await findCustomerFor(context.actor, input.customerId);
return [
{ label: "Customer", value: customer?.name ?? "Unknown customer" },
{ label: "Key", value: input.key },
{ label: "Title", value: input.title },
];
},
});summary gets the same context as execute, built by the tool base for the user or the agent the
call runs for, so it reads through the same services and access rules. It runs before the
permission check, so it only reads: a summary must not write or publish anything. A summary that
only takes input works too.
summary runs on the server. Fields without a value are not shown, and a summary that throws falls
back to the default. The chat gets it with the tool call and saves it with the message; an approval
of a run outside the chat stores it as summary. Render it anywhere with ToolFields from
@tulip-systems/ai/chat/client:
<ToolFields summary={approval.summary} input={approval.input} />The card of a tool call does not show the tool's raw output; the assistant tells what happened.
Capability catalog
Collect every tool that acts on app data in one object: the app's capability catalog. Agents and the MCP server resolve their tools from these definitions, so a capability has one kind, one confirmation and one permission, whoever calls it. Keep provider tools, such as a web search, out of it.
export const capabilities = {
search_prospects: searchProspects,
create_prospect: createProspect,
};
// defineAgent({ tools: { ...capabilities, web_search: webSearchTool } })
// Mcp.init({ tools: Object.values(capabilities) })Permissions
A tool only runs when its permission is authorized for the principal of the call: a user through
the tool base's authorize, an agent running as itself against its own identity.grants. The
check happens before every execution, for agents and for MCP requests. Every check fails closed: a
call that does not come through Agents, Chat or Mcp is refused, also for tools without a
permission, and a failing authorize refuses the call. The model receives the error and can
explain it to the user.
Errors reach the model or MCP client only as ORPCError messages, the same messages the app's API
shows its users. Any other error is logged on the server and replaced by a generic message, so a
database or provider error never leaks through a tool. Throw an ORPCError for errors the model
should see, such as "Project not found".
Permissions are typed by the tool base: type the permission parameter of authorize with the
app's access control, and an unknown resource or action on a tool fails to compile. The return type
of the base's context is what tools read from execute(input, { context }), without casts. Use
it to record who made a change: the user or the agent, via the run or the MCP app
(trigger.type === "mcp" carries the app's clientId and clientName).
Permission-gated tools must return their output instead of streaming it with an async iterable.
The same goes for tools checked by an asynchronous authorizeCapability (see
Capability checks).
Approvals
Tools with a confirmation other than "none" ask first by default: in the chat the user approves
or denies the call, outside the chat the run waits until an approver decided (see
Approvals). A denial goes back to the model, with the note, so it can
adjust. Over MCP, reads are marked read-only and deletes destructive, so the MCP client asks its
user before calling the others.
An admin can make an agent's tool stricter or looser per workspace, within the limits of its confirmation (see Agents).
Before the user is asked, the agent calls resolveApproval from the tool call for calls of
tools with a confirmation. It returns an AI SDK approval status: "approved" (with a reason the
chat shows) runs the call without asking, "denied" refuses it, undefined asks the user.
Agents sets resolveApproval from the agent's workspace settings and, in the chat, the chat's
approvedTools: an allowed tool runs, a denied one is refused, a tool approved for the whole chat
runs again, the rest asks.
"Approve once" needs nothing stored: it approves the exact tool call the chat saved.
Give tools that need more than a click confirmation: "passkey", such as deletes or sending an
invoice. The chat offers them no choices, asks approvalProof for the passkey first (see
Chat), and cannot approve them without it. The approval only
counts when the tool base's verifyProof accepts the proof, and is refused without
verifyProof (see Passkeys). Approvals with proof
hold for one call only, and MCP apps never run these tools.
Capability checks
authorizeCapability in the tool call says what this caller may do for the user, on top of what
the user may do. Every tool then also needs it to return true for the tool's name; a false
refuses the call like a missing permission.
Agents sets it from the agent's workspace settings, and Mcp from the user's settings for the app:
a tool that is not allowed is refused, an MCP app only runs what the user allowed, and an app the
user turned off runs nothing.
A returned string instead of false tells the model why the call was refused. The services'
refusals say who can change it ("An admin can allow it in the agent's settings."), so the model
passes that on instead of retrying.
It returns true synchronously for tools without a policy, such as a provider's web search, so
tools that stream keep streaming.
Client renderers
The chat shows every tool with a ToolCallCard: its title and an icon for its kind. The tool
sends both to the chat in its metadata ({ tulip: { kind, confirmation } }), so most tools need no
client code.
Write a ToolRenderer only for a custom card, such as a card for a provider tool without a Tulip
title. See Tool renderers.
"use client";
import { ToolCallCard, type ToolRenderer } from "@tulip-systems/ai/chat/client";
import { Search } from "lucide-react";
export const webSearchRenderer: ToolRenderer = {
toolName: "web_search",
render: (part) => (
<ToolCallCard part={part} title="Web search" icon={<Search className="size-3.5" />} />
),
};Guidance
- Keep
tool()reusable: policy (kind, confirmation, permission) belongs on it, transport details (MCP content) onMcp.init. - Create one tool base per app, so every tool follows the same rules.
- Collect app capabilities in one catalog and resolve agents and MCP from it.
- Keep server tools out of client renderers; a renderer only needs the tool's name.
- Use read tools before write tools for safer agent behavior.
- Give every tool that changes data a
confirmationof"approval"or"passkey". - Throw
ORPCErrorfor errors the model should read; everything else is hidden.