Connect Claude to the Casdoor MCP server
本指南介绍如何将 Claude 连接到 Casdoor 的 MCP 服务器,或连接到位于 Casdoor MCP 网关后的 MCP 服务器。涵盖三种方式:
- 自定义连接器,适用于 Claude Desktop、claude.ai 和 Claude 移动应用。Claude 从 Anthropic 的云发起连接,因此 Casdoor 必须可从互联网访问。
- Claude Code,命令行代理,从您的计算机发起连接。
- 在
claude_desktop_config.json中使用mcp-remote,适用于仅您的计算机可访问的 Casdoor 实例。
学习目标
- 让 Claude 向 Casdoor 自我标识,无需手动创建应用。
- 以适合您网络的方式将 MCP 服务器添加到 Claude。
- 登录并检查 Claude 是否可以调用工具。
所需条件
- 一个运行中的 Casdoor 实例。在生产环境中请使用 HTTPS。
- Claude Desktop、claude.ai 账户或 Claude Code
- Global administrator access to the Casdoor admin console
在 Casdoor 中接受 Claude
Claude 通过客户端 ID 元数据文档 (CIMD) 进行自我标识:其客户端 ID 是 Anthropic 发布的文档的 URL,Casdoor 读取该文档而不是要求 Claude 注册。当授权服务器不支持 CIMD 时,Claude 会回退到动态客户端注册 (DCR)。这两者由同一个开关控制:
- 在 Casdoor 管理控制台,前往 User Management > Organizations 并打开用户登录 Claude 所在的组织:如果是 Casdoor MCP 服务器,则为
built-in;如果是位于 MCP 网关后的服务器,则为该服务器所属的组织。 - 开启 Enable dynamic client registration。
- 保存组织。
此时,该组织的发现文档将声明 client_id_metadata_document_supported: true,并在 token_endpoint_auth_methods_supported 中列出 none,这是 Claude 在使用 CIMD 之前检查的两个值。
| Claude | 客户端 ID | 重定向 URI |
|---|---|---|
| Claude Desktop, claude.ai, 移动端 | https://claude.ai/oauth/mcp-oauth-client-metadata | https://claude.ai/api/mcp/auth_callback |
| Claude Code | https://claude.ai/oauth/claude-code-client-metadata | http://localhost:<port>/callback |
首次登录时,Casdoor 会为每个客户端 ID 创建一个应用,名称以 cimd_ 开头并带有标签 cimd。所有组织共享这一个应用,每个用户登录到其自己的组织。参见 接受通过元数据文档 URL 标识的客户端。
选项 1:添加自定义连接器
Claude Desktop、claude.ai 和移动应用共享连接器。Claude 从 Anthropic 云的地址范围 160.79.104.0/21 连接到 MCP 服务器和 Casdoor,因此两者都必须通过 HTTPS 可从互联网访问。有关当前步骤,请参阅 Claude 帮助中心中的 Get started with custom connectors。
Free、Pro 和 Max 计划:
- 在 Claude 中,前往 Customize > Connectors,然后选择 Add custom connector。
- 输入名称(例如
Casdoor)和 MCP 服务器的 URL:https://your-casdoor.com/api/mcp用于 Casdoor 的 MCP 服务器https://your-casdoor.com/api/server/<organization>/<server-name>用于 MCP 网关后面的服务器
- 在 OAuth 客户端选项中,保持 Use Claude's published identity (Recommended) 选中。
- 选择 Add,然后选择 Connect。
Team and Enterprise plans: 所有者在 Organization settings > Connectors > Add > Custom > Web 中使用相同的 URL 添加连接器,每个成员在自己的 Connectors 设置中选择 Connect。
当你选择 Connect 时,Claude 会打开 Casdoor 登录页面:
- 使用你的账户登录。
- 如果 Casdoor 显示同意屏幕,请点击 Allow。
- Casdoor 会将你重定向回 Claude。连接器显示为已连接。
你无法稍后更改连接器的认证方式。如果要切换到另一个 OAuth 客户端,请删除该连接器并重新添加。
使用你自己创建的应用
如果你不想启用动态客户端注册,请自行创建客户端:
- 在 Casdoor 管理控制台中,前往 Identity > Applications,并在用户所属的组织中添加一个应用,例如
claude-mcp。 - 将
https://claude.ai/api/mcp/auth_callback添加到 Redirect URLs。 - 保存应用并复制其 Client ID 和 Client Secret。
- 在 Add custom connector 对话框中,选择 Use your own OAuth client 并输入这些凭据。
选项 2:Claude Code
Claude Code 在你的计算机上运行,因此 Casdoor 只需从该计算机可达即可。
-
添加服务器:
claude mcp add --transport http casdoor https://your-casdoor.com/api/mcp对于位于 MCP 网关后的服务器,请使用网关 URL。添加
--scope project以通过.mcp.json与团队共享服务器,或添加--scope user以在所有项目中共享。 -
启动 Claude Code,运行
/mcp,选择服务器,然后选择 Authenticate。浏览器将打开 Casdoor 登录页面。
Claude Code 监听随机端口以接收回调。其文档列出了 http://localhost/callback 和 http://127.0.0.1/callback,而 Casdoor 匹配任意端口的回环重定向 URI,正如 RFC 8252 所要求的那样。要注销,请运行 claude mcp logout casdoor,或在 /mcp 中选择 Clear authentication。
要使用你自己创建的应用,请传入其凭据并固定端口,以便已知重定向 URI:
claude mcp add --transport http --client-id <client-id> --client-secret --callback-port 8080 casdoor https://your-casdoor.com/api/mcp
将 http://localhost:8080/callback 添加到应用的 Redirect URLs。参见 Connect Claude Code to tools via MCP。
选项 3:claude_desktop_config.json 和 mcp-remote
当 Casdoor 位于你的本地网络或计算机上时,Anthropic 的云端无法访问它。请在你的计算机上使用 mcp-remote 包运行连接,该包通过标准输入输出桥接 Claude Desktop 到远程服务器,并使用 DCR 执行 OAuth 2.0 流程。它需要 Node.js,其中提供了 npx。
-
在文本编辑器中打开 Claude Desktop 的配置文件:
Operating system Path macOS ~/Library/Application Support/Claude/claude_desktop_config.jsonWindows %APPDATA%\Claude\claude_desktop_config.jsonLinux ~/.config/Claude/claude_desktop_config.json -
添加服务器:
{
"mcpServers": {
"casdoor": {
"command": "npx",
"args": [
"mcp-remote",
"https://your-casdoor.com/api/mcp"
]
}
}
} -
Quit Claude Desktop completely, not only its window, and start it again.
mcp-remoteopens your browser for the sign-in.
mcp-remote 注册一个名称以 dcr_ 开头的应用,并在 http://localhost:3334/oauth/callback 监听回调。它将令牌存储在你的计算机上。
验证结果
让 Claude 使用服务器,例如:
- "列出 Casdoor 中的所有应用。"
- "向我展示名为
my-app的应用。" - "在组织
my-org中创建一个名为test-app的应用。"
Claude 调用 MCP 工具并使用来自服务器的数据进行回答。在 Casdoor 管理控制台中,Tokens 页面显示了你的用户在 cimd_ 或 dcr_ 应用中的令牌。
故障排除
Claude 提示无法连接到服务器
- 检查 URL 是否以
/api/mcp结尾,或者是网关 URL/api/server/<organization>/<server-name>。 - 对于自定义连接器,请检查 Casdoor 是否可以通过互联网访问且拥有有效的证书,并确保没有防火墙阻止
160.79.104.0/21。对于私有网络上的 Casdoor 实例,请使用 Claude Code 或mcp-remote。 - 自己调用服务器:
curl -i -X POST https://your-casdoor.com/api/mcp。Casdoor 返回 HTTP 401 和一个WWW-Authenticate标头,其中指定了受保护资源元数据,这正是触发 Claude 中登录流程的内容。
Casdoor 拒绝客户端 ID 元数据文档
错误 "client ID metadata documents are not accepted" 意味着组织的 Enable dynamic client registration 处于关闭状态。参见 Accept Claude in Casdoor。
Casdoor 报告重定向 URL 不匹配
对于你自己创建的应用,请检查 Redirect URLs 是否包含客户端的确切回调地址:连接器使用 https://claude.ai/api/mcp/auth_callback,带有 --callback-port 的 Claude Code 使用 http://localhost:<port>/callback。对于 mcp-remote,如果端口 3334 被占用,它会选择另一个端口;释放端口,删除 dcr_ 应用以及 ~/.mcp-auth 中 mcp-remote 缓存的凭据,然后重新连接。
工具调用因 insufficient_scope 而失败
The token lacks the scope that the tool requires. See MCP authorization and scopes.
保护连接安全
- HTTPS: 在生产环境中通过 HTTPS 提供服务,以保护 OAuth 2.0 流程。
- Least privilege: 使用仅拥有 Claude 所需权限的账户登录。
- Revocation: 要切断客户端,请在 Casdoor 管理控制台的 Tokens 页面上删除其令牌。
- Enterprise: 使用企业身份提供商,Claude Team 和 Enterprise 可以在无需浏览器登录的情况下获取令牌。参见 Enterprise-managed authorization。