Skip to content

datarobot_genai.drmcp.core.utils

utils

get_prompt_tags

get_prompt_tags(prompt: Prompt) -> set[str]

Extract tags from a prompt.

Parameters:

Name Type Description Default
prompt Prompt

MCP protocol Prompt

required
Returns
Set of tag strings, empty set if no tags found
Source code in datarobot_genai/drmcp/core/utils.py
def get_prompt_tags(prompt: MCPPrompt) -> set[str]:
    """
    Extract tags from a prompt.

    Args:
        prompt: MCP protocol Prompt

    Returns
    -------
        Set of tag strings, empty set if no tags found
    """
    # MCPPrompt has tags in meta.fastmcp.tags (fastmcp 3.x)
    if not (prompt.meta and isinstance(prompt.meta, dict)):
        return set()

    fastmcp_meta = prompt.meta.get("fastmcp")
    if not (fastmcp_meta and isinstance(fastmcp_meta, dict)):
        return set()

    tags = fastmcp_meta.get("tags")
    return set(tags) if tags else set()

get_resource_tags

get_resource_tags(resource: Resource) -> set[str]

Extract tags from a resource.

Parameters:

Name Type Description Default
resource Resource

MCP protocol Resource

required
Returns
Set of tag strings, empty set if no tags found
Source code in datarobot_genai/drmcp/core/utils.py
def get_resource_tags(resource: MCPResource) -> set[str]:
    """
    Extract tags from a resource.

    Args:
        resource: MCP protocol Resource

    Returns
    -------
        Set of tag strings, empty set if no tags found
    """
    # MCPResource has tags in meta.fastmcp.tags (fastmcp 3.x)
    if not (resource.meta and isinstance(resource.meta, dict)):
        return set()

    fastmcp_meta = resource.meta.get("fastmcp")
    if not (fastmcp_meta and isinstance(fastmcp_meta, dict)):
        return set()

    tags = fastmcp_meta.get("tags")
    return set(tags) if tags else set()

get_tool_tags

get_tool_tags(tool: Tool | Tool) -> set[str]

Extract tags from a tool, handling both FastMCP Tool and MCP protocol Tool types.

Parameters:

Name Type Description Default
tool Tool | Tool

Either a FastMCP Tool or MCP protocol Tool

required
Returns
Set of tag strings, empty set if no tags found
Source code in datarobot_genai/drmcp/core/utils.py
def get_tool_tags(tool: Tool | MCPTool) -> set[str]:
    """
    Extract tags from a tool, handling both FastMCP Tool and MCP protocol Tool types.

    Args:
        tool: Either a FastMCP Tool or MCP protocol Tool

    Returns
    -------
        Set of tag strings, empty set if no tags found
    """
    if isinstance(tool, Tool):
        # FastMCP Tool has tags directly as a set
        return getattr(tool, "tags", None) or set()

    # MCPTool has tags in meta.fastmcp.tags (fastmcp 3.x)
    if not (tool.meta and isinstance(tool.meta, dict)):
        return set()

    fastmcp_meta = tool.meta.get("fastmcp")
    if not (fastmcp_meta and isinstance(fastmcp_meta, dict)):
        return set()

    tags = fastmcp_meta.get("tags")
    return set(tags) if tags else set()

filter_tools_by_tags

filter_tools_by_tags(*, tools: list[Tool | Tool], tags: list[str] | None = None, match_all: bool = False) -> list[Tool | MCPTool]

Filter tools by tags.

Parameters:

Name Type Description Default
tools list[Tool | Tool]

List of tools to filter

required
tags list[str] | None

List of tags to filter by. If None, returns all tools

None
match_all bool

If True, tool must have all specified tags. If False, tool must have at least one tag.

False
Returns
List of tools that match the tag criteria
Source code in datarobot_genai/drmcp/core/utils.py
def filter_tools_by_tags(
    *,
    tools: list[Tool | MCPTool],
    tags: list[str] | None = None,
    match_all: bool = False,
) -> list[Tool | MCPTool]:
    """
    Filter tools by tags.

    Args:
        tools: List of tools to filter
        tags: List of tags to filter by. If None, returns all tools
        match_all: If True, tool must have all specified tags. If False, tool must have at least
            one tag.

    Returns
    -------
        List of tools that match the tag criteria
    """
    if not tags:
        return tools

    # Convert tags to set for O(1) lookup instead of O(n)
    tags_set = set(tags)

    return [
        tool
        for tool in tools
        if (tool_tags := get_tool_tags(tool))
        and (tags_set.issubset(tool_tags) if match_all else tags_set & tool_tags)
    ]