$npx -y skills add github/awesome-copilot --skill python-mcp-server-generatorGenerate a complete MCP server project in Python with tools, resources, and proper configuration
| 1 | # Generate Python MCP Server |
| 2 | |
| 3 | Create a complete Model Context Protocol (MCP) server in Python with the following specifications: |
| 4 | |
| 5 | ## Requirements |
| 6 | |
| 7 | 1. **Project Structure**: Create a new Python project with proper structure using uv |
| 8 | 2. **Dependencies**: Include mcp[cli] package with uv |
| 9 | 3. **Transport Type**: Choose between stdio (for local) or streamable-http (for remote) |
| 10 | 4. **Tools**: Create at least one useful tool with proper type hints |
| 11 | 5. **Error Handling**: Include comprehensive error handling and validation |
| 12 | |
| 13 | ## Implementation Details |
| 14 | |
| 15 | ### Project Setup |
| 16 | - Initialize with `uv init project-name` |
| 17 | - Add MCP SDK: `uv add "mcp[cli]"` |
| 18 | - Create main server file (e.g., `server.py`) |
| 19 | - Add `.gitignore` for Python projects |
| 20 | - Configure for direct execution with `if __name__ == "__main__"` |
| 21 | |
| 22 | ### Server Configuration |
| 23 | - Use `FastMCP` class from `mcp.server.fastmcp` |
| 24 | - Set server name and optional instructions |
| 25 | - Choose transport: stdio (default) or streamable-http |
| 26 | - For HTTP: optionally configure host, port, and stateless mode |
| 27 | |
| 28 | ### Tool Implementation |
| 29 | - Use `@mcp.tool()` decorator on functions |
| 30 | - Always include type hints - they generate schemas automatically |
| 31 | - Write clear docstrings - they become tool descriptions |
| 32 | - Use Pydantic models or TypedDicts for structured outputs |
| 33 | - Support async operations for I/O-bound tasks |
| 34 | - Include proper error handling |
| 35 | |
| 36 | ### Resource/Prompt Setup (Optional) |
| 37 | - Add resources with `@mcp.resource()` decorator |
| 38 | - Use URI templates for dynamic resources: `"resource://{param}"` |
| 39 | - Add prompts with `@mcp.prompt()` decorator |
| 40 | - Return strings or Message lists from prompts |
| 41 | |
| 42 | ### Code Quality |
| 43 | - Use type hints for all function parameters and returns |
| 44 | - Write docstrings for tools, resources, and prompts |
| 45 | - Follow PEP 8 style guidelines |
| 46 | - Use async/await for asynchronous operations |
| 47 | - Implement context managers for resource cleanup |
| 48 | - Add inline comments for complex logic |
| 49 | |
| 50 | ## Example Tool Types to Consider |
| 51 | - Data processing and transformation |
| 52 | - File system operations (read, analyze, search) |
| 53 | - External API integrations |
| 54 | - Database queries |
| 55 | - Text analysis or generation (with sampling) |
| 56 | - System information retrieval |
| 57 | - Math or scientific calculations |
| 58 | |
| 59 | ## Configuration Options |
| 60 | - **For stdio Servers**: |
| 61 | - Simple direct execution |
| 62 | - Test with `uv run mcp dev server.py` |
| 63 | - Install to Claude: `uv run mcp install server.py` |
| 64 | |
| 65 | - **For HTTP Servers**: |
| 66 | - Port configuration via environment variables |
| 67 | - Stateless mode for scalability: `stateless_http=True` |
| 68 | - JSON response mode: `json_response=True` |
| 69 | - CORS configuration for browser clients |
| 70 | - Mounting to existing ASGI servers (Starlette/FastAPI) |
| 71 | |
| 72 | ## Testing Guidance |
| 73 | - Explain how to run the server: |
| 74 | - stdio: `python server.py` or `uv run server.py` |
| 75 | - HTTP: `python server.py` then connect to `http://localhost:PORT/mcp` |
| 76 | - Test with MCP Inspector: `uv run mcp dev server.py` |
| 77 | - Install to Claude Desktop: `uv run mcp install server.py` |
| 78 | - Include example tool invocations |
| 79 | - Add troubleshooting tips |
| 80 | |
| 81 | ## Additional Features to Consider |
| 82 | - Context usage for logging, progress, and notifications |
| 83 | - LLM sampling for AI-powered tools |
| 84 | - User input elicitation for interactive workflows |
| 85 | - Lifespan management for shared resources (databases, connections) |
| 86 | - Structured output with Pydantic models |
| 87 | - Icons for UI display |
| 88 | - Image handling with Image class |
| 89 | - Completion support for better UX |
| 90 | |
| 91 | ## Best Practices |
| 92 | - Use type hints everywhere - they're not optional |
| 93 | - Return structured data when possible |
| 94 | - Log to stderr (or use Context logging) to avoid stdout pollution |
| 95 | - Clean up resources properly |
| 96 | - Validate inputs early |
| 97 | - Provide clear error messages |
| 98 | - Test tools independently before LLM integration |
| 99 | |
| 100 | Generate a complete, production-ready MCP server with type safety, proper error handling, and comprehensive documentation. |