A complete Model Context Protocol (MCP) implementation example featuring a product search service with both MCP and REST API endpoints.
This project demonstrates how to build, test, and deploy MCP services using modern Python development practices.
Author: Chandra Shettigar (chandra@devteds.com)
- Complete MCP Implementation - Full JSON-RPC 2.0 protocol support
- Multiple Interface Support - Both MCP protocol and REST API endpoints
- Production-Ready Code - Comprehensive testing, formatting, and validation
- Modern Development Environment - VS Code/Cursor devcontainer setup
- Real-World Example - Product search with inventory management
This code was written as a companion to the blog post: Building Your First MCP Server
The blog post covers the theory and best practices behind MCP service development, while this repository provides the complete working implementation.
- VS Code or Cursor editor
- Docker installed and running
- Dev Containers extension installed
git clone git@github.com:devteds/mcp-test-service.git
cd mcp-test-service
Open in VS Code/Cursor and select "Reopen in Container" when prompted.
python verify_setup.py
You should see:
β
All dependencies installed and working correctly!
π Your devcontainer is ready for MCP development
python -m mcp_service
The service will start on http://localhost:8000
In a new terminal:
# Test MCP client
python mcp_client_example.py
# Run test suite
python -m pytest tests/ -v
# Test REST API
curl http://localhost:8000/api/v1/products/search?query=iPhone
mcp-test/
βββ .devcontainer/ # Development container configuration
β βββ devcontainer.json # VS Code/Cursor devcontainer settings
β βββ Dockerfile # Python development environment
β βββ bashrc # Shell configuration with aliases
βββ mcp_service/ # Main MCP service package
β βββ __init__.py
β βββ main.py # FastAPI application and MCP server
β βββ server.py # MCP protocol implementation
β βββ handlers.py # MCP tool handlers
β βββ models.py # Pydantic data models
β βββ data.py # Sample product database
βββ tests/ # Comprehensive test suite
β βββ test_product_service.py
βββ mcp_client_example.py # Example MCP client usage
βββ verify_setup.py # Environment verification script
βββ requirements.txt # Python dependencies
βββ README.md # This file
The project includes a fully configured development environment with:
- Python 3.11 with all dependencies pre-installed
- VS Code Extensions: Python, Black, isort, flake8, pytest
- Automatic Formatting: Code formatting on save
- Port Forwarding: MCP service accessible on port 8000
- Shell Aliases: Convenient commands for common tasks
The devcontainer includes these helpful aliases:
mcp-start # Start the MCP service
mcp-test # Run the test suite
mcp-client # Run the MCP client example
mcp-format # Format code with black and isort
mcp-verify # Verify environment setup
The MCP service provides three tools:
search_products
- Search for products by name or categoryget_product_details
- Get detailed information about a specific productcheck_inventory
- Check stock levels for a product
The service includes sample products across three categories:
- Electronics: iPhone 15 Pro, MacBook Air M3
- Footwear: Nike Air Max, Adidas Ultraboost
- Appliances: (expandable)
MCP Protocol:
POST /api/v1/mcp
- Main MCP JSON-RPC endpoint
REST API:
GET /api/v1/mcp/capabilities
- Service capabilities discoveryGET /api/v1/products/search
- Product searchGET /api/v1/products/{id}
- Product detailsGET /api/v1/products/{id}/inventory
- Inventory checkGET /health
- Health checkGET /docs
- Interactive API documentation
python -m pytest tests/ -v
The test suite covers:
- β MCP protocol message handling
- β All MCP tool implementations
- β REST API endpoints
- β Error handling and edge cases
- β Data validation and serialization
# Test MCP capabilities discovery
curl http://localhost:8000/api/v1/mcp/capabilities
# Test product search
curl "http://localhost:8000/api/v1/products/search?query=iPhone"
# Test MCP client
python mcp_client_example.py
The project uses:
- Black for code formatting
- isort for import sorting
- flake8 for linting
- pytest for testing
Format code:
black . && isort .
- Format on save enabled
- Organize imports on save
- pytest integration
- flake8 linting enabled
python verify_setup.py
This script checks:
- β All required dependencies are installed
- β MCP SDK functionality works
- β Service imports are successful
- β Data functions are operational
Port 8000 already in use:
# Kill existing process
pkill -f "python -m mcp_service"
# Or use a different port
uvicorn mcp_service.main:app --port 8001
Dependencies missing:
# Rebuild devcontainer
# Command Palette > "Dev Containers: Rebuild Container"
Tests failing:
# Check service is running
curl http://localhost:8000/health
# Run tests with verbose output
python -m pytest tests/ -v --tb=short
- MCP Specification: Model Context Protocol Documentation
- MCP SDK: Official Python SDK
- JSON-RPC 2.0: Protocol Specification
- Protocol Implementation - Complete JSON-RPC 2.0 MCP server
- Tool Registration - Dynamic tool discovery and execution
- Type Safety - Pydantic models for data validation
- Error Handling - Proper MCP error responses
- Testing Strategy - Comprehensive test coverage
- Development Workflow - Modern Python development practices
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Make your changes and ensure tests pass
- Format code:
black . && isort .
- Run tests:
python -m pytest tests/ -v
- Commit changes:
git commit -m 'Add amazing feature'
- Push to branch:
git push origin feature/amazing-feature
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Author: Chandra Shettigar - Project creator and maintainer
- Anthropic for the Model Context Protocol specification
- FastAPI for the excellent web framework
- Pydantic for data validation and serialization
- MCP Community for tools and examples
Happy MCP Development! π
For questions or issues, please open a GitHub issue or refer to the related blog post for detailed explanations.