跳到主内容

MCP 故障排除

本页涵盖 Casdoor 内置 MCP 服务器的问题,以及使用 Casdoor 作为其 OAuth 2.0 提供商的自有 MCP 服务器的问题。

常见问题​

MCP 服务器返回 HTTP 401​

原因

  • 访问令牌已过期。
  • 令牌的 aud(受众)声明与 MCP 服务器的资源 URI 不匹配。
  • 请求中没有 Authorization: Bearer 标头。

解决方案

  1. 解码令牌,例如在 jwt.io 上,并检查 exp 声明。
  2. 检查 aud 声明是否完全等于您的 MCP 服务器的 URI,包括协议、主机和端口。
  3. 检查请求是否携带了 Authorization: Bearer <access-token> 标头。
  4. 如果您的客户端在 OAuth 2.0 流程中发送了 resource 参数,请检查它是否为 MCP 服务器的 URI。
# Example: Check token claims
curl -X GET https://your-mcp-server.com/api/mcp \
-H "Authorization: Bearer YOUR_TOKEN"

# If 401, inspect token at jwt.io:
# - exp: 1735689600 (must be in future)
# - aud: "https://your-mcp-server.com" (must match server URI)
# - scope: "read:application" (must include required scopes)

浏览器报告 CORS 错误​

原因

MCP 客户端的来源不是 Casdoor 信任的来源。Casdoor 允许来自您应用的 Redirect URLs 中的来源的跨域请求。

解决方案

  1. 在 Casdoor 管理控制台中,打开该应用的编辑页面。
  2. 将包含协议和非标准端口的 MCP 客户端来源 URL 添加到 Redirect URLs。对于本地开发,添加 http://localhost:<port>。

请参阅 从浏览器调用 API。

重定向 URL 不匹配​

原因

  • 授权请求的 redirect_uri 不在应用的 Redirect URLs 中。
  • 协议(http 或 https)、端口或末尾斜杠存在差异。

解决方案

  1. 从授权请求中复制确切的 redirect_uri。
  2. 将其添加到应用的 Redirect URLs 中。

以下 URL 都是不同的:

# These are all different redirect URIs:
http://localhost:3000/callback
https://localhost:3000/callback # Different scheme
http://localhost:3000/callback/ # Trailing slash
http://localhost:3001/callback # Different port

发现端点返回 HTTP 404​

原因

客户端请求的发现路径与其角色不符。

解决方案

为您的用例请求相应的端点:

  1. OAuth 2.0 授权服务器元数据 (RFC 8414):

    curl https://your-casdoor.com/.well-known/oauth-authorization-server
  2. OpenID Connect 发现:

    curl https://your-casdoor.com/.well-known/openid-configuration
  3. OAuth 2.0 受保护资源元数据 (RFC 9728):

    curl https://your-casdoor.com/.well-known/oauth-protected-resource

作为资源服务器的 MCP 服务器发布 oauth-protected-resource 文档以通告其授权服务器。

动态客户端注册被拒绝​

原因

  • 该组织未启用动态客户端注册 (DCR)。
  • 注册请求缺少必填字段。

解决方案

  1. 在 Casdoor 管理控制台中,打开组织的编辑页面并启用 Enable dynamic client registration。除非注册 URL 包含 organization 参数,否则客户端将在 built-in 组织中注册。

  2. 在注册请求中发送所有必需的元数据:

    {
    "client_name": "My MCP Client",
    "redirect_uris": ["https://client.example.com/callback"],
    "grant_types": ["authorization_code"],
    "token_endpoint_auth_method": "client_secret_basic"
    }

请参阅 动态客户端注册。

原因

  • 应用没有自定义范围。Casdoor 仅针对自定义范围请求用户同意。
  • 客户端未请求自定义范围。
  • 用户已授予或拒绝该应用所请求的范围。Casdoor 不会再次询问,且令牌仅包含已授予的范围。

解决方案

  1. 检查应用是否定义了这些范围,以及客户端是否请求了它们。请参阅 自定义范围。
  2. 若要再次测试同意屏幕,请在 Consents 页面上撤销之前的授权。

工具调用因 insufficient_scope 失败​

原因

访问令牌缺少工具所需的范围。

解决方案

  1. 从错误中读取 required_scope 和 granted_scopes:

    {
    "error": {
    "code": -32001,
    "message": "insufficient_scope",
    "data": {
    "tool": "add_application",
    "granted_scopes": ["read:application"],
    "required_scope": "write:application"
    }
    }
    }
  2. 使用所需范围请求新令牌:

    curl -X POST https://your-casdoor.com/api/login/oauth/access_token \
    -d "grant_type=client_credentials" \
    -d "client_id=YOUR_CLIENT_ID" \
    -d "client_secret=YOUR_CLIENT_SECRET" \
    -d "scope=read:application write:application"

请参阅 范围参考。

授权请求因 invalid_target 失败​

原因

  • resource 参数不是绝对 URI,不符合 RFC 8707 的要求。
  • 资源 URI 缺少 scheme。

