0Pricing
MCP Academy · レッスン

クライアントセッションを開始する

サーバーに接続し、ハンドシェイクを実行します。

「クライアントセッションを開始する」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

You Become the Caller

So far the server advertised tools and waited. Now you flip roles and write the client that reaches out, connects, and drives a server from your own Python code.

Same SDK, Client Side

The official mcp package you already installed ships the client too. No new dependency is needed to start connecting to servers.

from mcp import ClientSession, StdioServerParameters

Pick a Transport

A client first chooses how to reach the server. For a local process you use the stdio transport, talking over standard input and output.

from mcp.client.stdio import stdio_client

Describe How to Launch It

You tell stdio exactly how to start the server with StdioServerParameters: the command to run and any arguments it needs.

params = StdioServerParameters(
    command="python",
    args=["server.py"],
)

Open the Pipes

The stdio_client launches the server and hands you two streams, one to read replies and one to write requests. It is an async context manager.

async with stdio_client(params) as (read, write):
    ...

Wrap Streams in a Session

Raw streams are low level, so you wrap them in a ClientSession. The session speaks JSON-RPC for you and tracks the request ids.

async with ClientSession(read, write) as session:
    ...

Run the Handshake

Before anything else, call session.initialize(). This performs the initialize exchange so both sides agree on version and capabilities.

await session.initialize()

Everything Is Async

MCP clients are built on asyncio, so your connecting code lives inside an async function and you await each network call.

import asyncio

async def main():
    ...

asyncio.run(main())

Why a Session Object

The session is your handle for the whole conversation. Once initialized, you call methods on it to list and use what the server offers.

One Session per Server

Each server you connect to gets its own client session. To talk to several servers, you open several sessions, each over its own transport.

Clean Up Automatically

Because the session and transport are context managers, leaving the async with block shuts the server down and closes the streams for you.

Quick Check

Which call must run before you list or invoke anything?

Recap: Connecting In

You built a client: choose a transport, launch the server with parameters, wrap the streams in a session, and call initialize before doing real work.

よくある質問

「クライアントセッションを開始する」レッスンは無料ですか?

はい。「クライアントセッションを開始する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。

「クライアントセッションを開始する」で何を学びますか?

サーバーに接続し、ハンドシェイクを実行します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

MCP Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「クライアントセッションを開始する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMCP Academyレッスンでコードを書いて実行できますか?

はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. クライアントセッションを開始する
  2. ツールとリソースを検出する
  3. コードからツールを呼び出す
  4. LLM経由でツール呼び出しをルーティングする
← MCP Academyに戻る