Sub topic 2
Typed inputs and structured results
Assume you know Python, JSON Schema, terminal commands, and basic LLM tool calling. This chapter uses the Python MCP SDK.
Typed inputs
A type annotation tells MCP how to describe a tool argument. The model sees this schema before calling the tool. Use narrow types and constraints so invalid requests fail early.
A structured result is a returned object with named fields. It is easier for a host or model to process than an unformatted sentence.
from typing import Annotated, Literal
from mcp.server.fastmcp import FastMCP
from pydantic import BaseModel, Field
mcp = FastMCP("team-tools")
class OwnerResult(BaseModel):
service: str
team: str
OWNERS = {"billing": "payments-team", "search": "discovery-team"}
@mcp.tool()
def get_service_owner(
service: Literal["billing", "search"],
) -> OwnerResult:
"""Return the team that owns a supported service."""
return OwnerResult(service=service, team=OWNERS[service])
@mcp.tool()
def list_deploys(
environment: Literal["staging", "production"],
limit: Annotated[int, Field(ge=1, le=20)] = 5,
) -> list[str]:
"""List recent deployed versions, newest first."""
deploys = {
"staging": ["v1.4.2", "v1.4.1"],
"production": ["v1.4.0", "v1.3.9"],
}
return deploys[environment][:limit]Literal restricts a value to known choices. Field adds numeric constraints. The SDK derives input and output schemas from these annotations.
Reporting errors the model can act on
Raise a clear exception when a validly shaped request cannot be completed. Include the failed value and a useful next step. Avoid returning "error" as an ordinary successful result because the host may treat it as data.
@mcp.tool()
def find_owner(service: str) -> OwnerResult:
"""Find the owner of a service by name."""
team = OWNERS.get(service)
if team is None:
raise ValueError(
f"Unknown service {service!r}. Try billing or search."
)
return OwnerResult(service=service, team=team)The SDK turns the exception into a tool error. Do not expose stack traces, credentials, or internal paths.
Resources and prompts
Resources expose read-only context by URI. Prompts package reusable instructions. Neither should replace a tool that performs an action.
RUNBOOKS = {
"billing": "Check queue depth, then page payments-team if needed."
}
@mcp.resource("runbook://{service}")
def runbook(service: str) -> str:
"""Return the read-only runbook for a service."""
return RUNBOOKS.get(service, "No runbook found.")
@mcp.prompt()
def incident_summary(service: str) -> str:
"""Create an incident-summary instruction."""
return f"Summarize the impact, status, and next steps for {service}."Designing usable tools
Give each tool one clear job. Use a verb-based name, describe units and side effects, return stable fields, and keep defaults conservative. Separate read operations from destructive actions so a host can request confirmation for the latter.
Exercise
Which primitive should expose a read-only deployment runbook?
Show answer
A resource, because it provides contextual data without performing an action.