Feedback

Chat Icon

Practical MCP with FastMCP & LangChain

Engineering the Agentic Experience

Error Handling in FastMCP Servers
50%

Putting Everything Together

To get the final version of the dog age calculator server, use the following code:

cat > main.py << EOF
import os
from typing import Annotated
from dotenv import load_dotenv
from pydantic import Field
from fastmcp import FastMCP
from fastmcp.exceptions import ToolError

load_dotenv()

mcp = FastMCP(
    name=os.getenv("MCP_NAME"),
    instructions=os.getenv("MCP_INSTRUCTIONS"),
)

def get_breed_multiplier(breed: str) -> int | None:
    """Fetch the age multiplier for a given dog breed."""
    breed_multipliers = {
        "labrador": 7,
        "chihuahua": 5,
        "german shepherd": 8,
        "bulldog": 6,
        # In reality there are over 350 recognized breeds,
        # but for brevity we only list a few here.
    }
    # Returns None if the breed is not found
    return breed_multipliers.get(breed.lower())

@mcp.tool
def dog_to_human_age(
    age: Annotated[int, Field(ge=0, le=30, description="The dog's age in years")],
    breed: Annotated[str, Field(description="The dog's breed")],
) -> int:
    """Calculate the real age of a dog in human years based on its breed."""
    multiplier = get_breed_multiplier(breed)
    if multiplier is None:
        raise ToolError

Practical MCP with FastMCP & LangChain

Engineering the Agentic Experience

Enroll now to unlock current content and receive all future updates for free. Your purchase supports the author and fuels the creation of more exciting content. Act fast, as the price will rise as the course nears completion!