Skip to content

Commit 01b8ea3

Browse files
committed
change
1 parent ed39d07 commit 01b8ea3

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

packages/mcp/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "microsoft.teams.mcp"
3-
version = "0.0.1-alpha.1"
3+
version = "0.0.1-alpha.4"
44
description = "library for handling mcp with teams ai library"
55
authors = [{ name = "Microsoft", email = "[email protected]" }]
66
readme = "README.md"

packages/mcp/src/microsoft/teams/mcp/ai_plugin.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
from .transport import create_transport
2121

22+
REFETCH_TIMEOUT_MS = 24 * 60 * 60 * 1000 # 1 day
23+
2224

2325
class McpToolDetails(BaseModel):
2426
"""Details of an MCP tool."""
@@ -35,11 +37,11 @@ def __init__(
3537
self,
3638
transport: Optional[str] = None,
3739
available_tools: Optional[List[McpToolDetails]] = None,
38-
last_attempted_fetch: Optional[float] = None,
40+
last_fetched: Optional[float] = None,
3941
):
4042
self.transport = transport
4143
self.available_tools = available_tools or []
42-
self.last_attempted_fetch = last_attempted_fetch
44+
self.last_fetched = last_fetched
4345

4446

4547
class McpClientPluginParams:
@@ -69,7 +71,7 @@ def __init__(
6971
version: str = "0.0.0",
7072
cache: Optional[Dict[str, McpCachedValue]] = None,
7173
logger: Optional[logging.Logger] = None,
72-
refetch_timeout_ms: int = 24 * 60 * 60 * 1000, # 1 day
74+
refetch_timeout_ms: int = REFETCH_TIMEOUT_MS, # 1 day
7375
):
7476
super().__init__(name)
7577

@@ -78,6 +80,13 @@ def __init__(
7880
self._logger = logger.getChild(self.name) if logger else ConsoleLogger().create_logger(self.name)
7981
self._refetch_timeout_ms = refetch_timeout_ms
8082

83+
# If cache is provided, update last_fetched for entries with tools
84+
if cache:
85+
current_time = time.time() * 1000
86+
for cached_value in cache.values():
87+
if cached_value.available_tools and not cached_value.last_fetched:
88+
cached_value.last_fetched = current_time
89+
8190
# Track MCP server URLs and their parameters
8291
self._mcp_server_params: Dict[str, McpClientPluginParams] = {}
8392

@@ -105,7 +114,7 @@ def add_mcp_server(self, url: str, params: Optional[McpClientPluginParams] = Non
105114
self._cache[url] = McpCachedValue(
106115
transport=params.transport,
107116
available_tools=params.available_tools,
108-
last_attempted_fetch=None,
117+
last_fetched=time.time() * 1000, # Set to current time in milliseconds
109118
)
110119

111120
async def on_build_functions(self, functions: List[Function[BaseModel]]) -> List[Function[BaseModel]]:
@@ -127,7 +136,12 @@ async def on_build_functions(self, functions: List[Function[BaseModel]]) -> List
127136
return all_functions
128137

129138
async def _fetch_tools_if_needed(self) -> None:
130-
"""Fetch tools from MCP servers if needed."""
139+
"""
140+
Fetch tools from MCP servers if needed.
141+
142+
We check if there the cached value has met its expiration
143+
for being refetched. Or if the tools have never been fetched at all
144+
"""
131145
fetch_needed: List[Tuple[str, McpClientPluginParams]] = []
132146
current_time = time.time() * 1000 # Convert to milliseconds
133147

@@ -140,9 +154,8 @@ async def _fetch_tools_if_needed(self) -> None:
140154
should_fetch = (
141155
not cached_data
142156
or not cached_data.available_tools
143-
or not cached_data.last_attempted_fetch
144-
or (current_time - cached_data.last_attempted_fetch)
145-
> (params.refetch_timeout_ms or self._refetch_timeout_ms)
157+
or not cached_data.last_fetched
158+
or (current_time - cached_data.last_fetched) > (params.refetch_timeout_ms or self._refetch_timeout_ms)
146159
)
147160

148161
if should_fetch:
@@ -164,7 +177,7 @@ async def _fetch_tools_if_needed(self) -> None:
164177
if url not in self._cache:
165178
self._cache[url] = McpCachedValue()
166179
self._cache[url].available_tools = result
167-
self._cache[url].last_attempted_fetch = current_time
180+
self._cache[url].last_fetched = current_time
168181
self._cache[url].transport = params.transport
169182

170183
self._logger.debug(f"Cached {len(result)} tools for {url}")

0 commit comments

Comments
 (0)