创建 FastMCP 实例
用几行代码启动一个命名的服务器对象。
创建 FastMCP 实例 是 CoddyKit 上的免费 MCP Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 MCP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 MCP Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Meet FastMCP
Time to build for real! The Python SDK gives you FastMCP, a friendly class that turns plain functions into a working MCP server. 🚀
Import It First
Everything starts with one import. You pull FastMCP from the official package so the rest of your file has it ready to use.
from mcp.server.fastmcp import FastMCPCreate the Instance
Now you make a server instance. Calling FastMCP(...) hands you back one object that will hold every tool you add later.
mcp = FastMCP("demo")Give It a Name
That first string is your server's name. Clients show it to users, so pick something clear like "weather" or "notes" instead of "app1".
mcp = FastMCP("weather")The mcp Object Is Your Hub
That mcp variable is the hub of your whole server. You attach tools, resources, and prompts to it, then ask it to run.
Why It's Called Fast
FastMCP is fast because it hides the protocol plumbing. You write normal Python, and it generates the JSON-RPC schemas behind the scenes.
Almost a Whole Server
Believe it or not, these two lines are already a valid server skeleton. It just has no tools yet, so it can't do anything useful so far.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo")Add an Optional Description
You can pass extra hints too. Some setups accept an instructions string that tells clients what the server is for at a glance.
mcp = FastMCP("weather", instructions="Look up forecasts.")One Server Per File
A common pattern is one module holding a single mcp instance. It keeps each server small, focused, and easy to reason about.
Naming Stays Stable
Try to keep your server name stable over time. Clients and configs reference it, so renaming later means updating every place that points at it.
Nothing Runs Yet
Creating the instance does not start a server. It just builds the object in memory; starting it is a separate step you'll meet soon.
Quick Check
Let's lock in what the constructor argument does.
Recap
You imported FastMCP, created an mcp instance, and gave it a clear name. That object is the hub every capability will hang off. Next: a real tool! 🎉
常见问题解答
「创建 FastMCP 实例」课时是免费的吗?
是的 — 「创建 FastMCP 实例」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MCP Academy 课程的其余内容,请升级到 CoddyKit PRO。 MCP Academy 课程共包含 4 节课。
「创建 FastMCP 实例」这节课中我会学到什么?
用几行代码启动一个命名的服务器对象。 你通过在浏览器中直接运行的动手代码来练习 MCP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 MCP Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 MCP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「创建 FastMCP 实例」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 MCP Academy 课中编写并运行代码吗?
能。每节 MCP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 创建 FastMCP 实例
- 添加问候工具
- 使用标准输入输出运行服务器
- 确认已加载并列出工具