Warstwowa architektura serwera
Oddziel transport, handlery i logikę biznesową.
Warstwowa architektura serwera to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej MCP Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs MCP Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.pyRegister, 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. 🧱
Często zadawane pytania
Czy lekcja „Warstwowa architektura serwera” jest bezpłatna?
Tak — pełny tekst „Warstwowa architektura serwera” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu MCP Academy, przejdź na CoddyKit PRO. Kurs MCP Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Warstwowa architektura serwera”?
Oddziel transport, handlery i logikę biznesową. Ćwiczysz MCP Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć MCP Academy?
Nie wymagamy żadnego doświadczenia. MCP Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.
Ile czasu zajmuje lekcja „Warstwowa architektura serwera”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji MCP Academy?
Tak. Każda lekcja MCP Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Warstwowa architektura serwera
- Konfiguracja i sekrety przez środowisko
- Narzędzia idempotentne i świadome efektów ubocznych
- Wersjonowanie narzędzi i schematów