MCP与OAuth
此示例演示了如何使用OAuth2保护MCP服务器,如下所示 这 MCP规范.
入门
使用以下方式运行项目:
./mvnw spring-boot:run客户端凭据
通过调用获得令牌 /oauth2/token 端点:
curl -XPOST "http://localhost:8080/oauth2/token" \
--data grant_type=client_credentials \
--user "mcp-client:secret"
# And copy-paste the access token
# Or use JQ:
curl -XPOST "http://localhost:8080/oauth2/token" \
--data grant_type=client_credentials \
--user "mcp-client:secret" | jq -r ".access_token"使用浏览器流进行身份验证代码(PKCE)
通过首先调用authorize端点获取身份验证码
python3 pkce_generator.py然后通过调用 /oauth2/token 端点
python3 token_request.py使用MCP检查器
存储上一步生成的令牌,然后启动MCP检查器:
npx @modelcontextprotocol/inspector@0.6.0在MCP检查器中,粘贴您的令牌。点击连接,瞧!
请注意,令牌的有效期为15分钟
实施注意事项
依赖项
在Spring中,MCP服务器的OAuth2支持意味着添加:
- Spring Security (安全基础设施)
- Spring授权服务器 (发行代币)
- Spring安全:OAuth2资源服务器 (
使用令牌进行身份验证)
请注意,Spring Auth Server不支持反应式堆栈,因此仅在Servlet中发出令牌。
