|
| 1 | +# Deploying BioMCP as a Cloudflare Worker |
| 2 | + |
| 3 | +This guide explains how to deploy BioMCP as a Cloudflare Worker using Server-Sent Events (SSE) for communication. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +BioMCP now supports two deployment modes: |
| 8 | + |
| 9 | +1. **Local STDIO Mode**: The traditional mode where the server communicates via standard input/output. |
| 10 | +2. **Cloudflare Worker Mode**: Deployment as a Cloudflare Worker using SSE for communication. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- A Cloudflare account with Workers enabled |
| 15 | +- [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/install-and-update/) installed |
| 16 | +- For local development of the Worker mode: `pip install biomcp-python[worker]` |
| 17 | + |
| 18 | +## Architecture |
| 19 | + |
| 20 | +When deployed as a Cloudflare Worker, BioMCP works as follows: |
| 21 | + |
| 22 | +1. The Cloudflare Worker receives HTTP requests from clients |
| 23 | +2. The Worker forwards these requests to your remote MCP server |
| 24 | +3. The remote MCP server processes the requests and returns responses |
| 25 | +4. The Worker streams these responses back to clients using SSE |
| 26 | + |
| 27 | +### Architecture Diagram |
| 28 | + |
| 29 | +Below is an improved diagram of the setup: |
| 30 | + |
| 31 | +``` |
| 32 | ++-----------------------+ |
| 33 | +| Claude Desktop (or | |
| 34 | +| other client) | |
| 35 | ++----------+------------+ |
| 36 | + | |
| 37 | + v |
| 38 | ++----------+------------+ |
| 39 | +| Cloudflare Worker | |
| 40 | ++----------+------------+ |
| 41 | + | |
| 42 | + v |
| 43 | ++-------------------------------+ |
| 44 | +| FastMCP Python Service | |
| 45 | +| (Docker, hosted server) | |
| 46 | ++-------------------------------+ |
| 47 | +``` |
| 48 | + |
| 49 | +## Setup |
| 50 | + |
| 51 | +### 1. Configure Your Remote MCP Server |
| 52 | + |
| 53 | +First, you need to set up a remote MCP server that will handle the actual processing: |
| 54 | + |
| 55 | +#### Using Docker Compose |
| 56 | + |
| 57 | +A Docker Compose file is now provided for building and deploying the remote FastMCP Python service. You must set the `TAG` variable to specify the image version: |
| 58 | + |
| 59 | +```bash |
| 60 | +TAG=latest docker compose up -d |
| 61 | +``` |
| 62 | + |
| 63 | +- The service will be accessible on the configured port (default: 8000). |
| 64 | +- Ensure your server is reachable from Cloudflare Workers. |
| 65 | + |
| 66 | +#### Manual Installation |
| 67 | + |
| 68 | +```bash |
| 69 | +# Install with worker dependencies |
| 70 | +pip install biomcp-python[worker] |
| 71 | + |
| 72 | +# Run the server in worker mode |
| 73 | +biomcp run --mode worker --host 0.0.0.0 --port 8000 |
| 74 | +``` |
| 75 | + |
| 76 | +Make sure this server is accessible from the internet, or at least from Cloudflare Workers. |
| 77 | + |
| 78 | +### 2. Configure Cloudflare Worker |
| 79 | + |
| 80 | +Edit the `wrangler.toml` file to point to your remote MCP server: |
| 81 | + |
| 82 | +```toml |
| 83 | +[vars] |
| 84 | +REMOTE_MCP_SERVER_URL = "https://your-remote-mcp-server.com/mcp" |
| 85 | +# Add an API key if your server requires authentication |
| 86 | +MCP_SERVER_API_KEY = "your-api-key" |
| 87 | +``` |
| 88 | + |
| 89 | +### 3. Deploy the Worker |
| 90 | + |
| 91 | +Use Wrangler to deploy your Worker: |
| 92 | + |
| 93 | +```bash |
| 94 | +# Login to Cloudflare |
| 95 | +npx wrangler@latest login |
| 96 | + |
| 97 | +# Deploy the worker |
| 98 | +npx wrangler@latest deploy |
| 99 | + |
| 100 | +# Tail logs for debugging |
| 101 | +npx wrangler@latest tail |
| 102 | +``` |
| 103 | + |
| 104 | +## Benefits of Remote MCP |
| 105 | + |
| 106 | +- **Scalability:** Offloads heavy computation to a dedicated server, reducing load on the Worker and improving performance. |
| 107 | +- **Security:** The Worker acts as a secure proxy, hiding your backend and enabling API key protection. |
| 108 | +- **Flexibility:** You can update or scale the Python service independently of the Worker. |
| 109 | +- **Debugging:** Use `npx wrangler tail` for real-time logs and easier troubleshooting. |
| 110 | +- **Modern Deployment:** Docker Compose simplifies environment setup and reproducibility. |
| 111 | + |
| 112 | +## Usage |
| 113 | + |
| 114 | +Once deployed, your Cloudflare Worker will be available at a URL like: |
| 115 | +`https://biomcp-worker.<your-worker-subdomain>.workers.dev` |
| 116 | + |
| 117 | +Clients can connect to this endpoint using SSE: |
| 118 | + |
| 119 | +```javascript |
| 120 | +// Example client-side JavaScript |
| 121 | +const eventSource = new EventSource( |
| 122 | + "https://biomcp-worker.<your-worker-subdomain>.workers.dev", |
| 123 | +); |
| 124 | + |
| 125 | +eventSource.onmessage = (event) => { |
| 126 | + const data = JSON.parse(event.data); |
| 127 | + console.log("Received:", data); |
| 128 | + |
| 129 | + // Check for the end of the stream |
| 130 | + if (event.data === "[DONE]") { |
| 131 | + eventSource.close(); |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +eventSource.onerror = (error) => { |
| 136 | + console.error("EventSource error:", error); |
| 137 | + eventSource.close(); |
| 138 | +}; |
| 139 | +``` |
| 140 | + |
| 141 | +## Local Development |
| 142 | + |
| 143 | +For local development and testing, you can run the worker mode locally: |
| 144 | + |
| 145 | +```bash |
| 146 | +# Run the server in worker mode on localhost |
| 147 | +biomcp run --mode worker --host 127.0.0.1 --port 8000 |
| 148 | +``` |
| 149 | + |
| 150 | +Then use Wrangler to develop locally: |
| 151 | + |
| 152 | +```bash |
| 153 | +npx wrangler@latest dev |
| 154 | +``` |
| 155 | + |
| 156 | +## Troubleshooting |
| 157 | + |
| 158 | +### Worker Connection Issues |
| 159 | + |
| 160 | +If the Worker cannot connect to your remote MCP server: |
| 161 | + |
| 162 | +1. Ensure your remote server is publicly accessible |
| 163 | +2. Check that the `REMOTE_MCP_SERVER_URL` is correctly set |
| 164 | +3. Verify any authentication requirements |
| 165 | + |
| 166 | +### Performance Considerations |
| 167 | + |
| 168 | +- Cloudflare Workers have execution time limits (typically 30 seconds for free accounts) |
| 169 | +- Consider implementing timeouts and chunking for large responses |
| 170 | +- Monitor your Worker's performance in the Cloudflare dashboard |
| 171 | + |
| 172 | +## Security Considerations |
| 173 | + |
| 174 | +- Always use HTTPS for communication between the Worker and your remote MCP server |
| 175 | +- Consider implementing authentication for your remote MCP server |
| 176 | +- Do not expose sensitive information in your Worker code |
0 commit comments