-
Notifications
You must be signed in to change notification settings - Fork 865
001 state aware tool filtering #864
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
Changes from all commits
db5e84c
f403a46
254f530
10d8d46
78dc04c
4135bd2
3341af3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,11 @@ | ||||||||||||||||||||
| using System; | ||||||||||||||||||||
| using System.Collections.Generic; | ||||||||||||||||||||
| using System.Linq; | ||||||||||||||||||||
| using System.Threading; | ||||||||||||||||||||
| using MCPForUnity.Editor.Constants; | ||||||||||||||||||||
| using MCPForUnity.Editor.Helpers; | ||||||||||||||||||||
| using MCPForUnity.Editor.Services; | ||||||||||||||||||||
| using MCPForUnity.Editor.Services.Transport; | ||||||||||||||||||||
| using MCPForUnity.Editor.Tools; | ||||||||||||||||||||
| using UnityEditor; | ||||||||||||||||||||
| using UnityEngine.UIElements; | ||||||||||||||||||||
|
|
@@ -231,6 +233,30 @@ private void HandleToggleChange(ToolMetadata tool, bool enabled, bool updateSumm | |||||||||||||||||||
| { | ||||||||||||||||||||
| UpdateSummary(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Trigger tool reregistration with connected MCP server | ||||||||||||||||||||
| ReregisterToolsAsync(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private void ReregisterToolsAsync() | ||||||||||||||||||||
| { | ||||||||||||||||||||
| // Fire and forget - don't block UI | ||||||||||||||||||||
| ThreadPool.QueueUserWorkItem(_ => | ||||||||||||||||||||
| { | ||||||||||||||||||||
| try | ||||||||||||||||||||
| { | ||||||||||||||||||||
| var transportManager = MCPServiceLocator.TransportManager; | ||||||||||||||||||||
| var client = transportManager.GetClient(TransportMode.Http); | ||||||||||||||||||||
| if (client != null && client.IsConnected) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| client.ReregisterToolsAsync().Wait(); | ||||||||||||||||||||
|
||||||||||||||||||||
| client.ReregisterToolsAsync().Wait(); | |
| client.ReregisterToolsAsync().ContinueWith(t => | |
| { | |
| if (t.IsFaulted) | |
| { | |
| var ex = t.Exception?.GetBaseException(); | |
| McpLog.Warn($"Failed to reregister tools: {ex?.Message ?? "Unknown error"}"); | |
| } | |
| }); |
Copilot
AI
Mar 4, 2026
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.
In SetAllToolsState, HandleToggleChange is called for each changed tool (line 278), and HandleToggleChange itself calls ReregisterToolsAsync() (line 238). Then, after the loop, SetAllToolsState calls ReregisterToolsAsync() again on line 284. This means a bulk toggle of N tools will fire N+1 reregistration requests — one per tool plus the final explicit call. The per-tool calls during the bulk operation are wasteful and redundant; only the final one is necessary. The HandleToggleChange calls from SetAllToolsState should either skip reregistration (e.g. via a parameter like triggerReregister = false) or the extra ReregisterToolsAsync() at line 284 should be removed.
| // Trigger tool reregistration after bulk change | |
| ReregisterToolsAsync(); |
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.
suggestion (bug_risk): Avoid blocking on async ReregisterToolsAsync() using .Wait() inside ThreadPool work item.
Since this runs on a ThreadPool thread, synchronously waiting on
ReregisterToolsAsync()can still cause deadlocks or thread pool starvation if the async method ever captures a context or awaits other blocking work. Instead, keep this path fully async by using an async delegate, e.g.ThreadPool.QueueUserWorkItem(async _ => { ... await client.ReregisterToolsAsync().ConfigureAwait(false); });so you never block a worker thread.Suggested implementation: