1515"""MCP Proxy Manager for handling proxy content integration."""
1616
1717import logging
18+ from fastmcp .server .middleware .error_handling import RetryMiddleware
19+ from fastmcp .server .middleware .rate_limiting import RateLimitingMiddleware
1820from fastmcp .server .server import FastMCP
1921
2022
@@ -30,6 +32,7 @@ def __init__(self, target_mcp: FastMCP, read_only: bool = False):
3032 target_mcp: The target MCP server to add content to
3133 read_only: If true, disable tools that require write permissions OR that do not have `readOnlyHint` set.
3234 """
35+ self ._add_rate_limiting_middleware (target_mcp )
3336 self .target_mcp = target_mcp
3437 self .read_only = read_only
3538
@@ -42,6 +45,8 @@ async def add_proxy_content(self, proxy: FastMCP) -> None:
4245 Raises:
4346 Exception: If tools cannot be retrieved or added
4447 """
48+ self ._add_retry_middleware (proxy )
49+
4550 try :
4651 await self ._add_tools (proxy )
4752 await self ._add_resources (proxy )
@@ -115,3 +120,26 @@ async def _add_prompts(self, proxy: FastMCP) -> None:
115120 self .logger .debug ("Proxy doesn't have prompts method" )
116121 except Exception as e :
117122 self .logger .warning (f'Failed to get prompts from proxy: { e } ' )
123+
124+ def _add_retry_middleware (self , mcp : FastMCP ) -> None :
125+ """Add retry with exponential backoff middleware to target MCP server.
126+
127+ Args:
128+ mcp: The FastMCP instance to add exponential backoff to
129+ """
130+ self .logger .info ('Adding retry middleware' )
131+ mcp .add_middleware (RetryMiddleware ())
132+
133+ def _add_rate_limiting_middleware (self , mcp ):
134+ """Add retry with exponential backoff middleware to target MCP server.
135+
136+ Args:
137+ mcp: The FastMCP instance to add rate limiting to
138+ """
139+ self .logger .info ('Adding rate limiting middleware' )
140+ mcp .add_middleware (
141+ RateLimitingMiddleware (
142+ max_requests_per_second = 5 ,
143+ burst_capacity = 10 ,
144+ )
145+ )
0 commit comments