"""
FrankenClaw — MCP Tool Server

Standalone tool server that gives any MCP-capable agent hands:
search, vision, browser, web scraping, Google Drive, NotebookLM, and
Shopify. Models and providers are configurable per tool.

Transport: stdio (the MCP client spawns this process)
Framework: FastMCP (Python)
Architecture: Pure function. Request in, result out. No memory, no opinions.

Tools are auto-discovered from the tools/ directory. Drop a Python file in
there, define an async function with a docstring, restart FrankenClaw — done.
No edits to this file needed.

That's SPARC.
"""

import importlib
import inspect
import pkgutil
import sys

from mcp.server.fastmcp import FastMCP

import tools

# Create the server
mcp = FastMCP("FrankenClaw")

# Auto-discover tools from the tools/ package.
# Any async function in tools/*.py that doesn't start with _ gets registered.
# A module that fails to import (missing optional deps, bad credentials path,
# etc.) is logged and skipped — the rest of the server still comes up.
for importer, modname, ispkg in pkgutil.iter_modules(tools.__path__):
    try:
        module = importlib.import_module(f"tools.{modname}")
    except Exception as e:
        print(
            f"[frankenclaw] Skipping tools/{modname}.py — import failed: {e}",
            file=sys.stderr,
        )
        continue
    for name, func in inspect.getmembers(module, inspect.iscoroutinefunction):
        if not name.startswith("_"):
            mcp.tool()(func)


def main():
    """Boot FrankenClaw on stdio transport."""
    mcp.run(transport="stdio")


if __name__ == "__main__":
    main()
