Skip to content

Commit 15c8bbc

Browse files
authored
Add new MCP tool for "search with filters" (#202)
* add new tool for search with filters * add link to filter docs * fix: format with ruff * add imports
1 parent 0d45984 commit 15c8bbc

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/deepset_mcp/mcp/tool_registry.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
get_pipeline_logs as get_pipeline_logs_tool,
3838
list_pipelines as list_pipelines_tool,
3939
search_pipeline as search_pipeline_tool,
40+
search_pipeline_with_filters as search_pipeline_with_filters_tool,
4041
update_pipeline as update_pipeline_tool,
4142
validate_pipeline as validate_pipeline_tool,
4243
)
@@ -128,6 +129,10 @@ async def search_docs(query: str) -> str:
128129
search_pipeline_tool,
129130
ToolConfig(needs_client=True, needs_workspace=True, memory_type=MemoryType.EXPLORABLE),
130131
),
132+
"search_pipeline_with_filters": (
133+
search_pipeline_with_filters_tool,
134+
ToolConfig(needs_client=True, needs_workspace=True, memory_type=MemoryType.EXPLORABLE),
135+
),
131136
"list_indexes": (
132137
list_indexes_tool,
133138
ToolConfig(needs_client=True, needs_workspace=True, memory_type=MemoryType.EXPLORABLE),

src/deepset_mcp/tools/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
get_pipeline_logs,
2121
list_pipelines,
2222
search_pipeline,
23+
search_pipeline_with_filters,
2324
update_pipeline,
2425
validate_pipeline,
2526
)
@@ -49,6 +50,7 @@
4950
"get_pipeline_logs",
5051
"deploy_pipeline",
5152
"search_pipeline",
53+
"search_pipeline_with_filters",
5254
"create_pipeline",
5355
"update_pipeline",
5456
"validate_pipeline",

src/deepset_mcp/tools/pipeline.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import asyncio
6+
from typing import Any
67

78
import yaml
89
from pydantic import BaseModel
@@ -367,3 +368,52 @@ async def search_pipeline(
367368
return f"Failed to search using pipeline '{pipeline_name}': {e}"
368369
except Exception as e:
369370
return f"An unexpected error occurred while searching with pipeline '{pipeline_name}': {str(e)}"
371+
372+
373+
async def search_pipeline_with_filters(
374+
*,
375+
client: AsyncClientProtocol,
376+
workspace: str,
377+
pipeline_name: str,
378+
query: str,
379+
filters: dict[str, Any] | None = None,
380+
) -> DeepsetSearchResponse | str:
381+
"""Searches using a pipeline with filters.
382+
383+
Uses the specified pipeline to perform a search with the given query and filters.
384+
Filters follow the Haystack filter syntax: https://docs.haystack.deepset.ai/docs/metadata-filtering.
385+
Before executing the search, checks if the pipeline is deployed (status = DEPLOYED).
386+
Returns search results.
387+
388+
:param client: The async client for API communication.
389+
:param workspace: The workspace name.
390+
:param pipeline_name: Name of the pipeline to use for search.
391+
:param query: The search query to execute.
392+
:param filters: The filters to apply to the search.
393+
394+
:returns: Search results or error message.
395+
"""
396+
try:
397+
# First, check if the pipeline exists and get its status
398+
pipeline = await client.pipelines(workspace=workspace).get(pipeline_name=pipeline_name)
399+
400+
# Check if pipeline is deployed
401+
if pipeline.status != "DEPLOYED":
402+
return (
403+
f"Pipeline '{pipeline_name}' is not deployed (current status: {pipeline.status}). "
404+
f"Please deploy the pipeline first using the deploy_pipeline tool before attempting to search."
405+
)
406+
407+
# Execute the search
408+
return await client.pipelines(workspace=workspace).search(
409+
pipeline_name=pipeline_name, query=query, filters=filters if filters is not None else None
410+
)
411+
412+
except ResourceNotFoundError:
413+
return f"There is no pipeline named '{pipeline_name}' in workspace '{workspace}'."
414+
except BadRequestError as e:
415+
return f"Failed to search using pipeline '{pipeline_name}': {e}"
416+
except UnexpectedAPIError as e:
417+
return f"Failed to search using pipeline '{pipeline_name}': {e}"
418+
except Exception as e:
419+
return f"An unexpected error occurred while searching with pipeline '{pipeline_name}': {str(e)}"

0 commit comments

Comments
 (0)