mcp: don't close session when keepalive ping returns method-not-found (#900)

Since ping is optional per the MCP spec, a peer that doesn't implement
it will respond with a -32601 (method not found) error. Previously,
startKeepalive treated all ping errors identically, closing the session
even when the peer was perfectly healthy.

Now, a method-not-found response stops the keepalive goroutine without
closing the session.

Fixes #897
This commit is contained in:
Maciej Kisiel 2026-04-22 16:35:27 +02:00 • committed by GitHub
commit b37e497f0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -14,6 +14,7 @@ package mcp
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
@ -605,6 +606,10 @@ func startKeepalive(session keepaliveSession, interval time.Duration, cancelPtr
err := session.Ping(pingCtx, nil)
pingCancel()
if err != nil {
if errors.Is(err, jsonrpc2.ErrMethodNotFound) {
// Peer doesn't support ping, stop the keepalive process.
return
}
// Ping failed; log it before closing the session so the
// failure is observable to operators. See #218.
logger.Error("keepalive ping failed; closing session", "error", err)