添加 MCP Python SDK
将官方 mcp 包引入您的项目。
添加 MCP Python SDK 是 CoddyKit 上的免费 MCP Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 MCP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 MCP Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Time to Add the SDK
Your project is scaffolded, but it can't speak MCP yet. The next step is pulling in the official library that does all the heavy lifting. 📦
The Package Is Just mcp
The official Python SDK ships as a package simply named mcp. It contains everything you need to build servers and clients in plain Python.
Add It with uv
Inside your project folder, run uv add to install the SDK and record it as a dependency. One command grabs it and updates your project files.
uv add "mcp[cli]"Why the [cli] Extra
The [cli] extra adds the command-line tools that run and inspect servers. Without it you get the library but miss the handy mcp commands.
Quote the Brackets
Always wrap the name in quotes, like "mcp[cli]". Many shells treat square brackets as special characters, and quotes keep them literal.
pip Still Works Too
If you prefer plain pip, the same package installs the same way. Either tool gives you an identical SDK, so pick whichever fits your workflow.
pip install "mcp[cli]"It Records the Dependency
uv add doesn't just install; it writes mcp into your pyproject.toml. Anyone who clones your project gets the exact same requirement.
The Lockfile Pins Versions
uv also updates a uv.lock file that pins the exact resolved versions. That makes your installs reproducible on any machine, every time.
Confirm the Import
Verify the install worked by importing the SDK inside your environment. If nothing errors out, the mcp package is ready to use.
uv run python -c "import mcp; print('ok')"Where the Server Lives
The class you'll use most is FastMCP, found under mcp.server.fastmcp. It's the high-level way to define tools without touching raw protocol details.
from mcp.server.fastmcp import FastMCPOne Import, Big Power
That single import unlocks decorators for tools, resources, and prompts. The SDK turns ordinary Python functions into capabilities an AI can call. ✨
Quick Check
You want the SDK plus its command-line helpers.
The SDK Is In
You added mcp[cli], recorded it in pyproject.toml, locked the versions, and confirmed the import. Your project can now build a real MCP server. 🎯
常见问题解答
「添加 MCP Python SDK」课时是免费的吗?
是的 — 「添加 MCP Python SDK」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MCP Academy 课程的其余内容,请升级到 CoddyKit PRO。 MCP Academy 课程共包含 4 节课。
「添加 MCP Python SDK」这节课中我会学到什么?
将官方 mcp 包引入您的项目。 你通过在浏览器中直接运行的动手代码来练习 MCP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 MCP Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 MCP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「添加 MCP Python SDK」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 MCP Academy 课中编写并运行代码吗?
能。每节 MCP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 安装 Python 与 uv
- 添加 MCP Python SDK
- 认识 mcp CLI
- 服务器的项目布局