乐闻世界logo
搜索文章和话题

面试题手册

MCP 的版本管理和兼容性如何处理?

MCP 的版本管理和兼容性对于确保系统的稳定性和可维护性至关重要。以下是详细的版本管理策略和兼容性处理方法:版本管理策略MCP 版本管理应考虑以下方面:语义化版本控制:使用语义化版本号(SemVer)向后兼容性:确保新版本向后兼容旧版本废弃策略:明确的功能废弃和移除流程迁移指南:提供详细的版本迁移指南版本协商:客户端和服务器的版本协商机制1. 语义化版本控制from dataclasses import dataclassfrom typing import Optionalimport re@dataclassclass Version: """版本号""" major: int minor: int patch: int prerelease: Optional[str] = None build_metadata: Optional[str] = None def __str__(self) -> str: version_str = f"{self.major}.{self.minor}.{self.patch}" if self.prerelease: version_str += f"-{self.prerelease}" if self.build_metadata: version_str += f"+{self.build_metadata}" return version_str @classmethod def parse(cls, version_str: str) -> 'Version': """解析版本字符串""" # 匹配语义化版本格式 pattern = r'^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$' match = re.match(pattern, version_str) if not match: raise ValueError(f"无效的版本格式: {version_str}") major = int(match.group(1)) minor = int(match.group(2)) patch = int(match.group(3)) prerelease = match.group(4) build_metadata = match.group(5) return cls( major=major, minor=minor, patch=patch, prerelease=prerelease, build_metadata=build_metadata ) def is_compatible(self, other: 'Version') -> bool: """检查版本兼容性""" # 主版本号相同,次版本号向后兼容 if self.major != other.major: return False # 如果当前版本 >= 其他版本,则兼容 if (self.minor, self.patch) >= (other.minor, other.patch): return True return False def __lt__(self, other: 'Version') -> bool: """版本比较""" if self.major != other.major: return self.major < other.major if self.minor != other.minor: return self.minor < other.minor if self.patch != other.patch: return self.patch < other.patch # 预发布版本比较 if self.prerelease and not other.prerelease: return True if not self.prerelease and other.prerelease: return False if self.prerelease and other.prerelease: return self.prerelease < other.prerelease return False def __eq__(self, other: 'Version') -> bool: """版本相等""" return ( self.major == other.major and self.minor == other.minor and self.patch == other.patch and self.prerelease == other.prerelease )# 当前 MCP 版本MCP_VERSION = Version(1, 0, 0)2. 版本协商机制from typing import Dict, List, Optionalclass VersionNegotiator: """版本协商器""" def __init__(self, server_version: Version): self.server_version = server_version self.supported_versions: List[Version] = [ Version(1, 0, 0), Version(0, 9, 0), ] def negotiate_version( self, client_versions: List[Version] ) -> Optional[Version]: """协商最佳版本""" # 找到客户端支持的最高版本 client_versions_sorted = sorted(client_versions, reverse=True) for client_version in client_versions_sorted: # 检查服务器是否支持此版本 for server_version in self.supported_versions: if server_version.is_compatible(client_version): return server_version # 没有找到兼容版本 return None def get_server_info(self) -> Dict: """获取服务器版本信息""" return { "version": str(self.server_version), "supported_versions": [ str(v) for v in self.supported_versions ] }class MCPClient: """MCP 客户端""" def __init__(self, supported_versions: List[Version]): self.supported_versions = supported_versions self.negotiated_version: Optional[Version] = None async def connect(self, server_info: Dict) -> bool: """连接到服务器并协商版本""" server_version = Version.parse(server_info["version"]) server_supported_versions = [ Version.parse(v) for v in server_info["supported_versions"] ] # 创建临时协商器 negotiator = VersionNegotiator(server_version) # 协商版本 self.negotiated_version = negotiator.negotiate_version( self.supported_versions ) if not self.negotiated_version: print("无法协商兼容的版本") return False print(f"协商版本: {self.negotiated_version}") return True3. 功能废弃管理from enum import Enumfrom typing import Dict, List, Optionalfrom datetime import datetimeclass DeprecationStatus(Enum): """废弃状态""" STABLE = "stable" DEPRECATED = "deprecated" REMOVED = "removed"@dataclassclass DeprecationInfo: """废弃信息""" status: DeprecationStatus deprecated_in: Optional[Version] = None removed_in: Optional[Version] = None replacement: Optional[str] = None message: Optional[str] = Noneclass DeprecationManager: """废弃管理器""" def __init__(self): self.deprecations: Dict[str, DeprecationInfo] = {} def deprecate_feature( self, feature_name: str, deprecated_in: Version, removed_in: Version, replacement: str = None, message: str = None ): """标记功能为废弃""" self.deprecations[feature_name] = DeprecationInfo( status=DeprecationStatus.DEPRECATED, deprecated_in=deprecated_in, removed_in=removed_in, replacement=replacement, message=message ) def remove_feature(self, feature_name: str, removed_in: Version): """移除功能""" if feature_name in self.deprecations: self.deprecations[feature_name].status = DeprecationStatus.REMOVED self.deprecations[feature_name].removed_in = removed_in def check_feature( self, feature_name: str, current_version: Version ) -> tuple: """检查功能状态""" if feature_name not in self.deprecations: return True, None info = self.deprecations[feature_name] if info.status == DeprecationStatus.REMOVED: return False, f"功能 {feature_name} 已在版本 {info.removed_in} 中移除" if info.status == DeprecationStatus.DEPRECATED: if current_version >= info.removed_in: return False, f"功能 {feature_name} 已在版本 {info.removed_in} 中移除" warning = f"功能 {feature_name} 已废弃,将在版本 {info.removed_in} 中移除" if info.replacement: warning += f",请使用 {info.replacement}" return True, warning return True, None def get_deprecation_warnings( self, current_version: Version ) -> List[str]: """获取所有废弃警告""" warnings = [] for feature_name, info in self.deprecations.items(): if info.status == DeprecationStatus.DEPRECATED: if current_version < info.removed_in: warning = f"功能 {feature_name} 已废弃" if info.replacement: warning += f",请使用 {info.replacement}" warnings.append(warning) return warnings4. 版本迁移指南from typing import Dict, List, Callableclass MigrationGuide: """版本迁移指南""" def __init__(self): self.migrations: Dict[Version, List[Migration]] = {} def add_migration( self, from_version: Version, to_version: Version, migration_func: Callable, description: str ): """添加迁移步骤""" if from_version not in self.migrations: self.migrations[from_version] = [] migration = Migration( from_version=from_version, to_version=to_version, func=migration_func, description=description ) self.migrations[from_version].append(migration) def get_migration_path( self, from_version: Version, to_version: Version ) -> List[Migration]: """获取迁移路径""" if from_version == to_version: return [] # 简单实现:直接迁移 if from_version in self.migrations: for migration in self.migrations[from_version]: if migration.to_version == to_version: return [migration] # 复杂实现:查找多步迁移路径 return self._find_migration_path(from_version, to_version) def _find_migration_path( self, from_version: Version, to_version: Version, visited: set = None ) -> List[Migration]: """递归查找迁移路径""" if visited is None: visited = set() if from_version in visited: return [] visited.add(from_version) if from_version == to_version: return [] if from_version not in self.migrations: return [] for migration in self.migrations[from_version]: path = self._find_migration_path( migration.to_version, to_version, visited.copy() ) if path is not None: return [migration] + path return [] async def execute_migration( self, from_version: Version, to_version: Version, context: Dict ) -> bool: """执行迁移""" migration_path = self.get_migration_path(from_version, to_version) if not migration_path: print(f"无法找到从 {from_version} 到 {to_version} 的迁移路径") return False print(f"开始迁移: {from_version} -> {to_version}") for migration in migration_path: print(f"执行迁移: {migration.description}") try: await migration.func(context) print(f"迁移完成: {migration.description}") except Exception as e: print(f"迁移失败: {migration.description}, 错误: {e}") return False print(f"迁移完成: {from_version} -> {to_version}") return True@dataclassclass Migration: """迁移步骤""" from_version: Version to_version: Version func: Callable description: str5. 版本兼容性检查from typing import Dict, List, Tupleclass CompatibilityChecker: """兼容性检查器""" def __init__(self, current_version: Version): self.current_version = current_version self.compatibility_matrix: Dict[Version, List[Version]] = {} def add_compatibility( self, server_version: Version, compatible_client_versions: List[Version] ): """添加兼容性规则""" self.compatibility_matrix[server_version] = compatible_client_versions def check_compatibility( self, client_version: Version ) -> Tuple[bool, Optional[str]]: """检查客户端版本是否兼容""" # 检查服务器是否支持此版本 if self.current_version in self.compatibility_matrix: compatible_versions = self.compatibility_matrix[self.current_version] for compatible_version in compatible_versions: if client_version.is_compatible(compatible_version): return True, None # 使用默认兼容性规则 if self.current_version.is_compatible(client_version): return True, None return False, f"客户端版本 {client_version} 与服务器版本 {self.current_version} 不兼容" def get_compatible_versions(self) -> List[Version]: """获取所有兼容的客户端版本""" compatible_versions = [] if self.current_version in self.compatibility_matrix: compatible_versions = self.compatibility_matrix[self.current_version] return compatible_versions6. 版本信息 APIfrom fastapi import FastAPIfrom typing import Dictclass VersionInfoAPI: """版本信息 API""" def __init__( self, current_version: Version, supported_versions: List[Version], deprecation_manager: DeprecationManager, migration_guide: MigrationGuide ): self.current_version = current_version self.supported_versions = supported_versions self.deprecation_manager = deprecation_manager self.migration_guide = migration_guide def setup_routes(self, app: FastAPI): """设置路由""" @app.get("/version") async def get_version() -> Dict: """获取当前版本信息""" return { "version": str(self.current_version), "supported_versions": [str(v) for v in self.supported_versions], "deprecation_warnings": self.deprecation_manager.get_deprecation_warnings( self.current_version ) } @app.get("/version/compatibility/{client_version}") async def check_compatibility(client_version: str) -> Dict: """检查客户端版本兼容性""" checker = CompatibilityChecker(self.current_version) try: version = Version.parse(client_version) except ValueError: return { "compatible": False, "message": "无效的版本格式" } compatible, message = checker.check_compatibility(version) return { "compatible": compatible, "message": message, "server_version": str(self.current_version) } @app.get("/version/migration/{from_version}/{to_version}") async def get_migration_path( from_version: str, to_version: str ) -> Dict: """获取迁移路径""" try: from_ver = Version.parse(from_version) to_ver = Version.parse(to_version) except ValueError: return { "success": False, "message": "无效的版本格式" } path = self.migration_guide.get_migration_path(from_ver, to_ver) return { "success": True, "migration_path": [ { "from": str(m.from_version), "to": str(m.to_version), "description": m.description } for m in path ] }最佳实践:语义化版本:严格遵循语义化版本控制规范向后兼容:确保新版本向后兼容旧版本渐进废弃:提前通知功能废弃,给用户迁移时间文档完善:提供详细的版本迁移指南版本协商:实现客户端和服务器的版本协商机制测试覆盖:为每个版本编写兼容性测试通过完善的版本管理和兼容性处理,可以确保 MCP 系统的稳定性和可维护性。
阅读 0·2月19日 21:43

