GH-4602: Add WebClientFactory customization + Restore ChatMemoryRepos… #4843
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
GH-4602: Add ability to customize WebClient per connection name + Restore ChatMemoryRepository AutoConfiguration classes
🧩 Summary
This PR introduces two main changes:
💡 Why These Changes
WebClientFactory
Previously, all NamedClientMcpTransport instances shared the same WebClient.Builder template, limiting customization of HTTP client settings (e.g., timeouts, SSL configurations, and base URLs).
By introducing a dedicated WebClientFactory, each connection can now define its own WebClient.Builder, providing finer-grained control over HTTP client behavior and connection-level configuration.
AutoConfiguration Restoration
The Cassandra and CosmosDB ChatMemoryRepository AutoConfiguration classes were accidentally deleted, but the
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsfiles still reference them. Without these classes, Spring Boot auto-configuration will fail at runtime with ClassNotFoundException.🔧 Changes Made
1. WebClientFactory Interface
auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/main/java/org/springframework/ai/mcp/client/common/autoconfigure/WebClientFactory.javaWebClient.builder()for convenience2. DefaultWebClientFactory Implementation
auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-webflux/src/main/java/org/springframework/ai/mcp/client/webflux/autoconfigure/DefaultWebClientFactory.java@Configuration(proxyBeanMethods = false)for performance optimization@ConditionalOnMissingBean(WebClientFactory.class)enables custom overridesWebClient.builder()as the default factory behavior3. Updated StreamableHttpWebFluxTransportAutoConfiguration
auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-webflux/src/main/java/org/springframework/ai/mcp/client/webflux/autoconfigure/StreamableHttpWebFluxTransportAutoConfiguration.javaObjectProvider<WebClient.Builder>withWebClientFactoryfor per-connection customizationwebClientFactory.create(connectionName).baseUrl(...)4. Added Dependency
auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/pom.xmlspring-webfluxdependency (optional) to ensure WebClient is available in the common module5. Restored CassandraChatMemoryRepositoryAutoConfiguration
auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-cassandra/src/main/java/org/springframework/ai/model/chat/memory/repository/cassandra/autoconfigure/CassandraChatMemoryRepositoryAutoConfiguration.javaMETA-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsCassandraChatMemoryRepositorybean with proper Spring Boot integration6. Restored CassandraChatMemoryRepositoryProperties
auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-cassandra/src/main/java/org/springframework/ai/model/chat/memory/repository/cassandra/autoconfigure/CassandraChatMemoryRepositoryProperties.javakeyspace,table,messagesColumn,timeToLive,initializeSchema7. Restored CosmosDBChatMemoryRepositoryAutoConfiguration
auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-cosmos-db/src/main/java/org/springframework/ai/model/chat/memory/repository/cosmosdb/autoconfigure/CosmosDBChatMemoryRepositoryAutoConfiguration.javaMETA-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsCosmosDBChatMemoryRepositorybean with Azure Cosmos DB client integration8. Restored CosmosDBChatMemoryRepositoryProperties
auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-cosmos-db/src/main/java/org/springframework/ai/model/chat/memory/repository/cosmosdb/autoconfigure/CosmosDBChatMemoryRepositoryProperties.javaendpoint,key,connectionMode,databaseName,containerName,partitionKeyPath9. Removed Obsolete Integration Test Files
10. Enhanced Tests
customWebClientFactoryIsUsed: verifies per-connection customizationcustomWebClientFactoryPerConnectionCustomization: verifies factory is called for each connectiondefaultWebClientFactoryReturnsBuilder: verifies default behaviorfallbackToDefaultFactoryWhenNoCustomFactoryProvided: verifies default factory fallbackcustomWebClientFactoryTakesPrecedenceOverDefault: ensures custom factory overrides default💻 Usage Examples
Default Behavior (No Changes Required)
The existing configuration continues to work without modification.
When no custom factory is defined, the DefaultWebClientFactory is automatically used.
Custom WebClientFactory Example
Cassandra ChatMemoryRepository (Auto-configured)
CosmosDB ChatMemoryRepository (Auto-configured)
🧪 Testing
All existing and new tests pass locally.
WebClientFactory Tests:
AutoConfiguration Tests:
AutoConfiguration.imports🔙 Backward Compatibility
✅ Fully backward compatible — existing configurations continue to work unchanged.
📘 Documentation
No user-facing documentation updates are required, as:
⚙️ Performance Considerations
@Configuration(proxyBeanMethods = false)ensures optimal performance for default configuration📊 Statistics
🧾 Related Issue
Closes #4602
✅ Checklist
🗒️ Additional Notes
WebClientFactory Implementation
This implementation follows the approach proposed by @maxxedev to use the
WebClientFactory.create(connectionName)pattern, providing a clean and extensible API for per-connection customization.AutoConfiguration Restoration
The restoration of Cassandra and CosmosDB AutoConfiguration classes was necessary because:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsfiles still reference these classesClassNotFoundExceptionThe restored classes follow the same patterns as the existing Neo4j and JDBC implementations, ensuring consistency across all repository implementations.
✅ Result