Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions mcpgateway/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5853,6 +5853,7 @@ async def admin_search_tools(
q: str = Query("", description="Search query"),
include_inactive: bool = False,
limit: int = Query(100, ge=1, le=1000, description="Maximum number of results to return"),
gateway_id: Optional[str] = Query(None, description="Filter by gateway ID(s), comma-separated"),
db: Session = Depends(get_db),
user=Depends(get_current_user_with_permissions),
):
Expand All @@ -5866,6 +5867,7 @@ async def admin_search_tools(
q (str): Search query string to match against tool names, IDs, or descriptions
include_inactive (bool): Whether to include inactive tools in the search results
limit (int): Maximum number of results to return (1-1000)
gateway_id (Optional[str]): Filter by gateway ID(s), comma-separated
db (Session): Database session dependency
user: Current user making the request

Expand All @@ -5886,6 +5888,24 @@ async def admin_search_tools(

query = select(DbTool.id, DbTool.original_name, DbTool.custom_name, DbTool.display_name, DbTool.description)

# Apply gateway filter if provided. Support special sentinel 'null' to
# request tools with NULL gateway_id (e.g., RestTool/no gateway).
if gateway_id:
gateway_ids = [gid.strip() for gid in gateway_id.split(",") if gid.strip()]
if gateway_ids:
# Treat literal 'null' (case-insensitive) as a request for NULL gateway_id
null_requested = any(gid.lower() == "null" for gid in gateway_ids)
non_null_ids = [gid for gid in gateway_ids if gid.lower() != "null"]
if non_null_ids and null_requested:
query = query.where(or_(DbTool.gateway_id.in_(non_null_ids), DbTool.gateway_id.is_(None)))
LOGGER.debug(f"Filtering tool search by gateway IDs (including NULL): {non_null_ids} + NULL")
elif null_requested:
query = query.where(DbTool.gateway_id.is_(None))
LOGGER.debug("Filtering tool search by NULL gateway_id (RestTool)")
else:
query = query.where(DbTool.gateway_id.in_(non_null_ids))
LOGGER.debug(f"Filtering tool search by gateway IDs: {non_null_ids}")

if not include_inactive:
query = query.where(DbTool.enabled.is_(True))

Expand Down Expand Up @@ -6396,6 +6416,7 @@ async def admin_search_resources(
q: str = Query("", description="Search query"),
include_inactive: bool = False,
limit: int = Query(100, ge=1, le=1000),
gateway_id: Optional[str] = Query(None, description="Filter by gateway ID(s), comma-separated"),
db: Session = Depends(get_db),
user=Depends(get_current_user_with_permissions),
):
Expand All @@ -6409,6 +6430,7 @@ async def admin_search_resources(
q (str): Search query string.
include_inactive (bool): When True include resources that are inactive.
limit (int): Maximum number of results to return (bounded by the query parameter).
gateway_id (Optional[str]): Filter by gateway ID(s), comma-separated.
db (Session): Database session (injected dependency).
user: Authenticated user object from dependency injection.

Expand All @@ -6427,6 +6449,23 @@ async def admin_search_resources(
team_ids = [t.id for t in user_teams]

query = select(DbResource.id, DbResource.name, DbResource.description)

# Apply gateway filter if provided
if gateway_id:
gateway_ids = [gid.strip() for gid in gateway_id.split(",") if gid.strip()]
if gateway_ids:
null_requested = any(gid.lower() == "null" for gid in gateway_ids)
non_null_ids = [gid for gid in gateway_ids if gid.lower() != "null"]
if non_null_ids and null_requested:
query = query.where(or_(DbResource.gateway_id.in_(non_null_ids), DbResource.gateway_id.is_(None)))
LOGGER.debug(f"Filtering resource search by gateway IDs (including NULL): {non_null_ids} + NULL")
elif null_requested:
query = query.where(DbResource.gateway_id.is_(None))
LOGGER.debug("Filtering resource search by NULL gateway_id")
else:
query = query.where(DbResource.gateway_id.in_(non_null_ids))
LOGGER.debug(f"Filtering resource search by gateway IDs: {non_null_ids}")

if not include_inactive:
query = query.where(DbResource.enabled.is_(True))

Expand Down Expand Up @@ -6459,6 +6498,7 @@ async def admin_search_prompts(
q: str = Query("", description="Search query"),
include_inactive: bool = False,
limit: int = Query(100, ge=1, le=1000),
gateway_id: Optional[str] = Query(None, description="Filter by gateway ID(s), comma-separated"),
db: Session = Depends(get_db),
user=Depends(get_current_user_with_permissions),
):
Expand All @@ -6472,6 +6512,7 @@ async def admin_search_prompts(
q (str): Search query string.
include_inactive (bool): When True include prompts that are inactive.
limit (int): Maximum number of results to return (bounded by the query parameter).
gateway_id (Optional[str]): Filter by gateway ID(s), comma-separated.
db (Session): Database session (injected dependency).
user: Authenticated user object from dependency injection.

Expand All @@ -6490,6 +6531,23 @@ async def admin_search_prompts(
team_ids = [t.id for t in user_teams]

query = select(DbPrompt.id, DbPrompt.name, DbPrompt.description)

# Apply gateway filter if provided
if gateway_id:
gateway_ids = [gid.strip() for gid in gateway_id.split(",") if gid.strip()]
if gateway_ids:
null_requested = any(gid.lower() == "null" for gid in gateway_ids)
non_null_ids = [gid for gid in gateway_ids if gid.lower() != "null"]
if non_null_ids and null_requested:
query = query.where(or_(DbPrompt.gateway_id.in_(non_null_ids), DbPrompt.gateway_id.is_(None)))
LOGGER.debug(f"Filtering prompt search by gateway IDs (including NULL): {non_null_ids} + NULL")
elif null_requested:
query = query.where(DbPrompt.gateway_id.is_(None))
LOGGER.debug("Filtering prompt search by NULL gateway_id")
else:
query = query.where(DbPrompt.gateway_id.in_(non_null_ids))
LOGGER.debug(f"Filtering prompt search by gateway IDs: {non_null_ids}")

if not include_inactive:
query = query.where(DbPrompt.enabled.is_(True))

Expand Down
Loading
Loading