ホストに補完をリクエストする
クライアントを呼び出してLLMのプロンプトを実行します。
「ホストに補完をリクエストする」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。
「ホストに補完をリクエストする」で何を学びますか?
クライアントを呼び出してLLMのプロンプトを実行します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MCP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「ホストに補完をリクエストする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMCP Academyレッスンでコードを書いて実行できますか?
はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Samplingで可能になること
- ホストに補完をリクエストする
- メッセージと設定を整える
- Human-in-the-Loop承認