mcp-go
mcp-go copied to clipboard
the SSE server will disconnect clients after a period of inactivity.
If the client doesn't reconnect, it will freeze/stall. It's a bug?
No, bro. In the file client/sse.go, the default timeout is 30 seconds
I also met this issue.
When I'm using the sse client of this package, (the server is also developed by this package),
after start and init the sse client, ListTools works well.
But if I do nothing and keep the client running, after several minites, I call ListTools again, it will freeze.
Then I cancel the context, and call it again, I get this error:
request failed with status 400: {"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"Invalid session ID"}}
OK, I know what happened. This is definitely a bug.
See the code here: https://github.com/mark3labs/mcp-go/blob/a0e968a752722d87063eb36ea0d55938e752f6dd/client/sse.go#L131-L176
the ctx with timeout is defined above the for loop, so the readSSE will always be finished after c.sseReadTimeout, which by default is 30 seconds. This is the reason why it works well after we start the client but will be dead after several minites (actually it is 30 seconds by default).
So this ctx should be moved into the for loop I think. After modified the code here, now it works well, and won't freeze any more.
If you want to get more details, then see the code here:
https://github.com/mark3labs/mcp-go/blob/a0e968a752722d87063eb36ea0d55938e752f6dd/client/sse.go#L309-L337
All operation will call this sendRequest, it will create a new chan which is used to receive the response, and send to the endpoint. At the end of this function, it will wait for the response from responseChan.
But our readSSE routine is already dead, so it will never get any thing from this chan, this is why it freeze.
I found https://github.com/mark3labs/mcp-go/pull/88, maybe this PR can fix this issue.
I found #88, maybe this PR can fix this issue.
I do not think so. This PR might be merged before v0.18.0. I use tag v0.18.0 but the SSE still keep 30s connection.
@Gavinapdo Noop, it is not merged, still opening. In tag v0.18.0, there's no changes here: https://github.com/mark3labs/mcp-go/blob/v0.18.0/client/sse.go#L131-L176
@Gavinapdo Noop, it is not merged, still opening. In tag v0.18.0, there's no changes here: https://github.com/mark3labs/mcp-go/blob/v0.18.0/client/sse.go#L131-L176
You're right. Now I'm trying to extend the WithSSEReadTimeout of NewSSEMCPClient() and context.WithTimeout of start(), and it works.
@askie can you hep me #126