Skip to content

Streamablehttp throws exception 'Client failed to initialize by explicit API call' #433

@twypx

Description

@twypx

My MCP Server Code with python:

mcp = FastMCP(name="mcp_datetime")

@mcp.tool(name="get_datetime",description="""
This tool supports get current datetime.
""")
async def get_datetime()->TextContent:
    now = datetime.now()
    formatted = now.strftime("%Y-%m-%d %H:%M:%S")
    return TextContent(type="text",text=formatted)


if __name__ == '__main__':
    mcp.run(transport="streamable-http",host="0.0.0.0",port=8000,path="/mcp")

My client code is:

HttpClientStreamableHttpTransport transport = HttpClientStreamableHttpTransport.builder(some url )
                .build();

        // Create a synchronous McpClient


        try(McpSyncClient mcpClient = McpClient.sync(transport).requestTimeout(Duration.ofSeconds(10)).capabilities(McpSchema.ClientCapabilities.builder().sampling().build()).clientInfo(new McpSchema.Implementation("test","v1.0.0")).build()) {
            // Initialize the connection
            mcpClient.initialize();
            System.out.println("MCP client initialized.");

            // List available tools
            McpSchema.ListToolsResult tools = mcpClient.listTools();
            System.out.println("Available tools: " + tools.tools());

            // Call the "echo" tool
            Map<String, Object> params = Map.of("message", "Hello from Java over HTTP!");
            McpSchema.CallToolRequest callToolRequest = new McpSchema.CallToolRequest("get_datetime", params);
            McpSchema.CallToolResult result = mcpClient.callTool(callToolRequest);
            System.out.println("Tool call result: " + result.content());

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

The exception is:

Exception in thread "main" java.lang.RuntimeException: io.modelcontextprotocol.spec.McpError: Client failed to initialize by explicit API call
	at io.modelcontextprotocol.Demo.main(Demo.java:39)
Caused by: io.modelcontextprotocol.spec.McpError: Client failed to initialize by explicit API call
	at io.modelcontextprotocol.client.LifecycleInitializer.lambda$withIntitialization$2(LifecycleInitializer.java:289)
	at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:94)
	at reactor.core.publisher.SerializedSubscriber.onError(SerializedSubscriber.java:124)
	at reactor.core.publisher.SerializedSubscriber.onError(SerializedSubscriber.java:124)
	at reactor.core.publisher.FluxTimeout$TimeoutMainSubscriber.onError(FluxTimeout.java:219)
	at reactor.core.publisher.FluxMap$MapSubscriber.onError(FluxMap.java:134)
	at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:106)
	at reactor.core.publisher.Operators.error(Operators.java:198)
	at reactor.core.publisher.MonoError.subscribe(MonoError.java:53)
	at reactor.core.publisher.Mono.subscribe(Mono.java:4444)
	at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:103)
	at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onError(FluxPeekFuseable.java:234)
	at reactor.core.publisher.MonoFlatMap$FlatMapMain.onError(MonoFlatMap.java:180)
	at reactor.core.publisher.FluxHandle$HandleSubscriber.onError(FluxHandle.java:212)
	at reactor.core.publisher.SerializedSubscriber.onError(SerializedSubscriber.java:124)
	at reactor.core.publisher.SerializedSubscriber.onError(SerializedSubscriber.java:124)
	at reactor.core.publisher.FluxTimeout$TimeoutMainSubscriber.onError(FluxTimeout.java:219)
	at reactor.core.publisher.MonoCreate$DefaultMonoSink.error(MonoCreate.java:201)
	at io.modelcontextprotocol.spec.McpClientSession.lambda$sendRequest$9(McpClientSession.java:260)
	at reactor.core.publisher.LambdaMonoSubscriber.doError(LambdaMonoSubscriber.java:155)
	at reactor.core.publisher.LambdaMonoSubscriber.onError(LambdaMonoSubscriber.java:150)
	at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onError(FluxContextWrite.java:121)
	at reactor.core.publisher.MonoCreate$DefaultMonoSink.error(MonoCreate.java:201)
	at io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport.lambda$sendMessage$30(HttpClientStreamableHttpTransport.java:521)
	at reactor.core.publisher.FluxOnErrorReturn$ReturnSubscriber.onError(FluxOnErrorReturn.java:175)
	at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:106)
	at reactor.core.publisher.Operators.error(Operators.java:198)
	at reactor.core.publisher.FluxError.subscribe(FluxError.java:43)
	at reactor.core.publisher.Flux.subscribe(Flux.java:8660)
	at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:103)
	at reactor.core.publisher.FluxFlatMap$FlatMapMain.checkTerminated(FluxFlatMap.java:842)
	at reactor.core.publisher.FluxFlatMap$FlatMapMain.drainLoop(FluxFlatMap.java:608)
	at reactor.core.publisher.FluxFlatMap$FlatMapMain.drain(FluxFlatMap.java:588)
	at reactor.core.publisher.FluxFlatMap$FlatMapMain.onError(FluxFlatMap.java:451)
	at reactor.core.publisher.FluxFlatMap$FlatMapMain.checkTerminated(FluxFlatMap.java:842)
	at reactor.core.publisher.FluxFlatMap$FlatMapMain.drainLoop(FluxFlatMap.java:608)
	at reactor.core.publisher.FluxFlatMap$FlatMapMain.drain(FluxFlatMap.java:588)
	at reactor.core.publisher.FluxFlatMap$FlatMapMain.onError(FluxFlatMap.java:451)
	at reactor.core.publisher.FluxFlatMap$FlatMapMain.onNext(FluxFlatMap.java:416)
	at reactor.core.publisher.MonoFlatMapMany$FlatMapManyInner.onNext(MonoFlatMapMany.java:250)
	at reactor.core.publisher.FluxCreate$BufferAsyncSink.drain(FluxCreate.java:814)
	at reactor.core.publisher.FluxCreate$BufferAsyncSink.next(FluxCreate.java:739)
	at reactor.core.publisher.FluxCreate$SerializedFluxSink.next(FluxCreate.java:161)
	at io.modelcontextprotocol.client.transport.ResponseSubscribers$BodilessResponseLineSubscriber.hookOnComplete(ResponseSubscribers.java:287)
	at reactor.core.publisher.BaseSubscriber.onComplete(BaseSubscriber.java:197)
	at org.reactivestreams.FlowAdapters$FlowToReactiveSubscriber.onComplete(FlowAdapters.java:221)
	at java.net.http/jdk.internal.net.http.LineSubscriberAdapter$LineSubscription.loop(LineSubscriberAdapter.java:430)
	at java.net.http/jdk.internal.net.http.common.SequentialScheduler$LockingRestartableTask.run(SequentialScheduler.java:205)
	at java.net.http/jdk.internal.net.http.common.SequentialScheduler$CompleteRestartableTask.run(SequentialScheduler.java:149)
	at java.net.http/jdk.internal.net.http.common.SequentialScheduler$SchedulableTask.run(SequentialScheduler.java:230)
	at java.net.http/jdk.internal.net.http.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:303)
	at java.net.http/jdk.internal.net.http.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:256)
	at java.net.http/jdk.internal.net.http.LineSubscriberAdapter$LineSubscription.signalComplete(LineSubscriberAdapter.java:193)
	at java.net.http/jdk.internal.net.http.LineSubscriberAdapter.onComplete(LineSubscriberAdapter.java:114)
	at java.net.http/jdk.internal.net.http.Http1Response$Http1BodySubscriber.complete(Http1Response.java:339)
	at java.net.http/jdk.internal.net.http.Http1Response$Http1BodySubscriber.onComplete(Http1Response.java:390)
	at java.net.http/jdk.internal.net.http.ResponseContent$FixedLengthBodyParser.onSubscribe(ResponseContent.java:555)
	at java.net.http/jdk.internal.net.http.Http1Response$BodyReader.onSubscribe(Http1Response.java:773)
	at java.net.http/jdk.internal.net.http.Http1AsyncReceiver.handlePendingDelegate(Http1AsyncReceiver.java:395)
	at java.net.http/jdk.internal.net.http.Http1AsyncReceiver.flush(Http1AsyncReceiver.java:214)
	at java.net.http/jdk.internal.net.http.common.SequentialScheduler$LockingRestartableTask.run(SequentialScheduler.java:205)
	at java.net.http/jdk.internal.net.http.common.SequentialScheduler$CompleteRestartableTask.run(SequentialScheduler.java:149)
	at java.net.http/jdk.internal.net.http.common.SequentialScheduler$SchedulableTask.run(SequentialScheduler.java:230)
	at java.net.http/jdk.internal.net.http.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:303)
	at java.net.http/jdk.internal.net.http.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:256)
	at java.net.http/jdk.internal.net.http.Http1AsyncReceiver.subscribe(Http1AsyncReceiver.java:436)
	at java.net.http/jdk.internal.net.http.Http1Response.lambda$readBody$3(Http1Response.java:456)
	at java.net.http/jdk.internal.net.http.HttpClientImpl$DelegatingExecutor.execute(HttpClientImpl.java:158)
	at java.net.http/jdk.internal.net.http.Http1Response.readBody(Http1Response.java:422)
	at java.net.http/jdk.internal.net.http.Http1Exchange.readBodyAsync(Http1Exchange.java:376)
	at java.net.http/jdk.internal.net.http.Exchange.readBodyAsync(Exchange.java:191)
	at java.net.http/jdk.internal.net.http.MultiExchange.lambda$responseAsync0$4(MultiExchange.java:353)
	at java.base/java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:1150)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1773)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:840)
	Suppressed: java.lang.Exception: #block terminated with an error
		at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99)
		at reactor.core.publisher.Mono.block(Mono.java:1710)
		at io.modelcontextprotocol.client.McpSyncClient.initialize(McpSyncClient.java:180)
		at io.modelcontextprotocol.Demo.main(Demo.java:25)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions