Food MCP Server
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.
README
Food MCP Server
Overview
The Food MCP Server is a Model Context Protocol (MCP) implementation that provides comprehensive food hierarchy and nutrition data through a structured API. Built with the MCP Python SDK and StreamableHTTP transport, the server delivers validated data responses using Pydantic schemas for enterprise-grade data consistency.
Architecture
The server architecture leverages the MCP Python SDK with StreamableHTTP transport to enable web-based access. All data responses are structured through Pydantic schemas, ensuring consistent validation and serialization across all endpoints.
Core Features
- Structured Data Output: All tool responses utilize Pydantic schemas for data validation and serialization
- StreamableHTTP Transport: Web-accessible MCP server implementation
- MongoDB Atlas Integration: Cloud-based data storage solution for scalability
- Container Support: Docker-based deployment with optimized build configurations
- Comprehensive Tool Suite: Eleven distinct tools covering food hierarchy and nutrition data
Available Tools
Food Hierarchy Management
- get_all_food_hierarchy - Retrieve complete food hierarchy dataset
- get_categories - List all available food categories
- get_subcategories - Retrieve subcategories for a specified category
- get_food_items - List food items within category or subcategory
- search_food - Search food items using keyword parameters
- find_food_category - Locate category for specific food item
- list_all_foods - Retrieve all unique food names in dataset
- food_stats - Generate comprehensive dataset statistics
Food Nutrition Analysis
- list_food_names - List foods with available nutrition data
- get_food_nutrition - Retrieve complete nutrition information for specified food
- search_food_nutrition - Search nutrition data using keyword parameters
Data Structure Schemas
All tools return structured data using Pydantic validation schemas:
Food Hierarchy Schemas (schemas/food_hierarchy.py)
FoodHierarchyResponse- Complete hierarchy data structureFoodCategoriesResponse- Category listing responsesFoodSearchResponse- Search results with contextual informationFoodStats- Comprehensive dataset statistics
Food Nutrition Schemas (schemas/food_item.py)
FoodNutritionResponse- Complete nutrition data structureFoodNutrition- Detailed nutrition information with serving sizesServingInfo- Structured serving size information
Installation and Setup
Local Development Environment
-
Environment Configuration:
pip install -r requirements.txt cp .env.example .env # Configure your MONGODB_URI -
Schema Validation Testing:
python3 test_server.py -
Server Initialization:
python3 run_server.py # Server will be available at http://localhost:8000 -
MCP Inspector Integration:
npx @modelcontextprotocol/inspector http://localhost:8000/mcp -
Endpoint Access:
- MCP StreamableHTTP endpoint:
http://localhost:8000/mcp - CORS headers are enabled for browser access
- Server logs provide startup information including listening address
- MCP StreamableHTTP endpoint:
Container Deployment
-
Image Build Process:
docker build -t food-mcp-server . -
Container Execution:
docker run -e MONGODB_URI="your_mongodb_uri" -p 8000:8000 food-mcp-server -
Docker Compose Deployment:
docker-compose up --build
Configuration Management
Configure the following environment variables:
MONGODB_URI- MongoDB Atlas connection string (required)PYTHONPATH- Set to/appin container environmentPYTHONUNBUFFERED- Set to1for real-time logging output
Response Examples
Structured Categories Response
{
"categories": ["Vegetables", "Fruits", "Proteins"],
"total_count": 3
}
Structured Nutrition Response
{
"requested_name": "Apple",
"found": true,
"nutrition": {
"name": "Apple",
"display_portion_calories": 95,
"display_portion_size": "1 medium",
"nutrients": {
"calories": 95,
"protein_g": 0.5,
"carbs_g": 25.0,
"fiber_g": 4.0
}
}
}
Migration from FastMCP Implementation
This server implementation replaces the previous FastMCP implementation with several key advantages:
- Enhanced Control: Low-level server implementation provides complete control over MCP protocol implementation
- Structured Output: Pydantic schemas ensure consistent response format across all endpoints
- Type Safety: Comprehensive type checking and validation throughout the application
- Improved Debugging: Direct access to MCP internals for enhanced troubleshooting capabilities
- Production Readiness: Architecture designed specifically for production deployment scenarios
Testing and Validation
Execute the comprehensive test suite to verify system functionality:
python3 test_server.py
Test coverage includes:
- Pydantic schema validation and serialization
- JSON schema generation for tool definitions
- Structured data serialization processes
- Response format consistency verification
Development Guide
Project Structure
├── server.py # Main MCP server implementation
├── run_server.py # Application entry point script
├── schemas/ # Pydantic response schemas
│ ├── food_hierarchy.py # Hierarchy tool schemas
│ └── food_item.py # Nutrition tool schemas
├── services/ # Business logic services
├── utils/ # Database and utility functions
├── test_server.py # Comprehensive test suite
├── Dockerfile # Container configuration
└── docker-compose.yml # Deployment configuration
Adding New Tools
- Define Pydantic schema in appropriate
schemas/module - Add tool definition in
handle_list_tools()method - Implement tool handler in
handle_call_tool()method - Return
CallToolResultwith structured content - Add corresponding tests in
test_server.py
Production Deployment
The server is designed for production environments with the following features:
- Security: Non-root user execution in Docker container environment
- Logging: Structured logging implementation with appropriate severity levels
- Error Handling: Comprehensive error response mechanisms
- Health Monitoring: Docker health check configuration for container orchestration
- Resource Optimization: Multi-stage Docker builds for minimal image size
MCP Protocol Compliance
This server fully implements the Model Context Protocol specification:
- stdio Transport: Standard input/output communication protocol
- Tool Discovery: Dynamic tool listing with comprehensive schemas
- Structured Responses: Consistent response format across all endpoints
- Error Handling: Standardized error response structure
- Lifecycle Management: Proper initialization and cleanup procedures
Contributing Guidelines
- Follow established Pydantic schema patterns for data validation
- Add comprehensive test coverage for new features and functionality
- Ensure Docker build processes complete successfully
- Update documentation to reflect new tools and capabilities
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.