Skip to content

Add initialize(InitializeRequest) overload to MCP client#1055

Draft
nquinquenel wants to merge 1 commit into
modelcontextprotocol:mainfrom
nquinquenel:feature/940-initialize-request-overload
Draft

Add initialize(InitializeRequest) overload to MCP client#1055
nquinquenel wants to merge 1 commit into
modelcontextprotocol:mainfrom
nquinquenel:feature/940-initialize-request-overload

Conversation

@nquinquenel

@nquinquenel nquinquenel commented Jul 6, 2026

Copy link
Copy Markdown

Summary

  • Add initialize(InitializeRequest) overloads to McpAsyncClient and McpSyncClient, consistent with callTool(CallToolRequest)
  • Thread caller-provided initialize requests through LifecycleInitializer, with client-side protocol version validation and defensive copying of _meta
  • Remember a successful custom initialize request and resend it when the transport session is re-established (e.g. after McpTransportSessionNotFoundException); clear stored request on client close
  • Document the overload, ordering constraint, and session-recovery behavior in docs/client.md

Design notes

This follows the approach agreed in #940 (not the closed builder-level initializeRequestMeta() approach from #1046).

Call initialize(InitializeRequest) before any other client method if custom metadata is required; lazy initialization triggered by methods like listTools() or callTool() uses the default request built from client builder settings.

Test plan

  • ./mvnw -pl mcp-core -Dtest=LifecycleInitializerTests,McpAsyncClientTest,McpSyncClientTest test
  • ./mvnw -pl mcp-core verify
  • Unit tests cover custom request propagation, defensive _meta copy, unsupported protocol version rejection, init reuse, re-init with stored custom request, default request on re-init when none stored, and clear on close / closeGracefully
  • Client wire tests cover async/sync paths via MockMcpClientTransport

Fixes #940

@nquinquenel nquinquenel marked this pull request as draft July 6, 2026 08:21
@nquinquenel nquinquenel force-pushed the feature/940-initialize-request-overload branch 2 times, most recently from aad0166 to b62d67b Compare July 6, 2026 08:58
@nquinquenel nquinquenel force-pushed the feature/940-initialize-request-overload branch from 117d168 to 3d063e3 Compare July 6, 2026 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

McpClient should allow passing _meta in InitializeRequest

1 participant