hxf/backend/th_agenter/services/postgresql_tool_manager.py

51 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""PostgreSQL MCP工具全局管理器"""
from typing import Optional
from ..utils.logger import get_logger
logger = get_logger("postgresql_tool_manager")
# Try to import PostgreSQL MCP tool
try:
from th_agenter.services.mcp.postgresql_mcp import PostgreSQLMCPTool
POSTGRESQL_TOOL_AVAILABLE = True
except ImportError as e:
logger.warning(f"PostgreSQL MCP tool not available: {str(e)}. PostgreSQL functionality will be disabled.")
PostgreSQLMCPTool = None
POSTGRESQL_TOOL_AVAILABLE = False
class PostgreSQLToolManager:
"""PostgreSQL工具全局单例管理器"""
_instance: Optional['PostgreSQLToolManager'] = None
_postgresql_tool: Optional[PostgreSQLMCPTool] = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
@property
def postgresql_tool(self) -> Optional[PostgreSQLMCPTool]:
"""获取PostgreSQL工具实例"""
if not POSTGRESQL_TOOL_AVAILABLE:
return None
if self._postgresql_tool is None:
self._postgresql_tool = PostgreSQLMCPTool()
logger.info("创建全局PostgreSQL工具实例")
return self._postgresql_tool
def get_tool(self) -> Optional[PostgreSQLMCPTool]:
"""获取PostgreSQL工具实例别名方法"""
return self.postgresql_tool
# 全局实例
postgresql_tool_manager = PostgreSQLToolManager()
def get_postgresql_tool() -> PostgreSQLMCPTool:
"""获取全局PostgreSQL工具实例"""
return postgresql_tool_manager.get_tool()