解决方案

  1. 发送完整的 URL 作为 resource:

    # Correct:
    resource=https://your-server.com
    resource=https://api.example.com:8080

    # Incorrect:
    resource=your-server.com # Missing scheme
    resource=localhost:3000 # Missing scheme
  2. 请记住,resource 的值将成为访问令牌的 aud 声明。它必须与 MCP 服务器期望的受众完全一致。

Claude Desktop 或 Cursor 无法连接到您自己的 MCP 服务器​

原因

  • 您的 MCP 服务器未返回有效的受保护资源元数据。
  • WWW-Authenticate 标头缺失或格式错误。
  • 客户端无法访问发现端点。
  • 您的 MCP 服务器未能验证令牌。

解决方案

  1. 检查元数据端点是否返回有效的 JSON:

    curl https://your-mcp-server.com/.well-known/oauth-protected-resource

    预期响应:

    {
    "resource": "https://your-mcp-server.com",
    "authorization_servers": [
    "https://your-casdoor.com"
    ]
    }
  2. 检查未认证请求的 WWW-Authenticate 标头:

    curl -v https://your-mcp-server.com/api/mcp

    预期标头:

    WWW-Authenticate: Bearer realm="mcp-server",
    authorization_uri="https://your-casdoor.com/login/oauth/authorize",
    scope="read:application write:application"
  3. 手动运行 OAuth 2.0 流程:

    # 1. Get authorization code (requires browser)
    # 2. Exchange for token
    curl -X POST https://your-casdoor.com/api/login/oauth/access_token \
    -d "grant_type=authorization_code" \
    -d "code=AUTH_CODE" \
    -d "redirect_uri=YOUR_REDIRECT_URI" \
    -d "client_id=YOUR_CLIENT_ID" \
    -d "client_secret=YOUR_CLIENT_SECRET"

    # 3. Test MCP endpoint with token
    curl https://your-mcp-server.com/api/mcp \
    -H "Authorization: Bearer ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'

调试工具​

MCP检查器​

官方 MCP Inspector 可交互式地测试 MCP 连接。它可以浏览并调用工具,显示 JSON-RPC 请求和响应,并运行 OAuth 2.0 流程。

npx @modelcontextprotocol/inspector

发现端点​

检查每个发现端点是否返回有效的 JSON:

# OAuth Authorization Server
curl -s https://your-casdoor.com/.well-known/oauth-authorization-server | jq

# OIDC Configuration
curl -s https://your-casdoor.com/.well-known/openid-configuration | jq

# Protected Resource (for MCP resource servers)
curl -s https://your-mcp-server.com/.well-known/oauth-protected-resource | jq

令牌声明​

在 jwt.io 上解码访问令牌,或在命令行中操作:

# Alternative: Decode token with jq
echo "YOUR_JWT_TOKEN" | cut -d. -f2 | base64 -d | jq
ClaimCheck
audMatches the URI of the MCP server
scopeContains the scopes that the tools require
expIs in the future. The value is a Unix timestamp
issIs the URL of Casdoor
subIs the ID of the user
client_idIs the application that received the token

令牌自省​

询问 Casdoor 令牌是否有效及其内容(RFC 7662):

curl -X POST https://your-casdoor.com/api/login/oauth/introspect \
-H "Content-Type: application/x-www-form-urlencoded" \
-u "CLIENT_ID:CLIENT_SECRET" \
-d "token=ACCESS_TOKEN"

响应如下:

{
"active": true,
"scope": "read:application write:application",
"client_id": "your-client-id",
"username": "admin",
"exp": 1735689600,
"iat": 1735603200,
"aud": "https://your-mcp-server.com"
}

使用 curl 进行 OAuth 2.0 流程​

# 1. Test authorization endpoint (requires browser)
# Open in browser:
https://your-casdoor.com/login/oauth/authorize?
client_id=YOUR_CLIENT_ID&
redirect_uri=http://localhost:8080/callback&
response_type=code&
scope=read:application&
state=random-state

# 2. Exchange authorization code for token
curl -X POST https://your-casdoor.com/api/login/oauth/access_token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code" \
-d "code=AUTHORIZATION_CODE" \
-d "redirect_uri=http://localhost:8080/callback" \
-d "client_id=YOUR_CLIENT_ID" \
-d "client_secret=YOUR_CLIENT_SECRET"

# 3. Use access token with MCP server
curl https://your-mcp-server.com/api/mcp \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'

日志和网络跟踪​

  • MCP client:开启调试或详细模式,读取日志中的重定向、令牌交换和工具调用信息。

  • MCP server:记录每个传入请求、令牌验证的结果及失败原因,以及范围检查情况。

  • Browser:打开开发者工具。网络选项卡显示重定向和 API 调用,控制台显示 JavaScript 错误。

  • CLI clients:通过 mitmproxy 路由客户端以查看其请求:

    mitmproxy -p 8080
    # Configure client to use proxy: http://localhost:8080

另请参阅​