Skip to content

fix: Gate notifications on capabilities #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
16 changes: 12 additions & 4 deletions server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,12 @@ func (s *MCPServer) AddSessionTools(sessionID string, tools ...ServerTool) error

// It only makes sense to send tool notifications to initialized sessions --
// if we're not initialized yet the client can't possibly have sent their
// initial tools/list message
if session.Initialized() {
// initial tools/list message.
//
// For initialized sessions, honor tools.listChanged, which is specifically
// about whether notifications will be sent or not.
// see <https://modelcontextprotocol.io/specification/2025-03-26/server/tools#capabilities>
if session.Initialized() && s.capabilities.tools != nil && s.capabilities.tools.listChanged {
// Send notification only to this session
if err := s.SendNotificationToSpecificClient(sessionID, "notifications/tools/list_changed", nil); err != nil {
// Log the error but don't fail the operation
Expand Down Expand Up @@ -305,8 +309,12 @@ func (s *MCPServer) DeleteSessionTools(sessionID string, names ...string) error

// It only makes sense to send tool notifications to initialized sessions --
// if we're not initialized yet the client can't possibly have sent their
// initial tools/list message
if session.Initialized() {
// initial tools/list message.
//
// For initialized sessions, honor tools.listChanged, which is specifically
// about whether notifications will be sent or not.
// see <https://modelcontextprotocol.io/specification/2025-03-26/server/tools#capabilities>
if session.Initialized() && s.capabilities.tools != nil && s.capabilities.tools.listChanged {
// Send notification only to this session
if err := s.SendNotificationToSpecificClient(sessionID, "notifications/tools/list_changed", nil); err != nil {
// Log the error but don't fail the operation
Expand Down
59 changes: 59 additions & 0 deletions server/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,3 +858,62 @@ func TestMCPServer_SessionToolCapabilitiesBehavior(t *testing.T) {
})
}
}

func TestMCPServer_ToolNotificationsDisabled(t *testing.T) {
// This test verifies that when tool capabilities are disabled, we still
// add/delete tools correctly but don't send notifications about it.
//
// This is important because:
// 1. Tools should still work even if notifications are disabled
// 2. We shouldn't waste resources sending notifications that won't be used
// 3. The client might not be ready to handle tool notifications yet

// Create a server WITHOUT tool capabilities
server := NewMCPServer("test-server", "1.0.0", WithToolCapabilities(false))
ctx := context.Background()

// Create an initialized session
sessionChan := make(chan mcp.JSONRPCNotification, 1)
session := &sessionTestClientWithTools{
sessionID: "session-1",
notificationChannel: sessionChan,
initialized: true,
}

// Register the session
err := server.RegisterSession(ctx, session)
require.NoError(t, err)

// Add a tool
err = server.AddSessionTools(session.SessionID(),
ServerTool{Tool: mcp.NewTool("test-tool")},
)
require.NoError(t, err)

// Verify no notification was sent
select {
case <-sessionChan:
t.Error("Expected no notification to be sent when capabilities.tools.listChanged is false")
default:
// This is the expected case - no notification should be sent
}

// Verify tool was added to session
assert.Len(t, session.GetSessionTools(), 1)
assert.Contains(t, session.GetSessionTools(), "test-tool")

// Delete the tool
err = server.DeleteSessionTools(session.SessionID(), "test-tool")
require.NoError(t, err)

// Verify no notification was sent
select {
case <-sessionChan:
t.Error("Expected no notification to be sent when capabilities.tools.listChanged is false")
default:
// This is the expected case - no notification should be sent
}

// Verify tool was deleted from session
assert.Len(t, session.GetSessionTools(), 0)
}