"""{{ description }}"""

import os

import httpx
from fastmcp import FastMCP

mcp = FastMCP(name="{{ service_name }}_mcp")

API_BASE_URL = os.environ.get("{{ service_name | upper }}_API_URL", "https://api.example.com")
API_KEY = os.environ.get("{{ service_name | upper }}_API_KEY", "")


def _get_client() -> httpx.Client:
    """Create an HTTP client with auth headers."""
    headers = {"Accept": "application/json"}
    if API_KEY:
        headers["Authorization"] = f"Bearer {API_KEY}"
    return httpx.Client(base_url=API_BASE_URL, headers=headers, timeout=30.0)


@mcp.tool(
    annotations={
        "title": "Get Resource",
        "readOnlyHint": True,
        "idempotentHint": True,
        "openWorldHint": True,
    }
)
def {{ tool_prefix }}_get(resource_id: str) -> str:
    """Fetch a resource from the API by ID.

    Args:
        resource_id: The unique identifier of the resource to retrieve.
    """
    with _get_client() as client:
        response = client.get(f"/resources/{resource_id}")
        response.raise_for_status()
        data = response.json()
    return f"Resource {resource_id}: {data}"


@mcp.tool(
    annotations={
        "title": "Search Resources",
        "readOnlyHint": True,
        "idempotentHint": True,
        "openWorldHint": True,
    }
)
def {{ tool_prefix }}_search(query: str, limit: int = 10) -> str:
    """Search for resources matching a query.

    Args:
        query: The search query string.
        limit: Maximum number of results to return. Defaults to 10.
    """
    with _get_client() as client:
        response = client.get("/resources", params={"q": query, "limit": limit})
        response.raise_for_status()
        data = response.json()
    results = data if isinstance(data, list) else data.get("results", [])
    if not results:
        return f"No results found for '{query}'."
    lines = [f"Found {len(results)} result(s) for '{query}':"]
    for item in results:
        lines.append(f"  - {item}")
    return "\n".join(lines)


def main():
    """Run the MCP server with stdio transport."""
    mcp.run()


if __name__ == "__main__":
    main()
