openhands.agent_server) provides an HTTP/WebSocket interface for running OpenHands conversations remotely. It is stateless with respect to agent logic; containerization is handled by workspace implementations (e.g., DockerWorkspace) that launch this server inside a sandbox when needed.
Source: openhands-agent-server/
Core Responsibilities
The server provides:- Conversation lifecycle API – Start, run, pause, delete conversations (conversation_router.py)
- Event access – Search/get conversation events; WebSocket streaming (event_router.py, sockets.py)
- Execution utilities – Bash commands, file upload/download, simple Git ops (bash_router.py, file_router.py, git_router.py)
- Tool registry – List registered tools (tool_router.py)
- Server details – Health, uptime/idle info (server_details_router.py)
Architecture
- App construction and route wiring happen in api.py: a single
/apirouter includes conversation, event, tool, bash, git, file, vscode, and desktop routers. - Event and VSCode/Desktop services are initialized in the lifespan context (api.py).
Endpoints (selected)
Representative API surface under/api:
-
Conversations (conversation_router.py)
POST /api/conversations– start conversation (StartConversationRequest)POST /api/conversations/{conversation_id}/run– run agent loopPOST /api/conversations/{conversation_id}/events– send Message (optionallyrun: true)
-
Events (event_router.py)
GET /api/conversations/{conversation_id}/events/search– list eventsGET /api/conversations/{conversation_id}/events/{event_id}– get event
-
Bash (bash_router.py)
POST /api/bash/start_bash_command– start background commandGET /api/bash/bash_events/search– list bash events
-
Files (file_router.py)
POST /api/file/upload/{path}– upload file (absolute path)GET /api/file/download/{path}– download file (absolute path)
-
Tools (tool_router.py)
GET /api/tools/– list registered tools
-
Server details (server_details_router.py)
GET /server_info– uptime and idle_time
Authentication
- HTTP requests: header
X-Session-API-Keyif configured (dependencies.py) - WebSocket: query parameter
session_api_key(dependencies.py, sockets.py) - Configuration: environment-driven
Config(config.py)
Client SDK
Python examples for interacting with Agent Server:To create a new conversation via REST, post a StartConversationRequest to
/api/conversations.
See the JSON example in
conversation_router.py
(START_CONVERSATION_EXAMPLES).WebSocket streaming example (events)
WebSockets require passing the session key as a query parameter
(
session_api_key). See
sockets.py.WebSocket streaming example (bash-events)
Bash WebSocket events include BashCommand and BashOutput items. For filtering or paging
via REST, see
bash_router.py.
Design Notes
- The server itself does not manage Docker containers. Containerization and lifecycle are handled by workspace implementations such as
DockerWorkspacein theopenhands-workspacepackage, which run this server inside the container and connect via HTTP (docker/workspace.py). - Request/response models are Pydantic classes in
models.py(models.py). - Security: schema-level API key checks, path validation for file ops (absolute path enforcement), and typed payloads (dependencies.py, file_router.py).
Component Relationships
Last updated: 2025-12-09 06:57 UTC
Source commit (software-agent-sdk):
93d405c9
