Skip to content

Commit f5de751

Browse files
authored
Merge pull request #9 from genomoncology/remote_mcp
Adding the capability for remote MCP via Cloudflare using SSE.
2 parents 1ecb694 + d2a9316 commit f5de751

13 files changed

Lines changed: 2214 additions & 20 deletions

Dockerfile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ COPY tests ./tests
1818
COPY Makefile .
1919
COPY tox.ini .
2020

21-
# Install the package
22-
RUN pip install --upgrade pip && pip install .
21+
# Install the package with worker dependencies
22+
RUN pip install --upgrade pip && pip install .[worker]
2323

24-
# Expose port if necessary
25-
# EXPOSE 8000
24+
# Expose port for remote MCP connections
25+
EXPOSE 8000
2626

27-
# Run the MCP server
28-
CMD ["biomcp", "run"]
27+
# Set default mode to worker, but allow it to be overridden
28+
ENV MCP_MODE=stdio
29+
30+
# Run the MCP server with configurable mode
31+
CMD ["sh", "-c", "biomcp run --mode ${MCP_MODE}"]

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
biomcp-server:
3+
platform: linux/amd64
4+
build: .
5+
image: us.gcr.io/graceful-medley-134315/biomcp-server:${TAG}
6+
container_name: biomcp-server
7+
ports:
8+
- "8000:8000"
9+
environment:
10+
- MCP_MODE=worker # Can be set to 'stdio' or 'worker'
11+
restart: unless-stopped
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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

Comments
 (0)