MCP 的未来发展趋势是什么?有哪些挑战和机遇?

MCP 作为新兴的 AI 集成协议,具有广阔的发展前景和潜力。以下是 MCP 的未来发展趋势:1. 标准化推进行业认可:获得更多 AI 模型提供商和企业的认可协议完善:持续完善协议规范,解决现有局限性标准化组织:可能提交给标准化组织(如 W3C、IETF)进行标准化互操作性增强:与现有协议(如 OpenAPI、GraphQL)的互操作性2. 生态系统扩展更多语言支持:扩展到 Rust、Java、C#、PHP 等更多编程语言服务器生态:社区贡献更多针对特定领域的 MCP 服务器客户端集成:更多 AI 应用和平台原生支持 MCP工具库丰富:提供更多预构建的工具和资源3. 性能优化协议优化:引入二进制协议、压缩、批量操作等优化异步增强:更强大的异步和流式处理能力缓存机制:智能缓存策略减少重复计算边缘计算:支持边缘节点部署,降低延迟4. 安全性增强高级认证:支持 OAuth 2.0、SAML 等企业级认证细粒度权限:更精细的访问控制和权限管理安全审计:完整的安全审计和合规支持加密增强:端到端加密和密钥管理5. 功能扩展实时通信:支持 WebSocket 等实时双向通信流式处理:更好的流式数据处理能力事件驱动:支持事件订阅和推送机制多模态支持:增强对图像、音频、视频等多模态数据的支持6. 企业级特性多租户支持:完善的多租户隔离和管理高可用性:内置高可用和灾难恢复机制可观测性:完整的监控、日志和追踪能力治理工具:提供企业级治理和管理工具7. AI 模型集成更多模型支持:支持更多开源和商业 AI 模型模型适配器:提供模型适配器简化集成性能优化:针对不同模型的性能优化成本控制:智能的成本控制和优化8. 开发者体验更好的工具:更强大的开发、测试和调试工具文档完善:更全面和易懂的文档示例丰富:更多实际应用场景的示例社区支持:活跃的社区支持和交流9. 应用场景拓展企业应用:更多企业级应用场景物联网:IoT 设备和系统的集成边缘 AI:边缘计算和 AI 的结合自动化:更广泛的自动化应用10. 挑战和机遇挑战:与现有协议的竞争和兼容性社区建设和生态发展性能和可扩展性的平衡安全性和易用性的权衡机遇:成为 AI 集成的行业标准推动AI应用的大规模部署促进AI技术的民主化创造新的商业模式和机会预测:未来 2-3 年内,MCP 有望成为 AI 模型与外部系统集成的主流标准之一,被广泛采用于企业级应用、开发工具和各种 AI 产品中。其开放性和标准化特性将推动整个 AI 生态系统的发展。开发者现在学习和采用 MCP,将能够在未来的 AI 应用开发中占据有利位置。
阅读 0·2月19日 21:41

MCP 如何与其他 AI 框架(如 LangChain、LlamaIndex)集成?

MCP 可以与各种 AI 框架和工具集成,扩展其功能和应用场景。以下是详细的集成方法和最佳实践:集成架构设计MCP 集成应考虑以下方面:框架兼容性:确保与目标框架的兼容性性能影响:最小化对系统性能的影响功能完整性:保持 MCP 和框架功能的完整性错误处理:正确处理集成过程中的错误配置管理:统一管理集成配置1. 与 LangChain 集成from langchain.agents import AgentExecutor, create_openai_tools_agentfrom langchain_openai import ChatOpenAIfrom langchain.tools import Toolfrom mcp.server import Serverclass MCPLangChainIntegration: def __init__(self, mcp_server: Server): self.mcp_server = mcp_server self.langchain_tools = [] async def convert_mcp_to_langchain_tools(self) -> list: """将 MCP 工具转换为 LangChain 工具""" mcp_tools = await self.mcp_server.list_tools() langchain_tools = [] for tool_info in mcp_tools: tool = Tool( name=tool_info["name"], description=tool_info["description"], func=self._create_tool_wrapper(tool_info["name"]) ) langchain_tools.append(tool) return langchain_tools def _create_tool_wrapper(self, tool_name: str): """创建工具包装器""" async def wrapper(**kwargs): result = await self.mcp_server.call_tool( tool_name, kwargs ) return result return wrapper async def create_langchain_agent(self): """创建 LangChain Agent""" # 转换 MCP 工具 tools = await self.convert_mcp_to_langchain_tools() # 创建 LLM llm = ChatOpenAI( model="gpt-4", temperature=0 ) # 创建 Agent agent = create_openai_tools_agent(llm, tools) # 创建 AgentExecutor agent_executor = AgentExecutor( agent=agent, tools=tools, verbose=True ) return agent_executor async def run_agent(self, query: str): """运行 Agent""" agent_executor = await self.create_langchain_agent() result = await agent_executor.ainvoke({ "input": query }) return result["output"]2. 与 LlamaIndex 集成from llama_index.core import VectorStoreIndex, SimpleDirectoryReaderfrom llama_index.core.tools import QueryEngineTool, ToolMetadatafrom llama_index.core.agent import ReActAgentfrom mcp.server import Serverclass MCPLlamaIndexIntegration: def __init__(self, mcp_server: Server): self.mcp_server = mcp_server async def create_mcp_query_engine(self, tool_name: str): """创建 MCP 查询引擎""" async def query_engine_fn(query: str) -> str: result = await self.mcp_server.call_tool( tool_name, {"query": query} ) return result return query_engine_fn async def create_llamaindex_tools(self) -> list: """创建 LlamaIndex 工具""" mcp_tools = await self.mcp_server.list_tools() tools = [] for tool_info in mcp_tools: query_engine = await self.create_mcp_query_engine( tool_info["name"] ) tool = QueryEngineTool( query_engine=query_engine, metadata=ToolMetadata( name=tool_info["name"], description=tool_info["description"] ) ) tools.append(tool) return tools async def create_react_agent(self): """创建 ReAct Agent""" tools = await self.create_llamaindex_tools() agent = ReActAgent.from_tools( tools=tools, verbose=True ) return agent async def query_with_agent(self, query: str): """使用 Agent 查询""" agent = await self.create_react_agent() response = agent.query(query) return response.response3. 与 AutoGPT 集成from autogpt.agent.agent import Agentfrom autogpt.config import Configfrom autogpt.models.command import Commandfrom mcp.server import Serverclass MCPAutoGPTIntegration: def __init__(self, mcp_server: Server): self.mcp_server = mcp_server self.command_registry = {} async def register_mcp_commands(self): """注册 MCP 命令""" mcp_tools = await self.mcp_server.list_tools() for tool_info in mcp_tools: command = Command( name=tool_info["name"], description=tool_info["description"], function=self._create_command_function(tool_info["name"]) ) self.command_registry[tool_info["name"]] = command def _create_command_function(self, tool_name: str): """创建命令函数""" async def command_function(**kwargs): result = await self.mcp_server.call_tool( tool_name, kwargs ) return result return command_function async def create_autogpt_agent(self, config: Config): """创建 AutoGPT Agent""" # 注册 MCP 命令 await self.register_mcp_commands() # 创建 Agent agent = Agent( ai_name="MCP-Agent", ai_role="Assistant", commands=self.command_registry, config=config ) return agent async def run_autogpt_task(self, task: str): """运行 AutoGPT 任务""" config = Config() agent = await self.create_autogpt_agent(config) result = await agent.run(task) return result4. 与 FastAPI 集成from fastapi import FastAPI, HTTPException, Dependsfrom fastapi.middleware.cors import CORSMiddlewarefrom mcp.server import Serverfrom pydantic import BaseModelclass MCPFastAPIIntegration: def __init__(self, mcp_server: Server): self.mcp_server = mcp_server self.app = FastAPI(title="MCP API") self._setup_middleware() self._setup_routes() def _setup_middleware(self): """设置中间件""" self.app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) def _setup_routes(self): """设置路由""" @self.app.get("/tools") async def list_tools(): """列出所有工具""" tools = await self.mcp_server.list_tools() return {"tools": tools} @self.app.post("/tools/{tool_name}/call") async def call_tool(tool_name: str, params: dict): """调用工具""" try: result = await self.mcp_server.call_tool( tool_name, params ) return {"result": result} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @self.app.get("/resources") async def list_resources(): """列出所有资源""" resources = await self.mcp_server.list_resources() return {"resources": resources} @self.app.get("/resources/{uri}") async def read_resource(uri: str): """读取资源""" try: content = await self.mcp_server.read_resource(uri) return {"content": content} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @self.app.get("/prompts") async def list_prompts(): """列出所有提示词""" prompts = await self.mcp_server.list_prompts() return {"prompts": prompts} @self.app.get("/health") async def health_check(): """健康检查""" return {"status": "healthy"} def get_app(self) -> FastAPI: """获取 FastAPI 应用""" return self.app5. 与 WebSocket 集成import asyncioimport jsonfrom fastapi import WebSocketfrom mcp.server import Serverclass MCPWebSocketIntegration: def __init__(self, mcp_server: Server): self.mcp_server = mcp_server self.active_connections = [] async def handle_websocket(self, websocket: WebSocket): """处理 WebSocket 连接""" await websocket.accept() self.active_connections.append(websocket) try: while True: # 接收消息 data = await websocket.receive_text() message = json.loads(data) # 处理消息 response = await self._handle_message(message) # 发送响应 await websocket.send_text(json.dumps(response)) except Exception as e: print(f"WebSocket 错误: {e}") finally: self.active_connections.remove(websocket) async def _handle_message(self, message: dict) -> dict: """处理消息""" message_type = message.get("type") if message_type == "list_tools": tools = await self.mcp_server.list_tools() return {"type": "tools_list", "data": tools} elif message_type == "call_tool": result = await self.mcp_server.call_tool( message["tool_name"], message.get("params", {}) ) return {"type": "tool_result", "data": result} elif message_type == "list_resources": resources = await self.mcp_server.list_resources() return {"type": "resources_list", "data": resources} else: return {"type": "error", "message": "未知消息类型"} async def broadcast_message(self, message: dict): """广播消息到所有连接""" message_text = json.dumps(message) for connection in self.active_connections: try: await connection.send_text(message_text) except Exception as e: print(f"发送消息失败: {e}")6. 与 GraphQL 集成import strawberryfrom strawberry.types import Infofrom mcp.server import Server@strawberry.typeclass MCPTool: name: str description: str@strawberry.typeclass MCPResource: uri: str name: str description: str@strawberry.typeclass Query: @strawberry.field async def tools(self, info: Info) -> list[MCPTool]: """获取所有工具""" mcp_server = info.context["mcp_server"] tools = await mcp_server.list_tools() return [ MCPTool( name=tool["name"], description=tool["description"] ) for tool in tools ] @strawberry.field async def resources(self, info: Info) -> list[MCPResource]: """获取所有资源""" mcp_server = info.context["mcp_server"] resources = await mcp_server.list_resources() return [ MCPResource( uri=resource["uri"], name=resource["name"], description=resource["description"] ) for resource in resources ]@strawberry.typeclass Mutation: @strawberry.mutation async def call_tool( self, tool_name: str, params: dict, info: Info ) -> str: """调用工具""" mcp_server = info.context["mcp_server"] result = await mcp_server.call_tool(tool_name, params) return str(result)class MCPGraphQLIntegration: def __init__(self, mcp_server: Server): self.mcp_server = mcp_server self.schema = strawberry.Schema( query=Query, mutation=Mutation ) async def execute_query(self, query: str, variables: dict = None): """执行 GraphQL 查询""" context = {"mcp_server": self.mcp_server} result = await self.schema.execute( query, variable_values=variables, context_value=context ) if result.errors: return { "errors": [str(error) for error in result.errors] } return {"data": result.data}7. 与 gRPC 集成import grpcfrom concurrent import futuresimport mcp_pb2import mcp_pb2_grpcfrom mcp.server import Serverclass MCPServicer(mcp_pb2_grpc.MCPServicer): def __init__(self, mcp_server: Server): self.mcp_server = mcp_server async def ListTools( self, request: mcp_pb2.ListToolsRequest, context: grpc.ServicerContext ) -> mcp_pb2.ListToolsResponse: """列出工具""" tools = await self.mcp_server.list_tools() tool_protos = [ mcp_pb2.Tool( name=tool["name"], description=tool["description"] ) for tool in tools ] return mcp_pb2.ListToolsResponse(tools=tool_protos) async def CallTool( self, request: mcp_pb2.CallToolRequest, context: grpc.ServicerContext ) -> mcp_pb2.CallToolResponse: """调用工具""" params = dict(request.params) result = await self.mcp_server.call_tool( request.tool_name, params ) return mcp_pb2.CallToolResponse(result=str(result))class MCPGRPCIntegration: def __init__(self, mcp_server: Server, port: int = 50051): self.mcp_server = mcp_server self.port = port self.server = None async def start_server(self): """启动 gRPC 服务器""" self.server = grpc.aio.server( futures.ThreadPoolExecutor(max_workers=10) ) mcp_pb2_grpc.add_MCPServicer_to_server( MCPServicer(self.mcp_server), self.server ) self.server.add_insecure_port(f'[::]:{self.port}') await self.server.start() print(f"gRPC 服务器启动在端口 {self.port}") async def stop_server(self): """停止 gRPC 服务器""" if self.server: await self.server.stop(0) print("gRPC 服务器已停止")最佳实践:异步处理:使用异步编程避免阻塞错误处理:正确处理集成过程中的错误性能优化:缓存频繁调用的结果日志记录:记录所有集成操作测试覆盖:编写集成测试确保功能正常文档完善:提供清晰的集成文档通过与其他 AI 框架和工具的集成,可以扩展 MCP 的功能和应用场景。
阅读 0·2月19日 21:40

MCP 的安全性设计有哪些关键机制?

MCP 的安全性设计包含多个层面,确保 AI 模型与外部系统的交互是安全可控的:1. 认证和授权机制身份认证:支持多种认证方式(API Key、OAuth、JWT 等)访问控制:基于角色的权限管理(RBAC)令牌管理:安全的令牌生成、验证和刷新机制多租户支持:隔离不同用户或租户的数据和资源2. 通信安全加密传输:强制使用 TLS/SSL 加密所有通信证书验证:严格的证书验证和吊销检查安全协议:基于 JSON-RPC 2.0 的安全扩展防止中间人攻击:完整的证书链验证3. 输入验证和清理参数验证:严格验证所有输入参数的类型和格式SQL 注入防护:使用参数化查询,防止 SQL 注入XSS 防护:清理和转义用户输入,防止跨站脚本攻击命令注入防护:限制和验证系统命令执行4. 资源访问控制文件系统隔离:限制可访问的文件路径和权限网络访问限制:白名单机制控制外部网络访问资源配额:限制 CPU、内存、磁盘等资源使用操作审计:记录所有资源访问和修改操作5. 执行环境安全沙箱隔离:在隔离的沙箱环境中执行代码权限最小化:只授予必要的最小权限超时控制:设置执行超时,防止无限循环资源限制:限制内存、CPU 等资源使用6. 错误处理和日志安全错误消息:不泄露敏感信息的错误提示详细日志记录:记录所有操作和安全事件审计追踪:完整的操作审计链异常监控:实时监控异常行为7. 数据保护数据加密:敏感数据加密存储和传输数据脱敏:日志和错误消息中的敏感数据脱敏数据隔离:不同用户数据的严格隔离数据备份:安全的数据备份和恢复机制8. 速率限制和防护请求限流:防止 API 滥用和 DDoS 攻击并发控制:限制并发请求数量黑名单机制:阻止恶意 IP 或用户异常检测:检测和阻止异常行为模式安全最佳实践:定期进行安全审计和渗透测试及时更新依赖库和框架实施最小权限原则建立安全事件响应流程提供安全配置指南和文档通过这些多层安全机制,MCP 能够在提供强大功能的同时,确保系统的安全性和可靠性。
阅读 0·2月19日 21:40

如何对 MCP 进行测试?有哪些测试策略和最佳实践?

