-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Cache MCP tools to avoid calling multiple times when streaming #3236
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
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 |
|---|---|---|
|
|
@@ -155,6 +155,7 @@ def __init__( | |
| self._auth_scheme = auth_scheme | ||
| self._auth_credential = auth_credential | ||
| self._require_confirmation = require_confirmation | ||
| self._loaded_mcp_tools: List[BaseTool] | None = None | ||
|
|
||
| @retry_on_closed_resource | ||
| async def get_tools( | ||
|
|
@@ -170,6 +171,9 @@ async def get_tools( | |
| Returns: | ||
| List[BaseTool]: A list of tools available under the specified context. | ||
| """ | ||
| if self._loaded_mcp_tools is not None: | ||
| return self._loaded_mcp_tools | ||
|
Comment on lines
+174
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This caching implementation has a critical correctness issue and a potential race condition.
Due to the correctness bug, I recommend rethinking this caching strategy. An incorrect cache can be more harmful than no cache. Recommendation: If you are certain that a |
||
|
|
||
| headers = ( | ||
| self._header_provider(readonly_context) | ||
| if self._header_provider and readonly_context | ||
|
|
@@ -195,6 +199,8 @@ async def get_tools( | |
|
|
||
| if self._is_tool_selected(mcp_tool, readonly_context): | ||
| tools.append(mcp_tool) | ||
|
|
||
| self._loaded_mcp_tools = tools | ||
| return tools | ||
|
|
||
| async def close(self) -> None: | ||
|
|
||