AI Hints
Home›MCP Tutorial›Calling Tools from a Client
MCP · Lesson 54 of 120

Handling Errors

Is_error.

⏱ 2 min read▶ Video at 47:54🎓 Free
Watch this lesson in the MCP courseWatch on YouTube ↗Full MCP course

Tool errors don't raise exceptions in the client. They come back as results, with is error set. That's on purpose. The model can read the error, and decide what to do next.

Handling Errors
From the video: Handling Errors
  • No exceptions
  • is_error = true
  • The model decides

Three kinds of failure

We call a missing order, a tool that doesn't exist, and a call with the wrong argument type. Run it. All three come back as error results, each with a message explaining what went wrong.

client_errors.pypython
import asyncio
from mcp import Client
from northwind import mcp

bad_calls = [("order_status", {"order_id": 7}),
             ("track_parcel", {"code": "AB12"}),
             ("order_status", {"order_id": "soon"})]

async def main():
    async with Client(mcp) as client:
        for name, args in bad_calls:
            r = await client.call_tool(name, args)
            print(r.is_error, "|", r.content[0].text[:60])

asyncio.run(main())
Output
True | Error executing tool order_status: Order 7 not found
True | Unknown tool: track_parcel
True | Error executing tool order_status: 1 validation error for or
Also uses northwind.py
from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.exceptions import ToolError

mcp = MCPServer("northwind")
ORDERS = {1042: "shipped", 1043: "packing", 1044: "delivered"}

@mcp.tool()
def order_status(order_id: int) -> str:
    """Look up the status of a Northwind order by its number."""
    if order_id not in ORDERS:
        raise ToolError(f"Order {order_id} not found")
    return ORDERS[order_id]

@mcp.tool()
def delivery_cost(order_total: float) -> str:
    """UK delivery cost for an order total in pounds."""
    return "free" if order_total >= 50 else "£4.99"

@mcp.resource("policy://returns")
def returns_policy() -> str:
    """Northwind's returns policy."""
    return "You can return any unused item within 30 days of delivery."

@mcp.prompt()
def support_reply(question: str) -> str:
    """Draft a polite reply to a customer."""
    return f"Write a short, polite reply to this Northwind customer: {question}"

if __name__ == "__main__":
    mcp.run()

What to do with errors

In an agent, pass the error text back to the model, so it can fix its call or explain. In your own code, check is error before using the result. Connection problems, unlike tool errors, do raise exceptions.

keyvalue
Agentsgive it to the model
Your codecheck is_error
Tool errorsresults
Connection errorsexceptions

Summary

Tool errors come back as results with is error set, so models can recover. Next, using several servers.

Key takeaways
  • is_error
  • Error text
  • Let the model retry
Next up: Several Servers — one client each