Python MCP Server Development Framework

Python MCP Server Development Framework

A comprehensive framework for building reliable Python MCP servers that enforces incremental development practices through Cursor Rules, featuring built-in security measures and a smart rule system.

Category
访问服务器

README

Python MCP Server Development with Cursor Rules

A comprehensive development framework for Python MCP servers with enforced incremental development practices

License: MIT Python UV

🌟 What This Is

This repository showcases a production-ready development framework for building Python MCP (Model Context Protocol) servers using Cursor Rules. The system enforces incremental development practices and provides comprehensive guidelines for building reliable, secure, and maintainable MCP servers.

🚀 Key Features

🎯 Enforced Incremental Development

  • NEVER create multiple untested files at once
  • Mandatory TODO.md and TASKS.md management
  • Step-by-step development workflow
  • Built-in quality gates

🔧 Comprehensive Tooling

  • UV integration for modern Python dependency management
  • Type checking with mypy
  • Code formatting with black and isort
  • Testing with pytest and pytest-asyncio
  • Pre-commit hooks for quality assurance

🛡️ Security First

  • File path validation and directory traversal protection
  • SQL injection prevention
  • Input sanitization and validation
  • Comprehensive error handling

📋 Smart Rule System

  • Auto-attached rules based on file location
  • Manual rules for deployment and troubleshooting
  • Always-active rules for global standards
  • Context-aware development guidelines

📁 Repository Structure

python-mcp-development-framework/
├── .cursor/
│   └── rules/
│       ├── python_mcp_servers.mdc        # Main development rules
│       ├── global-standards.mdc          # Global standards
│       ├── file-server-rules.mdc         # File server specific rules
│       ├── database-server-rules.mdc     # Database server specific rules
│       ├── deployment-guide.mdc          # Deployment procedures
│       └── troubleshooting.mdc           # Troubleshooting guide
├── examples/
│   ├── file-server/                      # File server examples
│   └── db-server/                        # Database server examples
├── docs/                                 # Additional documentation
└── README.md                             # This file

🎯 Development Philosophy

The Incremental Approach

This framework is built around a strict incremental development methodology:

  1. 📋 Plan First - Update TODO.md with specific tasks
  2. 🔨 Implement One Thing - Build one component at a time
  3. 🧪 Test Immediately - Write and run tests before proceeding
  4. ✅ Verify - Ensure current step works completely
  5. 📝 Document - Update progress and notes

Why This Matters

  • Reduces bugs by catching issues early
  • Improves code quality through focused development
  • Enhances maintainability with clear progression
  • Increases reliability through comprehensive testing
  • Accelerates debugging with smaller change sets

🛠️ How the Rule System Works

Auto-Attached Rules

Rules automatically apply based on your file location:

# Working in file server? File server rules auto-apply
servers/file-server/main.py  → file-server-rules.mdc

# Working in database server? Database rules auto-apply
servers/db-server/main.py    → database-server-rules.mdc

Manual Rules

Call specific rules in Cursor chat when needed:

@deployment-guide    # Get deployment procedures
@troubleshooting     # Get debugging help

Always-Active Rules

Core development standards apply everywhere:

  • Type hints are mandatory
  • Error handling is required
  • Tests must be written
  • Documentation is enforced

🚀 Quick Start

1. Copy the Rules

# Copy the .cursor directory to your project
cp -r .cursor /path/to/your/mcp-project/

2. Initialize Your Project

# Initialize with UV
uv init your-mcp-project
cd your-mcp-project

# Add dependencies
uv add mcp fastapi uvicorn pydantic aiofiles
uv add --dev pytest pytest-asyncio black isort mypy ruff

3. Create Task Files

# Create required task management files
touch TODO.md TASKS.md

4. Start Development

The rules will automatically guide you through:

  • Creating minimal implementations first
  • Writing tests immediately
  • Following security best practices
  • Maintaining proper documentation

