طلب إكمال من المضيف
استدعوا العميل لتشغيل تعليمة LLM.
طلب إكمال من المضيف درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MCP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MCP Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Request Method
To ask for a generation, your server sends a sampling/createMessage request to the client over the protocol.
{ "method": "sampling/createMessage" }Reach the Client via Context
Inside a tool you get a Context object. Its session is your line back to the client that can run sampling.
async def tool(topic: str, ctx: Context) -> str:
...Call create_message
The Python SDK exposes ctx.session.create_message. Await it, pass your messages, and you get the model’s reply back.
result = await ctx.session.create_message(
messages=[...],
max_tokens=100,
)Build a SamplingMessage
Each turn you send is a SamplingMessage with a role like user and a content block holding the actual text.
SamplingMessage(role="user",
content=TextContent(type="text", text=prompt))Wrap Text in TextContent
Plain text goes inside TextContent with type set to text. That typed wrapper is how MCP carries the message body.
TextContent(type="text", text="Summarize this report")Always Set max_tokens
Give the model a ceiling with max_tokens. It caps how long the generated reply can be and keeps cost predictable.
max_tokens=200Read the Result
The reply’s content is a typed block. Check its type, and if it is text, read result.content.text for the answer.
if result.content.type == "text":
return result.content.textInspect Which Model Ran
The result also reports the model the client chose, so you can log exactly which model produced your output.
print(result.model) # e.g. claude-3-sonnet-20240307Know Why It Stopped
A stopReason field tells you why generation ended — like endTurn or hitting your max token limit.
result.stopReason # "endTurn"A Whole Tool, Briefly
So a sampling tool is: build a message, await create_message, then return the text — just a few lines.
r = await ctx.session.create_message(
messages=[msg], max_tokens=100)
return r.content.textIt Can Be Rejected
Remember the request may be denied by the user. Treat a rejection like any failure and handle it gracefully.
Quick Check
One detail about making the call from your tool.
Recap
Send sampling/createMessage via ctx.session, wrap prompts in SamplingMessage and TextContent, set max_tokens, then read result.content.text. 💬
الأسئلة الشائعة
هل درس «طلب إكمال من المضيف» مجاني؟
نعم — نص درس «طلب إكمال من المضيف» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.
ماذا ستتعلم في «طلب إكمال من المضيف»؟
استدعوا العميل لتشغيل تعليمة LLM. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟
لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «طلب إكمال من المضيف»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟
نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- ما الذي يتيحه Sampling؟
- طلب إكمال من المضيف
- تشكيل الرسائل والتفضيلات
- الموافقة بمشاركة الإنسان