Skip to main content
Glama
README.md
# ClaudeMCPGuard

Resilience middleware for [FastMCP](https://github.com/jlowin/fastmcp).

ClaudeMCPGuard improves MCP tool reliability with bounded retries, exponential backoff, per-tool timeouts, structured JSON logs, and an optional circuit breaker.

[![Python](https://img.shields.io/badge/Python-3.10%2B-3776AB)](https://www.python.org/) [![FastMCP](https://img.shields.io/badge/FastMCP-2.x%20%7C%203.x-5B6CFF)](https://github.com/jlowin/fastmcp) [![License](https://img.shields.io/badge/License-MIT-20A464)](LICENSE)

[Installation](#installation) · [Usage](#quick-start) · [Configuration](#configuration) · [Development](#development)

## Overview

| Capability | Description |
|---|---|
| Bounded retries | Retry failed calls with configurable exponential backoff and a maximum attempt count. |
| Per-tool timeouts | Set a default timeout and override it for individual tools. |
| Structured logging | Emit one JSON record per completed tool call, including duration and attempt count. |
| Circuit breaker | Temporarily block calls after repeated failures and allow a recovery probe. |
| Typed package | Ship typing information and support Python 3.10 and newer. |
| Privacy-conscious logs | Exclude tool arguments and result payloads from log records. |

## Installation

Install the published distribution from PyPI:

```bash
python -m pip install ClaudeMCPGuard
```

The distribution is named `ClaudeMCPGuard`. The Python import package remains `mcp_guard`.

For development:

```bash
git clone https://github.com/Nawfel1235/ClaudeMCPGuard.git
cd ClaudeMCPGuard
python -m pip install -e ".[dev]"
```

## Quick start

```python
from fastmcp import FastMCP
from mcp_guard import MCPGuard

mcp = FastMCP("example")
mcp.add_middleware(MCPGuard())

@mcp.tool
async def lookup_user(user_id: str) -> dict[str, str]:
    return {"user_id": user_id, "status": "active"}

if __name__ == "__main__":
    mcp.run()
```

## Configuration

```python
from fastmcp import FastMCP
from mcp_guard import MCPGuard, MCPGuardConfig

mcp = FastMCP("production-tools")
mcp.add_middleware(
    MCPGuard(
        MCPGuardConfig(
            max_attempts=4,
            initial_delay=0.2,
            backoff_multiplier=2.0,
            timeout_seconds=20.0,
            tool_timeouts={"slow_report": 60.0, "no_timeout": None},
            breaker_failure_threshold=5,
            breaker_recovery_timeout=30.0,
        )
    )
)
```

### Options

| Option | Default | Description |
|---|---:|---|
| `max_attempts` | `3` | Total attempts, including the first call. |
| `initial_delay` | `0.25` | Initial retry delay, in seconds. |
| `backoff_multiplier` | `2.0` | Multiplier applied after each failed attempt. |
| `timeout_seconds` | `30.0` | Default timeout. Set to `None` to disable it. |
| `tool_timeouts` | `{}` | Per-tool timeout overrides. A value of `None` disables the timeout for that tool. |
| `breaker_failure_threshold` | `None` | Consecutive failed calls required to open the circuit. Disabled when `None`. |
| `breaker_recovery_timeout` | `30.0` | Seconds before the circuit allows a recovery probe. |

Retries apply to exceptions raised by the downstream FastMCP pipeline. A timeout counts as a failed attempt. The circuit breaker counts one failed tool call after retries are exhausted. Cancellation is propagated and is never retried.

> **Important:** Retries can repeat side effects. Enable them only for idempotent operations or tools protected by idempotency keys.

## Structured logging

ClaudeMCPGuard uses Python's standard logging package and the logger named `mcp_guard`.

Example record:

```json
{
  "tool_name": "lookup_user",
  "timestamp": "2026-11-29T20:00:00+00:00",
  "duration_seconds": 0.0142,
  "success": true,
  "attempts": 2
}
```

Failure records include an `error` field. Tool arguments and result payloads are intentionally excluded.

## Claude Code skill

A ready-to-use skill is included at `skills/SKILL.md`. Copy it into your project at `.claude/skills/mcp-guard/SKILL.md`.

## Compatibility

ClaudeMCPGuard targets FastMCP's public `Middleware` and `MiddlewareContext` APIs. The dependency range is `fastmcp>=2.14,<4`. Review FastMCP's middleware documentation when upgrading across major versions.

## Development

```bash
python -m pip install -e ".[dev]"
pytest
ruff check .
mypy
```

## License

Released under the MIT License. See [LICENSE](LICENSE).

Maintained by [Nawfel](https://github.com/Nawfel1235).