Skip to content

Commit a7b38b8

Browse files
committed
chore(MCPServer): reuse func SendNotificationToClient() to send notification
1 parent 19fe996 commit a7b38b8

File tree

2 files changed

+8
-34
lines changed

2 files changed

+8
-34
lines changed

server/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
ErrSessionExists = errors.New("session already exists")
1818
ErrSessionNotInitialized = errors.New("session not properly initialized")
1919
ErrSessionDoesNotSupportTools = errors.New("session does not support per-session tools")
20-
ErrSessionDoesNotSupportLogging = errors.New("session does not support setting logging level")
20+
ErrSessionDoesNotSupportLogging = errors.New("session does not support setting and getting logging level")
2121

2222
// Notification-related errors
2323
ErrNotificationNotInitialized = errors.New("notification channel not initialized")

server/session.go

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ func (s *MCPServer) DeleteSessionTools(sessionID string, names ...string) error
344344
return nil
345345
}
346346

347-
// SendLogMessageToClient sends a log message to the current client
347+
// SendLogMessageToClient sends a log message notification to the current client.
348+
// The client sesssion is expected to be an instance of server.SessionWithLogging to support setting and getting minimum log level
348349
func(s *MCPServer) SendLogMessageToClient(ctx context.Context, msg mcp.LoggingMessageNotification) error {
349350
if s.capabilities.logging == nil || !(*s.capabilities.logging) {
350351
return fmt.Errorf("server does not support emitting log message notifications")
@@ -375,37 +376,10 @@ func(s *MCPServer) SendLogMessageToClient(ctx context.Context, msg mcp.LoggingMe
375376
return fmt.Errorf("message level(%s) is lower than client level(%s)", msg.Params.Level, clientLogLevel)
376377
}
377378

378-
notification := mcp.JSONRPCNotification{
379-
JSONRPC: mcp.JSONRPC_VERSION,
380-
Notification: mcp.Notification{
381-
Method: msg.Method,
382-
Params: mcp.NotificationParams{
383-
AdditionalFields: map[string]any{
384-
"level": msg.Params.Level,
385-
"data": msg.Params.Data,
386-
"logger": msg.Params.Logger,
387-
},
388-
},
389-
},
390-
}
391-
392-
select {
393-
case logSession.NotificationChannel() <- notification:
394-
return nil
395-
default:
396-
// Channel is blocked, if there's an error hook, use it
397-
if s.hooks != nil && len(s.hooks.OnError) > 0 {
398-
err := ErrNotificationChannelBlocked
399-
// Copy hooks pointer to local variable to avoid race condition
400-
hooks := s.hooks
401-
go func(sessionID string, hooks *Hooks) {
402-
// Use the error hook to report the blocked channel
403-
hooks.onError(ctx, nil, "notification", map[string]any{
404-
"method": msg.Method,
405-
"sessionID": sessionID,
406-
}, fmt.Errorf("failed to send a log message, notification channel blocked for session %s: %w", sessionID, err))
407-
}(logSession.SessionID(), hooks)
408-
}
409-
return ErrNotificationChannelBlocked
379+
params := map[string]any {
380+
"level": msg.Params.Level,
381+
"data": msg.Params.Data,
382+
"logger": msg.Params.Logger,
410383
}
384+
return s.SendNotificationToClient(ctx, msg.Method, params)
411385
}

0 commit comments

Comments
 (0)