Log MCPServer handler exceptions by kind and keep crash details off the wire (#3314)
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
This commit is contained in:
parent
f1c40b062f
commit
56af44727b
2 changed files with 1427 additions and 310 deletions
|
|
@ -1,4 +1,5 @@
|
|||
from mcp.server import MCPServer
|
||||
from mcp.server.mcpserver.exceptions import ToolError
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
|
|
@ -9,5 +10,5 @@ CATALOG = {"Dune": "Frank Herbert", "Neuromancer": "William Gibson"}
|
|||
def get_author(title: str) -> str:
|
||||
"""Look up the author of a book in the catalog."""
|
||||
if title not in CATALOG:
|
||||
raise ValueError(f"No book titled {title!r} in the catalog.")
|
||||
raise ToolError(f"No book titled {title!r} in the catalog.")
|
||||
return CATALOG[title]
|
||||
|
|
|
|||
11
docs_src/handling_errors/tutorial004.py
Normal file
11
docs_src/handling_errors/tutorial004.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from mcp.server import MCPServer
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
CATALOG = {"Dune": "Frank Herbert", "Neuromancer": "William Gibson"}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def get_author(title: str) -> str:
|
||||
"""Look up the author of a book in the catalog."""
|
||||
return CATALOG[title]
|
||||
Loading…
Reference in a new issue