Tools
Give the persona functions in your own code that it can decide to call during a conversation.
A tool is a function of yours that the persona can decide to call. When the user asks for something one of your tools does, the persona calls it, your code does the work, and the persona tells the user what came back.
What a tool can be
- Look something up: an order's status, a delivery date, today's opening hours.
- Do something: book an appointment, add an item to a cart, send a follow-up email.
- Change your page: show a product, turn to the next slide, update a score.
- Remember the user: save a note about them, or look one up from an earlier conversation. Bring your own memory shows a simpler way, with notes in the prompt.
- Bring in knowledge: search your own documents or knowledge base, search the web (Bring your own web search), or ask your own LLM (Can I bring my own LLM?).
Add tools to the conversation configuration
You add tools when your server configures the conversation, in the same POST /session-tokens request, under interface_setup.delegation. Set type to "responses", list your tools in responses.tools, and tell the persona how to use them in responses.instructions:
{
"type": "responses",
"responses": {
"instructions": "Use get_weather for any weather question, then answer in one short sentence.",
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "Looks up the current weather in a city.",
"parameters": { "type": "object", "properties": { "city": { "type": "string" } }, "required": ["city"] }
}
]
}
}Each tool has a name, a description that tells the persona when to use it, and parameters, a JSON Schema of the arguments it takes. The optional policy and annotations are covered below, and the API reference has every field.
type sets how the persona deals with what the user asks for:
"responses": the persona calls your tools itself. Use this to give it tools."client"(the default): the persona has none of your tools. When the user asks for something, it tells your page, and your page decides what to do (Let your page decide what to do)."none": no tools of yours at all (Tools off).
Errors with tools in the configuration
A problem with a tool doesn't fail the POST /session-tokens request. Nuance leaves that tool out of the conversation and lists it, with the reason, in the response's warnings. A tool is left out when it breaks a size limit, or when it comes after the first 16 valid tools (you can list up to 128). A tool that was left out is never called, so check warnings while you build.
You can't add tools after the conversation starts, only turn the ones you listed off and on again (Tools that come and go). Include every tool the conversation might need.
When the persona calls a tool
During the conversation, the persona decides on its own when to call one of your tools. Your code runs the tool and sends back the result.
Receive tool calls and send results
Whenever the persona decides to call one of your tools, the JavaScript SDK passes the tool call to your onToolCall handler. Run the tool and send the result back with sendToolResult. In a native app, you get the same tool calls as events instead (Native apps).
// In the page: run the backend's calls of your tools, change which tools it
// may call, and tell the persona what happens in the app.
import type {
Conversation,
NuanceLiveTool,
ToolCall,
ToolOutput,
} from "@nuancelabs/js-sdk";
const GET_WEATHER: NuanceLiveTool = {
type: "function",
name: "get_weather",
description: "Looks up the current weather in a city.",
parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
};
const BOOK_TABLE: NuanceLiveTool = {
type: "function",
name: "book_table",
description: "Books a table at the user's saved restaurant.",
parameters: {
type: "object",
properties: { time: { type: "string" } },
required: ["time"],
},
annotations: { destructiveHint: true },
};
/** Your app's own lookup and booking. */
declare function lookUpWeather(
city: string,
): Promise<{ sky: string; temperature_c: number }>;
declare function bookTable(time: string): Promise<{ booked_for: string }>;
/** One call's result, or an error the persona can tell the user. */
async function run(call: ToolCall): Promise<ToolOutput> {
const args = call.arguments ?? {};
try {
if (call.name === "get_weather" && typeof args.city === "string") {
return await lookUpWeather(args.city);
}
if (call.name === "book_table" && typeof args.time === "string") {
return await bookTable(args.time);
}
} catch {
return { error: { message: `${call.name} didn't answer` } };
}
return { error: { message: `Can't run ${call.name} with these arguments` } };
}
export function runTools(conversation: Conversation): void {
// Answer every call, with an error when it can't run: the backend goes on
// only once each call of its step has a result.
conversation.onToolCall(async (call) => {
await conversation.sendToolResult(call.callId, await run(call));
});
}
/** The user signed out: booking can't run until they're back. */
export async function onSignedOut(conversation: Conversation): Promise<void> {
await conversation.setTools([GET_WEATHER]);
await conversation.append("thinking", "The user just signed out.");
}
/** They signed in again: send every tool that can run, exactly as minted. */
export async function onSignedIn(conversation: Conversation): Promise<void> {
await conversation.setTools([GET_WEATHER, BOOK_TABLE]);
}
- Each tool call has the tool's
name, itsarguments(already parsed) and acallId. The persona chooses the arguments, so check them before you use them. - Answer every tool call; the persona continues once each one has a result. A result can be text, an object (sent as JSON), or text and images (Size limits).
- A slow tool is fine. The persona keeps talking with the user while it waits: if nobody has spoken for a few seconds, it gives a short update, and if it has just asked the user a question, it waits for their answer before giving the result.
When a tool call fails
When your tool couldn't do what was asked, say so in the result you send back. Send an error, such as {"error": {"message": "Card declined"}} or {"ok": false, "message": "Card declined"}. The persona may try again; if it gives up, it tells the user it couldn't do it and passes on your message. Anything else counts as a success, even {"status": "failed"}.
Results that need no reply
Some tools only change something on your page, such as updating a score, and the persona has nothing to say about them. For those, pass { reply: false } as the third argument of sendToolResult. The persona takes in the result but doesn't speak until the user does. The persona still tells the user when a tool call fails.
When Nuance can't finish the tool calls
Sometimes the problem is on Nuance's side rather than in your tool. The persona then tells the user it couldn't finish, and may try again. Errors lists the reasons; in a native app they arrive as response.failed (Native apps). Errors lists every code and whether to retry.
Control how the persona uses your tools
Change which tools are available during the conversation
When a tool can't work in your app's current state, such as booking while the user is signed out, take it away and give it back later. The persona is told about the change, so it knows what it can and can't do right now and can say so to the user. With the SDK, call conversation.setTools(tools), as the example above does; a native app sends session.update.
{ "type": "session.update", "session": { "delegation": { "type": "responses", "responses": { "tools": [{ "type": "function", "name": "get_weather", … }] } } } }You get session.updated back.
- Send the full list of tools that should be available now, each exactly as your server listed it in the token. You can only choose among the tools you listed, not add new ones.
- A tool call can still arrive for a tool you just took away. Answer it, for example with an error.
A refused update changes nothing (Errors).
Tool annotations
annotations describes how a tool behaves, using the tool hints from the Model Context Protocol (MCP), a common standard for describing tools to AI models. If you already describe your tools for MCP, you can copy their annotations over.
Nuance uses them to avoid doing an action twice. Mark a tool that sends a message, makes a booking, takes a payment or posts something publicly with {"destructiveHint": true}, and the persona calls it at most once per user request with the same arguments. A tool marked readOnlyHint or idempotentHint, or with no annotations, can be called again.
Tool policies: confirmation and safety resources
A tool's policy field takes two settings.
{"confirm": true}makes the persona check with the user and call the tool only after they clearly say yes. Use it for anything sensitive, the kind of action you'd want any assistant to check before doing: spending money, sending something on the user's behalf, changing their account, or anything that can't be undone.{"safety_resource": true}is for a tool that gives a crisis or safety resource, such as the 988 Suicide and Crisis Lifeline. The persona passes the answer on right away and reads out every number in it.
A phone number in a tool result ends the conversation for a policy violation, even if the persona never says it, and counts toward pauses after repeated violations. Only emergency numbers, 988 and the major national crisis lines (US, Canada, UK, Ireland, Australia, New Zealand) are allowed, even from a safety_resource tool. Keep other numbers out of results, including your own business's.
Without tools
Most apps will want type: "responses", described above. The other two settings give the persona none of your tools.
Let your page decide what to do
With {"type": "client"}, the default, you don't list any tools. Your prompt says in plain words what your app can do, and the persona flags when the user asks for one of those things. Your page then decides what to do and tells the persona how it went.
For example, a restaurant's prompt says "You can book tables." When the user says "Book me a table for four at 7", the persona says something like "Let me check" and your page gets a request:
{ "type": "session.delegation.created", "delegation": { "id": "del_1", "type": "delegation", "target": "client" } }The request includes only an id, not what the user asked for, so your page works that out from what the user said (Follow the conversation). It reports back with appends that include that id as delegation_id:
{ "type": "session.thinking.append", "delegation_id": "del_1", "content": "Checking tables for four at 7 pm." }
{ "type": "session.commentary.append", "delegation_id": "del_1", "content": "The table for four at 7 pm is booked." }session.thinking.appendreports progress. The persona may mention it, and the request stays open.session.commentary.appendis the result. The persona tells the user, and the request is done.
This suits simple apps. With type: "responses", the persona works out the details itself and gives your code a tool call with arguments, which is easier to handle reliably.
Tools off
With {"type": "none"}, the persona answers only from its prompt, what it knows and its built-in tools, and never asks your page for anything.