コードからツールを呼び出す
サーバーのツールを呼び出し、その結果を読み取ります。
「コードからツールを呼び出す」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
From Listing to Doing
Discovery told you what exists. Now you actually invoke a tool, sending arguments and reading back the result from your own code.
Call by Name
Use session.call_tool() with the tool name and a dictionary of arguments. The names must match the inputSchema you discovered.
result = await session.call_tool(
"add",
{"a": 2, "b": 3},
)Arguments Must Validate
Your arguments dictionary should satisfy the tool inputSchema. Sending the wrong shape leads the server to reject the call.
await session.call_tool("greet", {"name": "Ada"})Read the Content
The result carries a content list of blocks. Most tools return text, so you read the text off the first block.
print(result.content[0].text)Check for Errors
A tool that fails sets isError to true on the result instead of raising. Inspect it so your client can react gracefully.
if result.isError:
print("tool failed")Structured Output Too
Newer tools may also return structuredContent, a typed object alongside the text, when the server defines an output schema.
print(result.structuredContent)Pass Real Arguments
Arguments come from your program, a user, or a model. The client simply forwards them as a plain dict to the named tool.
args = {"city": "Paris"}
await session.call_tool("weather", args)It Is Still Async
Like every network call, call_tool is awaited. The await pauses until the server finishes the work and the result returns.
One Call, One Result
Each invocation is a request-response pair. The session matches the reply to your request by id, so results never get crossed.
Handle Slow Tools
Some tools take time. A robust client sets a timeout so a hung tool does not freeze your whole program.
You Are the Orchestrator
Calling tools from code means your program decides the order, combines results, and turns raw output into something useful for the user.
Quick Check
How do you run a discovered tool from client code?
Recap: Making the Call
You invoked tools end to end: call by name with valid arguments, read the content blocks, and check isError before trusting the output.
よくある質問
「コードからツールを呼び出す」レッスンは無料ですか?
はい。「コードからツールを呼び出す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。
「コードからツールを呼び出す」で何を学びますか?
サーバーのツールを呼び出し、その結果を読み取ります。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MCP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「コードからツールを呼び出す」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMCP Academyレッスンでコードを書いて実行できますか?
はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- クライアントセッションを開始する
- ツールとリソースを検出する
- コードからツールを呼び出す
- LLM経由でツール呼び出しをルーティングする