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 forAgentServer 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
HTTPTooManyRequests if max_agents limit is reached.
evict
list_agents
serve
serve_async
create_app
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
aiohttp.web.Application instance.
Example:
Lifecycle Hooks
Override these methods in a subclass for custom behavior:on_app_setup
on_agent_create
on_agent_evict
Endpoints
When the server is running, these endpoints are available:Real-time Endpoints
REST API Endpoints
These are enabled whenenable_rest_api=True (the default):
Request Formats
POST{base_path}/{id} (Generic)
Accepts a standard action format:
{base_path}/{id}/chat
Standard chat request:
?stream=true)
Send a request to {base_path}/{id}/chat?stream=true to get a Server-Sent Events (SSE) stream.
Request:
{base_path}/{id}/invoke
Agent.serve_many
Convenience method onAgent to start a multi-agent server: