電卓ツールを最初から最後まで作る
小規模ながら完全な算術ツールを構築します。
「電卓ツールを最初から最後まで作る」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Build One Complete Tool
Let's combine everything into one small calculator tool, from a clear name to a clean result the model can trust. 🧮
Start with the Server
First create a FastMCP instance to hold the tool. Give it a name so clients can identify your server.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calculator")Name the Operation
Pick a precise name. We will add two numbers, so the function add reads clearly to both you and the model.
Declare Typed Parameters
Use type hints so the model sends numbers, not text. We accept two floats to allow decimals as well as whole values.
def add(a: float, b: float) -> float:
...Write the Docstring
The docstring becomes the description, so state plainly what the tool does and what it returns.
def add(a: float, b: float) -> float:
"""Add two numbers and return the sum."""Decorate It as a Tool
Add the tool decorator so FastMCP registers the function and advertises it to any connected client.
@mcp.tool()
def add(a: float, b: float) -> float:
"""Add two numbers."""
return a + bReturn a Clear Result
Returning the sum works, but a labeled result reads better to the model and to the user who sees the answer.
return a + bAdd a Second Tool
One tool per job, so create a separate divide tool rather than packing modes into add. Each stays simple to call.
@mcp.tool()
def divide(a: float, b: float) -> float:
return a / bGuard Against Bad Input
Division by zero would crash, so check first and return a clear error message the model can read and explain.
if b == 0:
return "Error: cannot divide by zero."Run the Server
Finish with the run call so the server starts and listens for a client over stdio when you launch the file.
if __name__ == "__main__":
mcp.run()Test the Whole Tool
Launch it and let a client list your tools. Seeing add and divide with their descriptions confirms the wiring is correct.
Quick Check
Why give divide its own tool instead of a mode flag on add?
Recap
You built a full calculator tool: named clearly, typed parameters, docstring description, guarded errors, and a clean result the model trusts. ✅
よくある質問
「電卓ツールを最初から最後まで作る」レッスンは無料ですか?
はい。「電卓ツールを最初から最後まで作る」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。
「電卓ツールを最初から最後まで作る」で何を学びますか?
小規模ながら完全な算術ツールを構築します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MCP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「電卓ツールを最初から最後まで作る」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMCP Academyレッスンでコードを書いて実行できますか?
はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ツールに適切な名前と説明を付ける
- 型ヒントでパラメーターを宣言する
- 役立つ結果を返す
- 電卓ツールを最初から最後まで作る