diff --git a/mcp/shared.go b/mcp/shared.go index 69b8836a..5fccec76 100644 --- a/mcp/shared.go +++ b/mcp/shared.go @@ -346,18 +346,23 @@ func notifySessions[S Session, P Params](sessions []S, method string, params P) if sessions == nil { return } - // TODO: make this timeout configurable, or call handleNotify asynchronously. - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() // TODO: there's a potential spec violation here, when the feature list // changes before the session (client or server) is initialized. for _, s := range sessions { req := newRequest(s, params) - if err := handleNotify(ctx, method, req); err != nil { - // TODO(jba): surface this error better - log.Printf("calling %s: %v", method, err) - } + // Call handleNotify asynchronously to avoid blocking + go func(r Request) { + // Use a reasonable timeout to prevent goroutine leaks + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + if err := handleNotify(ctx, method, r); err != nil { + // TODO(jba): surface this error better + log.Printf("notify session, calling %s fails, err: %v", method, err) + } + }(req) + } }