Food MCP Server

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.

Category
访问服务器

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

  1. get_all_food_hierarchy - Retrieve complete food hierarchy dataset
  2. get_categories - List all available food categories
  3. get_subcategories - Retrieve subcategories for a specified category
  4. get_food_items - List food items within category or subcategory
  5. search_food - Search food items using keyword parameters
  6. find_food_category - Locate category for specific food item
  7. list_all_foods - Retrieve all unique food names in dataset
  8. food_stats - Generate comprehensive dataset statistics

Food Nutrition Analysis

  1. list_food_names - List foods with available nutrition data
  2. get_food_nutrition - Retrieve complete nutrition information for specified food
  3. 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 structure
  • FoodCategoriesResponse - Category listing responses
  • FoodSearchResponse - Search results with contextual information
  • FoodStats - Comprehensive dataset statistics

Food Nutrition Schemas (schemas/food_item.py)

  • FoodNutritionResponse - Complete nutrition data structure
  • FoodNutrition - Detailed nutrition information with serving sizes
  • ServingInfo - Structured serving size information

Installation and Setup

Local Development Environment

  1. Environment Configuration:

    pip install -r requirements.txt
    cp .env.example .env  # Configure your MONGODB_URI
    
  2. Schema Validation Testing:

    python3 test_server.py
    
  3. Server Initialization:

    python3 run_server.py
    # Server will be available at http://localhost:8000
    
  4. MCP Inspector Integration:

    npx @modelcontextprotocol/inspector http://localhost:8000/mcp
    
  5. Endpoint Access:

    • MCP StreamableHTTP endpoint: http://localhost:8000/mcp
    • CORS headers are enabled for browser access
    • Server logs provide startup information including listening address

Container Deployment

  1. Image Build Process:

    docker build -t food-mcp-server .
    
  2. Container Execution:

    docker run -e MONGODB_URI="your_mongodb_uri" -p 8000:8000 food-mcp-server
    
  3. Docker Compose Deployment:

    docker-compose up --build
    

Configuration Management

Configure the following environment variables:

  • MONGODB_URI - MongoDB Atlas connection string (required)
  • PYTHONPATH - Set to /app in container environment
  • PYTHONUNBUFFERED - Set to 1 for 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:

  1. Enhanced Control: Low-level server implementation provides complete control over MCP protocol implementation
  2. Structured Output: Pydantic schemas ensure consistent response format across all endpoints
  3. Type Safety: Comprehensive type checking and validation throughout the application
  4. Improved Debugging: Direct access to MCP internals for enhanced troubleshooting capabilities
  5. 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

  1. Define Pydantic schema in appropriate schemas/ module
  2. Add tool definition in handle_list_tools() method
  3. Implement tool handler in handle_call_tool() method
  4. Return CallToolResult with structured content
  5. 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

  1. Follow established Pydantic schema patterns for data validation
  2. Add comprehensive test coverage for new features and functionality
  3. Ensure Docker build processes complete successfully
  4. Update documentation to reflect new tools and capabilities

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选