stdioでサーバーを実行する
クライアントがローカルで接続できるよう、サーバーを起動します。
「stdioでサーバーを実行する」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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! ✅
よくある質問
「stdioでサーバーを実行する」レッスンは無料ですか?
はい。「stdioでサーバーを実行する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。
「stdioでサーバーを実行する」で何を学びますか?
クライアントがローカルで接続できるよう、サーバーを起動します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MCP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「stdioでサーバーを実行する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMCP Academyレッスンでコードを書いて実行できますか?
はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- FastMCPインスタンスを作成する
- Helloツールを追加する
- stdioでサーバーを実行する
- 読み込みとツール一覧を確認する