showcase/shell-docs/src/content/ag-ui/sdk/dart/client/overview.mdx
The Agent User Interaction Protocol Client SDK provides agent connectivity options for Dart applications. This package delivers flexible connection methods to AG-UI compatible agent implementations with full type safety and reactive programming support.
dart pub add ag_ui
AgUiClient is the main class for connecting to AG-UI compatible servers. It handles HTTP communication, SSE streaming, and binary protocol encoding/decoding.
Configure the client with server details and optional parameters:
final client = AgUiClient(
config: AgUiClientConfig(
baseUrl: 'http://localhost:8000',
headers: {'Authorization': 'Bearer token'},
timeout: Duration(seconds: 30),
retryConfig: RetryConfig(
maxRetries: 3,
baseDelay: Duration(seconds: 1),
),
),
);
Executes an agent with the provided input and streams events:
Stream<BaseEvent> runAgent(
String agentId,
RunAgentInput input, {
Map<String, String>? headers,
})
Executes an agent and returns raw SSE messages without decoding:
Stream<SseMessage> runAgentRaw(
String agentId,
RunAgentInput input, {
Map<String, String>? headers,
})
Cancels an active agent execution:
Future<void> cancelAgent(String agentId)
final input = SimpleRunAgentInput(
messages: [
UserMessage(id: 'msg_1', content: 'What is the weather today?'),
],
);
await for (final event in client.runAgent('weather-agent', input)) {
print('Received: ${event.type}');
}
try {
await for (final event in client.runAgent('agent', input)) {
// Process events
}
} on AgUiClientError catch (e) {
print('Client error: ${e.message}');
} on NetworkError catch (e) {
print('Network error: ${e.message}');
}
// Start agent
final stream = client.runAgent('long-running-agent', input);
// Listen with cancellation option
final subscription = stream.listen((event) {
// Process events
});
// Cancel when needed
await client.cancelAgent('long-running-agent');
await subscription.cancel();
<Card title="AgUiClient Reference" icon="plug" href="/sdk/dart/client/client" color="#3B82F6" iconType="solid"
Complete API reference for the AgUiClient class </Card>
Configuration object for client initialization:
baseUrl (String): Base URL of the AG-UI serverheaders (Map<String, String>?): Default headers for all requeststimeout (Duration?): Request timeout durationretryConfig (RetryConfig?): Retry configuration for failed requestsvalidateResponses (bool): Enable response validation (default: true)Controls retry behavior for failed requests:
class RetryConfig {
final int maxRetries;
final Duration baseDelay;
final Duration maxDelay;
final double backoffMultiplier;
const RetryConfig({
this.maxRetries = 3,
this.baseDelay = const Duration(seconds: 1),
this.maxDelay = const Duration(seconds: 30),
this.backoffMultiplier = 2.0,
});
}
<Card title="Configuration Reference" icon="gear" href="/sdk/dart/client/config" color="#3B82F6" iconType="solid"
Detailed configuration options and examples </Card>
The client provides comprehensive error handling with specific exception types:
AgUiClientError: General client errorsNetworkError: Network connectivity issuesValidationError: Input validation failuresServerError: Server-side errors (5xx status codes)TimeoutError: Request timeout errorsThe client automatically retries failed requests based on the retry configuration:
final config = AgUiClientConfig(
baseUrl: 'http://localhost:8000',
retryConfig: RetryConfig(
maxRetries: 5,
baseDelay: Duration(milliseconds: 500),
backoffMultiplier: 1.5,
),
);
The underlying SSE client handles Server-Sent Events with:
<Card title="SSE Client Reference" icon="stream" href="/sdk/dart/client/sse" color="#3B82F6" iconType="solid"
Server-Sent Events implementation details </Card>
Resource Management: Always dispose of clients when done:
client.dispose();
Error Handling: Implement comprehensive error handling:
stream.handleError((error) {
// Log and recover
});
Cancellation: Cancel long-running operations when appropriate:
await client.cancelAgent(agentId);
Headers: Use headers for authentication and tracking:
client.runAgent('agent', input, headers: {
'X-Request-ID': 'unique-id',
});