MCP 的测试策略对于确保系统质量和可靠性至关重要。以下是详细的测试方法和最佳实践:测试层次结构MCP 测试应涵盖以下层次:单元测试:测试单个函数和组件集成测试:测试组件之间的交互端到端测试:测试完整的用户场景性能测试:测试系统性能和可扩展性安全测试:测试安全漏洞和防护机制1. 单元测试import pytestfrom unittest.mock import Mock, AsyncMockfrom mcp.server import Serverclass TestMCPTools: @pytest.fixture def server(self): """创建测试服务器实例""" return Server("test-server") @pytest.mark.asyncio async def test_tool_registration(self, server): """测试工具注册""" @server.tool( name="test_tool", description="测试工具" ) async def test_tool(param: str) -> str: return f"Result: {param}" # 验证工具已注册 tools = await server.list_tools() assert any(t["name"] == "test_tool" for t in tools) @pytest.mark.asyncio async def test_tool_execution(self, server): """测试工具执行""" @server.tool( name="calculate", description="计算工具" ) async def calculate(a: int, b: int) -> int: return a + b # 执行工具 result = await server.call_tool("calculate", {"a": 2, "b": 3}) assert result == 5 @pytest.mark.asyncio async def test_parameter_validation(self, server): """测试参数验证""" @server.tool( name="validate", description="参数验证工具", inputSchema={ "type": "object", "properties": { "email": { "type": "string", "format": "email" } }, "required": ["email"] } ) async def validate(email: str) -> str: return f"Valid: {email}" # 测试有效参数 result = await server.call_tool( "validate", {"email": "test@example.com"} ) assert "Valid" in result # 测试无效参数 with pytest.raises(ValueError): await server.call_tool("validate", {"email": "invalid"})2. 集成测试class TestMCPIntegration: @pytest.mark.asyncio async def test_client_server_communication(self): """测试客户端-服务器通信""" from mcp.client import Client from mcp.server import Server # 创建服务器 server = Server("integration-test-server") @server.tool(name="echo", description="回显工具") async def echo(message: str) -> str: return message # 启动服务器 await server.start() try: # 创建客户端 client = Client("http://localhost:8000") # 测试通信 result = await client.call_tool("echo", {"message": "Hello"}) assert result == "Hello" finally: await server.stop() @pytest.mark.asyncio async def test_resource_access(self): """测试资源访问""" server = Server("resource-test-server") @server.resource( uri="file:///test.txt", name="测试文件", description="测试资源" ) async def test_resource() -> str: return "Test content" await server.start() try: client = Client("http://localhost:8000") # 读取资源 content = await client.read_resource("file:///test.txt") assert content == "Test content" finally: await server.stop()3. 端到端测试class TestMCPEndToEnd: @pytest.mark.asyncio async def test_complete_workflow(self): """测试完整工作流""" # 模拟用户场景:查询数据库并生成报告 server = Server("e2e-test-server") @server.tool(name="query_db", description="查询数据库") async def query_db(query: str) -> list: return [{"id": 1, "name": "Test"}] @server.tool(name="generate_report", description="生成报告") async def generate_report(data: list) -> str: return f"Report: {len(data)} items" await server.start() try: client = Client("http://localhost:8000") # 执行工作流 data = await client.call_tool("query_db", {"query": "SELECT *"}) report = await client.call_tool("generate_report", {"data": data}) assert "1 items" in report finally: await server.stop()4. 性能测试import asyncioimport timefrom locust import HttpUser, task, betweenclass MCPPerformanceTest(HttpUser): wait_time = between(1, 3) def on_start(self): """测试开始时的初始化""" self.client = Client(self.host) @task def tool_call_performance(self): """测试工具调用性能""" start_time = time.time() result = self.client.call_tool("test_tool", {"param": "value"}) elapsed = time.time() - start_time # 断言响应时间 assert elapsed < 1.0, f"响应时间过长: {elapsed}s" @task def concurrent_requests(self): """测试并发请求""" async def make_request(): return self.client.call_tool("test_tool", {"param": "value"}) # 并发执行 10 个请求 tasks = [make_request() for _ in range(10)] results = asyncio.run(asyncio.gather(*tasks)) # 验证所有请求都成功 assert all(results)5. 安全测试class TestMCPSecurity: @pytest.mark.asyncio async def test_authentication(self): """测试认证机制""" server = Server("security-test-server") # 配置认证 server.set_authenticator(lambda token: token == "valid-token") @server.tool(name="secure_tool", description="安全工具") async def secure_tool() -> str: return "Secure data" await server.start() try: # 测试有效令牌 client = Client("http://localhost:8000", token="valid-token") result = await client.call_tool("secure_tool", {}) assert result == "Secure data" # 测试无效令牌 client_invalid = Client("http://localhost:8000", token="invalid-token") with pytest.raises(AuthenticationError): await client_invalid.call_tool("secure_tool", {}) finally: await server.stop() @pytest.mark.asyncio async def test_sql_injection_prevention(self): """测试 SQL 注入防护""" server = Server("sql-injection-test-server") @server.tool(name="query", description="查询工具") async def query(sql: str) -> list: # 应该使用参数化查询 return execute_safe_query(sql) # 测试 SQL 注入尝试 malicious_sql = "SELECT * FROM users WHERE '1'='1'" result = await server.call_tool("query", {"sql": malicious_sql}) # 验证注入被阻止 assert result == [] @pytest.mark.asyncio async def test_rate_limiting(self): """测试速率限制""" server = Server("rate-limit-test-server") # 配置速率限制 server.set_rate_limit(max_requests=10, window=60) @server.tool(name="limited_tool", description="受限工具") async def limited_tool() -> str: return "Success" # 快速发送多个请求 for i in range(15): try: await server.call_tool("limited_tool", {}) except RateLimitError: # 预期的速率限制错误 assert i >= 10 break else: pytest.fail("未触发速率限制")6. Mock 和 Stubfrom unittest.mock import Mock, patchclass TestMCPWithMocks: @pytest.mark.asyncio async def test_with_external_dependency_mock(self): """使用 Mock 测试外部依赖""" server = Server("mock-test-server") @server.tool(name="fetch_data", description="获取数据") async def fetch_data(url: str) -> dict: # Mock 外部 API 调用 with patch('requests.get') as mock_get: mock_get.return_value.json.return_value = { "data": "mocked" } response = requests.get(url) return response.json() result = await server.call_tool( "fetch_data", {"url": "http://api.example.com"} ) assert result == {"data": "mocked"} mock_get.assert_called_once()7. 测试覆盖率# 使用 pytest-cov 生成覆盖率报告# 运行命令: pytest --cov=mcp --cov-report=htmlclass TestCoverage: @pytest.mark.asyncio async def test_all_code_paths(self): """测试所有代码路径""" server = Server("coverage-test-server") @server.tool(name="complex_tool", description="复杂工具") async def complex_tool(condition: bool) -> str: if condition: return "Branch A" else: return "Branch B" # 测试所有分支 result_a = await server.call_tool("complex_tool", {"condition": True}) assert result_a == "Branch A" result_b = await server.call_tool("complex_tool", {"condition": False}) assert result_b == "Branch B"最佳实践:测试金字塔:大量单元测试,适量集成测试,少量端到端测试独立性:每个测试应该独立运行,不依赖其他测试可重复性:测试结果应该可重复,不受环境因素影响快速反馈:单元测试应该快速执行,提供快速反馈持续集成:将测试集成到 CI/CD 流程中覆盖率目标:设定合理的代码覆盖率目标(如 80%)通过完善的测试策略,可以确保 MCP 系统的质量和可靠性。
阅读 0·2月19日 21:40

MCP 的插件系统是如何工作的?

