feat/enh: tool server function name filter list
This commit is contained in:
@@ -24,6 +24,7 @@ from fastapi.responses import HTMLResponse
|
||||
from starlette.responses import Response, StreamingResponse, JSONResponse
|
||||
|
||||
|
||||
from open_webui.utils.misc import is_string_allowed
|
||||
from open_webui.models.oauth_sessions import OAuthSessions
|
||||
from open_webui.models.chats import Chats
|
||||
from open_webui.models.folders import Folders
|
||||
@@ -1408,6 +1409,9 @@ async def process_chat_payload(request, form_data, user, metadata, model):
|
||||
headers=headers if headers else None,
|
||||
)
|
||||
|
||||
function_name_filter_list = mcp_server_connection.get(
|
||||
"function_name_filter_list", None
|
||||
)
|
||||
tool_specs = await mcp_clients[server_id].list_tool_specs()
|
||||
for tool_spec in tool_specs:
|
||||
|
||||
@@ -1420,6 +1424,15 @@ async def process_chat_payload(request, form_data, user, metadata, model):
|
||||
|
||||
return tool_function
|
||||
|
||||
if function_name_filter_list and isinstance(
|
||||
function_name_filter_list, list
|
||||
):
|
||||
if not is_string_allowed(
|
||||
tool_spec["name"], function_name_filter_list
|
||||
):
|
||||
# Skip this function
|
||||
continue
|
||||
|
||||
tool_function = make_tool_function(
|
||||
mcp_clients[server_id], tool_spec["name"]
|
||||
)
|
||||
@@ -1460,6 +1473,7 @@ async def process_chat_payload(request, form_data, user, metadata, model):
|
||||
"__files__": metadata.get("files", []),
|
||||
},
|
||||
)
|
||||
|
||||
if mcp_tools_dict:
|
||||
tools_dict = {**tools_dict, **mcp_tools_dict}
|
||||
|
||||
|
||||
@@ -27,6 +27,45 @@ def deep_update(d, u):
|
||||
return d
|
||||
|
||||
|
||||
def get_allow_block_lists(filter_list):
|
||||
allow_list = []
|
||||
block_list = []
|
||||
|
||||
if filter_list:
|
||||
for d in filter_list:
|
||||
if d.startswith("!"):
|
||||
# Domains starting with "!" → blocked
|
||||
block_list.append(d[1:])
|
||||
else:
|
||||
# Domains starting without "!" → allowed
|
||||
allow_list.append(d)
|
||||
|
||||
return allow_list, block_list
|
||||
|
||||
|
||||
def is_string_allowed(string: str, filter_list: Optional[list[str]] = None) -> bool:
|
||||
"""
|
||||
Checks if a string is allowed based on the provided filter list.
|
||||
:param string: The string to check (e.g., domain or hostname).
|
||||
:param filter_list: List of allowed/blocked strings. Strings starting with "!" are blocked.
|
||||
:return: True if the string is allowed, False otherwise.
|
||||
"""
|
||||
if not filter_list:
|
||||
return True
|
||||
|
||||
allow_list, block_list = get_allow_block_lists(filter_list)
|
||||
# If allow list is non-empty, require domain to match one of them
|
||||
if allow_list:
|
||||
if not any(string.endswith(allowed) for allowed in allow_list):
|
||||
return False
|
||||
|
||||
# Block list always removes matches
|
||||
if any(string.endswith(blocked) for blocked in block_list):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def get_message_list(messages_map, message_id):
|
||||
"""
|
||||
Reconstructs a list of messages in order up to the specified message_id.
|
||||
|
||||
@@ -34,6 +34,7 @@ from langchain_core.utils.function_calling import (
|
||||
)
|
||||
|
||||
|
||||
from open_webui.utils.misc import is_string_allowed
|
||||
from open_webui.models.tools import Tools
|
||||
from open_webui.models.users import UserModel
|
||||
from open_webui.utils.plugin import load_tool_module_by_id
|
||||
@@ -149,8 +150,20 @@ async def get_tools(
|
||||
)
|
||||
|
||||
specs = tool_server_data.get("specs", [])
|
||||
function_name_filter_list = tool_server_connection.get(
|
||||
"function_name_filter_list", None
|
||||
)
|
||||
|
||||
for spec in specs:
|
||||
function_name = spec["name"]
|
||||
if function_name_filter_list and isinstance(
|
||||
function_name_filter_list, list
|
||||
):
|
||||
if not is_string_allowed(
|
||||
function_name, function_name_filter_list
|
||||
):
|
||||
# Skip this function
|
||||
continue
|
||||
|
||||
auth_type = tool_server_connection.get("auth_type", "bearer")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user