Understanding MCP Tools

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous lesson, you learned that an LLM is like a brilliant mind trapped in a room. Tools are the hands that let it reach out and interact with the outside world.

You actually saw a tool in action during the very first example: the dog-age-to-human-age calculator.

When you asked the model to convert dog years, it didn’t try to guess the math based on its training data. Instead, it recognized that it had a specific tool designed for that exact purpose. It handed off the numbers to the tool, waited for the calculation, and presented the result back to you.

What Are Tools?

In the context of the Model Context Protocol (MCP), tools are executable functions that a server exposes to a host application. They allow the model to interact with external systems.

The Model is the Driver

It is important to understand the hierarchy of interaction in MCP. As a user, you do not call tools directly.

The Anatomy of a Tool

To the model, a tool isn’t magic; it’s a clearly defined schema. MCP uses JSON Schema to tell the model exactly what a tool does and what inputs it requires.

{
  "name": "calculate_dog_in_human_year",
  "description": "Calculates a dog's age in human years based on the following rules:\n- 1st year: 15 human years\n- 2nd year: +9 human years (Total 24)\n- 3rd year onwards: +4 human years for every year",
  "inputSchema": {
    "type": "object",
    "properties": {
      "dog_age": {
        "type": "integer",
        "description": "The age of the dog in years"
      }
    },
    "required": ["dog_age"]
  }
}

Human in the Loop

Because tools can perform actions—like modifying databases or sending emails—safety is a critical component of the protocol.

Setting Up Your Project

Now that you understand the theory, it is time to build. You will recreate the dog age calculator, but this time you will set it up in a dedicated project environment to prepare for deeper inspection.

$ mkdir lesson_2
$ cd lesson_2
$ uv init
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")
$ uv run --with mcp tools_demo_server.py
See forum comments
Download course materials from Github
Previous: Setting Up MCP Inspector Next: Executing Tools with Inspector