mirror of
https://github.com/mcp-use/mcp-use.git
synced 2026-09-14 18:06:01 +08:00
18f51eddb8
* move: session * move: client * move:config * move: exceptions * move: connectors -> client.connectors * move: task_managers -> client.task_managers * move: observability -> agents.observability * move: middleware -> client.middleware * move: managers -> agents.managers * move: auth -> client.auth * move: adapters -> agents.adapters * test: bc test * test: bc test * chore: remove relative imports * fix tests * chore: update docs * chore: deprecation warnings * chore: fix dangerous non absolute import * fix: remove legacy imports * telemetry
32 lines
1.2 KiB
Python
32 lines
1.2 KiB
Python
"""
|
|
Default logging middleware for MCP requests.
|
|
|
|
Simple debug logging for all MCP requests and responses.
|
|
"""
|
|
|
|
import time
|
|
from typing import Any
|
|
|
|
from mcp_use.client.middleware.middleware import Middleware, MiddlewareContext, NextFunctionT
|
|
from mcp_use.logging import logger
|
|
|
|
|
|
class LoggingMiddleware(Middleware):
|
|
"""Default logging middleware that logs all MCP requests and responses with logger.debug."""
|
|
|
|
async def on_request(self, context: MiddlewareContext[Any], call_next: NextFunctionT) -> Any:
|
|
"""Logs all MCP requests and responses with logger.debug."""
|
|
logger.debug(f"[{context.id}] {context.connection_id} -> {context.method}")
|
|
try:
|
|
result = await call_next(context)
|
|
duration = time.time() - context.timestamp
|
|
logger.debug(f"[{context.id}] {context.connection_id} <- {context.method} ({duration:.3f}s)")
|
|
return result
|
|
except Exception as e:
|
|
duration = time.time() - context.timestamp
|
|
logger.debug(f"[{context.id}] {context.connection_id} <- {context.method} FAILED ({duration:.3f}s): {e}")
|
|
raise
|
|
|
|
|
|
default_logging_middleware = LoggingMiddleware()
|