Skip to content

Commit dafa5aa

Browse files
committed
Handle agent_tool_request event type in MessageHandler
When the ElevenLabs agent calls a server-side tool (webhook), the SDK receives an `agent_tool_request` event. Previously this hit the default branch, logging "Unknown event type: agent_tool_request". This adds proper handling mirroring the existing `agent_tool_response` pattern: - `AgentToolRequest` event model with tool_name, tool_call_id, tool_type, and event_id (matching the AsyncAPI schema in elevenlabs/packages) - `onAgentToolRequest` callback in `ConversationCallbacks` - Handler in `MessageHandler` to parse and dispatch the event This allows SDK consumers to react to pending tool calls, e.g. showing a loading indicator while a webhook executes.
1 parent d529c54 commit dafa5aa

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

lib/src/messaging/message_handler.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class MessageHandler {
9696
_handleMcpConnectionStatus(json);
9797
break;
9898

99+
case 'agent_tool_request':
100+
callbacks.onDebug?.call(json);
101+
_handleAgentToolRequest(json);
102+
break;
103+
99104
case 'agent_tool_response':
100105
callbacks.onDebug?.call(json);
101106
_handleAgentToolResponse(json);
@@ -251,6 +256,11 @@ class MessageHandler {
251256
callbacks.onMcpConnectionStatus?.call(status);
252257
}
253258

259+
void _handleAgentToolRequest(Map<String, dynamic> json) {
260+
final request = AgentToolRequest.fromJson(json);
261+
callbacks.onAgentToolRequest?.call(request);
262+
}
263+
254264
void _handleAgentToolResponse(Map<String, dynamic> json) {
255265
final response = AgentToolResponse.fromJson(json);
256266
callbacks.onAgentToolResponse?.call(response);

lib/src/models/callbacks.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class ConversationCallbacks {
5353
/// Called when MCP connection status changes
5454
final void Function(McpConnectionStatus status)? onMcpConnectionStatus;
5555

56+
/// Called when the agent is about to call a server-side tool (webhook, etc.)
57+
final void Function(AgentToolRequest request)? onAgentToolRequest;
58+
5659
/// Called when an agent tool response is received
5760
final void Function(AgentToolResponse response)? onAgentToolResponse;
5861

@@ -94,6 +97,7 @@ class ConversationCallbacks {
9497
this.onUnhandledClientToolCall,
9598
this.onMcpToolCall,
9699
this.onMcpConnectionStatus,
100+
this.onAgentToolRequest,
97101
this.onAgentToolResponse,
98102
this.onDebug,
99103
this.onEndCallRequested,

lib/src/models/events.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,39 @@ class McpIntegration {
220220
}
221221
}
222222

223+
/// Agent tool request - sent when the agent is about to call a server-side tool
224+
class AgentToolRequest {
225+
/// Tool name
226+
final String toolName;
227+
228+
/// Tool call identifier
229+
final String toolCallId;
230+
231+
/// Tool type
232+
final String toolType;
233+
234+
/// Event identifier
235+
final int eventId;
236+
237+
AgentToolRequest({
238+
required this.toolName,
239+
required this.toolCallId,
240+
required this.toolType,
241+
required this.eventId,
242+
});
243+
244+
factory AgentToolRequest.fromJson(Map<String, dynamic> json) {
245+
final agentToolRequest =
246+
json['agent_tool_request'] as Map<String, dynamic>;
247+
return AgentToolRequest(
248+
toolName: agentToolRequest['tool_name'] as String,
249+
toolCallId: agentToolRequest['tool_call_id'] as String,
250+
toolType: agentToolRequest['tool_type'] as String,
251+
eventId: agentToolRequest['event_id'] as int,
252+
);
253+
}
254+
}
255+
223256
/// Agent tool response
224257
class AgentToolResponse {
225258
/// Tool name

0 commit comments

Comments
 (0)