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
81 changes: 81 additions & 0 deletions src/deepset_mcp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
list_component_families as list_component_families_tool,
search_component_definition as search_component_definition_tool,
)
from deepset_mcp.tools.indexes import (
create_index as create_index_tool,
get_index as get_index_tool,
list_indexes as list_indexes_tool,
update_index as update_index_tool,
)
from deepset_mcp.tools.pipeline import (
create_pipeline as create_pipeline_tool,
get_pipeline as get_pipeline_tool,
Expand Down Expand Up @@ -196,6 +202,81 @@ async def search_component_definitions(query: str) -> str:
return response


@mcp.tool()
async def list_indexes() -> str:
"""Retrieves a list of all indexes available in the deepset workspace.

Use this to get an overview of existing indexes and their configurations.
The response includes basic information for each index.
"""
workspace = get_workspace()
async with AsyncDeepsetClient() as client:
response = await list_indexes_tool(client=client, workspace=workspace)
return response


@mcp.tool()
async def get_index(index_name: str) -> str:
"""Fetches detailed configuration information for a specific index.

Use this to get the full configuration and details of a single index.

:param index_name: The name of the index to fetch.
"""
workspace = get_workspace()
async with AsyncDeepsetClient() as client:
response = await get_index_tool(client=client, workspace=workspace, index_name=index_name)
return response


@mcp.tool()
async def create_index(index_name: str, yaml_configuration: str, description: str | None = None) -> str:
"""Creates a new index in the deepset workspace.

Use this to create a new index with the given configuration.
Make sure the YAML configuration is valid before creating the index.

:param index_name: The name for the new index.
:param yaml_configuration: YAML configuration for the index.
:param description: Optional description for the index.
"""
workspace = get_workspace()
async with AsyncDeepsetClient() as client:
response = await create_index_tool(
client=client,
workspace=workspace,
index_name=index_name,
yaml_configuration=yaml_configuration,
description=description,
)
return response


@mcp.tool()
async def update_index(
index_name: str, updated_index_name: str | None = None, yaml_configuration: str | None = None
) -> str:
"""Updates an existing index in the deepset workspace.

Use this to update the name or configuration of an existing index.
You must provide at least one of updated_index_name or yaml_configuration.

:param index_name: The name of the index to update.
:param updated_index_name: Optional new name for the index.
:param yaml_configuration: Optional new YAML configuration.
"""
workspace = get_workspace()
async with AsyncDeepsetClient() as client:
response = await update_index_tool(
client=client,
workspace=workspace,
index_name=index_name,
updated_index_name=updated_index_name,
yaml_configuration=yaml_configuration,
)
return response


#
#
# @mcp.tool()
Expand Down
32 changes: 32 additions & 0 deletions src/deepset_mcp/tools/formatting_utils_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from deepset_mcp.api.indexes.models import Index, IndexList


def index_to_llm_readable_string(index: Index) -> str:
"""Creates a string representation of an index that is readable by LLMs."""
index_parts = [
f"""<index name="{index.name}" id="{index.pipeline_index_id}">

### Basic Information

**Name:** {index.name}
**ID:** {index.pipeline_index_id}
**Description:** {index.description if index.description else "No description provided"}\n'
"""
]

if index.config_yaml is not None:
index_parts.append("\n### Index Configuration")
index_parts.append(f"\n```yaml\n{index.config_yaml}\n```")

index_parts.append(f'\n</index name="{index.name}" id="{index.pipeline_index_id}">')

return "\n".join(index_parts)


def index_list_to_llm_readable_string(index_list: IndexList) -> str:
"""Creates a string representation of a list of indexes that is readable by LLMs."""
if not index_list.data:
return "No indexes found."

index_strings = [index_to_llm_readable_string(index) for index in index_list.data]
return "\n\n".join(index_strings)
70 changes: 70 additions & 0 deletions src/deepset_mcp/tools/indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from deepset_mcp.api.exceptions import BadRequestError, ResourceNotFoundError, UnexpectedAPIError
from deepset_mcp.api.protocols import AsyncClientProtocol
from deepset_mcp.tools.formatting_utils_index import index_list_to_llm_readable_string, index_to_llm_readable_string


async def list_indexes(client: AsyncClientProtocol, workspace: str) -> str:
"""Retrieves a list of all indexes available within the currently configured deepset workspace."""
response = await client.indexes(workspace=workspace).list()
return index_list_to_llm_readable_string(response)


async def get_index(client: AsyncClientProtocol, workspace: str, index_name: str) -> str:
"""Fetches detailed configuration information for a specific index, identified by its unique `index_name`."""
try:
response = await client.indexes(workspace=workspace).get(index_name)
except ResourceNotFoundError:
return f"There is no index named '{index_name}'. Did you mean to create it?"

return index_to_llm_readable_string(response)


async def create_index(
client: AsyncClientProtocol,
workspace: str,
index_name: str,
yaml_configuration: str,
description: str | None = None,
) -> str:
"""Creates a new index within the currently configured deepset workspace."""
try:
await client.indexes(workspace=workspace).create(
name=index_name, yaml_config=yaml_configuration, description=description
)
except ResourceNotFoundError:
return f"There is no workspace named '{workspace}'. Did you mean to configure it?"
except BadRequestError as e:
return f"Failed to create index '{index_name}': {e}"
except UnexpectedAPIError as e:
return f"Failed to create index '{index_name}': {e}"

return f"Index '{index_name}' created successfully."


async def update_index(
client: AsyncClientProtocol,
workspace: str,
index_name: str,
updated_index_name: str | None = None,
yaml_configuration: str | None = None,
) -> str:
"""Updates an existing index in the specified workspace.

This function can update either the name or the configuration of an existing index, or both.
At least one of updated_index_name or yaml_configuration must be provided.
"""
if not updated_index_name and not yaml_configuration:
return "You must provide either a new name or a new configuration to update the index."

try:
await client.indexes(workspace=workspace).update(
index_name=index_name, updated_index_name=updated_index_name, yaml_config=yaml_configuration
)
except ResourceNotFoundError:
return f"There is no index named '{index_name}'. Did you mean to create it?"
except BadRequestError as e:
return f"Failed to update index '{index_name}': {e}"
except UnexpectedAPIError as e:
return f"Failed to update index '{index_name}': {e}"

return f"Index '{index_name}' updated successfully."
Loading