|
| 1 | +# Copyright © 2011-2026 Splunk, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
1 | 15 | from collections.abc import Sequence |
2 | 16 | from dataclasses import dataclass |
3 | 17 |
|
|
6 | 20 |
|
7 | 21 | @dataclass(frozen=True) |
8 | 22 | class ToolFilters: |
| 23 | + """Allowlists by which Tools are filtered.""" |
| 24 | + |
9 | 25 | allowed_names: Sequence[str] |
10 | 26 | allowed_tags: Sequence[str] |
11 | 27 |
|
12 | 28 |
|
13 | | -def filter_tools(tools: Sequence[Tool], filters: ToolFilters) -> list[Tool]: |
14 | | - """Filters all tools by allowlists provided by user to the Agent |
| 29 | +def _is_allowed(tool: Tool, filters: ToolFilters) -> bool: |
| 30 | + return ( |
| 31 | + tool.name in filters.allowed_names |
| 32 | + or len(set(filters.allowed_tags).intersection(tool.tags or [])) > 0 |
| 33 | + ) |
15 | 34 |
|
16 | | - TODO: What happens when local and remote tools share names? |
17 | | - Does local overwrite remote (or vice versa)? Do we allow choice between overwriting, |
18 | | - prefixing both or raising exceptions? See tools.py:load_mcp_tools() |
19 | | - """ |
20 | 35 |
|
21 | | - def _predicate(tool: Tool) -> bool: |
22 | | - return ( |
23 | | - tool.name in filters.allowed_names |
24 | | - or len(set(filters.allowed_tags).intersection(tool.tags or [])) > 0 |
25 | | - ) |
| 36 | +def filter_tools(tools: Sequence[Tool], filters: ToolFilters) -> list[Tool]: |
| 37 | + """Filters all tools by allowlists provided by user to the Agent.""" |
26 | 38 |
|
27 | | - filtered_tools = list(filter(_predicate, tools)) |
| 39 | + filtered_tools = [t for t in tools if _is_allowed(t, filters)] |
28 | 40 | return filtered_tools |
0 commit comments