Provides comprehensive food hierarchy and nutrition data tools through MongoDB Atlas integration, enabling queries for food categories, subcategories, nutritional information, and dataset statistics.
Food MCP Server - StreamableHTTP Architecture with Structured Output
A Model Context Protocol (MCP) server providing comprehensive food hierarchy and nutrition data with structured output using Pydantic schemas, served over StreamableHTTP transport.
๐๏ธ Architecture
This server uses the MCP Python SDK with StreamableHTTP transport for web-based access, providing structured output through Pydantic schemas that ensure consistent data validation and serialization.
Key Features
Structured Output: All tool responses use Pydantic schemas for validation and serialization
StreamableHTTP Transport: Web-accessible MCP server with StreamableHTTP transport
MongoDB Atlas Integration: Cloud-based data storage for scalability
Docker Support: Containerized deployment with optimized builds
Comprehensive Tools: 11 tools covering food hierarchy and nutrition data
๐ ๏ธ Available Tools
Food Hierarchy Tools
get_all_food_hierarchy - Complete food hierarchy dataset
get_categories - List all food categories
get_subcategories - Subcategories for a category
get_food_items - Food items in category/subcategory
search_food - Search food items by keyword
find_food_category - Find category for specific food
list_all_foods - All unique food names
food_stats - Dataset statistics
Food Nutrition Tools
list_food_names - Foods with nutrition data
get_food_nutrition - Complete nutrition info for food
search_food_nutrition - Search nutrition by keyword
๐ Structured Output Schemas
All tools return structured data using Pydantic schemas:
Food Hierarchy Schemas (schemas/food_hierarchy.py)
FoodHierarchyResponse- Complete hierarchy dataFoodCategoriesResponse- Category listingsFoodSearchResponse- Search results with contextFoodStats- Dataset statistics
Food Nutrition Schemas (schemas/food_item.py)
FoodNutritionResponse- Complete nutrition dataFoodNutrition- Detailed nutrition with serving sizesServingInfo- Structured serving size information
๐ Quick Start
Local Development
Set up environment:
pip install -r requirements.txt cp .env.example .env # Add your MONGODB_URITest the schemas:
python3 test_server.pyRun the server:
python3 run_server.py # Server will start on http://localhost:8000Test with MCP Inspector:
npx @modelcontextprotocol/inspector http://localhost:8000/mcpAccess endpoints:
MCP StreamableHTTP endpoint:
http://localhost:8000/mcpThe server includes CORS headers for browser access
The server will log startup information including the listening address
Docker Deployment
Build the image:
docker build -t food-mcp-server .Run with environment:
docker run -e MONGODB_URI="your_mongodb_uri" -p 8000:8000 food-mcp-serverUse docker-compose:
docker-compose up --build
๐ง Configuration
Set the following environment variables:
MONGODB_URI- MongoDB Atlas connection string (required)PYTHONPATH- Set to/appin containerPYTHONUNBUFFERED- Set to1for real-time logging
๐ Example Tool Responses
Structured Categories Response
Structured Nutrition Response
๐ Migration from FastMCP
This server replaces the previous FastMCP implementation with several advantages:
Better Control: Low-level server provides complete control over MCP protocol
Structured Output: Pydantic schemas ensure consistent response format
Type Safety: Full type checking and validation
Better Debugging: Direct access to MCP internals for troubleshooting
Production Ready: Designed for production deployment scenarios
๐งช Testing
Run the test suite to verify everything works:
Tests cover:
Pydantic schema validation
JSON schema generation for tool definitions
Structured data serialization
Response format consistency
๐ Development
Project Structure
Adding New Tools
Define Pydantic schema in
schemas/Add tool definition in
handle_list_tools()Implement handler in
handle_call_tool()Return
CallToolResultwith structured contentAdd tests in
test_server.py
๐ Production Deployment
The server is production-ready with:
Security: Non-root user in Docker container
Logging: Structured logging with appropriate levels
Error Handling: Comprehensive error responses
Health Checks: Docker health check configuration
Resource Management: Multi-stage Docker builds for smaller images
๐ MCP Protocol Compliance
This server fully implements the MCP specification:
stdio Transport: Standard input/output communication
Tool Discovery: Dynamic tool listing with schemas
Structured Responses: Consistent response format
Error Handling: Proper error response structure
Lifecycle Management: Proper initialization and cleanup
๐ค Contributing
Follow the existing Pydantic schema patterns
Add comprehensive tests for new features
Ensure Docker builds pass
Update documentation for new tools
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
Provides comprehensive food hierarchy and nutrition data through structured tools that enable searching foods, browsing categories, and retrieving detailed nutritional information from a MongoDB Atlas database.