📊 Examples

File Server Example

# examples/file-server/main.py
from typing import Dict, Any
import aiofiles
import logging

logger = logging.getLogger(__name__)

async def read_file_tool(request: Dict[str, Any]) -> Dict[str, Any]:
    """Handle file read requests with security validation"""
    try:
        file_path = request.get("file_path")
        if not file_path:
            raise ValueError("file_path is required")
        
        # Security: Validate file path
        safe_path = validate_file_path(file_path)
        
        # Read file asynchronously
        async with aiofiles.open(safe_path, 'r') as f:
            content = await f.read()
        
        return {
            "status": "success",
            "data": {"content": content, "path": str(safe_path)}
        }
    except Exception as e:
        logger.error(f"File read error: {e}")
        return {"status": "error", "error": str(e)}

Database Server Example

# examples/db-server/main.py
from typing import Dict, Any
import aiosqlite
import logging

logger = logging.getLogger(__name__)

async def query_database_tool(request: Dict[str, Any]) -> Dict[str, Any]:
    """Handle database queries with SQL injection prevention"""
    try:
        table = request.get("table")
        if not table:
            raise ValueError("table is required")
        
        # Security: Validate table name
        if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', table):
            raise ValueError("Invalid table name")
        
        # Execute query safely
        async with aiosqlite.connect("database.db") as conn:
            async with conn.execute(f"SELECT * FROM {table} LIMIT 100") as cursor:
                rows = await cursor.fetchall()
                columns = [desc[0] for desc in cursor.description]
        
        return {
            "status": "success",
            "data": {"rows": rows, "columns": columns}
        }
    except Exception as e:
        logger.error(f"Database query error: {e}")
        return {"status": "error", "error": str(e)}

📚 Documentation

Core Rules

  1. Main Development Rules - Primary development guidelines
  2. Global Standards - Universal development standards
  3. File Server Rules - File operation specific guidelines
  4. Database Server Rules - Database operation guidelines

Operational Guides

  1. Deployment Guide - Production deployment procedures
  2. Troubleshooting - Debugging and problem resolution

🎯 Benefits

For Developers

  • Faster development with clear guidelines
  • Fewer bugs through incremental approach
  • Better code quality with enforced standards
  • Easier debugging with comprehensive logging

For Teams

  • Consistent code style across projects
  • Reduced onboarding time for new developers
  • Better collaboration with clear practices
  • Improved maintainability of codebases

For Projects

  • Higher reliability through comprehensive testing
  • Better security with built-in protections
  • Easier deployment with detailed guides
  • Faster troubleshooting with comprehensive documentation

🔄 Development Workflow Example

# TODO.md
## Current Sprint: File Server Basic Operations
- [ ] Implement file read functionality
- [ ] Add file write functionality
- [ ] Implement directory listing
- [ ] Add file metadata retrieval

## Next Sprint
- [ ] Add file streaming for large files
- [ ] Implement file caching
# TASKS.md
## Task: Implement File Read Functionality
**Status**: In Progress
**Priority**: High

### Subtasks:
1. [x] Create basic file read function
2. [x] Add path validation
3. [x] Write unit tests
4. [ ] Add error handling
5. [ ] Add logging

### Acceptance Criteria:
- [ ] Function can read text files
- [ ] Path validation prevents directory traversal
- [ ] All tests pass
- [ ] Proper error handling implemented
- [ ] Logging is comprehensive

🤝 Contributing

This showcase demonstrates best practices for MCP server development. Feel free to:

  1. Use these rules in your own projects
  2. Adapt the system to your specific needs
  3. Share improvements through issues and discussions
  4. Contribute examples for different use cases

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments


Remember: The key to successful development is not speed, but consistency and quality. This framework helps you achieve both through disciplined, incremental development practices.

🚀 Ready to transform your MCP server development? Copy the .cursor directory to your project and start building better, faster, and more reliably!

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选