from __future__ import annotations

"""Adapter that exposes MCP tools as local SDK tools."""

import asyncio
from dataclasses import dataclass
from typing import Any, Protocol

from agentrspy.core import Tool, ToolOutput

from .protocol import McpTool


class _ClientProtocol(Protocol):
    async def call_tool(self, name: str, arguments: Any) -> ToolOutput: ...


@dataclass(slots=True)
class McpToolAdapter(Tool):
    """Adapt an MCP tool definition to the local `Tool` trait."""

    client: _ClientProtocol
    definition: McpTool
    lock: asyncio.Lock

    def name(self) -> str:
        return self.definition.name

    def description(self) -> str:
        return self.definition.description

    def schema(self) -> dict:
        return dict(self.definition.input_schema or {})

    async def call(self, input_value: Any) -> ToolOutput:
        async with self.lock:
            return await self.client.call_tool(self.definition.name, input_value)
