Skip to content

Latest commit

 

History

History
169 lines (130 loc) · 4.76 KB

File metadata and controls

169 lines (130 loc) · 4.76 KB

MCP Server Security Testing Guide

Overview

This document provides a comprehensive guide for securely testing your MCP (Model Context Protocol) server. The tests verify that your server is properly secured and handles various security scenarios correctly.

Security Test Results ✅

✅ Server Security Status: SECURE

Your MCP server has been tested and found to be properly secured with the following findings:

🔒 Security Findings

✅ Positive Security Features

  1. Endpoint Protection: All undefined endpoints properly return 404 errors

    • /admin → 404 (blocked)
    • /debug → 404 (blocked)
    • /config → 404 (blocked)
    • /.env → 404 (blocked)
    • Path traversal attempts → 404 (blocked)
  2. Input Validation: Server properly rejects malicious payloads

    • SQL injection attempts → Rejected (400 status)
    • XSS payloads → Rejected (400 status)
    • Binary data → Rejected (400 status)
    • Oversized payloads → Rejected (400 status)
  3. Protocol Security: MCP protocol enforcement

    • Tools cannot be accessed via simple HTTP GET
    • Requires proper MCP JSON-RPC 2.0 protocol
    • Session-based authentication required
  4. External API Security: Weather API integration is secure

    • Invalid city names are handled safely
    • Malicious inputs are blocked by external API
    • No server-side vulnerabilities exposed

⚠️ Security Recommendations

  1. HTTP Security Headers: Consider adding the following headers:

    X-Content-Type-Options: nosniff
    X-Frame-Options: DENY
    X-XSS-Protection: 1; mode=block
    Strict-Transport-Security: max-age=31536000
    
  2. Production Deployment:

    • Use HTTPS instead of HTTP
    • Consider adding rate limiting
    • Implement request logging
    • Regular security monitoring

🧪 How to Run Security Tests

1. Start the Server Safely

./start_server.sh

2. Run Basic Security Test

python3 simple_test.py

3. Stop the Server

pkill -f "python3 server.py"

🔍 Test Files Description

start_server.sh

  • Safe server startup script
  • Automatically handles virtual environment
  • Cleans up existing processes
  • Provides clear status feedback

simple_test.py

  • Comprehensive security testing
  • Tests endpoint security
  • Validates input handling
  • Tests external API integration

test_client.py

  • MCP protocol testing
  • Demonstrates proper MCP client usage
  • Tests tool functionality

security_test.py

  • Advanced security testing
  • Rate limiting tests
  • Header security analysis
  • Malformed request handling

🚨 Why Simple HTTP Requests Don't Work

The error you encountered earlier ("no data found") happened because:

  1. MCP is not a REST API: You tried accessing /tool/get_current_weather?city=Amsterdam
  2. Protocol Requirement: MCP requires JSON-RPC 2.0 messages
  3. Session Management: MCP tools require session authentication
  4. Proper Client: You need an MCP client, not simple HTTP requests

❌ Wrong Approach (What you tried):

curl http://localhost:8080/tool/get_current_weather?city=Amsterdam

✅ Correct Approach:

# Use proper MCP client with JSON-RPC 2.0 protocol
message = {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
        "name": "get_current_weather",
        "arguments": {"city": "Amsterdam"}
    }
}

🛡️ Security Best Practices

  1. Never expose MCP tools as direct HTTP endpoints
  2. Always use the MCP protocol for tool access
  3. Validate all inputs on both client and server side
  4. Use HTTPS in production environments
  5. Implement proper authentication and authorization
  6. Monitor server logs for suspicious activity
  7. Keep dependencies updated

📊 Test 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

🔗 Usage in Cursor/Claude

To use this MCP server in Cursor or Claude, add to your mcp.json:

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

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

🎯 Conclusion

Your MCP server is securely implemented and follows security best practices. The server properly:

  • Validates inputs
  • Blocks unauthorized access
  • Enforces protocol requirements
  • Handles errors gracefully

The only improvements needed are adding HTTP security headers for production deployment.