from __future__ import annotations

"""Protocol types for MCP transport."""

from dataclasses import dataclass, field
from typing import Any

from agentrspy.core import ToolOutput


@dataclass(slots=True)
class McpMessage:
    """MCP JSON-RPC message envelope."""

    jsonrpc: str = "2.0"
    id: int | None = None
    method: str | None = None
    params: Any | None = None
    result: Any | None = None
    error: Any | None = None


@dataclass(slots=True)
class McpTool:
    """Tool metadata returned by MCP servers."""

    name: str
    description: str = ""
    input_schema: Any = field(default_factory=dict)


@dataclass(slots=True)
class McpCallToolResult:
    """Tool call result payload."""

    content: list[Any]
    is_error: bool = False

    def into_tool_output(self) -> ToolOutput:
        text = "\n".join(
            item.get("text", "")
            for item in self.content
            if isinstance(item, dict) and isinstance(item.get("text"), str)
        )
        output = ToolOutput.text(text)
        output.is_error = self.is_error
        return output
