Skip to content

Incomplete Error Handling Examples in MCP Tool Template #47

@farce1

Description

@farce1

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:

  1. Input validation
  2. Error handling with try/catch blocks
  3. Custom error messages for MCP clients
  4. Timeout handling for async operations
  5. 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions