The “SendNotificationToSpecificClient” interface quickly sends a disconnect notification to the client #746
Unanswered
f1025211271
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
hello, In streamable mode, I frequently send notifications to a client. When the client disconnects, how can I quickly detect that it has terminated—so I stop sending notifications immediately? Currently,
SendNotificationToSpecificClientonly fails when the notification chan becomes full, but by then the client may have already disconnected long ago, and I’m still sending unnecessarily.My code:
————————————————————
notifyOnce := func() error {
now := time.Now()
payload := map[string]any{
"stream_id": streamID, // 对应关系关键字段
"timestamp": now.Format(time.RFC3339Nano),
"unix": now.Unix(),
"session_id": sessionID, // 可选:便于调试
}
——————————
i see streamable_http.go
go func() {
for {
select {
case nt := <-session.notificationChannel:
select {
case writeChan <- &nt:
case <-done:
return
}
case samplingReq := <-session.samplingRequestChan:
// Send sampling request to client via SSE
jsonrpcRequest := mcp.JSONRPCRequest{
JSONRPC: "2.0",
ID: mcp.NewRequestId(samplingReq.requestID),
Request: mcp.Request{
Method: string(mcp.MethodSamplingCreateMessage),
},
Params: samplingReq.request.CreateMessageParams,
}
select {
case writeChan <- jsonrpcRequest:
case <-done:
return
}
case elicitationReq := <-session.elicitationRequestChan:
// Send elicitation request to client via SSE
jsonrpcRequest := mcp.JSONRPCRequest{
JSONRPC: "2.0",
ID: mcp.NewRequestId(elicitationReq.requestID),
Request: mcp.Request{
Method: string(mcp.MethodElicitationCreate),
},
Params: elicitationReq.request.Params,
}
select {
case writeChan <- jsonrpcRequest:
case <-done:
return
}
case rootsReq := <-session.rootsRequestChan:
// Send list roots request to client via SSE
jsonrpcRequest := mcp.JSONRPCRequest{
JSONRPC: "2.0",
ID: mcp.NewRequestId(rootsReq.requestID),
Request: mcp.Request{
Method: string(mcp.MethodListRoots),
},
}
select {
case writeChan <- jsonrpcRequest:
case <-done:
return
}
case <-done:
return //*** here Exited but without any cleanup******
}
}
}()
——————————————
Is there a way for me to quickly retrieve exceptions for this session?
All reactions