遠端MCP伺服器
在MCP整合基礎文章中,我們瞭解了MCP(Model Context Protocol)的基本概念和本地STDIO伺服器的配置。但MCP的真正威力在於遠端伺服器——透過HTTP/HTTPS協議連線的雲端服務,它們提供了無需本地安裝、開箱即用的強大功能。
本文將深入探討遠端MCP伺服器的配置、認證機制,以及如何整合Sentry、Linear等第三方服務,讓你的Claude Code能夠直接訪問企業級工具和資料。
什麼是遠端MCP伺服器
本地 vs 遠端MCP伺服器
本地MCP伺服器(STDIO傳輸):
- 透過標準輸入/輸出通訊
- 需要在本地機器上執行
- 需要安裝Node.js、Python等執行時
- 適合訪問本地資源和私有資料
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/files"]
}
}
}
遠端MCP伺服器(HTTP/SSE傳輸):
- 透過HTTP/HTTPS協議通訊
- 託管在雲端或遠端伺服器
- 只需提供URL即可連線
- 適合訪問SaaS服務和公共服務
{
"mcpServers": {
"sentry": {
"url": "https://mcp.sentry.dev/mcp",
"transport": "http"
}
}
}
遠端MCP伺服器的優勢
传统本地MCP服务器:
用户机器 → 安装Node.js → npx启动服务器 → 占用本地资源 → 配置复杂
远程MCP服务器:
用户机器 → 提供URL → 直接连接 → 即用即走
關鍵優勢:
-
零安裝體驗
- 無需本地安裝依賴
- 無需配置執行環境
- 立即可用
-
集中管理
- 服務提供商統一維護
- 自動更新和升級
- 統一的版本控制
-
安全性增強
- 憑證由服務提供商管理
- OAuth 2.1標準認證
- 無需本地儲存敏感資訊
-
效能最佳化
- 專業的伺服器基礎設施
- 更快的響應速度
- 負載均衡和容錯
-
跨平臺相容
- 不受本地環境限制
- Windows、macOS、Linux統一體驗
遠端MCP傳輸協議
傳輸協議演進
MCP遠端伺服器支援兩種主要傳輸方式:
1. HTTP + SSE(Server-Sent Events)
工作原理:
客户端 服务器
│ │
├───── HTTP POST ──────────────────→ │
│ (发送JSON-RPC请求) │
│ │
│ ├─ 处理请求
│ │
├───── SSE连接 ─────────────────────→ │
│<──── 数据流 ─────────── ─────────────┤
│<──── 持续更新 ──────────────────────┤
│ │
特點:
- 單向伺服器推送
- 自動重連機制
- 瀏覽器原生支援
- 適合實時資料流
配置示例:
{
"mcpServers": {
"sentry": {
"url": "https://mcp.sentry.dev/mcp",
"transport": "sse"
}
}
}
2. Streamable HTTP(推薦)
工作原理:
客户端 服务器
│ │
├───── HTTP POST (streamable) ────→ │
│ (发送请求,期望流式响应) │
│ │
│ ├─ 处理请求
│ │
│<──── HTTP Chunked Response ────────┤
│<──── 数据块流 ──────────────────────┤
特點:
- 雙向通訊
- 更高效的傳輸
- 更好的錯誤處理
- MCP 2025年推薦的傳輸方式
配置示例:
{
"mcpServers": {
"linear": {
"url": "https://mcp.linear.app",
"transport": "http"
}
}
}
協議對比表
| 特性 | HTTP + SSE | Streamable HTTP | STDIO |
|---|---|---|---|
| 傳輸方向 | 伺服器推送 | 雙向流式 | 雙向 |
| 連線方式 | 長連線 | 短連線/流式 | 持久程序 |
| 適用場景 | 實時通知 | 通用遠端服務 | 本地服務 |
| 瀏覽器支援 | 原生 | 需fetch API | 不支援 |
| 推薦度 | 向後相容 | ⭐推薦 | 本地必需 |
| 複雜度 | 中等 | 低 | 低 |
配置遠端MCP伺服器
方法一:使用CLI嚮導(推薦)
Claude Code提供了互動式嚮導來新增遠端MCP伺服器:
# 基本添加命令
claude mcp add sentry \
--url https://mcp.sentry.dev/mcp \
--transport http
# 添加到项目作用域
claude mcp add linear \
--url https://mcp.linear.app \
--transport http \
--scope project
# 添加到用户作用域
claude mcp add github-remote \
--url https://mcp.github.com \
--transport http \
--scope user
互動式流程示例:
$ claude mcp add sentry --url https://mcp.sentry.dev/mcp
✓ Detected remote MCP server
✓ Server supports OAuth 2.1 authentication
? Configure authentication:
❯ OAuth (Recommended)
API Key
Skip
Selected: OAuth
? Scope:
❯ user
project
local
✓ Sentry MCP server added successfully!
✓ Configuration saved to user scope
Next steps:
1. Restart Claude Code
2. First use will trigger OAuth flow
3. Grant permissions to complete setup
Restart Claude Code now? [Y/n]
方法二:手動編輯配置檔案
對於複雜配置,可以直接編輯配置檔案:
配置檔案位置:
# macOS
~/Library/Application Support/Claude/claude_desktop_config.json
# Linux
~/.config/Claude/claude_desktop_config.json
# Windows
%APPDATA%/Claude/claude_desktop_config.json
完整配置示例:
{
"mcpServers": {
"sentry": {
"url": "https://mcp.sentry.dev/mcp",
"transport": "http",
"oauth": {
"authorizationUrl": "https://sentry.io/oauth/authorize",
"tokenUrl": "https://sentry.io/oauth/token",
"clientId": "auto-registered",
"scopes": ["project:read", "event:read", "issue:read"]
},
"headers": {
"User-Agent": "Claude-Code/1.0"
},
"timeout": 30000
},
"linear": {
"url": "https://mcp.linear.app",
"transport": "http"
},
"notion": {
"url": "https://mcp.notion.so",
"transport": "http",
"oauth": {
"authorizationUrl": "https://auth.notion.com/oauth/authorize",
"tokenUrl": "https://api.notion.com/v1/oauth/token",
"scopes": ["read"]
}
},
"github-remote": {
"url": "https://api.githubcopilot.com/mcp/github",
"transport": "http"
}
}
}
配置選項詳解
基本選項
{
"mcpServers": {
"server-name": {
// 必需:服务器URL
"url": "https://example.com/mcp",
// 必需:传输方式
// - "http": Streamable HTTP (推荐)
// - "sse": HTTP + SSE
"transport": "http"
}
}
}
OAuth認證選項
{
"mcpServers": {
"server-name": {
"url": "https://example.com/mcp",
"transport": "http",
// OAuth配置
"oauth": {
// OAuth授权端点
"authorizationUrl": "https://example.com/oauth/authorize",
// Token交换端点
"tokenUrl": "https://example.com/oauth/token",
// 客户端ID(自动注册或手动提供)
"clientId": "your-client-id",
// 客户端密钥(可选,PKCE模式下不需要)
"clientSecret": "your-client-secret",
// 请求的权限范围
"scopes": ["read", "write"],
// 回调URL(通常是本地端口)
"redirectUrl": "http://localhost:3000/callback"
}
}
}
}
高階選項
{
"mcpServers": {
"server-name": {
"url": "https://example.com/mcp",
"transport": "http",
// 自定义HTTP头
"headers": {
"User-Agent": "Claude-Code/1.0",
"X-Custom-Header": "value"
},
// 超时设置(毫秒)
"timeout": 30000,
// 环境变量(用于敏感信息)
"env": {
"API_KEY": "${API_KEY}"
},
// 启用/禁用服务器
"enabled": true,
// 优先级(数字越大优先级越高)
"priority": 100
}
}
}
OAuth認證詳解
什麼是OAuth 2.1
OAuth 2.1是OAuth 2.0的下一代版本,提供了更強的安全保證:
核心安全特性:
-
強制PKCE(Proof Key for Code Exchange)
- 防止授權碼攔截攻擊
- 適用於公共客戶端
- 無需client_secret
-
隱式流被廢棄
- 不再返回access_token in URL
- 使用授權碼+PKCE替代
-
重新整理令牌輪換
- 每次重新整理獲得新的refresh_token
- 舊的refresh_token立即失效
- 減少令牌洩露風險
OAuth認證流程
標準OAuth 2.1流程
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ Claude │ │ OAuth Provider│ │ Server │
│ Code │ │ (Sentry等) │ │ (MCP) │
└──────┬──────┘ └───────┬───────┘ └──────┬──────┘
│ │ │
│ 1. 尝试调用MCP工具 │ │
├─────────────────────────────────→│ │
│ (需要认证) │ │
│ │ │
│ 2. 返回401,提供OAuth信息 │ │
│←─────────────────────────────────┤ │
│ { │ │
│ authorizationUrl: "...", │ │
│ tokenUrl: "...", │ │
│ clientId: "..." │ │
│ } │ │
│ │ │
│ 3. 打开浏览器,用户授权 │ │
│ ┌────────────────────────┐ │ │
│ │ Login to Sentry │ │ │
│ │ [授权界面] │ │ │
│ │ [Allow] [Deny] │ │ │
│ └────────────────────────┘ │ │
├─────────────────────────────────→│ │
│ │ │
│ 4. 获得authorization code │ │
│←─────────────────────────────────┤ │
│ code=xxx&state=yyy │ │
│ │ │
│ 5. 用code换取access_token │ │
├─────────────────────────────────→│ │
│ code + PKCE verifier │ │
│ │ │
│ 6. 返回access_token │ │
│←─────────────────────────────────┤ │
│ { │ │
│ access_token: "xxx", │ │
│ refresh_token: "yyy", │ │
│ expires_in: 3600 │ │
│ } │ │
│ │ │
│ 7. 安全存储token │ │
│ (Keychain/加密存储) │ │
│ │ │
│ 8. 使用access_token调用MCP │ │
├──────── ──────────────────────────────────────────────────────→│
│ Authorization: Bearer xxx │
│ │
│ 9. 返回数据 │
│←──────────────────────────────────────────────────────────────┤
│ │
│ 10. Token过期时自动刷新 │
├─────────────────────────────────→│ │
│ refresh_token │ │
│←─────────────────────────────────┤ │
│ 新的access_token │ │
在Claude Code中配置OAuth
Sentry OAuth配置
步驟1:新增Sentry MCP伺服器
claude mcp add sentry \
--url https://mcp.sentry.dev/mcp \
--transport http \
--scope user
步驟2:首次使用時觸發OAuth
$ claude
> 查看Sentry中的错误
🔐 需要OAuth认证
Sentry MCP服务器需要授权。
正在打开浏览器...
[浏览器自动打开 https://sentry.io/oauth/authorize...]
✓ 授权成功!
✓ Access token已安全存储
正在获取Sentry错误...
自動生成的配置:
{
"mcpServers": {
"sentry": {
"url": "https://mcp.sentry.dev/mcp",
"transport": "http",
"oauth": {
"authorizationUrl": "https://sentry.io/oauth/authorize",
"tokenUrl": "https://sentry.io/oauth/token",
"clientId": "mcp-claude-code-prod",
"scopes": [
"project:read",
"event:read",
"event:write",
"issue:read",
"issue:write"
],
"redirectUrl": "http://localhost:3000/callback"
}
}
}
}
Linear OAuth配置
步驟1:新增Linear MCP伺服器
claude mcp add linear \
--url https://mcp.linear.app \
--transport http \
--scope user
步驟2:OAuth授權流程
$ claude
> 列出我的Linear任务
🔐 Linear需要OAuth授权
正在打开Linear授权页面...
[Linear授权界面]
Claude Code 想要访问:
✓ 读取你的项目
✓ 读取和更新Issues
✓ 读取你的团队信息
[Allow] [Cancel]
点击"Allow"完成授权...
✓ Linear授权成功!
✓ Token已安全保存
正在获取Linear issues...
配置示例:
{
"mcpServers": {
"linear": {
"url": "https://mcp.linear.app",
"transport": "http",
"oauth": {
"authorizationUrl": "https://linear.app/oauth/authorize",
"tokenUrl": "https://api.linear.app/oauth/token",
"clientId": "auto-registered",
"scopes": ["read", "write", "issues:read", "issues:write", "projects:read"],
"redirectUrl": "http://localhost:3000/callback"
}
}
}
}
OAuth令牌管理
令牌儲存位置
# macOS - 使用Keychain
security find-generic-password \
-a "claude-code" \
-s "sentry-mcp-token" \
-w
# Linux - 使用Secret Service
secret-tool lookup claude-code sentry-mcp-token
# Windows - 使用Credential Manager
cmdkey /list | findstr sentry
令牌重新整理機制
Claude Code自動處理令牌重新整理:
// 内部刷新逻辑(伪代码)
async function callMCPWithAuth(server, tool, args) {
let token = await getStoredToken(server);
// 检查token是否即将过期
if (isTokenExpiringSoon(token)) {
console.log('Token即将过期,自动刷新...');
// 使用refresh_token获取新的access_token
const newToken = await refreshAccessToken(token.refreshToken);
// 存储新token
await storeToken(server, newToken);
// 使旧refresh_token失效
await revokeOldToken(token.refreshToken);
token = newToken;
}
// 使用access_token调用MCP
return await callMCP(server, tool, args, {
headers: {
'Authorization': `Bearer ${token.accessToken}`
}
});
}
手動撤銷授權
# 方法1:通过CLI删除服务器
claude mcp remove sentry
# 方法2:手动删除token
# macOS
security delete-generic-password -a claude-code -s sentry-mcp-token
# 方法3:在服务提供商处撤销
# 访问Sentry设置 → Applications → 撤销Claude Code权限
Sentry整合實戰
Sentry MCP伺服器功能
Sentry MCP伺服器提供了豐富的錯誤監控和除錯功能:
核心能力:
- 列出專案和Issues
- 獲取錯誤詳情和堆疊跟蹤
- 檢視事件聚合
- 觸發Seer AI根因分析
- 新增註釋和標籤
- 更新Issue狀態