MCP 的插件系统允许开发者扩展 MCP 服务器的功能,无需修改核心代码。以下是详细的插件架构和实现方法:插件架构设计MCP 插件系统应考虑以下方面:插件发现:自动发现和加载插件插件生命周期:管理插件的加载、初始化、卸载插件隔离:确保插件之间相互隔离插件通信:提供插件间的通信机制插件安全:限制插件的权限和资源访问1. 插件接口定义from abc import ABC, abstractmethodfrom typing import Dict, Any, Listclass MCPPlugin(ABC): """MCP 插件基类""" def __init__(self, config: Dict[str, Any] = None): self.config = config or {} self.name = self.__class__.__name__ self.version = getattr(self, 'VERSION', '1.0.0') @abstractmethod async def initialize(self, server): """初始化插件""" pass @abstractmethod async def shutdown(self): """关闭插件""" pass @abstractmethod def get_tools(self) -> List[Dict[str, Any]]: """获取插件提供的工具""" return [] @abstractmethod def get_resources(self) -> List[Dict[str, Any]]: """获取插件提供的资源""" return [] @abstractmethod def get_prompts(self) -> List[Dict[str, Any]]: """获取插件提供的提示词""" return [] def get_metadata(self) -> Dict[str, Any]: """获取插件元数据""" return { "name": self.name, "version": self.version, "description": getattr(self, 'DESCRIPTION', ''), "author": getattr(self, 'AUTHOR', ''), "dependencies": getattr(self, 'DEPENDENCIES', []) }2. 插件管理器import importlibimport inspectimport osfrom pathlib import Pathfrom typing import Dict, List, Typeclass PluginManager: def __init__(self, server): self.server = server self.plugins: Dict[str, MCPPlugin] = {} self.plugin_directories: List[str] = [] def add_plugin_directory(self, directory: str): """添加插件目录""" if directory not in self.plugin_directories: self.plugin_directories.append(directory) async def discover_plugins(self): """发现插件""" discovered_plugins = [] for directory in self.plugin_directories: plugin_path = Path(directory) if not plugin_path.exists(): continue # 遍历插件目录 for item in plugin_path.iterdir(): if item.is_file() and item.suffix == '.py': # 单文件插件 discovered_plugins.append(str(item)) elif item.is_dir() and (item / '__init__.py').exists(): # 包插件 discovered_plugins.append(str(item)) return discovered_plugins async def load_plugin(self, plugin_path: str) -> bool: """加载插件""" try: # 动态导入插件模块 spec = importlib.util.spec_from_file_location( "plugin_module", plugin_path ) if spec is None or spec.loader is None: return False module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) # 查找插件类 plugin_class = None for name, obj in inspect.getmembers(module): if (inspect.isclass(obj) and issubclass(obj, MCPPlugin) and obj is not MCPPlugin): plugin_class = obj break if plugin_class is None: return False # 创建插件实例 plugin = plugin_class() # 初始化插件 await plugin.initialize(self.server) # 注册插件 self.plugins[plugin.name] = plugin # 注册插件提供的工具、资源、提示词 self._register_plugin_tools(plugin) self._register_plugin_resources(plugin) self._register_plugin_prompts(plugin) return True except Exception as e: print(f"加载插件失败 {plugin_path}: {e}") return False def _register_plugin_tools(self, plugin: MCPPlugin): """注册插件工具""" tools = plugin.get_tools() for tool_info in tools: @self.server.tool( name=tool_info["name"], description=tool_info["description"] ) async def tool_wrapper(**kwargs): return await tool_info["function"](**kwargs) def _register_plugin_resources(self, plugin: MCPPlugin): """注册插件资源""" resources = plugin.get_resources() for resource_info in resources: @self.server.resource( uri=resource_info["uri"], name=resource_info["name"], description=resource_info["description"] ) async def resource_wrapper(): return await resource_info["function"]() def _register_plugin_prompts(self, plugin: MCPPlugin): """注册插件提示词""" prompts = plugin.get_prompts() for prompt_info in prompts: @self.server.prompt( name=prompt_info["name"], description=prompt_info["description"] ) async def prompt_wrapper(**kwargs): return await prompt_info["function"](**kwargs) async def unload_plugin(self, plugin_name: str) -> bool: """卸载插件""" if plugin_name not in self.plugins: return False plugin = self.plugins[plugin_name] try: # 关闭插件 await plugin.shutdown() # 从插件列表中移除 del self.plugins[plugin_name] return True except Exception as e: print(f"卸载插件失败 {plugin_name}: {e}") return False async def reload_plugin(self, plugin_name: str) -> bool: """重新加载插件""" if plugin_name not in self.plugins: return False # 先卸载 await self.unload_plugin(plugin_name) # 重新加载(需要记录插件路径) # 这里简化处理,实际需要保存插件路径 return True def get_plugin_info(self, plugin_name: str) -> Dict[str, Any]: """获取插件信息""" if plugin_name not in self.plugins: return {} plugin = self.plugins[plugin_name] return plugin.get_metadata() def list_plugins(self) -> List[Dict[str, Any]]: """列出所有插件""" return [ plugin.get_metadata() for plugin in self.plugins.values() ]3. 示例插件实现# plugins/database_plugin.pyclass DatabasePlugin(MCPPlugin): """数据库插件""" VERSION = "1.0.0" DESCRIPTION = "提供数据库查询和管理功能" AUTHOR = "Your Name" DEPENDENCIES = ["sqlalchemy"] def __init__(self, config: Dict[str, Any] = None): super().__init__(config) self.db_connection = None async def initialize(self, server): """初始化数据库连接""" from sqlalchemy import create_engine db_url = self.config.get("database_url", "sqlite:///mcp.db") self.db_connection = create_engine(db_url) print(f"数据库插件 {self.name} 已初始化") async def shutdown(self): """关闭数据库连接""" if self.db_connection: self.db_connection.dispose() print(f"数据库插件 {self.name} 已关闭") def get_tools(self) -> List[Dict[str, Any]]: """获取数据库工具""" return [ { "name": "query_database", "description": "执行数据库查询", "function": self._query_database }, { "name": "execute_sql", "description": "执行 SQL 语句", "function": self._execute_sql } ] def get_resources(self) -> List[Dict[str, Any]]: """获取数据库资源""" return [ { "uri": "db://schema", "name": "数据库模式", "description": "数据库表结构", "function": self._get_schema } ] def get_prompts(self) -> List[Dict[str, Any]]: """获取数据库提示词""" return [ { "name": "generate_query", "description": "生成 SQL 查询提示词", "function": self._generate_query_prompt } ] async def _query_database(self, query: str) -> str: """执行数据库查询""" from sqlalchemy import text with self.db_connection.connect() as conn: result = conn.execute(text(query)) rows = result.fetchall() return f"查询结果: {len(rows)} 行" async def _execute_sql(self, sql: str) -> str: """执行 SQL 语句""" from sqlalchemy import text with self.db_connection.connect() as conn: conn.execute(text(sql)) conn.commit() return "SQL 执行成功" async def _get_schema(self) -> str: """获取数据库模式""" from sqlalchemy import inspect inspector = inspect(self.db_connection) tables = inspector.get_table_names() return f"数据库表: {', '.join(tables)}" async def _generate_query_prompt(self, table_name: str) -> str: """生成查询提示词""" return f""" 请为表 {table_name} 生成 SQL 查询语句。 要求: 1. 只使用 SELECT 查询 2. 包含适当的 WHERE 条件 3. 添加 LIMIT 限制结果数量 """4. 插件配置import jsonfrom pathlib import Pathclass PluginConfig: def __init__(self, config_dir: str = "config/plugins"): self.config_dir = Path(config_dir) self.config_dir.mkdir(parents=True, exist_ok=True) def load_config(self, plugin_name: str) -> Dict[str, Any]: """加载插件配置""" config_file = self.config_dir / f"{plugin_name}.json" if not config_file.exists(): return {} with open(config_file, 'r') as f: return json.load(f) def save_config( self, plugin_name: str, config: Dict[str, Any] ): """保存插件配置""" config_file = self.config_dir / f"{plugin_name}.json" with open(config_file, 'w') as f: json.dump(config, f, indent=2) def get_all_configs(self) -> Dict[str, Dict[str, Any]]: """获取所有插件配置""" configs = {} for config_file in self.config_dir.glob("*.json"): plugin_name = config_file.stem with open(config_file, 'r') as f: configs[plugin_name] = json.load(f) return configs5. 插件依赖管理from typing import Dict, List, Setclass PluginDependencyManager: def __init__(self): self.dependencies: Dict[str, Set[str]] = {} self.dependents: Dict[str, Set[str]] = {} def add_dependency(self, plugin_name: str, dependency: str): """添加依赖关系""" if plugin_name not in self.dependencies: self.dependencies[plugin_name] = set() self.dependencies[plugin_name].add(dependency) if dependency not in self.dependents: self.dependents[dependency] = set() self.dependents[dependency].add(plugin_name) def get_load_order(self, plugins: List[str]) -> List[str]: """获取插件加载顺序(拓扑排序)""" visited = set() result = [] def visit(plugin_name: str): if plugin_name in visited: return visited.add(plugin_name) # 先加载依赖 for dep in self.dependencies.get(plugin_name, []): visit(dep) result.append(plugin_name) for plugin_name in plugins: visit(plugin_name) return result def check_circular_dependency(self) -> bool: """检查循环依赖""" visited = set() recursion_stack = set() def has_cycle(plugin_name: str) -> bool: visited.add(plugin_name) recursion_stack.add(plugin_name) for dep in self.dependencies.get(plugin_name, []): if dep not in visited: if has_cycle(dep): return True elif dep in recursion_stack: return True recursion_stack.remove(plugin_name) return False for plugin_name in self.dependencies: if plugin_name not in visited: if has_cycle(plugin_name): return True return False6. 插件沙箱import sysfrom typing import Anyclass PluginSandbox: def __init__(self): self.restricted_modules = { 'os', 'sys', 'subprocess', 'socket', 'pickle', 'shelve', 'marshal' } def create_sandbox(self, plugin_name: str) -> dict: """创建插件沙箱环境""" safe_globals = { '__builtins__': self._create_safe_builtins(), '__name__': f'plugin_{plugin_name}', } return safe_globals def _create_safe_builtins(self) -> dict: """创建安全的内置函数""" safe_builtins = {} # 允许的安全内置函数 allowed_builtins = [ 'abs', 'all', 'any', 'bool', 'dict', 'enumerate', 'filter', 'float', 'int', 'len', 'list', 'map', 'max', 'min', 'range', 'set', 'sorted', 'str', 'sum', 'tuple', 'type', 'zip' ] for name in allowed_builtins: if hasattr(__builtins__, name): safe_builtins[name] = getattr(__builtins__, name) return safe_builtins def execute_in_sandbox( self, code: str, sandbox_env: dict ) -> Any: """在沙箱中执行代码""" try: exec(code, sandbox_env) return True except Exception as e: print(f"沙箱执行失败: {e}") return False最佳实践:插件隔离:确保插件之间相互隔离,避免冲突依赖管理:正确处理插件依赖关系错误处理:插件错误不应影响核心系统版本兼容:支持插件版本管理和兼容性检查安全限制:限制插件的权限和资源访问文档完善:为每个插件提供清晰的文档通过完善的插件系统,可以灵活扩展 MCP 服务器的功能,满足各种定制化需求。
阅读 0·2月19日 21:39

如何在 MCP 中管理和使用提示词(Prompts)?

