fix(server): make FastMCP-2.x start correctly

• Replace deprecated `lifespan_kwargs=` with functools.partial
• Drop extra asyncio layer – call `server.run()` directly
• Add missing `functools` import

Now `python -m kicad_mcp.server` and `kicad-mcp` block and run cleanly.
This commit is contained in:
Lama 2025-07-18 14:34:41 -04:00
parent d7c269211a
commit af947ee182

View File

@ -5,6 +5,7 @@ import atexit
import os import os
import signal import signal
import logging import logging
import functools
from typing import Callable from typing import Callable
from fastmcp import FastMCP from fastmcp import FastMCP
@ -127,9 +128,11 @@ def create_server() -> FastMCP:
# Always print this now, as we rely on CLI # Always print this now, as we rely on CLI
logging.info(f"KiCad Python module setup removed; relying on kicad-cli for external operations.") logging.info(f"KiCad Python module setup removed; relying on kicad-cli for external operations.")
# Build a lifespan callable with the kwarg baked in (FastMCP 2.x dropped lifespan_kwargs)
lifespan_factory = functools.partial(kicad_lifespan, kicad_modules_available=kicad_modules_available)
# Initialize FastMCP server # Initialize FastMCP server
# Pass the availability flag (always False now) to the lifespan context mcp = FastMCP("KiCad", lifespan=lifespan_factory)
mcp = FastMCP("KiCad", lifespan=kicad_lifespan, lifespan_kwargs={"kicad_modules_available": kicad_modules_available})
logging.info(f"Created FastMCP server instance with lifespan management") logging.info(f"Created FastMCP server instance with lifespan management")
# Register resources # Register resources
@ -207,15 +210,15 @@ def setup_logging() -> None:
) )
async def main() -> None: def main() -> None:
"""Main server entry point.""" """Start the KiCad MCP server (blocking)."""
setup_logging() setup_logging()
logging.info("Starting KiCad MCP server...") logging.info("Starting KiCad MCP server...")
server = create_server() server = create_server()
try: try:
await server.run() server.run() # FastMCP manages its own event loop
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info("Server interrupted by user") logging.info("Server interrupted by user")
except Exception as e: except Exception as e:
@ -225,5 +228,4 @@ async def main() -> None:
if __name__ == "__main__": if __name__ == "__main__":
import asyncio main()
asyncio.run(main())