Model Sampling
Implementing Sampling with FastMCP
Our server at the moment only provides a human age of a dog based on its breed and age in dog years. Let's say we want our server to also be able to give recommendations for dog care based on the dog's age and breed. This is a more complex task that requires natural language generation, so it's a perfect candidate for using FastMCP's sampling capabilities.
The key addition is a call to ctx.sample() inside the tool. We pass a plain-text prompt via messages and an optional system_prompt to guide the model's behavior. Because the client may not support sampling, we wrap the call in a try/except so the tool still returns a useful answer even if the LLM step fails:
try:
result = await ctx.sample(
messages="A labrador dog is 5 years old",
system_prompt="You are a veterinary expert. Give one short health tip.",
)
tip = result.text or ""
except Exception:
tip = ""
This is the final implementation of our server:
cat > $HOME/workspace/puppy_guide/server/main.py << EOF
import os
from difflib import get_close_matches
from typing import Annotated
from dotenv import load_dotenv
from pydantic import Field
from fastmcp import Context
from fastmcp import FastMCP
from fastmcp.exceptions import ToolError
load_dotenv()
mcp = FastMCP(
name=os.getenv("MCP_NAME"),
instructions=os.getenv("MCP_INSTRUCTIONS"),
)
BREED_MULTIPLIERS = {
"labrador": 7,
"chihuahua": 5,
"german shepherd": 8,
"bulldog": 6,
}
def get_breed_multiplier(breed: str) -> int | None:
"""Fetch the age multiplier for a given dog breed."""
return BREED_MULTIPLIERS.get(breed.lower())
@mcp.tool
async 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")],
ctx: Context,
) -> str:
"""Calculate the real age of a dog in human years based on its breed."""
total_steps = 4
await ctx.report_progress(
progress=0,
total=total_steps,
message=f"Looking up breed multiplier for '{breed}'",
)
await ctx.info(f"Looking up breed multiplier for '{breed}'")
multiplier = get_breed_multiplier(breed)
if multiplier is None:
suggestions = get_close_matches(
breed.lower(), BREED_MULTIPLIERS.keys(), n=1, cutoff=0.6
)
if suggestions:
await ctx.info(
f"Breed '{breed}' not found, but found similar: {suggestions}"
)
result =Practical MCP with FastMCP & LangChain
Engineering the Agentic ExperienceEnroll 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!
