Sub topic 6
Typed inputs and structured results
This chapter assumes you have built the first tool from the previous chapter and know basic JSON Schema and Python type hints. It covers how type hints become the schema the model sees, and how to return data the model can parse reliably.
From type hints to input schema
FastMCP, the helper in the official Python SDK, reads your function signature and builds a JSON Schema for the tool's arguments. The model sees that schema when it lists tools. The server validates every call against it.
from typing import Annotated, Literal
from mcp.server.fastmcp import FastMCP
from pydantic import Field
mcp = FastMCP("team-tools")
DEPLOYS = {
"staging": ["v1.4.2", "v1.4.1", "v1.4.0"],
"production": ["v1.4.0", "v1.3.9"],
}
@mcp.tool()
def list_deploys(
environment: Literal["staging", "production"],
limit: Annotated[int, Field(ge=1, le=20)] = 5,
) -> list[str]:
"""List the most recent deployed versions for an environment, newest first."""
return DEPLOYS[environment][:limit]
if __name__ == "__main__":
mcp.run()Two annotations do the work here:
Literal["staging", "production"]becomes anenum. The model can only pick a listed value.Annotated[int, Field(ge=1, le=20)]becomesminimum: 1andmaximum: 20. The default of5makeslimitoptional.
If a call sends environment="prod" or limit=500, validation fails before your function runs. Your code can then assume the inputs are well formed.
Describing parameters
The docstring becomes the tool description. Parameters can carry their own descriptions through Field:
@mcp.tool()
def get_service_owner(
service: Annotated[str, Field(description="Short service name, such as 'billing'")],
) -> str:
"""Return the team that owns a service."""
return {"billing": "payments-team", "search": "discovery-team"}[service]Descriptions cost the model a few tokens. They often prevent wrong guesses, such as passing a full URL where a short name is expected.
Structured results
Returning a plain string works, but the model must read prose to find values. A typed return value is easier to use. When the return annotation is a Pydantic model, a TypedDict, or a similar type, recent SDK versions publish an output schema. They send the result as structured JSON alongside a text version for older clients.
from pydantic import BaseModel
class DeployInfo(BaseModel):
environment: str
version: str
rollback_available: bool
@mcp.tool()
def latest_deploy(environment: Literal["staging", "production"]) -> DeployInfo:
"""Return the newest deployment for an environment."""
versions = DEPLOYS[environment]
return DeployInfo(
environment=environment,
version=versions[0],
rollback_available=len(versions) > 1,
)Each field now has a name and a type. The model does not need to parse "Version v1.4.2 is live, you can roll back".
Common mistakes
- Using bare
strfor values with a fixed set of options. UseLiteralor anEnum. - Leaving numeric inputs unbounded. A model can ask for
limit=100000. - Omitting return annotations. Without one, the server cannot publish an output schema.
- Returning large nested objects. Return only the fields the model needs.
Exercise
A tool takes region: str and the model sometimes passes "us-east" and sometimes "US East 1". Which change fixes this at the schema level, and where is the invalid value caught?
Show answer
Change the annotation to Literal["us-east-1", "eu-west-1"], or an Enum with the valid regions. The schema then lists an enum the model must follow. The server rejects any other value during validation, before the tool function runs.