Skip to content

Commit 6960dce

Browse files
author
Tarek Mahmoud Sayed
committed
Clear tool cache at start of ListToolsAsync to prevent unbounded growth
1 parent 6172219 commit 6960dce

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

src/ModelContextProtocol.Core/Client/McpClient.Methods.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ public async ValueTask<IList<McpClientTool>> ListToolsAsync(
176176
RequestOptions? options = null,
177177
CancellationToken cancellationToken = default)
178178
{
179+
ToolCacheClearing?.Invoke();
180+
179181
List<McpClientTool>? tools = null;
180182
ListToolsRequestParams requestParams = new() { Meta = options?.GetMetaForRequest() };
181183
do
@@ -213,6 +215,11 @@ public async ValueTask<IList<McpClientTool>> ListToolsAsync(
213215
/// </summary>
214216
internal Action<Tool, string>? ToolRejected;
215217

218+
/// <summary>
219+
/// Invoked before enumerating tools to clear any previously cached tool definitions.
220+
/// </summary>
221+
internal Action? ToolCacheClearing;
222+
216223
/// <summary>
217224
/// Retrieves a list of available tools from the server.
218225
/// </summary>

src/ModelContextProtocol.Core/Client/McpClientImpl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ internal McpClientImpl(ITransport transport, string endpointName, McpClientOptio
7272

7373
ToolDiscovered = tool => _toolCache[tool.Name] = tool;
7474
ToolRejected = (tool, reason) => LogToolRejected(tool.Name, reason);
75+
ToolCacheClearing = () => _toolCache.Clear();
7576
}
7677

7778
private void RegisterHandlers(McpClientOptions options, NotificationHandlers notificationHandlers, RequestHandlers requestHandlers)

0 commit comments

Comments
 (0)