mcp-go icon indicating copy to clipboard operation
mcp-go copied to clipboard

the SSE server will disconnect clients after a period of inactivity.

Open askie opened this issue 8 months ago • 8 comments

If the client doesn't reconnect, it will freeze/stall. It's a bug?

askie avatar Mar 28 '25 09:03 askie

No, bro. In the file client/sse.go, the default timeout is 30 seconds

Image

Image

Wood-Q avatar Mar 29 '25 13:03 Wood-Q

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"}}

yp05327 avatar Apr 06 '25 09:04 yp05327

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.

yp05327 avatar Apr 07 '25 14:04 yp05327

I found https://github.com/mark3labs/mcp-go/pull/88, maybe this PR can fix this issue.

yp05327 avatar Apr 07 '25 14:04 yp05327

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 avatar Apr 08 '25 08:04 Gavinapdo

@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

yp05327 avatar Apr 08 '25 08:04 yp05327

@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.

Gavinapdo avatar Apr 08 '25 09:04 Gavinapdo

@askie can you hep me #126

jkwang-a avatar Apr 09 '25 11:04 jkwang-a