MCP 认证
对 Casdoor MCP 服务器 /api/mcp 的请求采用与 Casdoor API 请求相同的认证方式。本页介绍客户端如何发现服务器所需的内容,以及每种认证方式如何影响客户端可以调用的工具。
OAuth发现
Casdoor 发布 OAuth 2.0 受保护资源元数据 (RFC 9728),以便 MCP 客户端可以找到哪个授权服务器保护 MCP 端点:
curl https://your-casdoor.com/.well-known/oauth-protected-resource
响应中指定了授权服务器:
{
"resource": "https://your-casdoor.com",
"authorization_servers": ["https://your-casdoor.com"],
"bearer_methods_supported": ["header"],
"scopes_supported": ["openid", "profile", "email", "read", "write"],
"resource_signing_alg_values_supported": ["RS256"]
}
要获取单个应用的元数据,请将应用名称放在路径中:
curl https://your-casdoor.com/.well-known/my-app/oauth-protected-resource
当不同应用有不同的授权要求时,请使用特定于应用的端点。
使用自有 issuer 的组织
上述元数据指定了全局 issuer,因此连接到 /api/mcp 的 MCP 客户端会在默认组织 built-in 中注册并为用户登录。当其他组织的用户使用 MCP 服务器时,请提供其组织的端点:
https://your-casdoor.com/api/mcp/orgs/<organization>
这是同一个 MCP 服务器,具有相同的工具。只有受保护资源元数据不同:未认证请求的挑战指向 /.well-known/oauth-protected-resource/api/mcp/orgs/<organization>,其中将组织的 issuer 指定为授权服务器:
{
"resource": "https://your-casdoor.com/api/mcp/orgs/acme",
"authorization_servers": ["https://your-casdoor.com/orgs/acme"],
"bearer_methods_supported": ["header"],
"scopes_supported": ["openid", "profile", "email", "read", "write"]
}
首先为该组织开启 Use organization issuer。如果不开启,授权服务器仍然是全局 issuer。随后,客户端读取该组织的发现文档,在该组织中注册,并在其登录页面为用户登录。
认证方式
| Method | Tools that the client can call | Use it for |
|---|---|---|
| Access token | The tools that the scopes of the token allow. See MCP authorization and scopes | Automation and MCP clients. This is the recommended method |
| Client ID and client secret | The tools that the application may use | Service accounts |
| Session cookie | All tools, without scope checks | Interactive use in the browser |
使用访问令牌:
curl -X POST https://your-casdoor.com/api/mcp \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
使用应用的客户端 ID 和客户端密钥:
curl -X POST https://your-casdoor.com/api/mcp \
-u "CLIENT_ID:CLIENT_SECRET" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
未认证请求
对于 Casdoor 不授权的 /api/mcp 请求(例如没有凭据的请求),Casdoor 会返回 HTTP 401 状态码以及以下标头:
WWW-Authenticate: Bearer realm="casdoor", resource_metadata="https://your-casdoor.com/.well-known/oauth-protected-resource", scope="openid profile email read write"
resource_metadata is the URL of the protected resource metadata on the host of the request, including the base path of Casdoor. The scheme is http when the host contains no dot, such as localhost:8000, and https otherwise. scope lists the scopes that the client should request. An MCP client that follows the OAuth 2.0 specification reads the metadata from that URL and starts the authorization flow on its own.
响应体取决于请求内容:
| Request | Response |
|---|---|
A JSON-RPC request with an id | HTTP 401 with a JSON-RPC error, whose data gives the reason |
没有 id 的 JSON-RPC 通知,或非 JSON-RPC 格式的请求体 | HTTP 401,响应体为空 |
使用 POST 以外的方法(如 GET)的请求会收到 HTTP 405 响应,带有 Allow: POST 头且无挑战信息,无论是否已认证:MCP 服务器不会通过 GET 打开 SSE 流。已登录的用户或无权执行操作的令牌会收到 HTTP 403 响应且无挑战信息,因为重新登录也无法解决问题。
例如:
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32001,
"message": "Unauthorized",
"data": "Unauthorized operation"
}
}
外部 MCP 服务器 的代理端点 /api/server/<organization>/<server-name> 以相同方式响应,但不同之处在于它接受 GET 请求,其挑战信息中没有 scope,且没有凭据的 GET 请求会收到 HTTP 401 响应,响应体为空。