Build Your OmniagentTools

Executing Tools

Handle tool calls at runtime — implicit calls in your client, explicit calls on your server, and what to do about slow work

When the agent invokes a tool mid-conversation, the call is delivered to you and your code returns the result. Where it's delivered depends on the tool's execution flow: implicit calls go to your connected client, explicit calls go to a URL on your server.

Implicit tool calls

When an implicit tool is invoked during a session, the Napster API sends an event to your connected client with the tool call details. Your client handles the logic and sends the result back. This works over both WebRTC (via the Web SDK) and WebSocket connections.

Receiving a tool call

When the agent invokes an implicit tool, the server delivers a function_implicitly_called event to your client:

{
  "event": "function_implicitly_called",
  "data": {
    "item_id": "item_001",
    "call_id": "call_abc123",
    "name": "get_order_status",
    "arguments": {
      "order_id": "ORD-7890"
    },
    "metadata": {
      "tags": {
        "plan": "premium"
      },
      "external_client_id": "user_12345",
      "external_client_profile": {
        "name": "Jane"
      }
    }
  }
}
FieldDescription
item_idThe ID of the conversation item that triggered the tool call
call_idA unique identifier for this specific tool call — use this when sending the response
nameThe name of the tool being invoked
argumentsThe parameters the agent populated for the tool call
metadataThe session's context — see Session metadata

Sending the tool output

Once you've processed the tool call, send the result back with a send_function_output event:

{
  "type": "send_function_output",
  "data": {
    "call_id": "call_abc123",
    "output": {
      "status": "shipped",
      "tracking_number": "1Z999AA10123456784"
    },
    "delay": false
  }
}
FieldDescription
call_idThe call_id from the function_implicitly_called event
outputThe result of the tool call — this is passed back to the agent
delayWhen true, the response is held until the agent finishes speaking before being processed

Timeout

Tool calls have a default timeout of 10 seconds — explicit tools with an HTTP URL have a shorter 5-second limit on each request. If no response is received within that window, the Napster API sends "Failed to fetch information" to the model as the tool result and delivers a function_call_timeout event to your client.

{
  "event": "function_call_timeout",
  "data": {
    "call_id": "call_abc123",
    "function_name": "get_order_status"
  }
}

Match call_id to the pending call so your client can stop waiting on it. A result sent for that call after the timeout doesn't reach the agent.

For work that can't finish inside the window, respond with an interim result right away (so the agent can tell the user the result is on its way) and deliver the real outcome afterwards. The rule: the late outcome is delivered by whoever handled the tool call, over the connection it already holds. How that looks depends on the kind of tool — not on the session channel:

ToolWho handled the callDelivering the late outcome
ImplicitYour clientThe client injects it with a send_message command — see the example below.
Explicit, WebSocket URLYour functions serverThe server sends context_update on the tool connection. Works whether the session channel is WebRTC or WebSocket.
Explicit, HTTP URLYour server, per-requestNo persistent connection to push through — the work must fit inside the 5-second HTTP timeout. If it can't, switch the tool to a WebSocket URL.

Deferred results from an implicit tool

Your client handled the call, so it also delivers the outcome — using the session connection it already holds. First answer the tool call with an interim result so the agent can say the work has started; later, inject the real outcome as a system message with trigger_response: true so the agent announces it.

On WebRTC, both go through the Web SDK instance:

// 1. Beat the timeout — answer the tool call with an interim result
instance.sendCommand({
  type: "send_function_output",
  data: {
    call_id,
    output: { status: "working", message: "Generating the report now — it'll be ready shortly." },
  },
});

// 2. Later, when the real work finishes — inject the outcome
instance.sendCommand({
  type: "send_message",
  data: {
    role: "system",
    text: "The report is ready: 1,204 rows. Tell the user and offer to walk through it.",
    trigger_response: true,
  },
});

On a WebSocket session channel, the messages are identical — your client just writes them to the socket:

// 1. Interim result
ws.send(JSON.stringify({
  type: "send_function_output",
  data: { call_id, output: { status: "working", message: "Generating the report now." } },
}));

// 2. Later — the real outcome
ws.send(JSON.stringify({
  type: "send_message",
  data: { role: "system", text: "The report is ready: 1,204 rows.", trigger_response: true },
}));

Explicit tool calls

When a tool's flow is "explicit", the tool call is forwarded to the url you configured when creating it — either an HTTP endpoint or a WebSocket endpoint on your server, with any custom headers you set.

The flow is the same regardless of protocol:

  1. The LLM decides to call the tool based on the conversation and populates the parameters.
  2. The Napster API delivers the tool call to the url you configured, including the tool name and populated arguments.
  3. Your server executes the logic — for example, querying a database, calling an internal API, or triggering an action in a third-party system — and returns the result.
  4. The Napster API passes the result back to the LLM, which uses it to continue the conversation with the user.

Using an HTTP endpoint:

{
  "flow": "explicit",
  "url": "https://your-api.example.com/tools/get-order-status",
  "headers": {
    "x-api-key": "your-internal-api-key"
  }
}

With an HTTP URL, the platform makes a request to your endpoint per tool call. Simple to set up — but there's no connection outside of tool calls.

Each call is a POST with a JSON body. It carries the same fields as the implicit tool call event, including the session's metadata:

{
  "item_id": "item_001",
  "call_id": "call_abc123",
  "name": "get_order_status",
  "arguments": {
    "order_id": "ORD-7890"
  },
  "metadata": {
    "tags": {
      "plan": "premium"
    },
    "external_client_id": "user_12345",
    "external_client_profile": {
      "name": "Jane"
    }
  }
}

Return the result as a JSON response body within 5 seconds. The agent receives it as the tool output. A non-2xx status, a body that isn't JSON, or a slower response gives the agent "Failed to fetch information" instead.

Using a WebSocket endpoint:

{
  "flow": "explicit",
  "url": "wss://your-api.example.com/tools/get-order-status",
  "headers": {
    "x-api-key": "your-internal-api-key"
  }
}

With a WebSocket URL, the platform connects to your server at session start and keeps the connection open for the entire session. Tool calls skip the connection overhead — and the open connection turns your tool into much more than a request handler. It lets your server:

  • Know who it's talking to before the first call — receive the session ID and your own user/tenant context the moment the session starts.
  • Follow the conversation live — stream the full user↔agent exchange in real time, so your tool answers with complete context.
  • Drive the user's screen — push UI state to the client (confirmation cards, progress, page updates) in sync with the conversation.
  • Deliver slow results after the timeout — return an interim answer now and inject the real outcome into the conversation whenever it's ready.

Session metadata

Every tool call carries a metadata object with the context of the session it belongs to. Your backend sets this context when it creates the session, so it's the right place for anything your tool needs to know about who it's working for — a user ID, a tenant, an account, or a plan.

metadata is different from arguments:

  • arguments are what the agent fills in from the conversation, such as an order number the user just said.
  • metadata is what your backend set when it created the session. It stays the same for the whole session, and nothing said in the conversation changes it.

Use arguments for what the user is asking for, and metadata for who is asking.

metadata fieldSet withUse it for
tagstagsKey–value labels for routing and scoping — for example the tenant, region, or environment the session belongs to.
external_client_idexternalClientIdYour own ID for the end user. Look up their account, and scope what the tool returns to that user.
external_client_profileexternalClientProfileDetails about the end user — such as name or plan — to tailor the tool's result.

The tool payload uses snake_case, while the connection request uses camelCase. Fields you didn't set can be null.

Tool typeWhere metadata arrives
ImplicitIn each function_implicitly_called event
Explicit, HTTP URLIn each request body
Explicit, WebSocket URLOnce, in the initialize message at session start

Example: scope an order lookup to the signed-in user

Your backend creates the session for the signed-in user and passes their ID, tenant, and plan:

curl -X POST https://companion-api.napster.com/public/agents/agent_abc123/connections \
  -H "X-Api-Key: $NAPSTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "channelType": "webrtc",
    "externalClientId": "user_12345",
    "externalClientProfile": {
      "name": "Jane",
      "plan": "premium"
    },
    "tags": {
      "tenant": "acme"
    }
  }'

Later, the user asks "Where's my order ORD-7890?" and the agent calls get_order_status. Your HTTP tool receives the order number from the conversation in arguments, and the user's identity from your backend in metadata:

{
  "item_id": "item_001",
  "call_id": "call_abc123",
  "name": "get_order_status",
  "arguments": {
    "order_id": "ORD-7890"
  },
  "metadata": {
    "tags": {
      "tenant": "acme"
    },
    "external_client_id": "user_12345",
    "external_client_profile": {
      "name": "Jane",
      "plan": "premium"
    }
  }
}

Your tool looks up ORD-7890 for user_12345 in the acme tenant only, so it never returns another customer's order.

Phone calls

On SIP and VoIP calls, you don't create the session yourself, so the platform sets external_client_id to the phone number on the other end of the call:

Callexternal_client_id
SIP, inboundThe caller, exactly as your SIP server sends it — the part of the caller's SIP URI before the @. A call from sip:[email protected] gives "111"; a call from sip:[email protected] gives "+15551234567".
SIP, outboundThe destination you passed to start the call
VoIPThe caller's number from Twilio, in E.164 format (for example "+15551234567")

SIP doesn't reformat the number. Depending on your SIP server, it can be an extension, a national number, or a number with a + prefix. Normalize it before you match it against your own records.

Next steps

On this page