MCP 的提示词(Prompts)管理功能允许预定义和管理可重复使用的提示词模板,提高开发效率和一致性。以下是详细的实现方法:提示词定义MCP 提示词包含名称、描述和参数化模板:{ "name": "prompt_name", "description": "提示词描述", "arguments": [ { "name": "param1", "description": "参数1的描述", "required": true } ]}1. 提示词注册from mcp.server import Serverserver = Server("my-mcp-server")@server.prompt( name="code_review", description="代码审查提示词模板")async def code_review_prompt( code: str, language: str = "python") -> str: """生成代码审查提示词""" return f""" 请审查以下 {language} 代码,并提供改进建议: 代码: {code} 请关注以下方面: 1. 代码质量和可读性 2. 性能优化建议 3. 安全性问题 4. 最佳实践 5. 潜在的 bug """@server.prompt( name="data_analysis", description="数据分析提示词模板")async def data_analysis_prompt( data: str, analysis_type: str = "summary") -> str: """生成数据分析提示词""" return f""" 请对以下数据进行 {analysis_type} 分析: 数据: {data} 请提供: 1. 数据摘要 2. 关键洞察 3. 趋势分析 4. 可视化建议 """2. 提示词模板引擎from string import Templateimport jsonfrom typing import Dict, Anyclass PromptTemplateEngine: def __init__(self): self.templates = {} def register_template(self, name: str, template: str, parameters: list): """注册提示词模板""" self.templates[name] = { "template": template, "parameters": parameters } def render(self, name: str, **kwargs) -> str: """渲染提示词模板""" if name not in self.templates: raise ValueError(f"模板 '{name}' 不存在") template_info = self.templates[name] template = template_info["template"] # 验证必需参数 required_params = [ p["name"] for p in template_info["parameters"] if p.get("required", False) ] missing_params = [ param for param in required_params if param not in kwargs ] if missing_params: raise ValueError( f"缺少必需参数: {', '.join(missing_params)}" ) # 使用 Template 渲染 t = Template(template) return t.substitute(**kwargs) def list_templates(self) -> list: """列出所有模板""" return [ { "name": name, "description": info.get("description", ""), "parameters": info["parameters"] } for name, info in self.templates.items() ]3. 提示词版本管理from datetime import datetimefrom typing import Optionalclass PromptVersionManager: def __init__(self): self.versions = {} self.current_versions = {} def save_version( self, prompt_name: str, version: str, template: str, metadata: dict = None ): """保存提示词版本""" if prompt_name not in self.versions: self.versions[prompt_name] = {} self.versions[prompt_name][version] = { "template": template, "metadata": metadata or {}, "created_at": datetime.now().isoformat() } def set_current_version(self, prompt_name: str, version: str): """设置当前版本""" if prompt_name not in self.versions or \ version not in self.versions[prompt_name]: raise ValueError(f"版本 '{version}' 不存在") self.current_versions[prompt_name] = version def get_current_version(self, prompt_name: str) -> Optional[str]: """获取当前版本""" return self.current_versions.get(prompt_name) def get_version( self, prompt_name: str, version: str ) -> Optional[dict]: """获取指定版本""" if prompt_name not in self.versions: return None return self.versions[prompt_name].get(version) def list_versions(self, prompt_name: str) -> list: """列出所有版本""" if prompt_name not in self.versions: return [] return list(self.versions[prompt_name].keys())4. 提示词缓存from functools import lru_cacheimport hashlibclass PromptCache: def __init__(self, max_size: int = 1000): self.cache = {} self.max_size = max_size def _generate_key( self, prompt_name: str, params: dict ) -> str: """生成缓存键""" param_str = json.dumps(params, sort_keys=True) combined = f"{prompt_name}:{param_str}" return hashlib.md5(combined.encode()).hexdigest() def get(self, prompt_name: str, params: dict) -> Optional[str]: """获取缓存的提示词""" key = self._generate_key(prompt_name, params) return self.cache.get(key) def set(self, prompt_name: str, params: dict, prompt: str): """设置缓存""" # 如果缓存已满,删除最旧的条目 if len(self.cache) >= self.max_size: oldest_key = next(iter(self.cache)) del self.cache[oldest_key] key = self._generate_key(prompt_name, params) self.cache[key] = prompt def clear(self): """清空缓存""" self.cache.clear() def invalidate(self, prompt_name: str): """使指定提示词的缓存失效""" keys_to_remove = [ key for key in self.cache if key.startswith(hashlib.md5(prompt_name.encode()).hexdigest()[:16]) ] for key in keys_to_remove: del self.cache[key]5. 提示词测试和验证class PromptTester: def __init__(self, template_engine: PromptTemplateEngine): self.engine = template_engine def test_template( self, template_name: str, test_cases: list ) -> dict: """测试提示词模板""" results = { "template": template_name, "total_tests": len(test_cases), "passed": 0, "failed": 0, "errors": [] } for i, test_case in enumerate(test_cases): try: # 渲染提示词 prompt = self.engine.render( template_name, **test_case["params"] ) # 验证输出 if "expected_contains" in test_case: for expected in test_case["expected_contains"]: if expected not in prompt: results["errors"].append({ "test": i, "error": f"期望包含 '{expected}' 但未找到" }) results["failed"] += 1 break else: results["passed"] += 1 else: results["passed"] += 1 except Exception as e: results["errors"].append({ "test": i, "error": str(e) }) results["failed"] += 1 return results def validate_parameters( self, template_name: str, params: dict ) -> tuple[bool, str]: """验证参数""" try: # 尝试渲染提示词 self.engine.render(template_name, **params) return True, "" except Exception as e: return False, str(e)6. 提示词分析和优化import refrom collections import Counterclass PromptAnalyzer: def analyze(self, prompt: str) -> dict: """分析提示词""" return { "length": len(prompt), "word_count": len(prompt.split()), "sentence_count": len(re.split(r'[.!?]+', prompt)), "token_estimate": self._estimate_tokens(prompt), "complexity": self._calculate_complexity(prompt), "variables": self._extract_variables(prompt) } def _estimate_tokens(self, text: str) -> int: """估算 token 数量""" # 粗略估算:英文约 4 字符/token,中文约 1.5 字符/token chinese_chars = len(re.findall(r'[\u4e00-\u9fff]', text)) english_chars = len(re.findall(r'[a-zA-Z]', text)) return int(chinese_chars / 1.5 + english_chars / 4) def _calculate_complexity(self, prompt: str) -> float: """计算复杂度""" words = prompt.split() unique_words = set(words) # 词汇多样性 diversity = len(unique_words) / len(words) if words else 0 # 平均句子长度 sentences = re.split(r'[.!?]+', prompt) avg_sentence_length = sum(len(s.split()) for s in sentences) / len(sentences) if sentences else 0 return (diversity + min(avg_sentence_length / 20, 1)) / 2 def _extract_variables(self, prompt: str) -> list: """提取变量""" return re.findall(r'\{(\w+)\}', prompt)最佳实践:参数化设计:使用参数化模板提高复用性版本控制:对重要提示词实施版本管理缓存策略:缓存渲染结果提高性能测试覆盖:编写测试用例确保提示词质量性能监控:监控提示词的 token 使用和性能文档完善:为每个提示词提供清晰的文档通过完善的提示词管理机制,可以提高 MCP 系统的开发效率和一致性。
阅读 0·2月19日 21:38

MCP 如何支持多租户架构?

MCP 的多租户支持对于企业级应用至关重要,它允许在单一 MCP 服务器实例中为多个客户或组织提供隔离的服务。以下是详细的实现方法:多租户架构设计MCP 多租户应考虑以下方面:数据隔离:确保不同租户的数据完全隔离资源隔离:隔离计算资源和配额安全隔离:实现租户级别的认证和授权性能隔离:防止单个租户影响其他租户1. 租户识别和上下文from typing import Optionalfrom dataclasses import dataclass@dataclassclass TenantContext: """租户上下文""" tenant_id: str tenant_name: str user_id: str permissions: list quotas: dictclass TenantContextManager: def __init__(self): self.contexts = {} def create_context( self, tenant_id: str, tenant_name: str, user_id: str, permissions: list, quotas: dict = None ) -> TenantContext: """创建租户上下文""" context = TenantContext( tenant_id=tenant_id, tenant_name=tenant_name, user_id=user_id, permissions=permissions, quotas=quotas or self._get_default_quotas(tenant_id) ) self.contexts[tenant_id] = context return context def get_context(self, tenant_id: str) -> Optional[TenantContext]: """获取租户上下文""" return self.contexts.get(tenant_id) def set_current_context(self, tenant_id: str): """设置当前租户上下文""" context = self.get_context(tenant_id) if not context: raise ValueError(f"租户 {tenant_id} 不存在") # 使用线程本地存储或异步上下文变量 import contextvars current_tenant.set(context) def _get_default_quotas(self, tenant_id: str) -> dict: """获取默认配额""" return { "max_tools": 100, "max_resources": 1000, "max_requests_per_minute": 1000, "max_storage_mb": 1024 }# 当前租户上下文变量current_tenant = contextvars.ContextVar('current_tenant', default=None)2. 数据隔离from sqlalchemy import create_engine, Column, String, Integer, Textfrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy.orm import sessionmaker, scoped_sessionBase = declarative_base()class TenantData(Base): """租户数据表""" __tablename__ = 'tenant_data' id = Column(Integer, primary_key=True) tenant_id = Column(String(50), nullable=False, index=True) data_key = Column(String(100), nullable=False) data_value = Column(Text) __table_args__ = ( # 确保租户隔离 Index('idx_tenant_key', 'tenant_id', 'data_key', unique=True), )class MultiTenantDatabase: def __init__(self, database_url: str): self.engine = create_engine(database_url) Base.metadata.create_all(self.engine) self.SessionLocal = scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=self.engine) ) def get_session(self, tenant_id: str): """获取租户专属的数据库会话""" session = self.SessionLocal() # 添加租户过滤器 from sqlalchemy import event @event.listens_for(session, 'before_flush') def add_tenant_filter(session, context, instances): for instance in session.new: if hasattr(instance, 'tenant_id'): instance.tenant_id = tenant_id return session def query_tenant_data( self, tenant_id: str, data_key: str ) -> Optional[str]: """查询租户数据""" session = self.get_session(tenant_id) try: result = session.query(TenantData).filter( TenantData.tenant_id == tenant_id, TenantData.data_key == data_key ).first() return result.data_value if result else None finally: session.close() def save_tenant_data( self, tenant_id: str, data_key: str, data_value: str ): """保存租户数据""" session = self.get_session(tenant_id) try: existing = session.query(TenantData).filter( TenantData.tenant_id == tenant_id, TenantData.data_key == data_key ).first() if existing: existing.data_value = data_value else: new_data = TenantData( tenant_id=tenant_id, data_key=data_key, data_value=data_value ) session.add(new_data) session.commit() except Exception as e: session.rollback() raise e finally: session.close()3. 资源配额管理from collections import defaultdictimport timeclass QuotaManager: def __init__(self): self.quotas = {} self.usage = defaultdict(lambda: defaultdict(int)) self.rate_limits = {} def set_quota( self, tenant_id: str, quota_type: str, limit: int ): """设置租户配额""" if tenant_id not in self.quotas: self.quotas[tenant_id] = {} self.quotas[tenant_id][quota_type] = limit def check_quota( self, tenant_id: str, quota_type: str, amount: int = 1 ) -> bool: """检查配额是否足够""" if tenant_id not in self.quotas: return True limit = self.quotas[tenant_id].get(quota_type) if limit is None: return True current_usage = self.usage[tenant_id][quota_type] return current_usage + amount <= limit def consume_quota( self, tenant_id: str, quota_type: str, amount: int = 1 ) -> bool: """消耗配额""" if not self.check_quota(tenant_id, quota_type, amount): return False self.usage[tenant_id][quota_type] += amount return True def get_usage( self, tenant_id: str, quota_type: str ) -> int: """获取使用量""" return self.usage[tenant_id][quota_type] def reset_usage(self, tenant_id: str): """重置使用量""" if tenant_id in self.usage: self.usage[tenant_id].clear() def check_rate_limit( self, tenant_id: str, window: int = 60, max_requests: int = 100 ) -> bool: """检查速率限制""" now = time.time() if tenant_id not in self.rate_limits: self.rate_limits[tenant_id] = [] # 清理过期的请求记录 self.rate_limits[tenant_id] = [ timestamp for timestamp in self.rate_limits[tenant_id] if now - timestamp < window ] # 检查是否超过限制 if len(self.rate_limits[tenant_id]) >= max_requests: return False # 记录新请求 self.rate_limits[tenant_id].append(now) return True4. 租户级别的工具和资源from mcp.server import Serverfrom functools import wrapsclass MultiTenantServer(Server): def __init__(self, name: str, tenant_manager: TenantContextManager): super().__init__(name) self.tenant_manager = tenant_manager self.tenant_tools = defaultdict(dict) self.tenant_resources = defaultdict(dict) def tenant_tool( self, name: str, description: str, tenant_id: str = None ): """租户专属工具装饰器""" def decorator(func): # 注册工具 self.tenant_tools[tenant_id or "default"][name] = { "function": func, "description": description } @wraps(func) async def wrapper(*args, **kwargs): # 获取当前租户 context = current_tenant.get() if not context: raise PermissionError("未找到租户上下文") # 检查租户权限 if tenant_id and context.tenant_id != tenant_id: raise PermissionError("无权访问此工具") # 执行工具 return await func(*args, **kwargs) return wrapper return decorator def tenant_resource( self, uri: str, name: str, description: str, tenant_id: str = None ): """租户专属资源装饰器""" def decorator(func): # 注册资源 self.tenant_resources[tenant_id or "default"][uri] = { "function": func, "name": name, "description": description } @wraps(func) async def wrapper(*args, **kwargs): # 获取当前租户 context = current_tenant.get() if not context: raise PermissionError("未找到租户上下文") # 检查租户权限 if tenant_id and context.tenant_id != tenant_id: raise PermissionError("无权访问此资源") # 执行资源 return await func(*args, **kwargs) return wrapper return decorator async def list_tools(self, tenant_id: str = None) -> list: """列出可用工具""" context = current_tenant.get() if not context: return [] # 获取默认工具和租户专属工具 tools = [] # 添加默认工具 for name, tool_info in self.tenant_tools["default"].items(): tools.append({ "name": name, "description": tool_info["description"] }) # 添加租户专属工具 if context.tenant_id in self.tenant_tools: for name, tool_info in self.tenant_tools[context.tenant_id].items(): tools.append({ "name": name, "description": tool_info["description"] }) return tools5. 租户认证和授权import jwtfrom datetime import datetime, timedeltafrom typing import Dict, Anyclass TenantAuthenticator: def __init__(self, secret_key: str): self.secret_key = secret_key def generate_token( self, tenant_id: str, user_id: str, permissions: list, expires_in: int = 3600 ) -> str: """生成租户令牌""" payload = { "tenant_id": tenant_id, "user_id": user_id, "permissions": permissions, "exp": datetime.utcnow() + timedelta(seconds=expires_in), "iat": datetime.utcnow() } token = jwt.encode(payload, self.secret_key, algorithm="HS256") return token def verify_token(self, token: str) -> Dict[str, Any]: """验证租户令牌""" try: payload = jwt.decode(token, self.secret_key, algorithms=["HS256"]) return payload except jwt.ExpiredSignatureError: raise ValueError("令牌已过期") except jwt.InvalidTokenError: raise ValueError("无效的令牌") def check_permission( self, token: str, required_permission: str ) -> bool: """检查权限""" payload = self.verify_token(token) permissions = payload.get("permissions", []) return required_permission in permissions or "admin" in permissions6. 租户监控和报告from collections import defaultdictfrom datetime import datetime, timedeltaclass TenantMonitor: def __init__(self): self.metrics = defaultdict(lambda: defaultdict(list)) def record_metric( self, tenant_id: str, metric_name: str, value: float ): """记录指标""" timestamp = datetime.now() self.metrics[tenant_id][metric_name].append({ "value": value, "timestamp": timestamp }) # 限制历史记录大小 if len(self.metrics[tenant_id][metric_name]) > 1000: self.metrics[tenant_id][metric_name] = \ self.metrics[tenant_id][metric_name][-1000:] def get_metrics( self, tenant_id: str, metric_name: str, since: datetime = None ) -> list: """获取指标""" if tenant_id not in self.metrics: return [] if metric_name not in self.metrics[tenant_id]: return [] records = self.metrics[tenant_id][metric_name] if since: records = [ record for record in records if record["timestamp"] >= since ] return records def get_aggregated_metrics( self, tenant_id: str, metric_name: str, since: datetime = None ) -> dict: """获取聚合指标""" records = self.get_metrics(tenant_id, metric_name, since) if not records: return {} values = [record["value"] for record in records] return { "count": len(values), "sum": sum(values), "avg": sum(values) / len(values), "min": min(values), "max": max(values) } def generate_tenant_report( self, tenant_id: str, since: datetime = None ) -> dict: """生成租户报告""" if not since: since = datetime.now() - timedelta(days=7) report = { "tenant_id": tenant_id, "period": { "start": since, "end": datetime.now() }, "metrics": {} } if tenant_id in self.metrics: for metric_name in self.metrics[tenant_id]: report["metrics"][metric_name] = \ self.get_aggregated_metrics(tenant_id, metric_name, since) return report最佳实践:数据隔离:使用租户 ID 作为所有数据表的主键或索引配额管理:为每个租户设置合理的资源配额权限控制:实施细粒度的租户级权限控制性能监控:监控每个租户的资源使用情况安全审计:记录所有租户操作用于审计弹性扩展:根据租户需求动态扩展资源通过完善的多租户支持,可以在单一 MCP 服务器实例中为多个客户或组织提供隔离、安全、高效的服务。
阅读 0·2月19日 21:38

MCP 的消息格式是怎样的?有哪些常用的消息类型?

MCP 的消息格式基于 JSON-RPC 2.0 协议,并进行了扩展以支持 AI 模型与外部系统的交互。以下是详细的消息格式说明:基础消息结构所有 MCP 消息都遵循 JSON-RPC 2.0 的基本格式:{ "jsonrpc": "2.0", "id": "unique-request-id", "method": "method-name", "params": { ... }}1. 请求消息(Request)用于客户端向服务器发送工具调用请求:{ "jsonrpc": "2.0", "id": "req-123", "method": "tools/call", "params": { "name": "calculate", "arguments": { "expression": "2 + 2" } }}2. 响应消息(Response)服务器返回执行结果:{ "jsonrpc": "2.0", "id": "req-123", "result": { "content": [ { "type": "text", "text": "结果: 4" } ] }}3. 错误响应(Error Response)当请求失败时返回:{ "jsonrpc": "2.0", "id": "req-123", "error": { "code": -32602, "message": "Invalid params", "data": { "details": "参数 'expression' 不能为空" } }}4. 通知消息(Notification)服务器主动推送的消息(无需响应):{ "jsonrpc": "2.0", "method": "notifications/progress", "params": { "progress": 0.5, "message": "处理中..." }}常用方法类型tools/list - 获取可用工具列表{ "jsonrpc": "2.0", "id": "req-001", "method": "tools/list"}resources/list - 获取可用资源列表{ "jsonrpc": "2.0", "id": "req-002", "method": "resources/list"}resources/read - 读取资源内容{ "jsonrpc": "2.0", "id": "req-003", "method": "resources/read", "params": { "uri": "file:///data/config.json" }}prompts/list - 获取提示词列表{ "jsonrpc": "2.0", "id": "req-004", "method": "prompts/list"}错误代码MCP 定义了标准的错误代码:| 代码 | 名称 | 描述 ||------|------|------|| -32700 | Parse error | JSON 解析错误 || -32600 | Invalid Request | 无效的请求 || -32601 | Method not found | 方法不存在 || -32602 | Invalid params | 无效的参数 || -32603 | Internal error | 内部错误 || -32000 | Server error | 服务器错误 |内容类型(Content Types)MCP 支持多种内容类型:{ "type": "text", "text": "纯文本内容"}{ "type": "image", "data": "base64-encoded-image-data", "mimeType": "image/png"}{ "type": "resource", "uri": "file:///data/report.pdf", "mimeType": "application/pdf"}消息流(Message Streaming)对于长时间运行的操作,支持流式响应:{ "jsonrpc": "2.0", "id": "req-005", "method": "tools/call", "params": { "name": "generate_report", "arguments": { "stream": true } }}最佳实践:唯一 ID:每个请求必须有唯一的 ID类型验证:严格验证参数类型和格式错误处理:提供详细的错误信息和数据超时处理:实现请求超时机制日志记录:记录所有消息用于调试和审计理解 MCP 的消息格式对于实现兼容的服务器和客户端至关重要。
阅读 0·2月19日 21:35

