File size: 2,132 Bytes
ac05b4f
1fd9ef4
ac05b4f
 
1fd9ef4
 
ac05b4f
 
1fd9ef4
ac05b4f
 
 
 
 
 
1fd9ef4
 
ac05b4f
1fd9ef4
 
 
 
 
 
 
9f3407d
 
 
 
 
 
 
 
 
 
 
 
 
 
1fd9ef4
 
ab9ff97
 
a4e6b6b
1fd9ef4
ac05b4f
 
 
 
1fd9ef4
ac05b4f
 
ab9ff97
ac05b4f
ab9ff97
ac05b4f
1fd9ef4
ac05b4f
1fd9ef4
ac05b4f
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""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()