Skip to content

feat: add MCP tools caching to list_mcp_tools - #276

Open
Cosmos-Atom wants to merge 1 commit into
SAP:mainfrom
Cosmos-Atom:feat/mcp-tools-caching
Open

feat: add MCP tools caching to list_mcp_tools#276
Cosmos-Atom wants to merge 1 commit into
SAP:mainfrom
Cosmos-Atom:feat/mcp-tools-caching

Conversation

@Cosmos-Atom

@Cosmos-Atom Cosmos-Atom commented Aug 19, 2026

Copy link
Copy Markdown

Description

Adds optional in-process result caching for list_mcp_tools() to avoid redundant MCP session round-trips in agentic loops where the tool list rarely changes.

  • New CacheOptions(ttl, max_size) dataclass — pass to list_mcp_tools(cache=...)
  • Results cached per filter + auth-type combination with monotonic TTL (default 600 s)
  • cache.evict() for caller-triggered invalidation when tools change on the server
  • LRU eviction when max_size entries exceeded (default 32) — prevents unbounded memory growth
  • 19 new unit tests covering hit/miss, TTL expiry, LRU eviction, and evict()
  • user-guide.md updated with usage examples and CacheOptions API reference

Related Issue

Closes #178

Type of Change

  • New feature (non-breaking change that adds functionality)

How to Test

  1. Install the package locally: uv sync
  2. Run the new unit tests: uv run pytest tests/agentgateway/unit/test_tools_cache.py -v
  3. Run the full agentgateway test suite: uv run pytest tests/agentgateway/unit/ -q
  4. All 236 tests should pass

Checklist

  • I have read the Contributing Guidelines
  • I have verified that my changes solve the issue
  • I have added/updated automated tests to cover my changes
  • All tests pass locally
  • I have verified that my code follows the Code Guidelines
  • I have updated documentation (if applicable)
  • I have added type hints for all public APIs
  • My code does not contain sensitive information (credentials, tokens, etc.)
  • I have followed Conventional Commits for commit messages

Additional Notes

Cache is in-process only — not shared across client instances, processes, or Kubernetes pods. This matches the existing _TokenCache design. Concurrent cache misses produce redundant fetches but no data corruption (same accepted trade-off documented in _token_cache.py).

This PR was implemented with AI assistance (Claude) per the SAP GenAI contribution guidelines.

@Cosmos-Atom
Cosmos-Atom requested a review from a team as a code owner August 19, 2026 08:12
@cla-assistant

cla-assistant Bot commented Aug 19, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Cosmos-Atom
Cosmos-Atom force-pushed the feat/mcp-tools-caching branch from b0bc0b6 to e1d72c6 Compare August 19, 2026 08:24
@tiagoek

tiagoek commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

SDK Module Review

Check Status Findings
bdd ✅ PASS 0
binding-shape ✅ PASS 0
commits ✅ PASS 0
concurrency ✅ PASS 0
constants ✅ PASS 0
deletion-hygiene ✅ PASS 0
deps-supply ✅ PASS 0
disclosure ✅ PASS 0
docs ✅ PASS 0
errors-logging ✅ PASS 0
hardcode ✅ PASS 0
http-hygiene ✅ PASS 0
license-spdx ✅ PASS 0
patterns ✅ PASS 0
pr-size ✅ PASS 0
quality-gate-parity ✅ PASS 0
secrets ✅ PASS 0
telemetry ✅ PASS 0
testing-depth ✅ PASS 0
versioning ✅ PASS 0

0 finding(s): 0 posted as inline comment(s) on the affected lines, 0 not tied to a code line (listed above).


Generated by sdk-review-skill · v1

return time.monotonic() < self.expires_at


def _make_cache_key(filter: MCPToolFilter | None, user_scoped: bool) -> str:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn’t we create this cache key based on some user properties? @NicoleMGomes, any thoughts about it?

@Cosmos-Atom
Cosmos-Atom force-pushed the feat/mcp-tools-caching branch from 35538ec to faacebd Compare September 2, 2026 05:11
Adds optional in-process result caching for list_mcp_tools() to avoid
redundant MCP session round-trips in agentic loops where the tool list
rarely changes.

- New CacheOptions(ttl, max_size) dataclass accepted by list_mcp_tools(cache=...)
- Results cached per filter + auth-type combination with monotonic TTL (default 600 s)
- cache.evict() for caller-triggered invalidation
- LRU eviction when max_size entries exceeded (default 32)
- 19 new unit tests covering hit/miss, TTL expiry, LRU eviction, evict()
- user-guide.md updated with usage examples and CacheOptions API reference

Closes SAP#178
@Cosmos-Atom
Cosmos-Atom force-pushed the feat/mcp-tools-caching branch from faacebd to 3e662e0 Compare September 3, 2026 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support MCP tools caching

3 participants