Skip to content

Commit 4beb291

Browse files
simplified event unsubscription in primitive registration
1 parent a918739 commit 4beb291

1 file changed

Lines changed: 5 additions & 25 deletions

File tree

src/ModelContextProtocol/Server/McpServer.cs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,8 @@ public McpServer(ITransport transport, McpServerOptions options, ILoggerFactory?
6969
NotificationHandlers.RegisterRange(notificationHandlers);
7070
}
7171

72-
// Now that everything has been configured, subscribe to any necessary notifications.
73-
// TODO: Figure out why calling RegisterListChange here causes a crash.
74-
IListCapability<McpServerTool>? toolsCapabilities = capabilities?.Tools;
75-
if (toolsCapabilities?.Collection is { } tools)
76-
{
77-
void ChangedDelegate(object? sender, EventArgs e)
78-
=> _ = this.SendNotificationAsync(NotificationMethods.ToolListChangedNotification);
79-
80-
tools.Changed += ChangedDelegate;
81-
_disposables.Add(new(() => tools.Changed -= ChangedDelegate));
82-
}
83-
84-
// TODO: Figure out why calling RegisterListChange here causes a crash.
85-
IListCapability<McpServerPrompt>? promptsCapabilities = capabilities?.Prompts;
86-
if (promptsCapabilities?.Collection is { } prompts)
87-
{
88-
void ChangedDelegate(object? sender, EventArgs e)
89-
=> _ = this.SendNotificationAsync(NotificationMethods.PromptListChangedNotification);
90-
91-
prompts.Changed += ChangedDelegate;
92-
_disposables.Add(new(() => prompts.Changed -= ChangedDelegate));
93-
}
72+
RegisterListChange(capabilities?.Tools, NotificationMethods.ToolListChangedNotification);
73+
RegisterListChange(capabilities?.Prompts, NotificationMethods.PromptListChangedNotification);
9474
RegisterListChange(capabilities?.Resources, RequestMethods.ResourcesList);
9575

9676
// And initialize the session.
@@ -486,10 +466,10 @@ private void RegisterListChange<T>(IListCapability<T>? capability, string method
486466
if (capability?.Collection is { } collection)
487467
//&& capability.ListChanged is true)
488468
{
489-
void handler(object? sender, EventArgs e)
469+
void ChangedDelegate(object? sender, EventArgs e)
490470
=> _ = this.SendNotificationAsync(methodName);
491-
collection.Changed += handler;
492-
_disposables.Add(new(() => collection.Changed -= handler));
471+
collection.Changed += ChangedDelegate;
472+
_disposables.Add(new(() => collection.Changed -= ChangedDelegate));
493473
}
494474
}
495475

0 commit comments

Comments
 (0)