mcp: don't break the streamable client connection for transient errors (#723)
When POST requests in the streamableClientConn return a transient error, return this error to the caller rather than permanently breaking the connection. This is achieved by using the special sentinel ErrRejected error to the jsonrpc2 layer. In doing so, the change revealed a pre-existing bug: ErrRejected had the same code as ErrConnectionClosing, and jsonrpc2.WireError implements errors.Is, so the two sentinel values could be conflated. This is fixed by using a new internal code. There's more to do for #683: we should also retry transient errors in handleSSE. For #683
This commit is contained in:
parent
e009bacfcb
commit
875d1d30c2
2 changed files with 191 additions and 31 deletions
|
|
@ -792,13 +792,9 @@ func (c *Connection) write(ctx context.Context, msg Message) error {
|
|||
err = c.writer.Write(ctx, msg)
|
||||
}
|
||||
|
||||
// For rejected requests, we don't set the writeErr (which would break the
|
||||
// connection). They can just be returned to the caller.
|
||||
if errors.Is(err, ErrRejected) {
|
||||
return err
|
||||
}
|
||||
|
||||
if err != nil && ctx.Err() == nil {
|
||||
// For cancelled or rejected requests, we don't set the writeErr (which would
|
||||
// break the connection). They can just be returned to the caller.
|
||||
if err != nil && ctx.Err() == nil && !errors.Is(err, ErrRejected) {
|
||||
// The call to Write failed, and since ctx.Err() is nil we can't attribute
|
||||
// the failure (even indirectly) to Context cancellation. The writer appears
|
||||
// to be broken, and future writes are likely to also fail.
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ var (
|
|||
// Such failures do not indicate that the connection is broken, but rather
|
||||
// should be returned to the caller to indicate that the specific request is
|
||||
// invalid in the current context.
|
||||
ErrRejected = NewError(-32004, "rejected by transport")
|
||||
ErrRejected = NewError(-32005, "rejected by transport")
|
||||
)
|
||||
|
||||
const wireVersion = "2.0"
|
||||
|
|
|
|||
Loading…
Reference in a new issue