OpenCode MCP Gateway
Exposes local OpenCode instances as remote MCP servers for Claude and ChatGPT, enabling terminal access, session management, and interactive human-in-the-loop workflows. It simplifies deployment for local machines using Cloudflare Tunnels to provide secure public connectivity and OAuth support.
README
OpenCode MCP Gateway Cloudflare Desktop
[!WARNING] This project exposes remote shell execution, PTY control, session steering, and agent-driven code execution on the machine where it runs. Treat it like a personal-use remote code execution service. If this gateway is compromised, an attacker may be able to read files, execute commands, access credentials, damage data, or pivot deeper into your environment. Do not expose it to untrusted users. Use strong secrets. Keep the origin machine locked down.
This repository is the Cloudflare desktop deployment variant of opencode-mcp-gateway.
It is designed for:
- an Ubuntu desktop or laptop
- a local OpenCode server
- a public HTTPS MCP endpoint fronted by Cloudflare Tunnel
- Claude and ChatGPT OAuth-compatible remote MCP usage
- no VPS
Repository:
https://github.com/gjabdelnoor/opencode-mcp-gateway-cloudflare-desktop
Architecture
Claude / ChatGPT
|
v
https://mcp.example.com/mcp
|
v
Cloudflare Edge
|
v
cloudflared tunnel
|
v
http://127.0.0.1:3001
|
v
http://127.0.0.1:9999
|
v
OpenCode
Read This First
You need OpenCode installed and working before this gateway can do anything useful.
OpenCode docs:
- Intro:
https://opencode.ai/docs/ - Providers:
https://opencode.ai/docs/providers/ - Server mode:
https://opencode.ai/docs/server/
Detailed docs in this repo:
docs/ubuntu-cloudflare-desktop-setup.mddocs/session-change-map.md
Installation Paths
There are two practical install paths documented here.
Path A: Purchased Domain + Cloudflare DNS
This is the recommended path.
Use this when you want a stable hostname like:
https://mcp.example.com/mcp
Why this is better:
- stable OAuth issuer URL
- stable connector configuration
- better long-term reliability
- easier to run multiple gateways like
mcp1,mcp2,mcp3
Path B: Free Cloudflare trycloudflare Tunnel
This is the free, no-personal-domain path.
Use this when you want to experiment without buying or wiring a domain.
Why this is worse:
- hostname is temporary
- hostname can change on reconnect
- OAuth clients may break when the issuer URL changes
- not ideal for durable Claude or ChatGPT connectors
Quick Start
1. Install Ubuntu packages
sudo apt update
sudo apt install -y curl git python3 python3-pip python3-venv
2. Install and configure OpenCode
curl -fsSL https://opencode.ai/install | bash
Then configure a provider and start OpenCode locally:
opencode serve --hostname 127.0.0.1 --port 9999
3. Clone this repo and install dependencies
git clone https://github.com/gjabdelnoor/opencode-mcp-gateway-cloudflare-desktop.git
cd opencode-mcp-gateway-cloudflare-desktop
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
4. Create .env
cp .env.example .env
Example:
MCP_AUTH_TOKEN=replace-with-a-long-random-secret
MCP_CLIENT_ID=opencode-mcp-gateway
MCP_ALLOWED_CLIENT_IDS=opencode-mcp-gateway
PUBLIC_BASE_URL=https://mcp.example.com
DEFAULT_WORKSPACE_DIR="/home/YOUR_USER/AI Projects"
OPENCODE_HOST=127.0.0.1
OPENCODE_PORT=9999
GATEWAY_PORT=3001
ENABLE_RAW_BASH=true
DEFAULT_PLANNING_MODEL=opencode/minimax-m2.5-free
DEFAULT_BUILDING_MODEL=openai/gpt-5.4-mini
5. Choose a tunnel path
Purchased domain path
Install cloudflared:
curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
sudo dpkg -i cloudflared.deb
rm cloudflared.deb
Authenticate and create the tunnel:
cloudflared tunnel login
cloudflared tunnel create opencode-mcp-gateway
cloudflared tunnel route dns opencode-mcp-gateway mcp.example.com
Create ~/.cloudflared/config.yml:
tunnel: YOUR_TUNNEL_ID
credentials-file: /home/YOUR_USER/.cloudflared/YOUR_TUNNEL_ID.json
ingress:
- hostname: mcp.example.com
service: http://127.0.0.1:3001
originRequest:
httpHostHeader: mcp.example.com
- service: http_status:404
Set:
PUBLIC_BASE_URL=https://mcp.example.com
Run the tunnel:
cloudflared tunnel run opencode-mcp-gateway
Free trycloudflare path
Install cloudflared the same way, then run:
cloudflared tunnel --url http://127.0.0.1:3001
That gives you a temporary https://...trycloudflare.com URL.
Use that URL as:
PUBLIC_BASE_URL=https://YOUR-TEMP-HOST.trycloudflare.com
Important caveat:
- this path is still considered experimental here
- if the hostname changes, you will usually need to recreate the connector in Claude or ChatGPT
6. Start the gateway
source .venv/bin/activate
python main.py
Verify The Deployment
Check:
curl https://mcp.example.com/.well-known/oauth-authorization-server
curl https://mcp.example.com/.well-known/oauth-authorization-server/mcp
curl https://mcp.example.com/.well-known/oauth-protected-resource
curl -D - -o /dev/null https://mcp.example.com/mcp
You want:
- OAuth issuer:
https://mcp.example.com - token endpoint:
https://mcp.example.com/oauth/token - protected resource:
https://mcp.example.com/mcp - unauthorized
/mcpreturns401withWWW-Authenticateandresource_metadata
Connect ChatGPT Or Claude
MCP server URL:
https://mcp.example.com/mcp
OAuth discovery URLs:
- Claude:
https://mcp.example.com/.well-known/oauth-authorization-server - ChatGPT:
https://mcp.example.com/.well-known/oauth-authorization-server/mcp
Manual OAuth values when needed:
- OAuth Client ID:
opencode-mcp-gateway - OAuth Client Secret: your
MCP_AUTH_TOKEN - Token auth method:
client_secret_post - Scope:
mcp
Multiple Concurrent Agents
If you want multiple chatbot-controlled agents at once, run multiple gateway processes.
Recommended layout:
mcp1.example.com -> localhost:3001mcp2.example.com -> localhost:3002mcp3.example.com -> localhost:3003mcp4.example.com -> localhost:3004mcp5.example.com -> localhost:3005mcp6.example.com -> localhost:3006
Each instance should have its own:
PUBLIC_BASE_URLGATEWAY_PORTMCP_AUTH_TOKENMCP_CLIENT_ID
Configuration
| Variable | Description |
|---|---|
MCP_AUTH_TOKEN |
Bearer secret for OAuth token exchange and MCP access |
MCP_CLIENT_ID |
Main OAuth client ID accepted by the gateway |
MCP_ALLOWED_CLIENT_IDS |
Optional comma-separated allowlist of additional client IDs |
PUBLIC_BASE_URL |
External HTTPS base URL advertised in OAuth metadata |
DEFAULT_WORKSPACE_DIR |
Default project root for new sessions and PTYs |
OPENCODE_HOST |
OpenCode origin host |
OPENCODE_PORT |
OpenCode origin port |
GATEWAY_PORT |
Gateway listen port |
ENABLE_RAW_BASH |
Enables direct bash and bash_exec tools |
DEFAULT_PLANNING_MODEL |
Optional fallback model for planning-mode sessions |
DEFAULT_BUILDING_MODEL |
Optional fallback model for building-mode sessions |
BLOCKED_SESSION_MODELS |
Optional comma-separated models to reject even if OpenCode advertises them |
Troubleshooting
ChatGPT says the server URL is invalid
Use the full MCP URL:
https://mcp.example.com/mcp
Claude reaches login but the connector still fails
Check:
PUBLIC_BASE_URLis correct- protected resource metadata returns
https://mcp.example.com/mcp GET /mcpwithout auth returns401withWWW-Authenticate- the OAuth client secret exactly matches
MCP_AUTH_TOKEN
session_create or send_message looks stalled
Check:
curl http://127.0.0.1:9999/session/status
If OpenCode is retrying an unsupported model, set or adjust:
DEFAULT_PLANNING_MODEL=opencode/minimax-m2.5-free
DEFAULT_BUILDING_MODEL=openai/gpt-5.4-mini
switch_model rejects a model you thought should work
The gateway now validates against OpenCode’s live model catalog.
It will reject:
- anything not currently exposed by OpenCode
- I may have hardcoded it to reject the two known-bad models on my minimax plan... oops:
minimax-coding-plan/MiniMax-M2.5-highspeedminimax-coding-plan/MiniMax-M2.7-highspeed
Sessions are starting in the wrong folder
Set:
DEFAULT_WORKSPACE_DIR="/home/YOUR_USER/AI Projects"
This repo now defaults new sessions and PTYs to that workspace if you do not pass an explicit directory.
Several bots interfere with each other
Use separate gateway instances on separate hostnames and ports.
Full Docs
docs/ubuntu-cloudflare-desktop-setup.mddocs/session-change-map.md
Questions Or Security Concerns
If you have questions, comments, setup issues, or serious security concerns, contact @isnotgabe on Discord.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。