Skip to content

Commit c0f4403

Browse files
committed
distinguish routing 404
1 parent 8d3f236 commit c0f4403

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

client/transport/streamable_http.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ func (c *StreamableHTTP) SendRequest(
183183

184184
resp, err := c.sendHTTP(ctx, http.MethodPost, bytes.NewReader(requestBody), "application/json, text/event-stream")
185185
if err != nil {
186-
return nil, fmt.Errorf("failed to send request: %w", err)
186+
if errors.Is(err, errSessionTerminated) && request.Method == string(mcp.MethodInitialize) {
187+
// If the request is initialize, should not return a SessionTerminated error
188+
// It should be a genuine endpoint-routing issue.
189+
// ( Fall through to return StatusCode checking. )
190+
} else {
191+
return nil, fmt.Errorf("failed to send request: %w", err)
192+
}
187193
}
188194
defer resp.Body.Close()
189195

@@ -285,7 +291,7 @@ func (c *StreamableHTTP) sendHTTP(
285291
// universal handling for session terminated
286292
if resp.StatusCode == http.StatusNotFound {
287293
c.sessionID.CompareAndSwap(sessionID, "")
288-
return nil, fmt.Errorf("session terminated (404). need to re-initialize")
294+
return nil, errSessionTerminated
289295
}
290296

291297
return resp, nil
@@ -460,8 +466,10 @@ func (c *StreamableHTTP) listenForever() {
460466
}
461467

462468
var (
469+
errSessionTerminated = fmt.Errorf("session terminated (404). need to re-initialize")
463470
errGetMethodNotAllowed = fmt.Errorf("GET method not allowed")
464-
retryInterval = 1 * time.Second // a variable is convenient for testing
471+
472+
retryInterval = 1 * time.Second // a variable is convenient for testing
465473
)
466474

467475
func (c *StreamableHTTP) createGETConnectionToServer() error {

0 commit comments

Comments
 (0)