Uruchamianie serwera za pomocą stdio
Uruchom serwer, aby klient mógł połączyć się lokalnie.
Uruchamianie serwera za pomocą stdio to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 3 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.
Time to Start It Up
You have a server object and a tool. Now you need to actually run it so a client can connect and use what you built. 🏃
What stdio Means
The simplest transport is stdio: standard input and output. The client launches your script and talks to it through those two streams.
Call run()
Starting the server is one method. Calling mcp.run() hands control to FastMCP, which begins listening for messages right away.
mcp.run()Choose the Transport
Pass a transport argument to be explicit. For local use you want "stdio", which is also the default when you call run.
mcp.run(transport="stdio")The Main Guard
Wrap the call in the classic __main__ guard. That way the server starts only when the file is run directly, not when imported.
if __name__ == "__main__":
mcp.run()The Whole File
Here is a complete server: import, instance, tool, and run. Just a handful of lines and it speaks MCP.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo")
@mcp.tool()
def hello() -> str:
return "Hi!"
if __name__ == "__main__":
mcp.run()stdout Is Reserved
One golden rule: never print to standard output. That stream carries protocol messages, and stray text will corrupt them.
Log to stderr Instead
Need to debug? Write to stderr, not stdout. The client ignores stderr, so your notes stay out of the protocol stream.
import sys
print("starting up", file=sys.stderr)The Client Launches You
With stdio, you rarely run the script by hand. The host app starts your process for you using a configured command.
It Waits, Not Loops Idle
After run() the server blocks, waiting for messages. It isn't frozen; it's listening for the next request from the client.
stdio Is Local Only
Remember that stdio works only on the same machine, since it relies on a launched process. Remote servers use HTTP transport instead, which comes later.
Quick Check
Why must you avoid plain print() in a stdio server?
Recap
You ran the server with mcp.run() over stdio, guarded it with __main__, and kept stdout clean. It's live and listening. Let's confirm it works! ✅
Często zadawane pytania
Czy lekcja „Uruchamianie serwera za pomocą stdio” jest bezpłatna?
Tak — pełny tekst „Uruchamianie serwera za pomocą stdio” 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 „Uruchamianie serwera za pomocą stdio”?
Uruchom serwer, aby klient mógł połączyć się lokalnie. Ć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 3 z 4.
Ile czasu zajmuje lekcja „Uruchamianie serwera za pomocą stdio”?
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
- Tworzenie instancji FastMCP
- Dodawanie narzędzia Hello
- Uruchamianie serwera za pomocą stdio
- Sprawdzanie ładowania i listy narzędzi