Now it’s time to write the code for your MCP server. You’ll create a simple tool that converts a dog’s age to human years. While this might seem like a trivial example, it perfectly demonstrates the MCP pattern: exposing a function that an LLM can call when it needs to perform a specific task.
Create or replace the contents of main.py with the following code:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Hello World MCP", json_response=True)
@mcp.tool()
def calculate_dog_in_human_year(dog_age: int) -> int:
"""
Calculates a dog's age in human years based on the following rules:
- 1st year: 15 human years
- 2nd year: +9 human years (Total 24)
- 3rd year onwards: +4 human years for every year
"""
if dog_age < 0:
return "Age cannot be negative"
if dog_age == 0:
return 0
elif dog_age <= 1:
# 1 year old = 15 human years
return dog_age * 15
elif dog_age <= 2:
# 2 years old = 24 human years
# (15 for the first year + 9 for the second)
return 15 + ((dog_age - 1) * 9)
else:
# 2+ years old = 24 + 4 years for every year after 2
return 24 + ((dog_age - 2) * 4)
if __name__ == "__main__":
mcp.run(transport="streamable-http")
Examine each part of this code:
FastMCP Import and Initialization: You import FastMCP from the MCP SDK. FastMCP is a high-level abstraction that handles all the protocol details for you. When you create the mcp instance, you give it a name (“Hello World MCP”) that will identify your server to hosts. The json_response=True parameter ensures your responses are formatted as JSON.
The @mcp.tool() Decorator: This decorator is where the magic happens. It registers your function as a tool that can be called by AI applications. FastMCP automatically extracts the function name, parameters, and docstring to create the tool’s specification. The docstring is particularly important. It tells the LLM what the tool does and when to use it.
The Calculation Function: Your function implements a common formula for converting dog years to human years. The first year of a dog’s life equals about 15 human years, the second year adds 9 more (totaling 24), and each subsequent year adds 4 human years. While the actual aging process varies by breed and size, this formula provides a reasonable approximation for your demonstration.
Running the Server: The mcp.run() call starts your server using the streamable-http transport. This means your server will listen for HTTP connections on a local port. You’ll use ngrok to expose this server to the internet so Claude Desktop can connect to it.
See forum comments
This content was released on Apr 10 2026. The official support period is 6-months
from this date.
Learn how to write the code for your first MCP server using the FastMCP framework. This lesson covers using the @mcp.tool() decorator to expose a Python function as an AI tool, specifically implementing a calculator for converting dog years to human years.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Setting Up Development Environment
Next: Running MCP Server
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.