0Pricing
MCP Academy · درس

فتح جلسة عميل

اتصلوا بخادم وشغّلوا عملية المصافحة.

فتح جلسة عميل درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.

ماذا ستتعلم في «فتح جلسة عميل»؟

اتصلوا بخادم وشغّلوا عملية المصافحة. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟

لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «فتح جلسة عميل»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟

نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. فتح جلسة عميل
  2. اكتشاف الأدوات والموارد
  3. استدعاء الأدوات من التعليمات البرمجية
  4. توجيه استدعاءات الأدوات عبر LLM
← العودة إلى MCP Academy