Error Handling in FastMCP Servers
49%
Resource and Prompt Error Handling
In the previous examples, we focused on tool errors, but the same principles apply to resources and prompts. For example, if a resource fails to load due to a missing file or database entry, you can raise a ResourceError with a clear message:
from fastmcp.exceptions import ResourceError
@mcp.resource("config://{name}")
def get_config(name: str) -> str:
"""Read a configuration file by name."""
config_dir = Path("configs")
config_file = config_dir / f"{name}.json"
if not config_file.exists():
raise ResourceError(f"Configuration '{name}' not found.")
return config_file.read_text()
Same thing goes for prompts:
# Prompt: realistic use case
from fastmcp.exceptions import PromptError
TEMPLATESPractical 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!
