Spaces:
Running
Running
"""FoodWise MCP Server - Minimal FastMCP Implementation. | |
This follows FastMCP best practices for simplicity and maintainability. | |
Configuration is handled via environment variables or CLI arguments. | |
Usage: | |
# Basic usage | |
python -m foodwise.mcp_server | |
# With custom log level | |
MCP_LOG_LEVEL=DEBUG python -m foodwise.mcp_server | |
# Using FastMCP CLI with different transports | |
fastmcp run src/foodwise/mcp_server/main.py | |
fastmcp run src/foodwise/mcp_server/main.py --transport http --port 8080 | |
""" | |
import os | |
import sys | |
from pathlib import Path | |
# Add the src directory to Python path to ensure imports work | |
src_path = Path(__file__).parent.parent.parent | |
sys.path.insert(0, str(src_path)) | |
# Ensure environment variables from .env are available when running directly | |
try: | |
from dotenv import load_dotenv | |
# Load from project root if present | |
project_root = Path(__file__).resolve().parents[3] | |
env_path = project_root / ".env" | |
if env_path.exists(): | |
load_dotenv(env_path) | |
else: | |
load_dotenv() | |
except Exception: | |
# Continue without dotenv if not installed | |
pass | |
from fastmcp import FastMCP | |
from .fastmcp_tools import register_inventory_tools, register_shopping_tools | |
from .fastmcp_resources import register_resources, register_shopping_resources | |
from .fastmcp_prompts import register_prompts | |
# Create the MCP server - FastMCP handles all configuration automatically | |
# Server name can be overridden via MCP_SERVER_NAME environment variable | |
server_name = os.getenv("MCP_SERVER_NAME", "FoodWise") | |
mcp = FastMCP(server_name) | |
# Register all MCP primitives | |
register_inventory_tools(mcp) | |
register_shopping_tools(mcp) | |
register_resources(mcp) | |
register_shopping_resources(mcp) | |
register_prompts(mcp) | |
# FastMCP best practice: only run when executed directly, not when imported | |
if __name__ == "__main__": | |
# FastMCP handles transport, logging, and all other configuration | |
# Use CLI arguments or environment variables for customization: | |
# - MCP_LOG_LEVEL=DEBUG for verbose logging | |
# - fastmcp run main.py --transport http for HTTP transport | |
mcp.run() | |