-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
Issue Description
The example MCP tool (hello.py) doesn't show error handling patterns, which are crucial for production MCP servers.
Problem
- The example tool only shows a basic happy path implementation
- No demonstration of error handling, validation, or edge cases
- Production MCP servers need robust error handling to prevent crashes
Current Code
The hello.py example is very basic:
@mcp.tool()
async def hello(name: str) -> str:
return f"Hello, {name}!"Suggested Improvements
Add examples showing:
- Input validation
- Error handling with try/catch blocks
- Custom error messages for MCP clients
- Timeout handling for async operations
- Resource cleanup patterns
Example Enhancement
@mcp.tool()
async def hello(name: str) -> str:
"""
Say hello to someone.
Args:
name: The name to greet (must be non-empty)
Raises:
ValueError: If name is empty or invalid
"""
if not name or not name.strip():
raise ValueError("Name cannot be empty")
try:
# Example of more complex operation that could fail
validated_name = name.strip()
return f"Hello, {validated_name}!"
except Exception as e:
# Log error and return MCP-friendly error
logger.error(f"Error in hello tool: {e}")
raise RuntimeError(f"Failed to process greeting: {str(e)}")Impact
Without proper error handling examples, users might create fragile MCP servers that crash on unexpected input.
Metadata
Metadata
Assignees
Labels
No labels