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 an implicit 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 implicit 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

Implicit tool timeouts and slow work

An implicit tool call times out after 10 seconds. When it does, the agent receives "Failed to fetch information" and your client receives a function_call_timeout event:

{
  "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 in time, your client delivers the outcome later over 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 send_message and 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.

HTTP endpoints

To use an HTTP endpoint, set the tool's url to an https:// address when you create the tool:

{
  "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.

Receiving an HTTP tool call

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"
    }
  }
}

Sending the HTTP tool output

Return the result as the JSON response body. The agent receives the whole body as the tool output:

{
  "status": "shipped",
  "tracking_number": "1Z999AA10123456784"
}

A non-2xx status or a body that isn't JSON gives the agent "Failed to fetch information" instead.

HTTP tool timeouts and slow work

Your endpoint has 5 seconds to respond. A slower response gives the agent "Failed to fetch information". The platform cancels the request, and no function_call_timeout is sent.

An HTTP tool has no connection to deliver a later result over, so the work must fit inside the 5 seconds. If it can't, use a WebSocket endpoint instead.

WebSocket endpoints

To use a WebSocket endpoint, set the tool's url to a wss:// address when you create the tool:

{
  "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 and their results travel over that connection. The session's metadata arrives once, in the initialize message when the connection opens, not with each call.

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.
  • Hear what happens in the session — follow the conversation live, and receive data your client sends straight to your server.
  • Act on the session — push UI state to the client and inject content into the conversation (including slow results after the timeout).

WebSocket Tools covers everything the connection can do. The rest of this section covers tool calls.

Receiving a WebSocket tool call

Each call arrives on the connection as a function_call message:

{
  "session_id": "ws_abc123",
  "type": "function_call",
  "data": {
    "item_id": "item_001",
    "call_id": "call_abc123",
    "name": "get_order_status",
    "arguments": {
      "order_id": "ORD-7890"
    }
  }
}

Sending the WebSocket tool output

Answer on the same connection with a function_response that carries the same call_id:

{
  "type": "function_response",
  "data": {
    "call_id": "call_abc123",
    "content": {
      "status": "shipped",
      "tracking_number": "1Z999AA10123456784"
    },
    "delay": false
  }
}

See Tool calls and results for every field in both messages.

WebSocket tool timeouts and slow work

Your server has 10 seconds to send a function_response. If it doesn't, the agent receives "Failed to fetch information" and your server receives a function_call_timeout on the tool socket. The client doesn't receive one.

For work that can't finish in time, answer the call with an interim function_response, then deliver the outcome later with context_update on the tool socket. This works whether the session channel is WebRTC or WebSocket.

Session metadata

Every tool call has access to 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 are null.

Where metadata arrives depends on the tool type:

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://napster-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