Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions client/transport/streamable_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,26 +573,24 @@ func (c *StreamableHTTP) SendNotification(ctx context.Context, notification mcp.
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted {
// Handle unauthorized error
if resp.StatusCode == http.StatusUnauthorized {
if c.oauthHandler != nil {
return &OAuthAuthorizationRequiredError{
Handler: c.oauthHandler,
}
switch resp.StatusCode {
case http.StatusOK, http.StatusAccepted, http.StatusNoContent:
return nil
case http.StatusUnauthorized:
if c.oauthHandler != nil {
return &OAuthAuthorizationRequiredError{
Handler: c.oauthHandler,
}
return ErrUnauthorized
}

return ErrUnauthorized
default:
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf(
"notification failed with status %d: %s",
resp.StatusCode,
body,
)
}

return nil
}

func (c *StreamableHTTP) SetNotificationHandler(handler func(mcp.JSONRPCNotification)) {
Expand Down
30 changes: 30 additions & 0 deletions client/transport/streamable_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,36 @@ func TestStreamableHTTP_SendNotification_Unauthorized_StaticToken(t *testing.T)
}
}

// TestStreamableHTTP_SendNotification_Accepts204NoContent verifies that SendNotification
// treats HTTP 204 No Content as a success response per RFC 7231.
// See: https://github.com/mark3labs/mcp-go/issues/700
func TestStreamableHTTP_SendNotification_Accepts204NoContent(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}))
defer server.Close()

transport, err := NewStreamableHTTP(server.URL)
if err != nil {
t.Fatalf("Failed to create StreamableHTTP: %v", err)
}

if err := transport.Start(context.Background()); err != nil {
t.Fatalf("Failed to start transport: %v", err)
}

err = transport.SendNotification(context.Background(), mcp.JSONRPCNotification{
JSONRPC: "2.0",
Notification: mcp.Notification{
Method: "notifications/initialized",
},
})

if err != nil {
t.Fatalf("SendNotification should accept 204 No Content, got error: %v", err)
}
}

// TestStreamableHTTPHostOverride tests Host header override for StreamableHTTP transport
func TestStreamableHTTPHostOverride(t *testing.T) {
// Create a test server that captures the Host header
Expand Down
Loading