正常运转
要在本地运行此程序,请执行以下操作:
- 启动Cloudflare测试隧道:
cloudflared tunnel --url http://localhost:4000
- 将隧道主机复制到
@tunnel在……里面McpController. mix phx.server
这应该让一切准备就绪。然后转到https://claude.ai“管理 连接器”并添加一个自定义连接器。使用附带的Cloudflare URL "/mcp" 随便你怎么命名。之后点击“连接”,它应该 可用。你可以让它使用该工具,并给出“谁是最好的”这样的提示 名字以“blah”开头的人。
Screenshot of working Claude integration
远程MCP服务器认证实践
你在SaaS工作,高级领导说你必须把API包装起来 每个人都在谈论的新MCP。你几乎肯定读过 克劳德呕吐 规格 我真的不知道该怎么办。这意味着什么?有很多 SHOULDs、terms等,但具体来说,你只想添加你的 SaaS的工具面向流行的客户。此外 身份验证规范 有很多缺点,但实际上克劳德似乎并不是 跟随它。
本文档详细介绍了如何执行 远程MCP服务器,特别是与Claude web应用程序客户端。
我将接受任何想详细说明其他客户(在 特别是ChatGPT!)在实践中也实现了规范的客户端部分。
此repo也是一个使用此协议的Elixir应用程序。随时部署 它或在本地运行,ngrok或cloudflare隧道指向它。
克劳德
基本上,让我们看看实现这一目标需要什么:
Screenshot of adding an integration to Claude
假设您将集成放在那里,路径为 /mcp.第一件事 当您添加集成时,Anthropic将测试初始化 与它的联系:
POST /mcp HTTP/1.1
Accept: application/json, text/event-stream
Accept-Encoding: gzip
Content-Type: application/json
{
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {
"name": "claude-ai",
"version": "0.1.0"
}
},
"jsonrpc": "2.0",
"id": 0
}由于请求尚未通过身份验证,我们可以用401和 根据规范 一 告诉Claude在哪里找到oauth-protected-resources的特定标头 元数据。
HTTP/1.1 401 Unauthorized
Www-Authenticate: Bearer resource_metadata=https://garbage-actress-light-legislature.trycloudflare.com/.well-known/oauth-protected-resourceGET /mcp
Accept: text/event-stream我们可以用同样的401来回应。
此时,克劳德很满意,不会再发出任何请求,直到你 尝试连接:
Screenshot of connecting to an integration to Claude
根据此 图表
然后它应该打电话给那个人 oauth-protected-resource 端点 在401标头中返回,并查看 authorization_servers 要查找的字段 这 /.well-known/oauth-authorization-server 主机,按 规格. 然而,我无法让它这样做,相反,它总是只调用 我的集成所居住的主机上的路径。
GET /.well-known/oauth-authorization-server HTTP/1.1它希望收到以下三条数据的响应:
{
registration_endpoint: "https://some_host.com/register_oauth_client",
authorization_endpoint: "https://some_host.com/authorize_oauth_client",
token_endpoint: "https://some_host.com/oauth_token"
}然后,它将POST到该端点,通过OAuth DCR注册客户端。
POST /register_oauth_client HTTP/1.1
{
client_name: "claudeai",
grant_types: [
"authorization_code",
"refresh_token"
],
redirect_uris: [
"https://claude.ai/api/mcp/auth_callback"
],
response_types: [
"code"
],
scope: "claudeai",
token_endpoint_auth_method: "none"
}这应该按照 RFC 7591.注意 请求指定 token_endpoint_auth_method: "none",暗示它 不会发送 client_secret 关于后者 /token 电话。我尝试了各种 答案,如包括 client_secret 以及一个值 token_endpoint_auth_method 属于的 client_secret_post,但找不到克劳德 稍后再透露这个秘密。因此,我们应该将其视为公共客户, 而不是机密客户。后面的请求确实使用了PKCE。
我还尝试返回不同的范围,但它们被忽略了。克劳德坚持 使用自己 claudeai 范围。
所以我们只需要用一个 client_id.
HTTP/1.1 201 Created
{
"client_id": "public-client-id-for-claude"
}请注意,此时 Claude为给定的集成保存此客户端 领域。如果您使用web界面删除客户端,然后尝试添加 再次,它不会使 register 再次呼叫。
在这一点上,它是一个标准的3标签授权代码授予OAuth流,用于 使用PKCE的公共客户端。
Claude将用户的浏览器重定向到授权端点,并使用 state param,PKCE,以及 client_id 早些时候返回:
GET /authorize_oauth_client?....以下是包含的查询参数(为便于阅读,此处列出):
"client_id": "claude-ai-oauth-client"
"code_challenge": "
"
"code_challenge_method": "S256"
"redirect_uri": "https://claude.ai/api/mcp/auth_callback"
"response_type": "code"
"scope": "claudeai"
"state": ""我们进行授权并重定向回Claude的:
HTTP/1.1 302 Found
Location: https://claude.ai/api/mcp/auth_callback?state=&code=code-for-claude最后克劳德得到一个令牌:
POST /oauth_token HTTP/1.1
{
"client_id": "claude-ai-oauth-client",
"code": "code-for-claude",
"code_verifier": "PKCE Stuff",
"grant_type": "authorization_code",
"redirect_uri": "https://claude.ai/api/mcp/auth_callback"
}我们返回OAuth令牌:
HTTP/1.1 201 Created
{
"access_token": "access-token-for-claude",
"refresh_token": "refresh-token-for-claude",
"expires_in": 6000
}最后克劳德又试了一次 POST 走向整合之路 初始化,但这次包括它的令牌!
POST /mcp
Authorization: Bearer access-token-for-claude
{
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {
"name": "claude-ai",
"version": "0.1.0"
}
},
"jsonrpc": "2.0",
"id": 0
}现在似乎每个人都在谈论“工具”,所以我们可能想 对此做出回应。只是说说而已 "tools": {} 表示工具 能力。具体工具将来自后续请求。这 我们能够应对的能力是明确的 这里
HTTP 200 OK
Mcp-Session-Id: "some-session-id"
{
"jsonrpc": "2.0",
"id": 0,
"result": {
"protocolVersion": "2025-03-26",
"capabilities": {
"tools": {}
},
"serverInfo": {
"name": "My Test Server",
"version": "1.0.0"
}
}
}注: id 必须与请求匹配,并且 protocolVersion 必须匹配 (请注意,Claude web应用程序使用旧版本的协议)。
虽然规范上说你“可以”建立一个 Mcp-Session-Id,克劳德应用程序 需要它。如果你不提供,它就不会继续请求工具和其他 能力。
此外,无论你在这里用什么能力来回应,似乎克劳德 应用程序总是请求 tools/list, prompts/list,以及 resources/list.
克劳德很高兴,初始化并发送:
POST /mcp
{
"jsonrpc": "2.0",
"method": "notifications/initialized"
}接下来是获取工具的请求。
POST /mcp
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/list",
"params": {}
}{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [
{
"name": "Best Person Looker-upper",
"description": "Helps the agent identify who the best person is.",
"inputSchema": {
"type": "object",
"properties": {
"startsWith": {"type": "string"}
},
"required": ["startsWith"]
}
}
]
}
}工具架构为 这里.
请注意,克劳德似乎 _保存_ 将这些工具提供给给定的客户端(MCP主机/ 地址也许?).如果你稍后更改它们,当你重新连接时,它不会重新访问 他们。
