Skip to content

Commit 43600ac

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, call ID, type, and parameters - `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 43600ac

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,44 @@ 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+
/// Parameters passed to the tool
235+
final Map<String, dynamic> parameters;
236+
237+
/// Event identifier
238+
final int eventId;
239+
240+
AgentToolRequest({
241+
required this.toolName,
242+
required this.toolCallId,
243+
required this.toolType,
244+
required this.parameters,
245+
required this.eventId,
246+
});
247+
248+
factory AgentToolRequest.fromJson(Map<String, dynamic> json) {
249+
final agentToolRequest =
250+
json['agent_tool_call'] as Map<String, dynamic>;
251+
return AgentToolRequest(
252+
toolName: agentToolRequest['tool_name'] as String,
253+
toolCallId: agentToolRequest['tool_call_id'] as String,
254+
toolType: agentToolRequest['tool_type'] as String,
255+
parameters: agentToolRequest['parameters'] as Map<String, dynamic>? ?? {},
256+
eventId: agentToolRequest['event_id'] as int,
257+
);
258+
}
259+
}
260+
223261
/// Agent tool response
224262
class AgentToolResponse {
225263
/// Tool name

0 commit comments

Comments
 (0)