远程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状态
配置Sentry MCP
方法1:官方远程服务器(推荐)
# 添加官方Sentry MCP服务器
claude mcp add sentry \
--url https://mcp.sentry.dev/mcp \
--transport http \
--scope user
方法2:自托管(企业用户)
如果你使用自托管的Sentry实例:
{
"mcpServers": {
"sentry-self-hosted": {
"url": "https://sentry.yourcompany.com/mcp",
"transport": "http",
"oauth": {
"authorizationUrl": "https://sentry.yourcompany.com/oauth/authorize",
"tokenUrl": "https://sentry.yourcompany.com/oauth/token",
"clientId": "your-custom-client-id",
"scopes": ["project:read", "event:read", "event:write"]
},
"headers": {
"X-Sentry-Host": "sentry.yourcompany.com"
}
}
}
}
实际使用示例
示例1:快速诊断高频错误
用户: 查看production项目中最近24小时错误率最高的5个问题
Claude: [通过Sentry MCP查询]
查询结果:
🔴 高频错误报告(最近24小时)
1. TypeError: Cannot read property 'user' of undefined
- 文件: src/middleware/auth.js:45
- 发生次数: 1,234次
- 影响用户: 456人
- 首次出现: 18小时前
- 链接: https://sentry.io/organizations/acme/issues/12345/
2. PaymentGatewayError: Timeout waiting for response
- 文件: src/api/payment.js:89
- 发生次数: 892次
- 影响用户: 312人
- 首次出现: 12小时前
3. ReferenceError: processOrder is not defined
- 文件: src/services/order.js:156
- 发生次数: 567次
- 影响用户: 201人
- 首次出现: 6小时前
需要我分析第一个错误的详细信息吗?
示例2:深入分析单个错误
用户: 分析第一个错误的根因,并使用Seer AI
Claude: [调用Sentry Seer AI工具]
正在触发Seer AI根因分析...
📊 错误分析报告
## 基本信息
- Issue ID: #12345
- 错误类型: TypeError
- 错误消息: Cannot read property 'user' of undefined
- 影响用户: 456人
- 发生次数: 1,234次
- 最近发生: 2分钟前
## 根因分析(Seer AI)
### 问题根源
auth.js中间件在处理session对象时,没有正确处理session为null的情况。
### 触发条件
1. 用户的session已过期
2. 浏览器cookie中仍然保留了过期的session ID
3. 后端尝试用这个过期的session ID获取session,返回null
4. 代码直接访问null.user,抛出TypeError
### 错误堆栈
at authenticate (src/middleware/auth.js:45:15) at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5) at next (node_modules/express/lib/router/route.js:144:13) at SessionStore.get (src/services/session.js:23:8)
### 修复建议
添加session有效性检查:
```javascript
// 在访问req.session.user之前
if (!req.session || !req.session.user) {
return res.status(401).json({
error: 'Session expired',
code: 'SESSION_INVALID'
});
}
需要我帮你应用这个修复吗?
#### 示例3:批量更新错误状态
用户: 将所有与支付相关的已解决错误标记为released
Claude: [通过Sentry MCP批量更新]
正在查找支付相关的resolved issues...
找到23个支付相关的resolved issues:
- PaymentGatewayError (#12345)
- PaymentValidationFailed (#12346)
- ...
- PaymentTimeoutError (#12367)
批量更新中... ✓ 已更新 #12345 → released ✓ 已更新 #12346 → released ✓ 已更新 #12347 → released ... ✓ 已更新 #23个issues
更新摘要:
- 从 resolved → released: 23个
- 总耗时: 3.2秒
### Sentry MCP工具参考
**可用工具列表:**
| 工具名 | 功能 | 权限要求 |
|--------|------|----------|
| `list_projects` | 列出所有项目 | project:read |
| `list_issues` | 列出Issues | issue:read |
| `get_issue_details` | 获取Issue详情 | issue:read |
| `get_issue_events` | 获取Issue的事件列表 | event:read |
| `get_event_details` | 获取单个事件详情 | event:read |
| `create_issue_comment` | 添加评论 | issue:write |
| `update_issue_status` | 更新状态 | issue:write |
| `trigger_seer_ai` | 触发AI分析 | event:read |
| `search_issues` | 搜索Issues | issue:read |
| `get_error_tags` | 获取错误标签 | event:read |
| `add_event_tag` | 添加标签 | event:write |
| `get_stack_trace` | 获取堆栈跟踪 | event:read |
| `get_breadcrumbs` | 获取面包屑导航 | event:read |
| `get_user_context` | 获取用户上下文 | event:read |
| `get_request_data` | 获取请求数据 | event:read |
## Linear集成实战
### Linear MCP服务器功能
Linear是现代化的项目管理和Issue追踪工具,其MCP服务器提供了:
**核心能力:**
- 搜索和列出Issues
- 创建、更新Issues
- 管理项目和工作流
- 分配任务和设置优先级
- 添加评论和附件
- 管理标签和状态
### 配置Linear MCP
```bash
# 添加Linear MCP服务器
claude mcp add linear \
--url https://mcp.linear.app \
--transport http \
--scope user
首次授权:
$ claude
> 查看我的Linear任务
🔐 需要Linear授权
正在打开浏览器...
[Linear授权页面]
Claude Code 请求访问:
✓ 读取你的工作区
✓ 读取和创建Issues
✓ 读取和更新项目
✓ 读取你的团队信息
[Connect Workspace]
✓ Linear授权成功!
实际使用示例
示例1:从任务直接生成代码
用户: 显示分配给我的高优先级任务
Claude: [通过Linear MCP查询]
你的高优先级任务:
1. [FE-234] 实现用户头像上传功能
状态: In Progress
优先级: High
分配给: 你
预计工时: 4h
描述:
需要实现用户头像上传功能,包括:
- 支持JPG、PNG格式
- 文件大小限制5MB
- 前端裁剪为正方形
- 自动压缩为200x200
- 上传到S3存储
2. [FE-235] 优化首页加载性能
状态: Todo
优先级: Urgent
分配给: 你
预计工时: 6h
描述:
首页当前加载时间3.5秒,需要优化到1秒内。
────────────────────────────────────────
你想从哪个任务开始?
用户: 开始FE-234
Claude: [获取FE-234的详细信息]
正在读取任务详情...
任务:FE-234 - 实现用户头像上传功能
详细需求:
- 用户可以从个人设置页面上传头像
- 支持拖拽上传和点击选择文件
- 前端使用react-image-crop进行裁剪
- 后端接收上传并存储到S3
- 更新用户profile中的avatar_url字段
技术栈:
- 前端:React + react-image-crop
- 后端:Node.js + AWS SDK
- 存储:S3
- 数据库:PostgreSQL
我建议按以下步骤实现:
1. 创建前端AvatarUpload组件
2. 实现图片选择和裁剪功能
3. 创建后端API端点 /api/user/avatar
4. 实现S3上传逻辑
5. 更新数据库用户记录
开始实现第一步吗?
用户: 是的,开始
Claude: [创建AvatarUpload组件]
✓ 创建 src/components/AvatarUpload.jsx
✓ 添加react-image-crop依赖
✓ 实现文件选择功能
✓ 实现图片预览和裁剪
✓ 添加验证(格式、大小)
组件代码:
```jsx
import React, { useState, useRef } from 'react';
import ReactCrop from 'react-image-crop';
import 'react-image-crop/dist/ReactCrop.css';
export function AvatarUpload({ currentAvatar, onUploadSuccess }) {
const [crop, setCrop] = useState({ unit: '%', width: 100 });
const [src, setSrc] = useState(null);
// ... 完整实现
return (
<div className="avatar-upload">
{/* 上传界面 */}
</div>
);
}
需要继续实现后端API吗?
```text
用户: 继续
Claude: [创建后端API端点]
✓ 创建 src/api/user/avatar.js
✓ 实现multer文件中间件
✓ 配置S3上传
✓ 添加数据库更新逻辑
✓ 错误处理和验证
API端点:
POST /api/user/avatar
Content-Type: multipart/form-data
响应:
{
"success": true,
"avatarUrl": "https://s3.../avatar-123.jpg"
}
下一步:更新Linear任务状态?
用户: 是的,标记为已完成
Claude: [通过Linear MCP更新任务]
✓ FE-234状态已更新为 "Done"
✓ 添加了完成评论:
"实现了完整的头像上传功能:
- 前端React组件 + react-image-crop
- 后端Node.js API + S3存储
- 包含文件验证、裁剪、压缩
- 单元测试覆盖率85%"
任务完成摘要:
- 创建文件:4个
- 修改文件:3个
- 添加测试:2个测试文件
- 代码行数:+350行
是否要创建pull request?
示例2:批量创建任务
用户: 根据这份需求文档创建Linear任务
需求文档:
我们要重构用户认证系统,包括以下功能:
1. 实现OAuth2.0登录(Google、GitHub)
2. 添加双因素认证(2FA)
3. 实现密码重置流程
4. 添加会话管理
5. 实现单点登录(SSO)
Claude: [通过Linear MCP批量创建]
正在分析需求并创建任务...
✓ 创建 AUTH-101: 实现OAuth2.0登录
状态: Backlog
优先级: High
预计工时: 8h
标签: authentication, oauth
✓ 创建 AUTH-102: 添加双因素认证(2FA)
状态: Backlog
优先级: High
预计工时: 12h
标签: authentication, security
✓ 创建 AUTH-103: 实现密码重置流程
状态: Backlog
优先级: Medium
预计工时: 4h
标签: authentication
✓ 创建 AUTH-104: 添加会话管理
状态: Backlog
优先级: Medium
预计工时: 6h
标签: authentication
✓ 创建 AUTH-105: 实现单点登录(SSO)
状态: Backlog
优先级: Low
预计工时: 16h
标签: authentication, sso
创建完成!共5个任务
查看项目:https://linear.app/team/PROJECT/auth-refactor
Linear MCP工具参考
可用工具列表:
| 工具名 | 功能 | 权限要求 |
|---|---|---|
search_issues | 搜索Issues | read |
get_issue | 获取单个Issue详情 | read |
create_issue | 创建新Issue | write |
update_issue | 更新Issue | write |
list_projects | 列出项目 | read |
get_teams | 获取团队信息 | read |
add_comment | 添加评论 | write |
change_status | 更改状态 | write |
assign_issue | 分配任务 | write |
set_priority | 设置优先级 | write |
add_label | 添加标签 | write |