Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

rate_limiting: deprecate a burst_capacity below 1 (#5296)

nate nowackb38126837027
diff --git a/docs/servers/middleware.mdx b/docs/servers/middleware.mdx
index 1617be564..f5ac8c5f4 100644
--- a/docs/servers/middleware.mdx
+++ b/docs/servers/middleware.mdx
@@ -512,7 +512,7 @@ mcp.add_middleware(RateLimitingMiddleware(
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `max_requests_per_second` | `float` | `10.0` | Sustained request rate |
-| `burst_capacity` | `int` | `20` | Maximum burst size |
+| `burst_capacity` | `int` | twice the rate, at least 1 for a positive rate | Maximum burst size; `1` allows no bursts beyond the steady rate |
| `get_client_id` | `Callable` | `None` | Custom client identification |
For sliding window rate limiting:
diff --git a/fastmcp_slim/fastmcp/server/middleware/rate_limiting.py b/fastmcp_slim/fastmcp/server/middleware/rate_limiting.py
index 7891aa94b..720101370 100644
--- a/fastmcp_slim/fastmcp/server/middleware/rate_limiting.py
+++ b/fastmcp_slim/fastmcp/server/middleware/rate_limiting.py
@@ -2,6 +2,7 @@
import inspect
import time
+import warnings
from collections import defaultdict, deque
from collections.abc import Awaitable, Callable
from typing import Any, cast
@@ -9,6 +10,8 @@ from typing import Any, cast
import anyio
from mcp import MCPError
+from fastmcp._warnings import FastMCPDeprecationWarning
+
from .middleware import CallNext, Middleware, MiddlewareContext
@@ -123,12 +126,31 @@ class RateLimitingMiddleware(Middleware):
Args:
max_requests_per_second: Sustained requests per second allowed
- burst_capacity: Maximum burst capacity. If None, defaults to 2x max_requests_per_second
- (at least 1 for a positive rate)
+ burst_capacity: Maximum burst capacity, at least 1. If None or 0, defaults to
+ 2x max_requests_per_second (at least 1 for a positive rate). Values below 1
+ are deprecated.
get_client_id: Function to extract client ID from context. Can be sync or async.
If None, uses global limiting
global_limit: If True, apply limit globally; if False, per-client
"""
+ if burst_capacity is not None and burst_capacity < 1:
+ if max_requests_per_second > 0:
+ instead = (
+ "Pass burst_capacity=1 for no bursting beyond the steady rate."
+ )
+ else:
+ instead = (
+ "To reject every request, omit burst_capacity: with "
+ "max_requests_per_second=0 the default capacity is 0."
+ )
+ warnings.warn(
+ f"RateLimitingMiddleware(burst_capacity={burst_capacity!r}) is deprecated "
+ "and will raise a ValueError in FastMCP 5: a bucket that holds less than "
+ "one token can't admit a request, so 0 falls back to the default and other "
+ f"values reject every request. {instead}",
+ FastMCPDeprecationWarning,
+ stacklevel=2,
+ )
self.max_requests_per_second = max_requests_per_second
default_capacity = int(max_requests_per_second * 2)
if max_requests_per_second > 0:
diff --git a/tests/server/middleware/test_rate_limiting.py b/tests/server/middleware/test_rate_limiting.py
index 1955780fb..7d8bd4432 100644
--- a/tests/server/middleware/test_rate_limiting.py
+++ b/tests/server/middleware/test_rate_limiting.py
@@ -1,12 +1,13 @@
"""Tests for rate limiting middleware."""
import asyncio
+import warnings
from unittest.mock import AsyncMock, MagicMock
import pytest
from mcp import MCPError
-from fastmcp import FastMCP
+from fastmcp import FastMCP, FastMCPDeprecationWarning
from fastmcp.client import Client
from fastmcp.server.middleware.middleware import MiddlewareContext
from fastmcp.server.middleware.rate_limiting import (
@@ -183,6 +184,35 @@ class TestRateLimitingMiddleware:
middleware = RateLimitingMiddleware(max_requests_per_second=0)
assert middleware.burst_capacity == 0
+ @pytest.mark.parametrize(
+ "burst_capacity, expected",
+ [(0, 20), (-1, -1), (0.5, 0.5)],
+ )
+ def test_burst_capacity_below_one_is_deprecated(self, burst_capacity, expected):
+ """Values below 1 warn but keep their 4.x behavior until FastMCP 5 (#5292)."""
+ with pytest.warns(FastMCPDeprecationWarning, match="burst_capacity=1"):
+ middleware = RateLimitingMiddleware(
+ max_requests_per_second=10, burst_capacity=burst_capacity
+ )
+ assert middleware.burst_capacity == expected
+
+ def test_zero_rate_deprecation_points_to_omitting_burst_capacity(self):
+ """The reject-all setup keeps its behavior and gets a migration that preserves it."""
+ with pytest.warns(FastMCPDeprecationWarning, match="omit burst_capacity"):
+ middleware = RateLimitingMiddleware(
+ max_requests_per_second=0, burst_capacity=0
+ )
+ assert middleware.burst_capacity == 0
+ with warnings.catch_warnings():
+ warnings.simplefilter("error", FastMCPDeprecationWarning)
+ assert RateLimitingMiddleware(max_requests_per_second=0).burst_capacity == 0
+
+ @pytest.mark.parametrize("burst_capacity", [None, 1, 20])
+ def test_valid_burst_capacity_does_not_warn(self, burst_capacity):
+ with warnings.catch_warnings():
+ warnings.simplefilter("error", FastMCPDeprecationWarning)
+ RateLimitingMiddleware(burst_capacity=burst_capacity)
+
def test_init_custom(self):
"""Test custom initialization."""