|
| 1 | +# Simple Private Gateway Example |
| 2 | + |
| 3 | +A demonstration of how to use the MCP Python SDK as a private gateway with optional API key authentication over streamable HTTP or SSE transport with custom extensions for private gateway connectivity (SNI hostname support). |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Optional API Key authentication (Bearer token or custom header) |
| 8 | +- Supports both StreamableHTTP and SSE transports |
| 9 | +- Custom extensions for private gateway (SNI hostname) - **Both transports** |
| 10 | +- Can combine authentication + extensions (for authenticated private gateway) |
| 11 | +- Interactive command-line interface |
| 12 | +- Tool calling |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +cd examples/clients/simple-private-gateway |
| 18 | +uv sync --reinstall |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### 1. Start an MCP server |
| 24 | + |
| 25 | +You can use any MCP server. For example: |
| 26 | + |
| 27 | +```bash |
| 28 | +# Example without authentication - StreamableHTTP transport |
| 29 | +cd examples/servers/simple-tool |
| 30 | +uv run mcp-simple-tool --transport streamable-http --port 8081 |
| 31 | + |
| 32 | +# Or with SSE transport |
| 33 | +cd examples/servers/simple-tool |
| 34 | +uv run mcp-simple-tool --transport sse --port 8081 |
| 35 | +``` |
| 36 | + |
| 37 | +### 2. Run the client |
| 38 | + |
| 39 | +The client will interactively prompt you for: |
| 40 | + |
| 41 | +- Server URL (or press Enter to configure port/protocol/hostname separately) |
| 42 | + - If you provide a full URL, it will be used directly |
| 43 | + - If you press Enter, you'll be prompted for: port, protocol, and hostname (for SNI) |
| 44 | +- Transport type (streamable-http or sse) |
| 45 | +- Authentication type (none or API Key) |
| 46 | +- For API Key: API key value, header name, and format (Bearer or direct) |
| 47 | + |
| 48 | +```bash |
| 49 | +# Run the client interactively |
| 50 | +uv run mcp-simple-private-gateway |
| 51 | +``` |
| 52 | + |
| 53 | +Follow the prompts to configure your connection. |
| 54 | + |
| 55 | +### 3. Use the interactive interface |
| 56 | + |
| 57 | +The client provides several commands: |
| 58 | + |
| 59 | +- `list` - List available tools |
| 60 | +- `call <tool_name> [args]` - Call a tool with optional JSON arguments |
| 61 | +- `quit` - Exit |
| 62 | + |
| 63 | +## Examples |
| 64 | + |
| 65 | +### Example 1: Private Gateway without Authentication (StreamableHTTP) |
| 66 | + |
| 67 | +```markdown |
| 68 | +🚀 Simple Private Gateway |
| 69 | + |
| 70 | +📝 Server Configuration |
| 71 | +================================================== |
| 72 | +Server URL [https://localhost:8081]: |
| 73 | +Server port [8081]: 8081 |
| 74 | +Protocol [https]: https |
| 75 | +Server hostname [mcp.deepwiki.com]: mcp.deepwiki.com |
| 76 | + |
| 77 | +Transport type: |
| 78 | + 1. streamable-http (default) |
| 79 | + 2. sse |
| 80 | +Select transport [1]: 1 |
| 81 | + |
| 82 | +Authentication: |
| 83 | + 1. No authentication (default) |
| 84 | + 2. API Key authentication |
| 85 | +Select authentication [1]: 1 |
| 86 | +================================================== |
| 87 | + |
| 88 | +🔗 Connecting to: https://localhost:8081/mcp |
| 89 | +📡 Server hostname: mcp.deepwiki.com |
| 90 | +🚀 Transport type: streamable-http |
| 91 | +🔐 Authentication: None |
| 92 | + |
| 93 | +📡 Opening StreamableHTTP transport connection with extensions... |
| 94 | +🤝 Initializing MCP session... |
| 95 | +⚡ Starting session initialization... |
| 96 | +✨ Session initialization complete! |
| 97 | + |
| 98 | +✅ Connected to MCP server at https://localhost:8081/mcp |
| 99 | +Session ID: abc123... |
| 100 | + |
| 101 | +🎯 Interactive MCP Client (Private Gateway) |
| 102 | +Commands: |
| 103 | + list - List available tools |
| 104 | + call <tool_name> [args] - Call a tool |
| 105 | + quit - Exit the client |
| 106 | + |
| 107 | +mcp> list |
| 108 | +📋 Available tools: |
| 109 | +1. echo |
| 110 | + Description: Echo back the input text |
| 111 | + |
| 112 | +mcp> call echo {"text": "Hello, world!"} |
| 113 | +🔧 Tool 'echo' result: |
| 114 | +Hello, world! |
| 115 | + |
| 116 | +mcp> quit |
| 117 | +👋 Goodbye! |
| 118 | +``` |
| 119 | + |
| 120 | +### Example 2: SSE Transport without Authentication |
| 121 | + |
| 122 | +```markdown |
| 123 | +🚀 Simple Private Gateway |
| 124 | + |
| 125 | +📝 Server Configuration |
| 126 | +================================================== |
| 127 | +Server URL [https://localhost:8081]: |
| 128 | +Server port [8081]: 8081 |
| 129 | +Protocol [https]: https |
| 130 | +Server hostname [mcp.deepwiki.com]: mcp.deepwiki.com |
| 131 | + |
| 132 | +Transport type: |
| 133 | + 1. streamable-http (default) |
| 134 | + 2. sse |
| 135 | +Select transport [1]: 2 |
| 136 | + |
| 137 | +Authentication: |
| 138 | + 1. No authentication (default) |
| 139 | + 2. API Key authentication |
| 140 | +Select authentication [1]: 1 |
| 141 | +================================================== |
| 142 | + |
| 143 | +🔗 Connecting to: https://localhost:8081/sse |
| 144 | +📡 Server hostname: mcp.deepwiki.com |
| 145 | +🚀 Transport type: sse |
| 146 | +🔐 Authentication: None |
| 147 | + |
| 148 | +📡 Opening SSE transport connection with extensions... |
| 149 | +🤝 Initializing MCP session... |
| 150 | +⚡ Starting session initialization... |
| 151 | +✨ Session initialization complete! |
| 152 | + |
| 153 | +✅ Connected to MCP server at https://localhost:8081/sse |
| 154 | + |
| 155 | +🎯 Interactive MCP Client (Private Gateway) |
| 156 | +Commands: |
| 157 | + list - List available tools |
| 158 | + call <tool_name> [args] - Call a tool |
| 159 | + quit - Exit the client |
| 160 | + |
| 161 | +mcp> list |
| 162 | +📋 Available tools: |
| 163 | +1. echo |
| 164 | + Description: Echo back the input text |
| 165 | + |
| 166 | +mcp> quit |
| 167 | +👋 Goodbye! |
| 168 | +``` |
| 169 | + |
| 170 | +### Example 3: API Key Authentication with Bearer Token (StreamableHTTP) |
| 171 | + |
| 172 | +```markdown |
| 173 | +🚀 Simple Private Gateway |
| 174 | + |
| 175 | +📝 Server Configuration |
| 176 | +================================================== |
| 177 | +Server URL [https://localhost:8081]: |
| 178 | +Server port [8081]: 8081 |
| 179 | +Protocol [https]: https |
| 180 | +Server hostname [mcp.deepwiki.com]: api.mcp.example.com |
| 181 | + |
| 182 | +Transport type: |
| 183 | + 1. streamable-http (default) |
| 184 | + 2. sse |
| 185 | +Select transport [1]: 1 |
| 186 | + |
| 187 | +Authentication: |
| 188 | + 1. No authentication (default) |
| 189 | + 2. API Key authentication |
| 190 | +Select authentication [1]: 2 |
| 191 | +Enter API key: sk-1234567890abcdef |
| 192 | + |
| 193 | +API Key format: |
| 194 | + 1. Bearer token (Authorization: Bearer <key>) - default |
| 195 | + 2. Custom header with key only |
| 196 | +Select format [1]: 1 |
| 197 | +================================================== |
| 198 | + |
| 199 | +🔗 Connecting to: https://localhost:8081/mcp |
| 200 | +📡 Server hostname: api.mcp.example.com |
| 201 | +🚀 Transport type: streamable-http |
| 202 | +🔐 Authentication: API Key |
| 203 | +🔑 Header: Authorization |
| 204 | +🎯 Format: Bearer token |
| 205 | + |
| 206 | +🔑 Setting up API key authentication (header: Authorization)... |
| 207 | +📡 Opening StreamableHTTP transport connection with extensions and apikey auth... |
| 208 | +🤝 Initializing MCP session... |
| 209 | +⚡ Starting session initialization... |
| 210 | +✨ Session initialization complete! |
| 211 | + |
| 212 | +✅ Connected to MCP server at https://localhost:8081/mcp |
| 213 | +Session ID: key123... |
| 214 | + |
| 215 | +🎯 Interactive MCP Client (Private Gateway with APIKEY) |
| 216 | +Commands: |
| 217 | + list - List available tools |
| 218 | + call <tool_name> [args] - Call a tool |
| 219 | + quit - Exit the client |
| 220 | + |
| 221 | +mcp> list |
| 222 | +📋 Available tools: |
| 223 | +1. secure-data |
| 224 | + Description: Access secure data with API key |
| 225 | + |
| 226 | +mcp> quit |
| 227 | +👋 Goodbye! |
| 228 | +``` |
| 229 | + |
| 230 | +### Example 4: API Key Authentication with Custom Header (SSE) |
| 231 | + |
| 232 | +```markdown |
| 233 | +🚀 Simple Private Gateway |
| 234 | + |
| 235 | +📝 Server Configuration |
| 236 | +================================================== |
| 237 | +Server URL [https://localhost:8081]: |
| 238 | +Server port [8081]: 8082 |
| 239 | +Protocol [https]: https |
| 240 | +Server hostname [mcp.deepwiki.com]: custom.mcp.example.com |
| 241 | + |
| 242 | +Transport type: |
| 243 | + 1. streamable-http (default) |
| 244 | + 2. sse |
| 245 | +Select transport [1]: 2 |
| 246 | + |
| 247 | +Authentication: |
| 248 | + 1. No authentication (default) |
| 249 | + 2. API Key authentication |
| 250 | +Select authentication [1]: 2 |
| 251 | +Enter API key: my-secret-api-key-123 |
| 252 | + |
| 253 | +API Key format: |
| 254 | + 1. Bearer token (Authorization: Bearer <key>) - default |
| 255 | + 2. Custom header with key only |
| 256 | +Select format [1]: 2 |
| 257 | +Enter header name [X-API-Key]: X-API-Key |
| 258 | +================================================== |
| 259 | + |
| 260 | +🔗 Connecting to: https://localhost:8082/sse |
| 261 | +📡 Server hostname: custom.mcp.example.com |
| 262 | +🚀 Transport type: sse |
| 263 | +🔐 Authentication: API Key |
| 264 | +🔑 Header: X-API-Key |
| 265 | +🎯 Format: Direct key |
| 266 | + |
| 267 | +🔑 Setting up API key authentication (header: X-API-Key)... |
| 268 | +📡 Opening SSE transport connection with extensions and apikey auth... |
| 269 | +🤝 Initializing MCP session... |
| 270 | +⚡ Starting session initialization... |
| 271 | +✨ Session initialization complete! |
| 272 | + |
| 273 | +✅ Connected to MCP server at https://localhost:8082/sse |
| 274 | + |
| 275 | +🎯 Interactive MCP Client (Private Gateway with APIKEY) |
| 276 | +Commands: |
| 277 | + list - List available tools |
| 278 | + call <tool_name> [args] - Call a tool |
| 279 | + quit - Exit the client |
| 280 | + |
| 281 | +mcp> list |
| 282 | +📋 Available tools: |
| 283 | +1. api-tool |
| 284 | + Description: Tool requiring custom API key header |
| 285 | + |
| 286 | +mcp> quit |
| 287 | +👋 Goodbye! |
| 288 | +``` |
| 289 | + |
| 290 | +## Configuration |
| 291 | + |
| 292 | +The client uses interactive prompts for configuration. You'll be asked to provide: |
| 293 | + |
| 294 | +- **Server URL**: The full URL of your MCP server (default: <https://localhost:8081>1>) |
| 295 | + - If you provide a URL, it will be used directly |
| 296 | + - If you press Enter (empty), you'll be prompted for individual components: |
| 297 | + - **Server port**: The port where your MCP server is running (default: 8081) |
| 298 | + - **Protocol**: The protocol to use (default: https) |
| 299 | + - **Server hostname**: The hostname for SNI (Server Name Indication) used in private gateway setup (default: mcp.deepwiki.com) |
| 300 | +- **Transport type**: Choose between `streamable-http` or `sse` (default: streamable-http) |
| 301 | + - StreamableHTTP servers typically use `/mcp` endpoint |
| 302 | + - SSE servers typically use `/sse` endpoint |
| 303 | +- **Authentication**: Choose authentication method (default: no authentication) |
| 304 | + - **None**: No authentication |
| 305 | + - **API Key**: API key-based authentication |
| 306 | + - **API Key**: Your API key value |
| 307 | + - **Format**: Bearer token (Authorization: Bearer <key>) or custom header |
| 308 | + - **Header name**: Custom header name if not using Bearer format (default: X-API-Key) |
| 309 | + |
| 310 | +## Use Cases |
| 311 | + |
| 312 | +This client supports multiple scenarios: |
| 313 | + |
| 314 | +1. **Private Gateway without Auth**: Use custom SNI hostname for HTTPS private gateway connectivity |
| 315 | +2. **Private Gateway with API Key**: Use API key authentication (Bearer or custom header) with private gateway |
| 316 | +3. **Both Transports**: Works with both StreamableHTTP and SSE transports in all scenarios |
0 commit comments