0Pricing
MCP Academy · 课时

分层服务器架构

分离传输层、处理器和业务逻辑。

分层服务器架构 是 CoddyKit 上的免费 MCP Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 MCP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 MCP Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why One Big File Hurts

A server crammed into one file mixes transport, tool logic, and data calls. As it grows, every change risks breaking something far away.

Think in Layers

A clean server splits into layers: transport at the edge, MCP handlers in the middle, and pure business logic underneath.

The Transport Layer

The outer transport layer only moves messages, over stdio or HTTP. It should know nothing about what your tools actually do.

Handlers Are Thin Adapters

Your tool and resource handlers should be thin: parse arguments, call a service, shape the result. Keep real work out of them.

@mcp.tool()
def summarize(text: str) -> str:
    return service.summarize(text)

The Service Layer Does the Work

Put the actual logic in a plain service module with no MCP imports. It is just normal Python you can call from anywhere.

def summarize(text: str) -> str:
    return shorten(clean(text))

Why the Split Pays Off

With logic decoupled from MCP, you can test it without spinning up a server, and reuse it in a CLI or web app later.

Dependencies Point Inward

Keep the rule one-way: handlers may import services, but services must never import handlers or the transport above them.

A Folder Per Concern

Map layers to folders so the structure tells the story: server entry, handlers, services, and models each get their own place.

src/
  server.py
  handlers/
  services/
  models.py

Register, Don't Define, in Entry

Your server.py should wire handlers to the FastMCP instance, not contain logic. It is an assembly point, not a workhorse.

from handlers import register_tools
register_tools(mcp)

Cross-Cutting Helpers Stay Shared

Logging, config, and validation are shared concerns. Give them a common module so every layer uses the same helpers.

Grow Without Rewrites

Layering lets the server scale in size: add a new tool by adding a handler plus a service, leaving everything else untouched.

Quick Check

Where should the real business logic of a tool live?

Recap: Layered Design

Split your server into transport, thin handlers, and pure services, with dependencies pointing inward. Next: configuration. 🧱

常见问题解答

「分层服务器架构」课时是免费的吗?

是的 — 「分层服务器架构」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MCP Academy 课程的其余内容,请升级到 CoddyKit PRO。 MCP Academy 课程共包含 4 节课。

「分层服务器架构」这节课中我会学到什么?

分离传输层、处理器和业务逻辑。 你通过在浏览器中直接运行的动手代码来练习 MCP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 MCP Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 MCP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「分层服务器架构」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 MCP Academy 课中编写并运行代码吗?

能。每节 MCP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 分层服务器架构
  2. 通过环境配置设置与机密信息
  3. 幂等且感知副作用的工具
  4. 工具与模式的版本管理
← 返回 MCP Academy