Skip to content

Commit 2f195dc

Browse files
authored
Only list MCP tools once, not at every agent run step (#3227)
1 parent 3a86855 commit 2f195dc

File tree

1 file changed

+7
-4
lines changed
  • pydantic_ai_slim/pydantic_ai

1 file changed

+7
-4
lines changed

pydantic_ai_slim/pydantic_ai/mcp.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class MCPServer(AbstractToolset[Any], ABC):
113113
_read_stream: MemoryObjectReceiveStream[SessionMessage | Exception]
114114
_write_stream: MemoryObjectSendStream[SessionMessage]
115115
_server_info: mcp_types.Implementation
116+
_tools: list[mcp_types.Tool] | None
116117

117118
def __init__(
118119
self,
@@ -148,6 +149,7 @@ def __post_init__(self):
148149
self._enter_lock = Lock()
149150
self._running_count = 0
150151
self._exit_stack = None
152+
self._tools = None
151153

152154
@abstractmethod
153155
@asynccontextmanager
@@ -194,13 +196,14 @@ def server_info(self) -> mcp_types.Implementation:
194196
async def list_tools(self) -> list[mcp_types.Tool]:
195197
"""Retrieve tools that are currently active on the server.
196198
197-
Note:
198-
- We don't cache tools as they might change.
199-
- We also don't subscribe to the server to avoid complexity.
199+
Note that we don't subscribe to the server to avoid complexity.
200200
"""
201+
if self._tools is not None:
202+
return self._tools
201203
async with self: # Ensure server is running
202204
result = await self._client.list_tools()
203-
return result.tools
205+
self._tools = result.tools
206+
return self._tools
204207

205208
async def direct_call_tool(
206209
self,

0 commit comments

Comments
 (0)