返回市场
简单认证0

简单认证0

作者:CefBoud2 星标更新:2025-08-28

项目介绍

MCP Auth0 认证

此仓库是从 MCP Python SDK 的 simple-auth 示例中分叉出来的。

与原始示例不同,该示例使用自托管的授权服务器,而这个版本使用 Auth0 作为授权服务器。


第一步:设置 Auth0

  1. Auth0 注册。
  2. 启用 动态客户端注册
  3. 将连接提升到域级别(详情)。
    动态客户端是第三方应用程序,需要动态连接。
    首先,获取一个 管理令牌
  4. 将 MCP 服务器注册为 Auth0 中的 API,以便它可以作为生成的 JWT 中的有效 audience/resource 使用。
    URL 必须完全匹配,包括尾部斜杠(例如,http://localhost:8042/)。

第二步:启动资源服务器(MCP 服务器)

# 在端口 8042 上启动资源服务器,并连接到 Auth0 授权服务器
# 用你的 Auth0 域名替换 https://xxxxxxxxx.us.auth0.com/
uv run mcp-simple-auth0-rs --port=8042 --auth-server=https://xxxxxxxxx.us.auth0.com/ --transport=streamable-http

第三步:使用 Claude Desktop 测试

// 将 MCP 服务器添加到你的 `claude_desktop_config.json`
// Auth0 要求在 /authorize GET 请求中包含 `audience` 参数以返回 JWT 而不是 JWE。
// 参见:https://auth0.com/docs/secure/tokens/access-tokens/get-access-tokens#control-access-token-audience
// 我们使用 `pnpm dlx` 来运行修改版 `mcp-remote` 的特定 git 提交,
// 其中包括 `audience` 查询参数以确保返回 JWT。
// 或者你可以克隆仓库
// `git clone https://github.com/CefBoud/mcp-remote.git && cd mcp--remote && npm i && npm run build && npm link`
// 然后使用 `mcp-remote` 命令而不使用 pnpm。
"auth0-mcp": {
  "command": "pnpm",
  "args": [
    "dlx",
    "github:CefBoud/mcp-remote#8226c8b08cf281b782ccc0967f4664ec087f7269",
    "http://localhost:8042/mcp",
    "--resource",
    "http://localhost:8042/"
  ]
}

注意:Auth0 要求在 /authorize GET 请求中包含 audience 参数以发出 JWT 而不是 JWE。 上述 mcp-remote 版本包含了这种行为。

当你启动 Claude Desktop 时,会打开一个浏览器标签页提示进行授权:

alt text

授权后,你会被重定向到由 mcp-remote 启动的 localhost 服务器来兑换授权码以换取令牌:

alt text

你可以通过以下命令检查保存的令牌和挑战:

ls -lat ~/.mcp-auth/*/

工作原理

RFC 9728 发现

客户端 → 资源服务器:

curl http://localhost:8042/.well-known/oauth-protected-resource
{
  "resource": "http://localhost:8042/",
  "authorization_servers": [
    "https://xxxxxxxxx.us.auth0.com/"
  ],
  "scopes_supported": [],
  "bearer_methods_supported": [
    "header"
  ]
}

客户端 → 授权服务器:

curl https://xxxxxxxxx.us.auth0.com/.well-known/oauth-authorization-server
{
  "issuer": "https://xxxxxxxxx.us.auth0.com/",
  "authorization_endpoint": "https://xxxxxxxxx.us.auth0.com/authorize",
  "token_endpoint": "https://xxxxxxxxx.us.auth0.com/oauth/token",
  "device_authorization_endpoint": "https://xxxxxxxxx.us.auth0.com/oauth/device/code",
  "userinfo_endpoint": "https://xxxxxxxxx.us.auth0.com/userinfo",
  "mfa_challenge_endpoint": "https://xxxxxxxxx.us.auth0.com/mfa/challenge",
  "jwks_uri": "https://xxxxxxxxx.us.auth0.com/.well-known/jwks.json",
  "registration_endpoint": "https://xxxxxxxxx.us.auth0.com/oidc/register",
  "revocation_endpoint": "https://xxxxxxxxx.us.auth0.com/oauth/revoke"
  // ...
}

客户端根据 RFC 7591 动态注册应用,然后继续执行常规的 OAuth 流程。