Skip to content

Error Handling

pcp_mcp.errors

Error mapping from httpx to MCP ToolErrors.

PCPConnectionError

Bases: PCPError

Cannot connect to pmproxy.

PCPError

Bases: Exception

Base PCP error.

PCPMetricNotFoundError

Bases: PCPError

Metric does not exist.

handle_pcp_error

handle_pcp_error(e: Exception, operation: str) -> ToolError

Convert PCP/httpx exceptions to MCP ToolErrors.

Parameters:

Name Type Description Default
e Exception

The exception to convert.

required
operation str

Description of the operation that failed.

required

Returns:

Type Description
ToolError

A ToolError with an appropriate message.

Source code in src/pcp_mcp/errors.py
def handle_pcp_error(e: Exception, operation: str) -> ToolError:
    """Convert PCP/httpx exceptions to MCP ToolErrors.

    Args:
        e: The exception to convert.
        operation: Description of the operation that failed.

    Returns:
        A ToolError with an appropriate message.
    """
    match e:
        case httpx.ConnectError():
            return ToolError("Cannot connect to pmproxy. Is it running? (systemctl start pmproxy)")
        case httpx.HTTPStatusError() as he if he.response.status_code == 400:
            return ToolError(f"Bad request during {operation}: {he.response.text}")
        case httpx.HTTPStatusError() as he if he.response.status_code == 404:
            return ToolError(f"Metric not found during {operation}")
        case httpx.HTTPStatusError() as he:
            return ToolError(f"pmproxy error ({he.response.status_code}): {he.response.text}")
        case httpx.TimeoutException():
            return ToolError(f"Request timed out during {operation}")
        case PCPConnectionError():
            return ToolError(str(e))
        case PCPMetricNotFoundError():
            return ToolError(f"Metric not found: {e}")
        case _:
            return ToolError(f"Error during {operation}: {e}")