verify_webhook
Validate webhook requests from Replicate using HMAC-SHA256 to ensure authenticity and prevent unauthorized access to your system.
Instructions
Verify that a webhook request came from Replicate using HMAC-SHA256.
Args:
payload: The webhook payload to verify
signature: The signature from the X-Replicate-Signature header
secret: The webhook signing secret from get_webhook_secret
Returns:
True if signature is valid, False otherwise
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| payload | Yes | ||
| signature | Yes | ||
| secret | Yes |
Implementation Reference
- Core handler function for the 'verify_webhook' tool. Computes the expected HMAC-SHA256 signature from the serialized payload and secret, then compares it securely to the provided signature.@mcp.tool() def verify_webhook(payload: WebhookPayload, signature: str, secret: str) -> bool: """Verify that a webhook request came from Replicate using HMAC-SHA256. Args: payload: The webhook payload to verify signature: The signature from the X-Replicate-Signature header secret: The webhook signing secret from get_webhook_secret Returns: True if signature is valid, False otherwise """ if not signature or not secret: return False # Convert payload to canonical JSON string payload_str = json.dumps(payload.model_dump(), sort_keys=True) # Calculate expected signature expected = hmac.new(secret.encode(), payload_str.encode(), hashlib.sha256).hexdigest() # Compare signatures using constant-time comparison return hmac.compare_digest(signature, expected)
- Pydantic model defining the structure of the 'payload' input parameter for verify_webhook tool.class WebhookPayload(BaseModel): """The full payload of a webhook request.""" event: WebhookEvent prediction: Dict[str, Any] = Field(..., description="Full prediction object at time of event")
- src/mcp_server_replicate/server.py:902-902 (registration)The @mcp.tool() decorator registers the verify_webhook function with the FastMCP server using the default name based on the function name.@mcp.tool()