Abrir uma sessão de cliente
Conecte-se a um servidor e execute o handshake.
Abrir uma sessão de cliente é uma aula grátis de MCP Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de MCP Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de MCP Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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, StdioServerParametersPick 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_clientDescribe 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.
Perguntas Frequentes
A aula “Abrir uma sessão de cliente” é grátis?
Sim — o texto completo de “Abrir uma sessão de cliente” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de MCP Academy, atualize para CoddyKit PRO. O curso de MCP Academy inclui 4 aulas no total.
O que vou aprender em “Abrir uma sessão de cliente”?
Conecte-se a um servidor e execute o handshake. Você pratica MCP Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar MCP Academy?
Nenhuma experiência prévia é necessária. MCP Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.
Quanto tempo leva a aula “Abrir uma sessão de cliente”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de MCP Academy?
Sim. Cada aula de MCP Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Abrir uma sessão de cliente
- Descobrir ferramentas e recursos
- Invocar ferramentas pelo código
- Encaminhar chamadas de ferramentas por um LLM