Skip to content

fix(mcp): cache empty tools lists - #4796

Open
CoralGarden52 wants to merge 1 commit into
openai:mainfrom
CoralGarden52:fix/mcp-empty-tools-cache
Open

fix(mcp): cache empty tools lists#4796
CoralGarden52 wants to merge 1 commit into
openai:mainfrom
CoralGarden52:fix/mcp-empty-tools-cache

Conversation

@CoralGarden52

Copy link
Copy Markdown

This pull request fixes MCP tools-list caching when a server returns a valid empty tool list.

With cache_tools_list=True, MCPServer.list_tools() stores an empty result as [], but the cache-hit check used list truthiness. As a result, every later discovery call fetched the empty list again. The cache-hit check now distinguishes the unpopulated None sentinel from a populated empty list.

The regression test exercises the public MCPServerStdio context-manager and list_tools() boundary, verifies cached_tools == [], and confirms that two calls make only one underlying MCP request. Existing invalidation, pagination, filtering, mutation-isolation, and non-empty caching behavior remains covered by the same test module.

Validation:

  • make format
  • make lint
  • make typecheck
  • make tests

All repository verification commands passed locally.

@sylvesterkaczmarek sylvesterkaczmarek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I traced this through the cache sentinel and invalidation paths. _tools_list is None is the unpopulated state, while [] is a valid server result, so switching the hit check away from list truthiness fixes the repeated-fetch case without changing the dirty-cache contract: invalidate_tools_cache() still forces the next lookup through the MCP request path. The cached value also remains the raw tool list before filtering, so this does not make a filtered view the new source of truth.

The regression exercises the public context-manager/list_tools() boundary, confirms cached_tools == [], and pins a single underlying request across two lookups. I did not find a blocking issue in this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants