Skip to main content
Glama
gqy20

Europe PMC Literature Search MCP Server

logging.py1.69 kB
""" 日志和计时中间件 """ import logging import time from typing import Any from fastmcp.server.middleware import Middleware, MiddlewareContext class LoggingMiddleware(Middleware): """日志中间件""" def __init__(self, logger: logging.Logger | None = None): self.logger = logger or logging.getLogger(__name__) async def on_message(self, context: MiddlewareContext, call_next): """记录请求日志""" start_time = time.time() self.logger.info(f"开始处理 {context.method}") try: result = await call_next(context) processing_time = round(time.time() - start_time, 2) self.logger.info(f"{context.method} 处理成功,耗时 {processing_time}s") return result except Exception as e: processing_time = round(time.time() - start_time, 2) self.logger.error(f"{context.method} 处理失败,耗时 {processing_time}s,错误: {e}") raise class TimingMiddleware(Middleware): """计时中间件 - 自动添加性能统计""" def __init__(self): self.logger = logging.getLogger(__name__) async def on_message(self, context: MiddlewareContext, call_next): """自动添加计时信息""" start_time = time.time() result = await call_next(context) processing_time = round(time.time() - start_time, 2) # 如果结果是字典,添加计时信息 if isinstance(result, dict): result["processing_time"] = processing_time result["timestamp"] = time.time() return result

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/gqy20/article-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server