From 6de85ec3ed6590e26085abb0b145e91373e2672f Mon Sep 17 00:00:00 2001 From: Guglielmo Colombo Date: Tue, 7 Jul 2026 13:43:10 +0200 Subject: [PATCH] mcp: do not enforce TTL on cached tool (#1053) This PR removes the TTL enforcing on the cached tools, when used to generate the http-parameters for CallTool, according to updated [spec](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/2972) > Clients **MUST** construct `Mcp-Param-*` headers using the most recently obtained `inputSchema` for the tool. --- mcp/cache.go | 12 ------------ mcp/client.go | 17 +++++++---------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/mcp/cache.go b/mcp/cache.go index 6aa3f61..9837337 100644 --- a/mcp/cache.go +++ b/mcp/cache.go @@ -54,18 +54,6 @@ func (mc *methodCache[R]) put(key string, result R) { } } -func (mc *methodCache[R]) forEachValid(f func(R)) { - mc.mu.Lock() - defer mc.mu.Unlock() - for key, entry := range mc.cachedValues { - if !entry.isValid() { - delete(mc.cachedValues, key) - continue - } - f(entry.result) - } -} - func (mc *methodCache[R]) invalidate() { mc.mu.Lock() defer mc.mu.Unlock() diff --git a/mcp/client.go b/mcp/client.go index 52c1e06..4c0a458 100644 --- a/mcp/client.go +++ b/mcp/client.go @@ -584,19 +584,16 @@ func (cs *ClientSession) Wait() error { // outgoing request context for transport-layer features (e.g. x-mcp-header // param annotations). func (cs *ClientSession) lookupTool(name string) *Tool { - var found *Tool - cs.toolsCache.forEachValid(func(r *ListToolsResult) { - if found != nil { - return - } - for _, t := range r.Tools { + cs.toolsCache.mu.Lock() + defer cs.toolsCache.mu.Unlock() + for _, entry := range cs.toolsCache.cachedValues { + for _, t := range entry.result.Tools { if t.Name == name { - found = t - return + return t } } - }) - return found + } + return nil } // registerElicitationWaiter registers a waiter for an elicitation complete