Streamable HTTP Transport 實戰 — 將 MCP Server 部署到雲端
2026-05-11 · MCP 進階 · Eric Cheung
前言:stdio 嘅天花板
快速結論(TL;DR)
- stdio 只適合本地單機開發,要遠端存取、多人共用或者 24/7 運行就要升級做 HTTP Transport
- Streamable HTTP 係目前最新推薦,全雙工單連接雙向通訊,支援 resumability 同 serverless 部署,但要 MCP SDK v1.8+
- Production 部署最少要做四件事:API Key 認證、Rate Limiting、Graceful Shutdown,同喺 Claude Code 用 type: http 註冊遠端 Server
- 部署平台可以揀 Cloudflare Workers、Railway/Render 或者 VPS,Railway 由 GitHub push 到上線大約 5 分鐘搞掂
Day 1 嘅 MCP Server 用 stdio transport — 簡單直接,適合本地開發。但當你想將 MCP Server 部署到 Cloud、分享俾團隊用、或者需要 24/7 運行嘅時候,stdio 就唔夠用了。今日我哋學點樣將 MCP Server 升級到 HTTP Transport,解鎖遠端存取。
三個核心需求推動呢次升級:① 遠端存取(唔使 SSH 入 server)、② 多用戶共享(成個團隊用同一個 MCP Server)、③ Production 穩定性(Health check、graceful shutdown、logging)。
Part 1:三種 Transport 全面比較
1. stdio(Standard I/O)
- 通訊方式: stdin/stdout — 透過 process pipe 傳 JSON-RPC
- 優點: 零配置、低延遲、適合本地開發
- 缺點: 只支援本機 single-client、冇 authentication、唔可以遠端存取
- 適用場景: 個人開發、local prototyping
2. HTTP SSE(Server-Sent Events)
- 通訊方式: Client → Server 用 HTTP POST · Server → Client 用 SSE stream
- 優點: 支援遠端存取、可多 Client、可以用 HTTP middleware
- 缺點: SSE 係單向 streaming(需要兩個 connection)、Proxy 兼容性問題
- 適用場景: 小團隊內部使用、簡單嘅 remote deployment
3. Streamable HTTP(最新推薦)
- 通訊方式: 全雙工 HTTP streaming — single connection 雙向通訊
- 優點: 刪除 SSE 限制、更好的 proxy 兼容、支援 resumability、可部署到 serverless
- 缺點: 需要 MCP SDK v1.8+、文檔較新
- 適用場景: Production deployment、Cloud hosting、multi-tenant SaaS
Part 2:從 stdio 遷移到 Streamable HTTP
Step 1:安裝依賴
npm install @modelcontextprotocol/sdk@latest express cors
Step 2:重寫 Transport Layer
原本嘅 stdio 版本:
// ❌ Old: stdio transport (Day 1 version)
const transport = new StdioServerTransport();
await server.connect(transport);
升級後嘅 Streamable HTTP 版本:
// ✅ New: Streamable HTTP transport (Day 3 version)
import express from "express";
import { createStreamableHttpTransport } from
"@modelcontextprotocol/sdk/server/streamableHttp.js";
const app = express();
app.use(express.json());
// MCP endpoint
app.post("/mcp", async (req, res) => {
const transport = createStreamableHttpTransport({
req, res,
sessionId: req.headers["x-session-id"],
});
await server.connect(transport);
});
// Health check
app.get("/health", (req, res) => {
res.json({ status: "ok", uptime: process.uptime() });
});
app.listen(3000, () => {
console.log("MCP Server running on http://localhost:3000");
});
Part 3:Production 部署 Checklist
1. Authentication
// API Key middleware
app.use("/mcp", (req, res, next) => {
const apiKey = req.headers["x-api-key"];
if (apiKey !== process.env.MCP_API_KEY) {
return res.status(401).json({ error: "Unauthorized" });
}
next();
});
2. Rate Limiting
import rateLimit from "express-rate-limit";
app.use("/mcp", rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 100, // 100 requests per minute
}));
3. Graceful Shutdown
process.on("SIGTERM", async () => {
console.log("Shutting down gracefully...");
await server.close();
process.exit(0);
});
4. Claude Code 註冊(遠端連接)
{
"mcpServers": {
"my-remote-server": {
"type": "http",
"url": "https://mcp.example.com/mcp",
"headers": {
"x-api-key": "$MCP_API_KEY"
}
}
}
}
Part 4:Cloud Deployment 選項
- Cloudflare Workers — Serverless、全球 edge network、適合 lightweight MCP
- Railway / Render — Managed hosting、auto-deploy from GitHub、內置 HTTPS
- Fly.io — 全球分散部署、自動 scaling、超低延遲
- VPS / VDS — 完全控制、適合 heavy workload、需自行管理
- Docker + K8s — Containerized deployment、適合 enterprise scale
快速部署到 Railway(5 分鐘)
- ① GitHub push 你個 MCP Server repo
- ② Railway → New Project → Deploy from GitHub
- ③ Set environment variables(MCP_API_KEY, NODE_ENV, PORT)
- ④ Railway 自動檢測 Node.js project、build、deploy
- ⑤ 將 Railway 生成嘅 URL 填入 Claude Code MCP config → 完成!
總結:由 Local 到 Cloud 嘅關鍵一步
將 MCP Server 從 stdio 升級到 Streamable HTTP,係由「個人工具」進化到「團隊基礎設施」嘅關鍵一步。你而家嘅 MCP Server 可以 24/7 運行、俾全 team 共用、有 proper auth 同 logging — 真正嘅 Production Ready。
Written by Eric Cheung · Learn One AI Thing Every Day · Day 3: Streamable HTTP Transport