8000 GitHub - Brave-Python/python-mcp-server at security-testing
[go: up one dir, main page]

Skip to content

Brave-Python/python-mcp-server

Β 
Β 

Repository files navigation

Python MCP Server πŸš€

Introduction

This is a secure, production-ready server implementing the Model Context Protocol (MCP) with Server-Sent Events (SSE). It provides a comprehensive example of how to build, secure, and test an MCP server with proper security practices.

πŸ”§ Features

  • Weather Tool: Get current weather for any city using wttr.in API
  • Math Tool: Simple addition functionality
  • Secret Word Tool: Generate random words
  • Security Hardened: Comprehensive security testing and validation
  • Production Ready: Docker support and proper error handling

πŸš€ Quick Start

1. Set up your Python environment

python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

2. Install the required packages

pip install -r requirements.txt

3. Start the server safely

# Use the safe startup script (recommended)
./start_server.sh

# Or start manually
python server.py

The server will start on http://localhost:8080 by default.

πŸ§ͺ Testing & Security

Security Status: βœ… SECURE

This server has been comprehensively tested for security vulnerabilities and is production-ready.

Run Security Tests

# Basic security test (recommended)
python3 simple_test.py

# Comprehensive MCP protocol test
python3 test_client.py

# Advanced security analysis
python3 security_test.py

Security Features βœ…

  • Input Validation: All malicious payloads are properly rejected
  • Endpoint Protection: Undefined endpoints return 404
  • Protocol Enforcement: MCP protocol properly enforced
  • External API Security: Safe integration with weather API
  • Error Handling: Graceful error handling and logging

πŸ”’ Important Security Notes

⚠️ Why Simple HTTP Requests Don't Work

DON'T DO THIS:

# ❌ This will NOT work - MCP is not a REST API
curl http://localhost:8080/tool/get_current_weather?city=Amsterdam

DO THIS INSTEAD: Use proper MCP clients (like Cursor, Claude Desktop) that implement the JSON-RPC 2.0 protocol.

MCP Protocol Requirements

MCP tools require:

  • JSON-RPC 2.0 protocol
  • Proper session management
  • Structured message format
  • MCP-compatible client

🌐 Usage in MCP Clients

Cursor IDE

Add to your mcp.json file:

{
  "demo-mcp": {
    "url": "https://your-domain.com/sse"
  }
}

Claude Desktop

Add to your MCP configuration:

{
  "mcpServers": {
    "demo-mcp": {
      "url": "https://your-domain.com/sse"
    }
  }
}

Important: Replace your-domain.com with your actual server domain and use HTTPS in production.

🐳 Docker Deployment

Build and Run

# Build the container
docker build -t mcp-server .

# Run the container
docker run -p 8080:8080 mcp-server

Production Environment

For deployment on cloud platforms, make sure to:

  • Use HTTPS (not HTTP)
  • Set proper environment variables
  • Enable logging and monitoring
  • Use the provided Dockerfile

πŸ“ Project Structure

β”œβ”€β”€ server.py              # Main MCP server implementation
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ Dockerfile            # Container configuration
β”œβ”€β”€ start_server.sh       # Safe server startup script
β”œβ”€β”€ simple_test.py        # Basic security testing
β”œβ”€β”€ test_client.py        # MCP protocol testing
β”œβ”€β”€ security_test.py      # Advanced security analysis
β”œβ”€β”€ SECURITY_TESTING.md   # Detailed security documentation
└── README.md            # This file

πŸ› οΈ Available Tools

1. Weather Tool

get_current_weather(city: str) -> str

Get current weather information for any city.

2. Math Tool

add(a: int, b: int) -> int

Add two numbers together.

3. Secret Word Tool

get_secret_word() -> str

Get a random secret word.

πŸ”§ Development

Server Management

# Start server
./start_server.sh

# Stop server
pkill -f "python3 server.py"

# Check server status
ps aux | grep "python3 server.py"

Testing During Development

# Quick security check
python3 simple_test.py

# Full protocol test
python3 test_client.py

πŸ›‘οΈ Security Recommendations

For Production

  1. Use HTTPS: Never use HTTP in production
  2. Add Security Headers: Implement recommended HTTP security headers
  3. Monitor Logs: Set up proper logging and monitoring
  4. Rate Limiting: Consider implementing rate limiting
  5. Keep Updated: Regularly update dependencies

Security Headers (Recommended)

# Add these headers in production
"X-Content-Type-Options": "nosniff"
"X-Frame-Options": "DENY"
"X-XSS-Protection": "1; mode=block"
"Strict-Transport-Security": "max-age=31536000"

πŸ“Š Test Results Summary

Test Category Status Details
Server Access βœ… Pass Server responds correctly
Endpoint Security βœ… Pass All undefined endpoints blocked
Input Validation βœ… Pass Malicious inputs rejected
Protocol Security βœ… Pass MCP protocol enforced
External APIs βœ… Pass Weather API works securely
Headers ⚠️ Warning Security headers recommended

πŸ†˜ Troubleshooting

Common Issues

  1. "TypeError: FastMCP.init() got an unexpected keyword argument 'logger'"

    • Solution 5F05 : Already fixed in this version. The server uses log_level instead of logger.
  2. "No data found or available" when accessing tools via HTTP

    • Cause: MCP is not a REST API
    • Solution: Use proper MCP clients, not direct HTTP requests
  3. SSE endpoint timeout

    • Cause: Normal behavior for streaming endpoints
    • Solution: Use MCP clients that handle SSE properly

Getting Help

  • Check SECURITY_TESTING.md for detailed security information
  • Run python3 simple_test.py to verify server status
  • Ensure virtual environment is activated
  • Check server logs for detailed error information

πŸ“ License

This project is open source and available under the MIT License.

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Run security tests: python3 simple_test.py
  4. Submit a pull request

⚠️ Security Notice: This server has been tested for common security vulnerabilities and is considered secure for production use. Always use HTTPS in production environments.

About

Python MCP Server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 83.6%
  • Dockerfile 16.4%
0