Skip to main content
The AgentServer class manages multiple agent instances on a single server, routing clients to the correct agent based on URL path using the unified transport layer. It supports both local agent classes and remote agent transports through the AgentRegistry.

Import

AgentServerConfig

Configuration dataclass for AgentServer lifecycle and security.

Parameters

float | None
default:"300.0"
Seconds before evicting idle agents (no connections). Set to None to disable auto-eviction.
int | None
default:"None"
Maximum concurrent agents. Returns 429 Too Many Requests if limit reached. None = unlimited.
Callable[[Request], Awaitable[bool]] | None
default:"None"
Async function to validate requests. Return True to allow, False to reject (401).
list[str] | None
default:"None"
CORS allowed origins. None = allow all (*).
str
default:"/agent"
Base path for agent routes.
bool
default:"True"
Enable REST endpoints (GET/PUT state, DELETE agent).
bool
default:"False"
Enable GET /agents endpoint. Off by default for security.

AgentServer

Parameters

Agent | AgentRegistry
required
Either a single Agent instance to use as a template for each client, or an AgentRegistry for multi-agent routing with different agent types and transports.
AgentServerConfig | None
Optional configuration. Uses defaults if not provided.

Methods

get_or_create

Get or lazily create an agent by ID. Raises: HTTPTooManyRequests if max_agents limit is reached.

evict

Evict an agent, closing all connections and removing it from the registry.

list_agents

Return list of active agent IDs.

serve

Start the multi-agent server (blocking).

serve_async

Start the multi-agent server (async).

create_app

Create and return the configured aiohttp Application with all agent routes registered. Use this method when you need full control over the app, such as:
  • Adding custom routes or middleware
  • Integrating with existing aiohttp applications
  • Running with custom server configurations
Returns: Configured aiohttp.web.Application instance. Example:

Lifecycle Hooks

Override these methods in a subclass for custom behavior:

on_app_setup

Called after the aiohttp app is created but before serving. Use this to add custom routes, middleware, or configuration. Example:

on_agent_create

Called when a new agent is created (on first connection).

on_agent_evict

Called when an agent is about to be evicted.

Endpoints

When the server is running, these endpoints are available:

Real-time Endpoints

REST API Endpoints

These are enabled when enable_rest_api=True (the default):

Request Formats

POST {base_path}/{id} (Generic) Accepts a standard action format:
POST {base_path}/{id}/chat Standard chat request:
Response:
Streaming Chat (?stream=true) Send a request to {base_path}/{id}/chat?stream=true to get a Server-Sent Events (SSE) stream. Request:
Response (Content-Type: text/event-stream):
POST {base_path}/{id}/invoke
Response:

Agent.serve_many

Convenience method on Agent to start a multi-agent server:
Equivalent to:

Example

Basic Usage

With Configuration

Custom Server with Hooks