WebSockets¶
MCP Hangar provides a WebSocket endpoint for real-time streaming of domain events.
Endpoint¶
| Endpoint | Description |
|---|---|
/api/ws/events | All domain events (filterable via subscribe message) |
Connecting¶
Use any WebSocket client. Example with websocat:
Event Stream (/api/ws/events)¶
Streams all domain events as they occur. Each message is a JSON object:
{
"event_type": "McpServerStarted",
"mcp_server_id": "math",
"mode": "subprocess",
"tools_count": 5,
"startup_duration_ms": 120,
"timestamp": "2026-03-23T10:15:30.123456"
}
Event Types¶
All events from domain/events.py are published:
McpServerStateChanged-- State transition with old/new state.McpServerStarted-- MCP Server initialization complete.McpServerStopped-- MCP Server shut down.McpServerDegraded-- MCP Server marked as degraded.HealthCheckPassed/HealthCheckFailed-- Health check results.ToolInvocationCompleted/ToolInvocationFailed-- Tool call results.ProviderDiscovered/ProviderRegistered/ProviderDeregistered-- Discovery events.CircuitBreakerStateChanged-- Circuit breaker transitions.
Filtering¶
After connecting, send a JSON subscribe message to filter events:
The server acknowledges with:
You can update filters at any time by sending another subscribe message. Omit a key to not filter on that dimension (e.g., omit mcp_server_ids to receive events from all MCP servers).
Queue and Backpressure¶
Each WebSocket connection has an internal message queue (EventStreamQueue). If a client falls behind:
- Messages queue up to the buffer limit.
- Beyond the limit, oldest messages are dropped.
- The client receives a warning frame indicating dropped messages.
Authentication¶
When auth is enabled, pass credentials via the initial HTTP upgrade headers:
Connection Lifecycle¶
- Client sends WebSocket upgrade to
/api/ws/events. - Server accepts the connection.
- (Optional) Client sends a
subscribemessage to set filters. - Server streams matching events as JSON messages.
- Client can send updated
subscribemessages at any time. - On disconnect, the subscription is cleaned up automatically.