feat: Middleware [WIP] #269
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Description
Changes
This PR introduces a new middleware system for
mcp-use
. This system provides a flexible way to intercept and process MCP requests and responses, enabling features like logging, metrics, performance monitoring, and custom request/response handling.A default logging middleware has been added and is enabled by default to provide basic request/response visibility. Additionally, a comprehensive set of metrics and analytics middleware is included for advanced monitoring.
Implementation Details
Core Middleware Architecture (
mcp_use/middleware/middleware.py
):Middleware
base class allows for creating custom middleware with strongly-typed hooks for different MCP methods (e.g.,on_call_tool
,on_initialize
).MiddlewareContext
provides a unified, typed context for all middleware operations.MiddlewareManager
orchestrates the execution of the middleware chain.CallbackClientSession
acts as an adapter to integrate the middleware system with the existingClientSession
without requiring changes to upstream callers.Built-in Middleware:
LoggingMiddleware
(mcp_use/middleware/logging.py
): Provides default debug-level logging for all MCP requests and responses.MetricsMiddleware
,PerformanceMetricsMiddleware
,ErrorTrackingMiddleware
, andCombinedAnalyticsMiddleware
(mcp_use/middleware/metrics.py
): A suite of middleware for collecting detailed metrics, performance data, and error analytics.Integration:
MCPClient
has been updated to accept amiddleware
argument in its constructor.default_logging_middleware
is automatically included.CallbackClientSession
.Example Usage
The new
examples/example_middleware.py
provides a runnable example of this.Backwards Compatibility
These changes are fully backwards compatible. The
middleware
argument inMCPClient
is optional. Existing code that does not use middleware will continue to work as before, with the only change being the addition of default debug logging for MCP requests.