-
Notifications
You must be signed in to change notification settings - Fork 295
mcp: debounce server change notifications #671
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
Open
jba
wants to merge
3
commits into
modelcontextprotocol:main
Choose a base branch
from
jba:notifications
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+60
−28
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ type Server struct { | |
| sendingMethodHandler_ MethodHandler | ||
| receivingMethodHandler_ MethodHandler | ||
| resourceSubscriptions map[string]map[*ServerSession]bool // uri -> session -> bool | ||
| pendingNotifications map[string]*time.Timer // notification name -> timer for pending notification send | ||
| } | ||
|
|
||
| // ServerOptions is used to configure behavior of the server. | ||
|
|
@@ -149,6 +150,7 @@ func NewServer(impl *Implementation, options *ServerOptions) *Server { | |
| sendingMethodHandler_: defaultSendingMethodHandler[*ServerSession], | ||
| receivingMethodHandler_: defaultReceivingMethodHandler[*ServerSession], | ||
| resourceSubscriptions: make(map[string]map[*ServerSession]bool), | ||
| pendingNotifications: make(map[string]*time.Timer), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -158,15 +160,13 @@ func (s *Server) AddPrompt(p *Prompt, h PromptHandler) { | |
| // (It's possible an item was replaced with an identical one, but not worth checking.) | ||
| s.changeAndNotify( | ||
| notificationPromptListChanged, | ||
| &PromptListChangedParams{}, | ||
| func() bool { s.prompts.add(&serverPrompt{p, h}); return true }) | ||
| } | ||
|
|
||
| // RemovePrompts removes the prompts with the given names. | ||
| // It is not an error to remove a nonexistent prompt. | ||
| func (s *Server) RemovePrompts(names ...string) { | ||
| s.changeAndNotify(notificationPromptListChanged, &PromptListChangedParams{}, | ||
| func() bool { return s.prompts.remove(names...) }) | ||
| s.changeAndNotify(notificationPromptListChanged, func() bool { return s.prompts.remove(names...) }) | ||
| } | ||
|
|
||
| // AddTool adds a [Tool] to the server, or replaces one with the same name. | ||
|
|
@@ -235,8 +235,7 @@ func (s *Server) AddTool(t *Tool, h ToolHandler) { | |
| // (It's possible a tool was replaced with an identical one, but not worth checking.) | ||
| // TODO: Batch these changes by size and time? The typescript SDK doesn't. | ||
| // TODO: Surface notify error here? best not, in case we need to batch. | ||
| s.changeAndNotify(notificationToolListChanged, &ToolListChangedParams{}, | ||
| func() bool { s.tools.add(st); return true }) | ||
| s.changeAndNotify(notificationToolListChanged, func() bool { s.tools.add(st); return true }) | ||
| } | ||
|
|
||
| func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHandler, error) { | ||
|
|
@@ -419,14 +418,13 @@ func AddTool[In, Out any](s *Server, t *Tool, h ToolHandlerFor[In, Out]) { | |
| // RemoveTools removes the tools with the given names. | ||
| // It is not an error to remove a nonexistent tool. | ||
| func (s *Server) RemoveTools(names ...string) { | ||
| s.changeAndNotify(notificationToolListChanged, &ToolListChangedParams{}, | ||
| func() bool { return s.tools.remove(names...) }) | ||
| s.changeAndNotify(notificationToolListChanged, func() bool { return s.tools.remove(names...) }) | ||
| } | ||
|
|
||
| // AddResource adds a [Resource] to the server, or replaces one with the same URI. | ||
| // AddResource panics if the resource URI is invalid or not absolute (has an empty scheme). | ||
| func (s *Server) AddResource(r *Resource, h ResourceHandler) { | ||
| s.changeAndNotify(notificationResourceListChanged, &ResourceListChangedParams{}, | ||
| s.changeAndNotify(notificationResourceListChanged, | ||
| func() bool { | ||
| if _, err := url.Parse(r.URI); err != nil { | ||
| panic(err) // url.Parse includes the URI in the error | ||
|
|
@@ -439,14 +437,13 @@ func (s *Server) AddResource(r *Resource, h ResourceHandler) { | |
| // RemoveResources removes the resources with the given URIs. | ||
| // It is not an error to remove a nonexistent resource. | ||
| func (s *Server) RemoveResources(uris ...string) { | ||
| s.changeAndNotify(notificationResourceListChanged, &ResourceListChangedParams{}, | ||
| func() bool { return s.resources.remove(uris...) }) | ||
| s.changeAndNotify(notificationResourceListChanged, func() bool { return s.resources.remove(uris...) }) | ||
| } | ||
|
|
||
| // AddResourceTemplate adds a [ResourceTemplate] to the server, or replaces one with the same URI. | ||
| // AddResourceTemplate panics if a URI template is invalid or not absolute (has an empty scheme). | ||
| func (s *Server) AddResourceTemplate(t *ResourceTemplate, h ResourceHandler) { | ||
| s.changeAndNotify(notificationResourceListChanged, &ResourceListChangedParams{}, | ||
| s.changeAndNotify(notificationResourceListChanged, | ||
| func() bool { | ||
| // Validate the URI template syntax | ||
| _, err := uritemplate.New(t.URITemplate) | ||
|
|
@@ -461,8 +458,7 @@ func (s *Server) AddResourceTemplate(t *ResourceTemplate, h ResourceHandler) { | |
| // RemoveResourceTemplates removes the resource templates with the given URI templates. | ||
| // It is not an error to remove a nonexistent resource. | ||
| func (s *Server) RemoveResourceTemplates(uriTemplates ...string) { | ||
| s.changeAndNotify(notificationResourceListChanged, &ResourceListChangedParams{}, | ||
| func() bool { return s.resourceTemplates.remove(uriTemplates...) }) | ||
| s.changeAndNotify(notificationResourceListChanged, func() bool { return s.resourceTemplates.remove(uriTemplates...) }) | ||
| } | ||
|
|
||
| func (s *Server) capabilities() *ServerCapabilities { | ||
|
|
@@ -497,18 +493,43 @@ func (s *Server) complete(ctx context.Context, req *CompleteRequest) (*CompleteR | |
| return s.opts.CompletionHandler(ctx, req) | ||
| } | ||
|
|
||
| // Map from notification name to its corresponding params. The params have no fields, | ||
| // so a single struct can be reused. | ||
| var changeNotificationParams = map[string]Params{ | ||
| notificationToolListChanged: &ToolListChangedParams{}, | ||
| notificationPromptListChanged: &PromptListChangedParams{}, | ||
| notificationResourceListChanged: &ResourceListChangedParams{}, | ||
| } | ||
|
|
||
| // How long to wait before sending a change notification. | ||
| const notificationDelay = 10 * time.Millisecond | ||
|
|
||
| // changeAndNotify is called when a feature is added or removed. | ||
| // It calls change, which should do the work and report whether a change actually occurred. | ||
| // If there was a change, it notifies a snapshot of the sessions. | ||
| func (s *Server) changeAndNotify(notification string, params Params, change func() bool) { | ||
| var sessions []*ServerSession | ||
| // Lock for the change, but not for the notification. | ||
| // If there was a change, it sets a timer to send a notification. | ||
| // This debounces change notifications: a single notification is sent after | ||
| // multiple changes occur in close proximity. | ||
| func (s *Server) changeAndNotify(notification string, change func() bool) { | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
| if change() { | ||
| sessions = slices.Clone(s.sessions) | ||
| // Stop the outstanding delayed call, if any. | ||
| if t := s.pendingNotifications[notification]; t != nil { | ||
| t.Stop() | ||
| } | ||
| // | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cruft? |
||
| s.pendingNotifications[notification] = time.AfterFunc(notificationDelay, func() { s.notifySessions(notification) }) | ||
| } | ||
| s.mu.Unlock() | ||
| notifySessions(sessions, notification, params) | ||
| } | ||
|
|
||
| // notifySessions sends the notification n to all existing sessions. | ||
| // It is called asynchronously by changeAndNotify. | ||
| func (s *Server) notifySessions(n string) { | ||
| s.mu.Lock() | ||
| sessions := slices.Clone(s.sessions) | ||
| s.pendingNotifications[n] = nil | ||
| s.mu.Unlock() // Don't hold the lock during notification: it causes deadlock. | ||
| notifySessions(sessions, n, changeNotificationParams[n]) | ||
| } | ||
|
|
||
| // Sessions returns an iterator that yields the current set of server sessions. | ||
|
|
@@ -1068,7 +1089,6 @@ func (ss *ServerSession) Elicit(ctx context.Context, params *ElicitParams) (*Eli | |
|
|
||
| resolved, err := schema.Resolve(nil) | ||
| if err != nil { | ||
| fmt.Printf(" resolve err: %s", err) | ||
| return nil, err | ||
| } | ||
| if err := resolved.Validate(res.Content); err != nil { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -349,6 +349,8 @@ func notifySessions[S Session, P Params](sessions []S, method string, params P) | |
| if sessions == nil { | ||
| return | ||
| } | ||
| // Notify with the background context, so the messages are sent on the | ||
| // standalone stream. | ||
| // TODO: make this timeout configurable, or call handleNotify asynchronously. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this TODO now obsolete? Are all notifications asynchronous? |
||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can just call t.Reset() and probably avoid some allocation, no?