Skip to content

Commit 1d971b0

Browse files
NielsRoggeclaudeWauplin
authored
[Tiny Agents] Add tools to config (#3242)
* Add tools to config * Simplify MCP tool filtering to use allowed_tools parameter - Replace complex include/exclude structure with simple allowed_tools list - Follow OpenAI specs for tool filtering (allowlist approach only) - Simplify _filter_tools method to use list comprehension - Update type definitions in types.py - Remove example files as requested by maintainers - Add comprehensive unit tests for allowed_tools functionality This addresses PR feedback from @julien-c and @Wauplin: - Eliminates confusion about include/exclude precedence - Aligns with industry standards (OpenAI) - Provides cleaner, simpler API 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Address comments * Update src/huggingface_hub/inference/_mcp/mcp_client.py --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Lucain <[email protected]>
1 parent 5d1b512 commit 1d971b0

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,7 @@ dmypy.json
139139
# Spell checker config
140140
cspell.json
141141

142-
tmp*
142+
tmp*
143+
144+
# Claude Code
145+
CLAUDE.md

src/huggingface_hub/inference/_mcp/mcp_client.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,27 @@ async def add_mcp_server(self, type: ServerType, **params: Any):
139139
- args (List[str], optional): Arguments for the command
140140
- env (Dict[str, str], optional): Environment variables for the command
141141
- cwd (Union[str, Path, None], optional): Working directory for the command
142+
- allowed_tools (List[str], optional): List of tool names to allow from this server
142143
- For SSE servers:
143144
- url (str): The URL of the SSE server
144145
- headers (Dict[str, Any], optional): Headers for the SSE connection
145146
- timeout (float, optional): Connection timeout
146147
- sse_read_timeout (float, optional): SSE read timeout
148+
- allowed_tools (List[str], optional): List of tool names to allow from this server
147149
- For StreamableHTTP servers:
148150
- url (str): The URL of the StreamableHTTP server
149151
- headers (Dict[str, Any], optional): Headers for the StreamableHTTP connection
150152
- timeout (timedelta, optional): Connection timeout
151153
- sse_read_timeout (timedelta, optional): SSE read timeout
152154
- terminate_on_close (bool, optional): Whether to terminate on close
155+
- allowed_tools (List[str], optional): List of tool names to allow from this server
153156
"""
154157
from mcp import ClientSession, StdioServerParameters
155158
from mcp import types as mcp_types
156159

160+
# Extract allowed_tools configuration if provided
161+
allowed_tools = params.pop("allowed_tools", None)
162+
157163
# Determine server type and create appropriate parameters
158164
if type == "stdio":
159165
# Handle stdio server
@@ -211,7 +217,15 @@ async def add_mcp_server(self, type: ServerType, **params: Any):
211217
response = await session.list_tools()
212218
logger.debug("Connected to server with tools:", [tool.name for tool in response.tools])
213219

214-
for tool in response.tools:
220+
# Filter tools based on allowed_tools configuration
221+
filtered_tools = [tool for tool in response.tools if tool.name in allowed_tools]
222+
223+
if allowed_tools:
224+
logger.debug(
225+
f"Tool filtering applied. Using {len(filtered_tools)} of {len(response.tools)} available tools: {[tool.name for tool in filtered_tools]}"
226+
)
227+
228+
for tool in filtered_tools:
215229
if tool.name in self.sessions:
216230
logger.warning(f"Tool '{tool.name}' already defined by another server. Skipping.")
217231
continue

src/huggingface_hub/inference/_mcp/types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ class StdioServerConfig(TypedDict):
1616
args: List[str]
1717
env: Dict[str, str]
1818
cwd: str
19+
allowed_tools: NotRequired[List[str]]
1920

2021

2122
class HTTPServerConfig(TypedDict):
2223
type: Literal["http"]
2324
url: str
2425
headers: Dict[str, str]
26+
allowed_tools: NotRequired[List[str]]
2527

2628

2729
class SSEServerConfig(TypedDict):
2830
type: Literal["sse"]
2931
url: str
3032
headers: Dict[str, str]
33+
allowed_tools: NotRequired[List[str]]
3134

3235

3336
ServerConfig = Union[StdioServerConfig, HTTPServerConfig, SSEServerConfig]

0 commit comments

Comments
 (0)