mcp认证go sdk
钥匙斗篷
看 Keycloak MCP支持。使用Keycloak v26.2.1进行测试。
$ podman run -d \
--name keycloak-http \
-p 8090:8080 \
-e KC_BOOTSTRAP_ADMIN_USERNAME=admin \
-e KC_BOOTSTRAP_ADMIN_PASSWORD=admin \
quay.io/keycloak/keycloak:latest start-dev创建领域、客户端、用户、作用域、角色、映射:
$ uv run --with python-keycloak setup-keycloak.pyMCP服务器
$ go run .MCP客户端
使用 这 MCP客户端:
$ CLIENT_ID=mcp-test-client CLIENT_SECRET=secret ./mcp-cli --server-url http://localhost:7777/mcp
Connecting to MCP server...
Please open the following URL in your browser: http://localhost:8090/realms/mcp-realm/protocol/openid-connect/auth?client_id=mcp-test-client&code_challenge=ESpw0QNnZzJ0J56mPTM9v4S9Ia7YrPQuuVCSxFJLa0g&code_challenge_method=S256&redirect_uri=http%3A%2F%2Flocalhost%3A3142&resource=http%3A%2F%2Flocalhost%3A7777%2Fmcp&response_type=code&scope=mcp%3Atools%3Aread+mcp%3Atools%3Awrite&state=3FOF7URSAHKGTQDCXV3HWAKQEU
Connected to MCP server
Interactive MCP Client
Commands:
list - List available tools
call [args] - Call a tool
quit - Exit the client
mcp> list
Available tools:
1. echo
echo input back
2. to_upper
returns the input string in uppercase
mcp>
mcp> call echo {"input": "fasdfasdf"}
Tool 'echo' result:
fasdfasdfMCP检查员
这个工具在使用和使用oauth时非常挑剔。 将0.16.7用作0.16.8 破碎的.
在Keycloak UI中,导航到mcp测试客户端并将“Web Origins”设置为 * 以及“有效重定向URI”到 http://localhost:6274/oauth/callback* -这些是mcp检查员需要的。
$ npx @modelcontextprotocol/inspector@0.16.7打开 Authentication 在左侧的下拉菜单中设置:
Transport Type: `Streamable HTTP`
URL: `http://localhost:7777/mcp`
Client ID: `mcp-test-client`
Scope: `mcp:tools:read mcp:tools:write`
Bearer Token: make sure this is really empty. Select all the `*`s, press delete and press escape.压力 Connect 浏览器应弹出一个表单以通过Keycloak进行身份验证。用户 mcp-user/user123 或 mcp-admin/admin123.
故障排除
如果出现问题,请使用检查器中“打开授权设置”中的“清除OAuth状态”。 在浏览器控制台和Keycloak日志中查找错误: podman logs keycloak-http.
$ curl -sv http://localhost:7777/mcp 2>&1 | grep -i auth
< HTTP/1.1 401 Unauthorized
< Www-Authenticate: Bearer resource_metadata=http://localhost:7777/.well-known/oauth-protected-resource/mcp$ curl -s http://localhost:7777/.well-known/oauth-protected-resource/mcp | jq .
{
"resource": "http://localhost:7777/mcp",
"authorization_servers": [
"http://localhost:8090/realms/mcp-realm"
],
"jwks_uri": "http://localhost:8090/realms/mcp-realm/protocol/openid-connect/certs",
"scopes_supported": [
"mcp:tools:read",
"mcp:tools:write"
],
"bearer_methods_supported": [
"header"
]
}使用tcpflow查看流量:
# tcpflow -c -i lo port 7777使用 https://www.jwt.io/ 以解码JWT。
