"""A thin MCP tool; the caller owns the HTTP client and its lifetime."""
from typing import Annotated

from mcp.server.fastmcp import FastMCP
from mcp.server.fastmcp.exceptions import ToolError
from pydantic import Field

from http_api_client import Artifact, ArtifactClient, AdapterError, ID_PATTERN


def make_server(client: ArtifactClient) -> FastMCP:
    server = FastMCP("Inventory practice")

    @server.tool()
    async def get_artifact(
        item_id: Annotated[str, Field(pattern=ID_PATTERN, strict=True)],
    ) -> Artifact:
        """Read one inventory item by an identifier such as KIT-42."""
        try:
            return await client.get_artifact(item_id)
        except AdapterError as error:
            raise ToolError(str(error)) from None

    return server
