Using WebSocket Tools
This guide shows how to extend a basic WebSocket conversation to work with WebSocket tools.
While this guide runs through creating and using a sync WebSocket tool, the core workflow remains the same with async WebSocket tools.
Prerequisites
Before adding tools to your WebSocket conversation, you should have:
- A working WebSocket server that connects to Phonic (see Via WebSockets guide)
Adding Tool Support
To handle tool calls in your existing WebSocket server, you need to:
- Listen for
tool_callmessages from Phonic - Process the tool call with your business logic
- Send back a
tool_call_outputmessage with the result
Step 1: Choose How to Provide Tools
You can provide WebSocket tools in two ways:
- Pre-defined WebSocket tools: create the tool once with the Tools API or Phonic UI, then reference it by name from an agent or conversation config. Use pre-defined WebSocket tools to avoid having to define the same tool across agents or conversations.
- Inline WebSocket tools: define the tool inline in the WebSocket
configmessage. Inline WebSocket tools are not persisted to your workspace. Use inline WebSocket tools when the parameters for the tool may change between conversations.
Option A: Create a Pre-defined WebSocket Tool
Create pre-defined WebSocket tools when the same tool should be reused across agents or conversations:
Step 2: Make the Tool Available
Pre-defined Tools
Add pre-defined tools to your agent’s configuration by name:
Inline WebSocket Tools
For inline WebSocket tools, pass the tool definition inline when you start the WebSocket conversation. The tool_schema follows OpenAI’s function tool shape.
Inline tools currently support type: "custom_websocket" only.
Step 3: Handle Tool Calls in Your WebSocket Server
Modify your existing WebSocket message handler to process tool_call messages: