MCP Python SDK Server initialization is the process of creating an MCP server instance, registering its capabilities (tools, resources, prompts), and starting it to listen for incoming connections from MCP clients (like Claude Desktop, Cursor, or custom clients).
CN: MCP Python SDK Server 初始化是创建 MCP 服务器实例、注册其能力(工具、资源、提示词),并启动服务器以监听来自 MCP 客户端(如 Claude Desktop、Cursor 或自定义客户端)连接的过程。
What Does Server Initialization Include
| 组件 | EN | CN |
|---|---|---|
| 安装与环境配置 | Install MCP SDK and set up Python environment | 安装 MCP SDK 并配置 Python 环境 |
| 创建 Server 实例 | Instantiate FastMCP or MCPServer class | 实例化 FastMCP 或 MCPServer 类 |
| 注册能力 | Register tools, resources, and prompts via decorators | 通过装饰器注册工具、资源和提示词 |
| 选择传输协议 | Choose transport: stdio (local) or HTTP/SSE (remote) | 选择传输协议:stdio(本地)或 HTTP/SSE(远程) |
| 启动服务器 | Call .run() or use CLI to start the server | 调用 .run() 或使用 CLI 启动服务器 |
Code Implementation
Check “uv ” ststus
uv is an extremely fast Python package and project manager written in Rust. It replaces multiple tools — pip, pip-tools, pipx, poetry, pyenv, and virtualenv — with a single unified tool.
CN: uv 是一个用 Rust 编写的极速 Python 包和项目管理器。它用一个统一工具取代了 pip、pip-tools、pipx、poetry、pyenv 和 virtualenv 等多个工具。
Bash
uv --version
# if return: "uv 0.8.12" or likes this, that means your envirionment has installed "uv"
# if you see: 'uv' is not recognized as an internal or external command,
# that means you have to install uv at first

Install uv (Windows)
PowerShell
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

Environment Setup
bash
# EN: Create a new project with uv (recommended package manager)
# CN: 使用 uv 创建新项目(推荐的包管理器)
uv init my-mcp-server
cd my-mcp-server
# EN: Create and activate virtual environment
# CN: 创建并激活虚拟环境
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# EN: Install MCP SDK (v1.x stable for production)
# CN: 安装 MCP SDK(生产环境使用 v1.x 稳定版)
uv add "mcp[cli]>=1.27,<2"
# EN: Or using pip
# CN: 或使用 pip
pip install "mcp[cli]>=1.27,<2"
# EN: Create server file
# CN: 创建服务器文件
touch server.py
Basic Server Initialization
# EN: server.py - Basic MCP Server Initialization
# CN: server.py - 基础 MCP Server 初始化
# EN: Import FastMCP - the high-level framework for building MCP servers
# CN: 导入 FastMCP - 用于构建 MCP 服务器的高级框架
# EN: FastMCP handles all protocol complexities (JSON-RPC, session management, etc.)
# CN: FastMCP 处理所有协议复杂性(JSON-RPC、会话管理等)
from mcp.server.fastmcp import FastMCP
# ============================================================
# EN: Step 1: Create the server instance
# CN: 步骤 1:创建服务器实例
# EN: FastMCP("name") creates a server with a descriptive name
# CN: FastMCP("name") 创建一个带有描述性名称的服务器
# EN: This name appears in client logs and helps identify your server
# CN: 此名称出现在客户端日志中,有助于识别你的服务器
# ============================================================
mcp = FastMCP("My First MCP Server")
# ============================================================
# EN: Step 2: Register a tool (function that LLM can call)
# CN: 步骤 2:注册工具(LLM 可以调用的函数)
# EN: @mcp.tool decorator automatically:
# CN: @mcp.tool 装饰器自动完成:
# EN: - Uses function name as tool name
# CN: - 使用函数名作为工具名
# EN: - Uses docstring as tool description for LLM
# CN: - 使用 docstring 作为给 LLM 的工具描述
# EN: - Uses type hints to generate JSON schema for inputs
# CN: - 使用类型提示生成输入的 JSON schema
# ============================================================
@mcp.tool
def add(a: int, b: int) -> int:
"""Add two integers together.""" # EN: This becomes the tool description
return a + b # CN: 这成为工具的描述
# ============================================================
# EN: Step 3: Register a resource (read-only data for LLM)
# CN: 步骤 3:注册资源(LLM 可用的只读数据)
# EN: @mcp.resource(uri) exposes data at a specific URI
# CN: @mcp.resource(uri) 在特定 URI 下暴露数据
# ============================================================
@mcp.resource("resource://config")
def get_config() -> dict:
"""Provide application configuration to the LLM."""
return {
"app_name": "My MCP Server",
"version": "1.0.0",
"environment": "development"
}
# ============================================================
# EN: Step 4: Register a prompt (pre-written template for users)
# CN: 步骤 4:注册提示词(为用户预写的模板)
# EN: Prompts help users accomplish specific tasks with guidance
# CN: 提示词帮助用户在指导下完成特定任务
# ============================================================
@mcp.prompt
def code_review(code: str) -> str:
"""Generate a code review prompt template."""
return f"""
Please review this code and provide feedback on:
1. Code quality and readability
2. Potential bugs or edge cases
3. Performance considerations
4. Security concerns
Code:
{code}
"""
# ============================================================
# EN: Step 5: Run the server
# CN: 步骤 5:运行服务器
# EN: The if __name__ == "__main__" block ensures the server runs
# CN: if __name__ == "__main__" 块确保服务器运行
# EN: when the file is executed directly (not when imported)
# CN: 当文件被直接执行时(而不是被导入时)
# EN: Default transport is "stdio" - works with Claude Desktop, Cursor, etc.
# CN: 默认传输协议是 "stdio" - 适用于 Claude Desktop、Cursor 等
# ============================================================
if __name__ == "__main__":
# EN: mcp.run() starts the server and blocks until stopped
# CN: mcp.run() 启动服务器并阻塞直到停止
# EN: It handles all connection management for you
# CN: 它为你处理所有连接管理
mcp.run()

