Backend mcp management
MCP Server Registration and Management¶
Problem¶
The MCP Gateway needs to discover and register backend MCP servers so that their tools can be aggregated and presented to clients as a unified MCP server. When an MCPServerRegistration custom resource is created or updated in Kubernetes, the MCP Controller must:
- Discover the MCP server endpoint from the referenced HTTPRoute
- Update the broker and router configuration
- Initialize connections to the backend MCP server
- Discover available tools
- Register for state change notifications
- Handle configuration changes
- Periodically ensure that the backend MCP Server is alive and healthy
The broker needs a robust mechanism to manage the lifecycle of each upstream MCP server connection, including periodic health checks, reconnection logic, and graceful handling of configuration changes.
Solution¶
The MCP Gateway uses a two-phase registration process:
- Controller Phase: The MCP Controller watches for MCPServerRegistration resources, discovers server endpoints from HTTPRoutes, and writes aggregated configuration to ConfigMaps
- Broker Phase: The MCP Broker reads configuration changes and manages each upstream MCP server through an
MCPManagerstruct that handles the full lifecycle of the connection
Each upstream MCP server is managed by a dedicated MCPManager instance that runs as a background routine, handling:
- Initial connection and discovery
- Periodic health checks and reconnection
- State change notification subscriptions such as
notifications/tools/list_changed - Graceful shutdown
Registration Flow¶
sequenceDiagram
participant Controller as MCP Controller
participant ConfigMap as ConfigMap
participant Broker as MCP Broker
participant Manager as MCPManager
participant Server as MCP Server
Note over Controller: Watch MCPServerRegistration resources
Controller->>ConfigMap: Write aggregated config
Note right of ConfigMap: Config includes:<br/>- Server URL<br/>- Hostname<br/>- Tool Prefix<br/>- Credentials
ConfigMap->>Broker: Config change notification
Broker->>Broker: OnConfigChange()
alt Server not registered
Broker->>Broker: mcpManager := NewMCPManager(config)
Broker->>Manager: Start() [background routine]
Broker->>Broker: upstreamMCPS[id] = mcpManager
else Server already registered
Broker->>Broker: Check if config changed
alt Config changed
Broker->>Broker: upstreamMCPS[id].Stop()
Broker->>Broker: mcpManager := NewMCPManager(config)
Broker->>Manager: Start() [background routine]
Broker->>Broker: upstreamMCPS[id] = mcpManager
end
end
Note over Manager,Server: MCPManager lifecycle
Manager->>Server: POST /mcp "initialize"
Server->>Manager: initialize response
Note right of Manager: Validate protocol version<br/>and capabilities
Manager->>Server: POST /mcp "notifications/initialized"
Note right of Manager: Subscribe to state change<br/>notifications via SSE
Manager->>Server: GET /mcp [SSE connection]
Manager->>Server: POST /mcp "tools/list"
Server->>Manager: tools/list response
Manager->>Broker: Register discovered tools
Manager->>Manager: Periodic health checks
Manager->>Server: [monitor for notifications]
Controller->>Broker: Fetch status updates /status
MCPManager Responsibilities¶
The MCPManager is responsible for managing a single upstream MCP server connection. It handles:
- Initialization: Establishes connection, validates protocol version and capabilities
- Discovery: Fetches initial tool list and registers tools with the broker
- Notification Subscription: Sets up SSE connection for state change events
- Health Monitoring: Periodically checks connection health and reconnects if needed
- Notification Handling: Processes state change notifications and forwards them to the broker
- Graceful Shutdown: Cleans up connections and resources when stopped
State Change Notifications¶
The MCPManager subscribes to state change notifications from the upstream MCP server:
notifications/tools/list_changednotifications/resources/list_changednotifications/prompts/list_changednotifications/roots/list_changed
When a notification is received, the MCPManager:
- Updates its internal state (e.g., re-fetches tool list)
- Forwards the notification to the broker
- The broker forwards it to all connected clients
For more details, see the notifications design documentation.
Error Handling and Retry Logic¶
The MCPManager implements exponential backoff retry for:
- Initial connection failures
- Discovery failures
- Health check failures
- Notification connection drops
Retries are handled in background routines to avoid blocking the main broker operations.
Status and Health¶
The broker exposes status information about registered servers:
- Connection status
- Last successful discovery time
- Tool count
- Error messages (if any)
This information is available via the /status endpoint for debugging and monitoring.