Title: refactor: extract MCP client initialization logic into LifecycleInitializer by tzolov · Pull Request #370 · modelcontextprotocol/java-sdk · GitHub
Open Graph Title: refactor: extract MCP client initialization logic into LifecycleInitializer by tzolov · Pull Request #370 · modelcontextprotocol/java-sdk
X Title: refactor: extract MCP client initialization logic into LifecycleInitializer by tzolov · Pull Request #370 · modelcontextprotocol/java-sdk
Description: Create new LifecycleInitializer class to handle protocol initialization phase Move initialization logic from McpAsyncClient to dedicated initializer Add javadocs for MCP initialization process Implement protocol version negotiation and capability exchange Add exception handling for transport session recovery Include test suite for LifecycleInitializer Simplify McpAsyncClient by delegating initialization responsibilities Refactor MCP client initialization logic by extracting it from McpAsyncClient into a dedicated LifecycleInitializer class. This improves separation of concerns, maintainability, and testability of the initialization process. Motivation and Context The McpAsyncClient class was becoming too complex with initialization logic mixed in with client operations. This refactoring addresses several issues: Separation of Concerns: Initialization logic was tightly coupled with client operations, making the code harder to maintain and test Better Error Handling: Centralized exception handling for transport session recovery and re-initialization Improved Testability: Initialization logic can now be tested independently with comprehensive test coverage Cleaner API: The McpAsyncClient interface is now cleaner and focused on its primary responsibilities How Has This Been Tested? Comprehensive Unit Tests: Added 403 lines of tests in LifecycleInitializerTests.java covering: Successful initialization scenarios Concurrent initialization requests Protocol version negotiation Error handling and recovery Timeout scenarios Transport session exception handling Graceful shutdown procedures Existing Test Suite: All existing McpAsyncClient tests continue to pass, ensuring backward compatibility Integration Testing: Verified that the refactored code works with existing transport implementations (Stdio, SSE) Breaking Changes No breaking changes - This is an internal refactoring that maintains full API compatibility. All public methods of McpAsyncClient remain unchanged and behave identically. Types of changes Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) [] Breaking change (fix or feature that would cause existing functionality to change) Documentation update Note: While marked as potentially breaking, this is actually a non-breaking internal refactoring. Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed Additional context Implementation Notes: The LifecycleInitializer follows the MCP specification for initialization phase requirements Proper handling of McpTransportSessionNotFoundException with automatic re-initialization Comprehensive JavaDoc documentation explaining the MCP initialization protocol Thread-safe implementation supporting concurrent initialization requests Files Changed: New: LifecycleInitializer.java (348 lines) - Core initialization logic New: LifecycleInitializerTests.java (403 lines) - Comprehensive test suite Modified: McpAsyncClient.java - Simplified by delegating to LifecycleInitializer
Open Graph Description: Create new LifecycleInitializer class to handle protocol initialization phase Move initialization logic from McpAsyncClient to dedicated initializer Add javadocs for MCP initialization process Impl...
X Description: Create new LifecycleInitializer class to handle protocol initialization phase Move initialization logic from McpAsyncClient to dedicated initializer Add javadocs for MCP initialization process Impl...
Opengraph URL: https://github.com/modelcontextprotocol/java-sdk/pull/370
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:7ff59f8a-175f-3c7a-2e58-9b3da0b40aa7 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | EA0C:259088:9D882F6:D4F2B3D:6A5E97D8 |
| html-safe-nonce | cac5c8c065b0536909e4e04be1bc888127d39801efd8b77ce2db0383b4f5aa84 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTBDOjI1OTA4ODo5RDg4MkY2OkQ0RjJCM0Q6NkE1RTk3RDgiLCJ2aXNpdG9yX2lkIjoiODUzNTMyNTQyODk0MDcwNzgwMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bc0ef5be7cf3c29fc2e0cd7507fd5cf56be0f9f6b841f4733b90ad5ed9b76102 |
| hovercard-subject-tag | pull_request:2638503494 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/java-sdk/pull/370/files |
| twitter:image | https://avatars.githubusercontent.com/u/1351573?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1351573?s=400&v=4 |
| og:image:alt | Create new LifecycleInitializer class to handle protocol initialization phase Move initialization logic from McpAsyncClient to dedicated initializer Add javadocs for MCP initialization process Impl... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7c7e31acb6a895494e518b880f5ccf39604f7fa9a8f2f3c64145efc3b776256d |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/java-sdk git https://github.com/modelcontextprotocol/java-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 919609219 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/java-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 919609219 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/java-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2d2ac9bdd71d5f53f2b731c9330677e38624e301 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width