You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The [MCP Security](https://github.com/spring-ai-community/mcp-security) community library provides OAuth 2.0 authorization support for Spring AI MCP clients. It supports both `HttpClient`-based and `WebClient`-based (WebFlux) MCP clients used with the [Spring AI MCP Client Boot Starter](https://docs.spring.io/spring-ai/reference/2.0-SNAPSHOT/api/mcp/mcp-client-boot-starter-docs.html).
444
-
445
-
!!! note
446
-
This is a community project (`org.springaicommunity`), not officially part of the MCP Java SDK. It requires Spring AI 2.0.x (`mcp-client-security` version 0.1.x). For Spring AI 1.1.x, use version 0.0.6.
-**Authorization Code** — For user-present scenarios. The client sends requests with a bearer token on behalf of the user. Use `OAuth2AuthorizationCodeSyncHttpRequestCustomizer` (HttpClient) or `McpOAuth2AuthorizationCodeExchangeFilterFunction` (WebClient).
471
-
-**Client Credentials** — For machine-to-machine communication without a user in the loop. Use `OAuth2ClientCredentialsSyncHttpRequestCustomizer` (HttpClient) or `McpOAuth2ClientCredentialsExchangeFilterFunction` (WebClient).
472
-
-**Hybrid** — For mixed scenarios where some calls (e.g., `tools/list` on startup) use client credentials, while user-specific calls (e.g., `tools/call`) use authorization code tokens. Use `OAuth2HybridSyncHttpRequestCustomizer` (HttpClient) or `McpOAuth2HybridExchangeFilterFunction` (WebClient).
473
-
474
-
!!! tip
475
-
Use the **Hybrid** flow when Spring AI's autoconfiguration initializes MCP clients on startup (listing tools before a user is present), but tool calls are user-authenticated.
476
-
477
-
### Setup
478
-
479
-
Add the following to `application.properties`:
480
-
481
-
```properties
482
-
# Ensure MCP clients are sync
483
-
spring.ai.mcp.client.type=SYNC
484
-
# Disable auto-initialization (most MCP servers require authentication on every request)
When using `spring-ai-starter-mcp-client`, the transport is backed by the JDK's `HttpClient`. Expose a `McpSyncHttpClientRequestCustomizer` bean and an `AuthenticationMcpTransportContextProvider` on the client:
Replace `OAuth2AuthorizationCodeSyncHttpRequestCustomizer` with `OAuth2ClientCredentialsSyncHttpRequestCustomizer` or `OAuth2HybridSyncHttpRequestCustomizer` for the corresponding flow.
550
-
551
-
### WebClient-Based Client
552
-
553
-
When using `spring-ai-starter-mcp-client-webflux`, the transport is backed by Spring's reactive `WebClient`. Expose a `WebClient.Builder` bean configured with an `ExchangeFilterFunction`:
Replace `McpOAuth2AuthorizationCodeExchangeFilterFunction` with `McpOAuth2ClientCredentialsExchangeFilterFunction` or `McpOAuth2HybridExchangeFilterFunction` for the corresponding flow.
581
-
582
-
When using the chat client's `.stream()` method, Reactor does not preserve thread-locals. Inject the authentication into the Reactor context manually:
- Only `McpSyncClient` is supported; async clients are not.
595
-
- Set `spring.ai.mcp.client.initialized=false` when servers require authentication on every request, as Spring AI autoconfiguration will otherwise attempt to list tools on startup without a token.
596
-
- The SSE transport is supported by the client module (unlike the server module).
Clients can control the minimum logging level they receive through the `mcpClient.setLoggingLevel(level)` request. Messages below the set level will be filtered out.
757
757
Supported logging levels (in order of increasing severity): DEBUG (0), INFO (1), NOTICE (2), WARNING (3), ERROR (4), CRITICAL (5), ALERT (6), EMERGENCY (7)
758
758
759
-
## MCP Security
760
-
761
-
The [MCP Security](https://github.com/spring-ai-community/mcp-security) community library provides OAuth 2.0 resource server support and API key authentication for Spring AI MCP servers.
762
-
763
-
!!! note
764
-
This is a community project (`org.springaicommunity`), not officially part of the MCP Java SDK. It requires Spring AI 2.0.x (`mcp-server-security` version 0.1.x) and is compatible with **Spring WebMVC-based servers only**. For Spring AI 1.1.x, use version 0.0.6.
Clients send the key using the `X-API-Key: api01.mycustomapikey` header format (`<id>.<secret>`).
925
-
926
-
!!! warning
927
-
`InMemoryApiKeyEntityRepository` uses bcrypt for key hashing and is not suited for high-traffic production use. Provide a custom `ApiKeyEntityRepository` implementation for production deployments.
928
-
929
-
### Known Limitations
930
-
931
-
- The deprecated SSE transport is not supported. Use Streamable HTTP or Stateless transport.
932
-
- Spring WebFlux-based servers are not supported — WebMVC only.
933
-
- Opaque tokens are not supported. JWT is required for OAuth2.
934
-
935
759
## Error Handling
936
760
937
761
The SDK provides comprehensive error handling through the McpError class, covering protocol compatibility, transport communication, JSON-RPC messaging, tool execution, resource management, prompt handling, timeouts, and connection issues. This unified error handling approach ensures consistent and reliable error management across both synchronous and asynchronous operations.
0 commit comments