# SettleOnSuccessMiddleware

> FastMCP middleware that settles payments on chain only upon tool success.

```python
from ai_economy_sdk import SettleOnSuccessMiddleware
```

```python
class SettleOnSuccessMiddleware(gate: PaymentGate)
```

FastMCP middleware that settles payments on chain only upon tool success.

If the tool raises an exception or returns an error result (`is_error=True`),
the payment reservation in ReplayStore is released and the buyer is not charged.

It also refuses (with `PaymentRequiredError`)
any tool call with a positive price that did not pass through the gate's HTTP
middleware, for example a call made over a transport the gate does not wrap.

## Constructor parameters

- `gate` (`PaymentGate`, required):

## Methods

### `on_call_tool`

```python
async def on_call_tool(context: Any, call_next: Any) -> Any
```

**Parameters**

- `context` (`Any`, required):

- `call_next` (`Any`, required):

Source: [ai_economy_sdk/gate.py](https://github.com/x402-avm/ai-economy-sdk/blob/main/ai_economy_sdk/gate.py#L107)

Source: https://x402-avm.github.io/reference/python/ai-economy-sdk/settle-on-success-middleware
