|
| 1 | +# MCPM Router Design |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The MCPM Router serves as an intermediary layer within the MCPM (Model Context Protocol Manager) project. The router acts in a dual role: |
| 6 | + |
| 7 | +1. **As an MCP Client**: Connects to multiple downstream MCP servers |
| 8 | +2. **As an MCP Server**: Provides a unified interface to upstream MCP clients |
| 9 | + |
| 10 | +This design allows for aggregation of capabilities from multiple MCP servers while providing a single, stable connection point for clients. |
| 11 | + |
| 12 | +## Architecture |
| 13 | + |
| 14 | +```mermaid |
| 15 | +flowchart TB |
| 16 | + subgraph clients[Upstream Clients] |
| 17 | + c1[MCP Client 1] |
| 18 | + c2[MCP Client 2] |
| 19 | + cn[MCP Client N] |
| 20 | + end |
| 21 | + |
| 22 | + subgraph router[MCPM Router] |
| 23 | + r[MCPRouter] |
| 24 | + ch[ClientHandler] |
| 25 | + sh[ServerHandler] |
| 26 | + cm[ConnectionManager] |
| 27 | + r --> ch |
| 28 | + r --> sh |
| 29 | + ch --> cm |
| 30 | + sh --> cm |
| 31 | + end |
| 32 | + |
| 33 | + subgraph servers[Downstream Servers] |
| 34 | + s1[MCP Server 1] |
| 35 | + s2[MCP Server 2] |
| 36 | + sm[MCP Server M] |
| 37 | + end |
| 38 | + |
| 39 | + c1 <--SSE--> ch |
| 40 | + c2 <--SSE--> ch |
| 41 | + cn <--SSE--> ch |
| 42 | + |
| 43 | + sh <--STDIO/SSE--> s1 |
| 44 | + sh <--STDIO/SSE--> s2 |
| 45 | + sh <--STDIO/SSE--> sm |
| 46 | + |
| 47 | + classDef default fill:#f9f9f9,stroke:#333,stroke-width:1px; |
| 48 | + classDef routerClass fill:#e1f5fe,stroke:#0277bd,stroke-width:2px; |
| 49 | + class router routerClass; |
| 50 | +``` |
| 51 | + |
| 52 | +## Key Components |
| 53 | + |
| 54 | +### `MCPRouter` |
| 55 | + |
| 56 | +The main orchestrator class that provides a unified API for the application: |
| 57 | +- Initializes and coordinates all internal components |
| 58 | +- Provides methods for connecting to downstream servers |
| 59 | +- Handles server and client routing through appropriate handlers |
| 60 | +- Manages namespacing of capabilities across different servers |
| 61 | +- Offers a clean, high-level interface for application code |
| 62 | + |
| 63 | +### `ConnectionManager` |
| 64 | + |
| 65 | +Maintains a registry of connections to downstream servers, providing methods to: |
| 66 | +- Add, retrieve, and remove downstream server connections |
| 67 | +- Lookup downstream servers by ID |
| 68 | + |
| 69 | +### `ServerHandler` |
| 70 | + |
| 71 | +Manages connections to downstream MCP servers: |
| 72 | +- Establishes and maintains connections using stdio or SSE transports |
| 73 | +- Aggregates capabilities from all connected servers |
| 74 | +- Routes requests from upstream clients to the appropriate downstream server |
| 75 | +- Handles notifications from downstream servers |
| 76 | + |
| 77 | +### `ClientHandler` |
| 78 | + |
| 79 | +Serves upstream MCP clients: |
| 80 | +- Provides an SSE server endpoint for clients to connect |
| 81 | +- Handles client connections/disconnections transparently |
| 82 | +- Routes client requests to the `ServerHandler` |
| 83 | +- Delivers responses and notifications back to clients |
| 84 | + |
| 85 | +### `ConnectionDetails` and `ConnectionType` |
| 86 | + |
| 87 | +Defines the configuration for connecting to downstream servers: |
| 88 | +- Supports multiple transport protocols (STDIO, SSE) |
| 89 | +- Validates configuration based on the connection type |
| 90 | +- Stores necessary connection parameters (command, args, env, URL) |
| 91 | + |
| 92 | +## Communication Flow |
| 93 | + |
| 94 | +### Downstream Connections (Router as Client) |
| 95 | + |
| 96 | +1. Router creates persistent connections to downstream MCP servers using STDIO or SSE |
| 97 | +2. Connections are maintained regardless of upstream client presence |
| 98 | +3. Server capabilities are fetched and aggregated with namespacing |
| 99 | +4. Notifications from servers are routed to appropriate upstream clients |
| 100 | + |
| 101 | +### Upstream Connections (Router as Server) |
| 102 | + |
| 103 | +1. Router provides an SSE server interface for upstream clients |
| 104 | +2. Clients can connect/disconnect at will without affecting downstream connections |
| 105 | +3. Client requests are routed to appropriate downstream servers |
| 106 | +4. Responses and notifications are delivered back to clients |
| 107 | + |
| 108 | +## Request Routing |
| 109 | + |
| 110 | +```mermaid |
| 111 | +sequenceDiagram |
| 112 | + participant Client as MCP Client |
| 113 | + participant CH as ClientHandler |
| 114 | + participant Router as MCPRouter |
| 115 | + participant SH as ServerHandler |
| 116 | + participant Server as MCP Server |
| 117 | + |
| 118 | + Client->>CH: Request |
| 119 | + CH->>Router: Forward request |
| 120 | + Router->>Router: Parse namespaced ID |
| 121 | + Router->>SH: Route to appropriate server |
| 122 | + SH->>Server: Forward request with denormalized ID |
| 123 | + Server->>SH: Response |
| 124 | + SH->>Router: Forward response |
| 125 | + Router->>CH: Route to originating client |
| 126 | + CH->>Client: Deliver response |
| 127 | +``` |
| 128 | + |
| 129 | +## Capability Aggregation |
| 130 | + |
| 131 | +1. Upon connection to a downstream server, all capabilities are fetched |
| 132 | +2. Capabilities (tools, resources, prompts) are namespaced with server ID |
| 133 | +3. Namespaced capabilities are added to aggregate pool |
| 134 | +4. Clients see all capabilities from all servers as a unified collection |
| 135 | +5. When a server disconnects, its capabilities are removed from the pool |
| 136 | + |
| 137 | +## Error Handling |
| 138 | + |
| 139 | +1. Connection errors are isolated to affected servers |
| 140 | +2. Standard JSON-RPC error responses for client requests |
| 141 | +3. Proper error propagation from downstream servers to clients |
| 142 | +4. Graceful handling of client and server disconnections |
| 143 | + |
| 144 | +## Benefits of This Design |
| 145 | + |
| 146 | +1. **Decoupling**: Upstream clients are decoupled from downstream servers |
| 147 | +2. **Resilience**: Client disconnections don't affect server connections |
| 148 | +3. **Aggregation**: Multiple capabilities from different servers appear as one |
| 149 | +4. **Flexibility**: Supports different transport protocols (STDIO, SSE) |
| 150 | +5. **Scalability**: Can manage multiple clients and servers simultaneously |
| 151 | +6. **Clean API**: The `MCPRouter` provides a simple, unified interface for applications |
| 152 | + |
| 153 | +## Implementation Notes |
| 154 | + |
| 155 | +- All communication follows the MCP protocol specification |
| 156 | +- Asynchronous operation using Python's asyncio |
| 157 | +- Type-safe interfaces using Python type hints |
| 158 | +- Clean separation of concerns between components |
0 commit comments