A Calculator Tool, Start to Finish
Build a small but complete arithmetic tool.
A Calculator Tool, Start to Finish is a free MCP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. ✅
Frequently asked questions
Is the “A Calculator Tool, Start to Finish” lesson free?
Yes — the full text of “A Calculator Tool, Start to Finish” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.
What will I learn in “A Calculator Tool, Start to Finish”?
Build a small but complete arithmetic tool. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start MCP Academy?
No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “A Calculator Tool, Start to Finish” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this MCP Academy lesson?
Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Name & Describe a Tool Well
- Declare Parameters with Type Hints
- Return Useful Results
- A Calculator Tool, Start to Finish