0Pricing
MCP Academy · Lesson

Open a Client Session

Connect to a server and run the handshake.

Open a Client Session is a free MCP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Open a Client Session” lesson free?

Yes — the full text of “Open a Client Session” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Open a Client Session”?

Connect to a server and run the handshake. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MCP Academy?

No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Open a Client Session” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MCP Academy lesson?

Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Open a Client Session
  2. Discover Tools & Resources
  3. Invoke Tools from Code
  4. Route Tool Calls Through an LLM
← Back to MCP Academy