如何部署和运维 MCP 系统?有哪些最佳实践?

MCP 的部署和运维对于生产环境的稳定运行至关重要。以下是详细的部署策略和运维最佳实践:部署架构MCP 可以采用多种部署架构:单机部署:适合开发和测试环境容器化部署:使用 Docker 容器Kubernetes 部署:适合大规模生产环境无服务器部署:使用 AWS Lambda、Azure Functions 等1. Docker 容器化部署# DockerfileFROM python:3.11-slimWORKDIR /app# 安装依赖COPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txt# 复制应用代码COPY . .# 暴露端口EXPOSE 8000# 健康检查HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD curl -f http://localhost:8000/health || exit 1# 启动应用CMD ["python", "-m", "mcp.server", "--host", "0.0.0.0", "--port", "8000"]# docker-compose.ymlversion: '3.8'services: mcp-server: build: . ports: - "8000:8000" environment: - MCP_HOST=0.0.0.0 - MCP_PORT=8000 - LOG_LEVEL=info - DATABASE_URL=postgresql://user:pass@db:5432/mcp volumes: - ./config:/app/config - ./logs:/app/logs depends_on: - db - redis restart: unless-stopped healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8000/health"] interval: 30s timeout: 10s retries: 3 db: image: postgres:15 environment: - POSTGRES_DB=mcp - POSTGRES_USER=user - POSTGRES_PASSWORD=pass volumes: - postgres_data:/var/lib/postgresql/data restart: unless-stopped redis: image: redis:7-alpine volumes: - redis_data:/data restart: unless-stoppedvolumes: postgres_data: redis_data:2. Kubernetes 部署# deployment.yamlapiVersion: apps/v1kind: Deploymentmetadata: name: mcp-server labels: app: mcp-serverspec: replicas: 3 selector: matchLabels: app: mcp-server template: metadata: labels: app: mcp-server spec: containers: - name: mcp-server image: your-registry/mcp-server:latest ports: - containerPort: 8000 env: - name: MCP_HOST value: "0.0.0.0" - name: MCP_PORT value: "8000" - name: DATABASE_URL valueFrom: secretKeyRef: name: mcp-secrets key: database-url resources: requests: memory: "256Mi" cpu: "250m" limits: memory: "512Mi" cpu: "500m" livenessProbe: httpGet: path: /health port: 8000 initialDelaySeconds: 30 periodSeconds: 10 readinessProbe: httpGet: path: /ready port: 8000 initialDelaySeconds: 5 periodSeconds: 5---apiVersion: v1kind: Servicemetadata: name: mcp-serverspec: selector: app: mcp-server ports: - protocol: TCP port: 80 targetPort: 8000 type: LoadBalancer---apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: name: mcp-server-hpaspec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: mcp-server minReplicas: 3 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 - type: Resource resource: name: memory target: type: Utilization averageUtilization: 803. CI/CD 流水线# .github/workflows/deploy.ymlname: Deploy MCP Serveron: push: branches: [main] pull_request: branches: [main]jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.11' - name: Install dependencies run: | pip install -r requirements.txt pip install pytest pytest-cov - name: Run tests run: | pytest --cov=mcp --cov-report=xml - name: Upload coverage uses: codecov/codecov-action@v3 build: needs: test runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Build Docker image run: | docker build -t mcp-server:${{ github.sha }} . - name: Push to registry run: | echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin docker tag mcp-server:${{ github.sha }} your-registry/mcp-server:latest docker push your-registry/mcp-server:latest deploy: needs: build runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - name: Deploy to Kubernetes uses: azure/k8s-deploy@v4 with: manifests: | k8s/deployment.yaml images: | your-registry/mcp-server:latest kubeconfig: ${{ secrets.KUBE_CONFIG }}4. 监控和日志# monitoring.pyfrom prometheus_client import Counter, Histogram, Gauge, start_http_serverimport loggingfrom logging.handlers import RotatingFileHandler# Prometheus 指标REQUEST_COUNT = Counter('mcp_requests_total', 'Total requests', ['method', 'endpoint'])REQUEST_DURATION = Histogram('mcp_request_duration_seconds', 'Request duration')ACTIVE_CONNECTIONS = Gauge('mcp_active_connections', 'Active connections')ERROR_COUNT = Counter('mcp_errors_total', 'Total errors', ['error_type'])# 日志配置def setup_logging(): logger = logging.getLogger('mcp') logger.setLevel(logging.INFO) # 文件处理器 file_handler = RotatingFileHandler( 'logs/mcp.log', maxBytes=10*1024*1024, # 10MB backupCount=5 ) file_handler.setFormatter( logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ) # 控制台处理器 console_handler = logging.StreamHandler() console_handler.setFormatter( logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') ) logger.addHandler(file_handler) logger.addHandler(console_handler) return logger# 启动监控服务器def start_metrics_server(port: int = 9090): start_http_server(port) logging.info(f"Metrics server started on port {port}")5. 配置管理# config.pyimport osfrom pydantic import BaseSettings, Fieldclass MCPSettings(BaseSettings): # 服务器配置 host: str = Field(default="0.0.0.0", env="MCP_HOST") port: int = Field(default=8000, env="MCP_PORT") # 数据库配置 database_url: str = Field(..., env="DATABASE_URL") database_pool_size: int = Field(default=10, env="DATABASE_POOL_SIZE") # Redis 配置 redis_url: str = Field(default="redis://localhost:6379", env="REDIS_URL") # 日志配置 log_level: str = Field(default="INFO", env="LOG_LEVEL") log_file: str = Field(default="logs/mcp.log", env="LOG_FILE") # 安全配置 secret_key: str = Field(..., env="SECRET_KEY") jwt_algorithm: str = Field(default="HS256", env="JWT_ALGORITHM") # 性能配置 max_connections: int = Field(default=100, env="MAX_CONNECTIONS") request_timeout: int = Field(default=30, env="REQUEST_TIMEOUT") # 缓存配置 cache_ttl: int = Field(default=3600, env="CACHE_TTL") class Config: env_file = ".env" case_sensitive = False# 加载配置settings = MCPSettings()6. 备份和恢复#!/bin/bash# backup.sh# 数据库备份backup_database() { echo "Backing up database..." pg_dump $DATABASE_URL > backups/db_$(date +%Y%m%d_%H%M%S).sql echo "Database backup completed"}# 配置备份backup_config() { echo "Backing up configuration..." tar -czf backups/config_$(date +%Y%m%d_%H%M%S).tar.gz config/ echo "Configuration backup completed"}# 日志备份backup_logs() { echo "Backing up logs..." tar -czf backups/logs_$(date +%Y%m%d_%H%M%S).tar.gz logs/ echo "Logs backup completed"}# 清理旧备份cleanup_old_backups() { echo "Cleaning up old backups (older than 7 days)..." find backups/ -name "*.sql" -mtime +7 -delete find backups/ -name "*.tar.gz" -mtime +7 -delete echo "Cleanup completed"}# 主函数main() { mkdir -p backups backup_database backup_config backup_logs cleanup_old_backups echo "All backups completed successfully"}main7. 故障排查# diagnostics.pyimport psutilimport asynciofrom typing import Dict, Anyclass SystemDiagnostics: @staticmethod def get_system_info() -> Dict[str, Any]: """获取系统信息""" return { "cpu_percent": psutil.cpu_percent(interval=1), "memory": { "total": psutil.virtual_memory().total, "available": psutil.virtual_memory().available, "percent": psutil.virtual_memory().percent }, "disk": { "total": psutil.disk_usage('/').total, "used": psutil.disk_usage('/').used, "percent": psutil.disk_usage('/').percent }, "network": { "connections": len(psutil.net_connections()), "io_counters": psutil.net_io_counters()._asdict() } } @staticmethod async def check_database_connection(db_url: str) -> bool: """检查数据库连接""" try: # 实现数据库连接检查 return True except Exception as e: logging.error(f"Database connection failed: {e}") return False @staticmethod async def check_redis_connection(redis_url: str) -> bool: """检查 Redis 连接""" try: # 实现 Redis 连接检查 return True except Exception as e: logging.error(f"Redis connection failed: {e}") return False @staticmethod def get_service_status() -> Dict[str, bool]: """获取服务状态""" return { "database": asyncio.run(SystemDiagnostics.check_database_connection(settings.database_url)), "redis": asyncio.run(SystemDiagnostics.check_redis_connection(settings.redis_url)), "api": True # 如果能运行到这里,API 服务是正常的 }最佳实践:容器化:使用 Docker 容器确保环境一致性自动化部署:使用 CI/CD 自动化部署流程监控告警:实施全面的监控和告警机制日志集中:集中管理日志,便于分析和排查备份策略:定期备份重要数据和配置灾难恢复:制定并测试灾难恢复计划安全加固:实施安全加固措施性能优化:持续监控和优化系统性能通过完善的部署和运维策略,可以确保 MCP 系统在生产环境中的稳定运行。
阅读 0·2月19日 21:35