Skip to content

conversions

Class info

🛈 DocStrings

Conversions between internal and MCP types.

from_mcp_uri

from_mcp_uri(uri: str) -> str

Convert MCP URI to internal format.

Source code in src/mcp_server_llmling/conversions.py
def from_mcp_uri(uri: str) -> str:
    """Convert MCP URI to internal format."""
    try:
        if not uri:
            msg = "URI cannot be empty"
            raise ValueError(msg)  # noqa: TRY301

        try:
            scheme, rest = uri.split("://", 1)
        except ValueError as exc:
            msg = f"Invalid URI format: {uri}"
            raise ValueError(msg) from exc

        match scheme:
            case "http" | "https":
                return uri.rstrip("/")

            case "file":
                # Remove host part and decode path
                path = rest.split("/", 1)[1] if "host/" in rest else rest
                parts = [urllib.parse.unquote(p) for p in path.split("/")]
                path = _denormalize_windows_path("/".join(parts))
                return f"file:///{path}"

            case "resource":
                name = rest.split("/", 1)[1] if "/" in rest else rest
                return urllib.parse.unquote(name)

            case _:
                msg = f"Unsupported URI scheme: {scheme}"
                raise ValueError(msg)  # noqa: TRY301

    except Exception as exc:
        if isinstance(exc, ValueError):
            raise
        msg = f"Failed to convert URI {uri!r}"
        raise ValueError(msg) from exc

to_mcp_argument

to_mcp_argument(arg: PromptParameter) -> PromptArgument

Convert to MCP PromptArgument.

Source code in src/mcp_server_llmling/conversions.py
def to_mcp_argument(arg: PromptParameter) -> types.PromptArgument:
    """Convert to MCP PromptArgument."""
    return types.PromptArgument(
        name=arg.name,
        description=arg.description,
        required=arg.required,
    )

to_mcp_message

to_mcp_message(msg: PromptMessage) -> PromptMessage

Convert internal PromptMessage to MCP PromptMessage.

Source code in src/mcp_server_llmling/conversions.py
def to_mcp_message(msg: PromptMessage) -> types.PromptMessage:
    """Convert internal PromptMessage to MCP PromptMessage."""
    role: types.Role = "assistant" if msg.role == "assistant" else "user"
    content = types.TextContent(type="text", text=msg.get_text_content())
    return types.PromptMessage(role=role, content=content)

to_mcp_prompt

to_mcp_prompt(prompt: BasePrompt) -> Prompt

Convert to MCP Prompt.

Source code in src/mcp_server_llmling/conversions.py
def to_mcp_prompt(prompt: BasePrompt) -> types.Prompt:
    """Convert to MCP Prompt."""
    if prompt.name is None:
        msg = "Prompt name not set. This should be set during registration."
        raise ValueError(msg)
    args = [to_mcp_argument(arg) for arg in prompt.arguments]
    return types.Prompt(name=prompt.name, description=prompt.description, arguments=args)

to_mcp_resource

to_mcp_resource(resource: LoadedResource) -> Resource

Convert LoadedResource to MCP Resource.

Source code in src/mcp_server_llmling/conversions.py
def to_mcp_resource(resource: LoadedResource) -> types.Resource:
    """Convert LoadedResource to MCP Resource."""
    return types.Resource(
        uri=to_mcp_uri(resource.metadata.uri),
        name=resource.metadata.name or "",
        description=resource.metadata.description,
        mimeType=resource.metadata.mime_type,
    )

to_mcp_tool

to_mcp_tool(tool: LLMCallableTool) -> Tool

Convert internal Tool to MCP Tool.

Source code in src/mcp_server_llmling/conversions.py
def to_mcp_tool(tool: LLMCallableTool) -> types.Tool:
    """Convert internal Tool to MCP Tool."""
    schema = tool.get_schema()
    return types.Tool(
        name=schema["function"]["name"],
        description=schema["function"]["description"],
        inputSchema=schema["function"]["parameters"],  # pyright: ignore
        annotations=types.ToolAnnotations(
            title=tool.name,
            readOnlyHint=tool.hints.read_only if tool.hints else None,
            destructiveHint=tool.hints.destructive if tool.hints else None,
            idempotentHint=tool.hints.idempotent if tool.hints else None,
            openWorldHint=tool.hints.open_world if tool.hints else None,
        ),
    )

to_mcp_uri

to_mcp_uri(uri: str) -> AnyUrl

Convert internal URI to MCP-compatible AnyUrl.

Source code in src/mcp_server_llmling/conversions.py
def to_mcp_uri(uri: str) -> AnyUrl:
    """Convert internal URI to MCP-compatible AnyUrl."""
    try:
        if not uri:
            msg = "URI cannot be empty"
            raise ValueError(msg)  # noqa: TRY301

        try:
            scheme, rest = uri.split("://", 1)
        except ValueError as exc:
            msg = f"Invalid URI format: {uri}"
            raise ValueError(msg) from exc

        match scheme:
            case "http" | "https":
                return AnyUrl(uri)

            case "file":
                path = _normalize_windows_path(rest.lstrip("/"))
                if not path:
                    msg = "Empty path in file URI"
                    raise ValueError(msg)  # noqa: TRY301
                parts = path.split("/")
                encoded = [urllib.parse.quote(part) for part in parts if part]
                return AnyUrl(f"file://host/{'/'.join(encoded)}")

            case "text" | "python" | "cli" | "callable" | "image":
                name = urllib.parse.quote(rest)
                return AnyUrl(f"resource://host/{name}")

            case _:
                msg = f"Unsupported URI scheme: {scheme}"
                raise ValueError(msg)  # noqa: TRY301

    except Exception as exc:
        if isinstance(exc, ValueError):
            raise
        msg = f"Failed to convert URI {uri!r} to MCP format"
        raise ValueError(msg) from exc