refactor: reverse visibility for list_tools/_list_tools methods

This commit is contained in:
Jeremiah Lowin 2026-01-17 14:32:53 -05:00
commit 5fe471dc60
12 changed files with 25 additions and 25 deletions

View file

@ -13,7 +13,7 @@ Example:
def __init__(self, db_url: str):
self.db = Database(db_url)
async def list_tools(self) -> list[Tool]:
async def _list_tools(self) -> list[Tool]:
rows = await self.db.fetch("SELECT * FROM tools")
return [self._make_tool(row) for row in rows]

View file

@ -123,10 +123,10 @@ class AggregateProvider(Provider):
# Tools
# -------------------------------------------------------------------------
async def list_tools(self) -> Sequence[Tool]:
async def _list_tools(self) -> Sequence[Tool]:
"""List all tools from all providers (with transforms applied)."""
results = await gather(
*[p._list_tools() for p in self._providers],
*[p.list_tools() for p in self._providers],
return_exceptions=True,
)
return self._collect_list_results(results, "list_tools")

View file

@ -100,7 +100,7 @@ class Provider:
# Internal transform chain building
# -------------------------------------------------------------------------
async def _list_tools(self) -> Sequence[Tool]:
async def list_tools(self) -> Sequence[Tool]:
"""List tools with all transforms applied.
Builds a middleware chain: base transforms (in order).
@ -111,7 +111,7 @@ class Provider:
"""
async def base() -> Sequence[Tool]:
return await self.list_tools()
return await self._list_tools()
chain = base
for transform in self.transforms:
@ -237,10 +237,10 @@ class Provider:
return await chain(name, version=version)
# -------------------------------------------------------------------------
# Public list/get methods (override these to provide components)
# Private list/get methods (override these to provide components)
# -------------------------------------------------------------------------
async def list_tools(self) -> Sequence[Tool]:
async def _list_tools(self) -> Sequence[Tool]:
"""Return all available tools.
Override to provide tools dynamically. Returns ALL versions of all tools.
@ -264,7 +264,7 @@ class Provider:
Returns:
The Tool if found, or None to continue searching other providers.
"""
tools = await self.list_tools()
tools = await self._list_tools()
matching = [t for t in tools if t.name == name]
if version:
matching = [t for t in matching if version.matches(t.version)]
@ -380,7 +380,7 @@ class Provider:
"""
# Fetch all component types in parallel
results = await gather(
self.list_tools(),
self._list_tools(),
self.list_resources(),
self.list_resource_templates(),
self.list_prompts(),

View file

@ -482,7 +482,7 @@ class FastMCPProvider(Provider):
# Tool methods
# -------------------------------------------------------------------------
async def list_tools(self) -> Sequence[Tool]:
async def _list_tools(self) -> Sequence[Tool]:
"""List all tools from the mounted server as FastMCPProviderTools.
Runs the mounted server's middleware so filtering/transformation applies.

View file

@ -174,10 +174,10 @@ class FileSystemProvider(LocalProvider):
# Override provider methods to support reload mode
async def list_tools(self) -> Sequence[Tool]:
async def _list_tools(self) -> Sequence[Tool]:
"""Return all tools, reloading if in reload mode."""
await self._ensure_loaded()
return await super().list_tools()
return await super()._list_tools()
async def get_tool(
self, name: str, version: VersionSpec | None = None

View file

@ -492,7 +492,7 @@ class LocalProvider(Provider):
# Provider interface implementation
# =========================================================================
async def list_tools(self) -> Sequence[Tool]:
async def _list_tools(self) -> Sequence[Tool]:
"""Return all visible tools."""
return [
v

View file

@ -349,7 +349,7 @@ class OpenAPIProvider(Provider):
# Provider interface
# -------------------------------------------------------------------------
async def list_tools(self) -> Sequence[Tool]:
async def _list_tools(self) -> Sequence[Tool]:
"""Return all tools created from the OpenAPI spec."""
return list(self._tools.values())

View file

@ -517,7 +517,7 @@ class ProxyProvider(Provider):
# Tool methods
# -------------------------------------------------------------------------
async def list_tools(self) -> Sequence[Tool]:
async def _list_tools(self) -> Sequence[Tool]:
"""List all tools from the remote server."""
try:
client = await self._get_client()

View file

@ -867,14 +867,14 @@ class FastMCP(Provider, Generic[LifespanResultT]):
# Provider interface overrides (aggregate from sub-providers)
# -------------------------------------------------------------------------
async def list_tools(self) -> Sequence[Tool]:
async def _list_tools(self) -> Sequence[Tool]:
"""Aggregate tools from all sub-providers.
This is the Provider interface implementation. The inherited _list_tools()
This is the Provider interface implementation. The inherited list_tools()
applies server-level transforms over this method.
"""
results = await gather(
*[p._list_tools() for p in self._providers],
*[p.list_tools() for p in self._providers],
return_exceptions=True,
)
return self._collect_list_results(results, "list_tools")
@ -1106,7 +1106,7 @@ class FastMCP(Provider, Generic[LifespanResultT]):
)
# Query through full transform chain (provider transforms + server transforms + visibility)
tools = await self._list_tools()
tools = await self.list_tools()
# Get auth context (skip_auth=True for STDIO which has no auth concept)
skip_auth, token = _get_auth_context()

View file

@ -24,7 +24,7 @@ class SimpleProvider(Provider):
super().__init__()
self._tools = tools or []
async def list_tools(self) -> list[Tool]:
async def _list_tools(self) -> list[Tool]:
return self._tools

View file

@ -621,12 +621,12 @@ class TestProviderToolTransformations:
def my_tool(x: int) -> int:
return x
# Add layer to provider (layers are applied by server, not list_tools)
# Add layer to provider (layers are applied by server, not _list_tools)
layer = ToolTransform({"my_tool": ToolTransformConfig(name="renamed")})
provider.add_transform(layer)
# Provider's list_tools returns raw tools (transforms applied when queried via chain)
original_tools = await provider.list_tools()
# Provider's _list_tools returns raw tools (transforms applied when queried via list_tools)
original_tools = await provider._list_tools()
assert original_tools[0].name == "my_tool"
# Transform modifies them when applied via call_next

View file

@ -48,7 +48,7 @@ class SimpleToolProvider(Provider):
self.list_tools_call_count = 0
self.get_tool_call_count = 0
async def list_tools(self) -> list[Tool]:
async def _list_tools(self) -> list[Tool]:
self.list_tools_call_count += 1
return self._tools
@ -73,7 +73,7 @@ class ListOnlyProvider(Provider):
self._tools = tools
self.list_tools_call_count = 0
async def list_tools(self) -> list[Tool]:
async def _list_tools(self) -> list[Tool]:
self.list_tools_call_count += 1
return self._tools