|
| 1 | +from pathlib import Path |
| 2 | +from typing import Dict, Optional, TYPE_CHECKING |
| 3 | + |
| 4 | +from langchain_mcp_adapters.client import MultiServerMCPClient # noqa: F401 # 预留给未来可能的直接使用 |
| 5 | + |
| 6 | +from utils.logger import get_logger |
| 7 | +from .mcp_language_config import MCPLanguageConfigService |
| 8 | +from pure_auto_codeql.paths import prompts_dir |
| 9 | +from pure_auto_codeql.paths import get_repo_root |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from pure_auto_codeql.agents.cve_analysis_agent import CVEAnalysisAgent as _CVEAnalysisAgent |
| 13 | + from pure_auto_codeql.agents.unified_sink_path_agent import UnifiedSinkPathAgent as _UnifiedSinkPathAgent |
| 14 | + from pure_auto_codeql.agents.unified_source_analysis_agent import UnifiedSourceAnalysisAgent as _UnifiedSourceAnalysisAgent |
| 15 | + from pure_auto_codeql.agents.path_analysis_agent import PathAnalysisAgent as _PathAnalysisAgent |
| 16 | + from pure_auto_codeql.agents.codeql_gen_agents.codeql_gen_agent import CodeQLGenAgent as _CodeQLGenAgent |
| 17 | + from pure_auto_codeql.agents.codeql_gen_agents.codeql_error_agent import CodeQLErrorAgent as _CodeQLErrorAgent |
| 18 | + from pure_auto_codeql.agents.codeql_gen_agents.codeql_fix_inplace_agent import ( |
| 19 | + CodeQLFixInplaceAgent as _CodeQLFixInplaceAgent, |
| 20 | + ) |
| 21 | + from pure_auto_codeql.agents.codeql_gen_agents.codeql_breakpoint_detect_agent import ( |
| 22 | + CodeQLBreakpointAgent as _CodeQLBreakpointAgent, |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +logger = get_logger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +AGENT_TYPES: Dict[str, str] = { |
| 30 | + "cve_analysis": "pure_auto_codeql.agents.cve_analysis_agent.CVEAnalysisAgent", |
| 31 | + "unified_sink_path": "pure_auto_codeql.agents.unified_sink_path_agent.UnifiedSinkPathAgent", |
| 32 | + "unified_source_analysis": "pure_auto_codeql.agents.unified_source_analysis_agent.UnifiedSourceAnalysisAgent", |
| 33 | + "path_analysis": "pure_auto_codeql.agents.path_analysis_agent.PathAnalysisAgent", |
| 34 | + "codeql_gen": "pure_auto_codeql.agents.codeql_gen_agents.codeql_gen_agent.CodeQLGenAgent", |
| 35 | + "codeql_error": "pure_auto_codeql.agents.codeql_gen_agents.codeql_error_agent.CodeQLErrorAgent", |
| 36 | + "codeql_fix_inplace": "pure_auto_codeql.agents.codeql_gen_agents.codeql_fix_inplace_agent.CodeQLFixInplaceAgent", |
| 37 | + "codeql_breakpoint_detect": "pure_auto_codeql.agents.codeql_gen_agents.codeql_breakpoint_detect_agent.CodeQLBreakpointAgent", |
| 38 | + "template_refinement": "pure_auto_codeql.agents.codeql_gen_agents.template_refinement_agent.TemplateRefinementAgent", |
| 39 | + "sink_verification": "pure_auto_codeql.agents.sink_verification_agent.SinkVerificationAgent", |
| 40 | + "source_verification": "pure_auto_codeql.agents.source_verification_agent.SourceVerificationAgent", |
| 41 | +} |
| 42 | + |
| 43 | +AGENT_MCP_PROFILES: Dict[str, list[str]] = { |
| 44 | + "cve_analysis": [], |
| 45 | + "unified_sink_path": ["filesystem", "ripgrep", "language-server"], |
| 46 | + # "unified_source_analysis": ["language-server"], |
| 47 | + "unified_source_analysis": ["tree_sitter", "language-server", "filesystem"], |
| 48 | + "source_analysis": ["tree_sitter", "language-server", "filesystem"], |
| 49 | + "path_analysis": ["tree_sitter", "ripgrep"], |
| 50 | + "codeql_gen": [], |
| 51 | + "codeql_generation": ["filesystem", "ripgrep", "language-server"], |
| 52 | + "codeql_error": ["filesystem", "ripgrep", "language-server"], |
| 53 | + "codeql_fix_inplace": ["filesystem", "ripgrep", "language-server"], |
| 54 | + "codeql_breakpoint_detect": ["tree_sitter", "ripgrep"], |
| 55 | + "template_refinement": ["filesystem"], |
| 56 | + "source_sink_fallback": ["ripgrep"], # 需要 ripgrep 以支持 lsplookup 工具 |
| 57 | + "sink_verification": ["ripgrep", "language-server"], # 需要 ripgrep 和 LSP 以支持 LSPFunctionLookupTool |
| 58 | + "source_verification": ["ripgrep", "language-server"], # 需要 ripgrep 和 LSP 以支持 LSPFunctionLookupTool |
| 59 | + "default": [], |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +class AgentMCPConfigService: |
| 64 | + """ |
| 65 | + Agent 级别的 MCP 配置管理服务。 |
| 66 | + """ |
| 67 | + |
| 68 | + def __init__(self, language_config_service: Optional[MCPLanguageConfigService] = None): |
| 69 | + """ |
| 70 | + 初始化配置服务。 |
| 71 | + """ |
| 72 | + self._language_config_service = language_config_service or MCPLanguageConfigService() |
| 73 | + |
| 74 | + def get_config_for_agent( |
| 75 | + self, |
| 76 | + agent_type: str, |
| 77 | + language: Optional[str] = None, |
| 78 | + workspace_path: Optional[str] = None, |
| 79 | + ) -> Dict[str, Dict]: |
| 80 | + # 获取Agent对应的MCP配置文件,如果不存在则使用默认配置 |
| 81 | + profile = AGENT_MCP_PROFILES.get(agent_type, AGENT_MCP_PROFILES["default"]) |
| 82 | + |
| 83 | + # 针对 Source 分析类 Agent,根据语言调整 MCP 组合策略 |
| 84 | + normalized_language = (language or "").lower() |
| 85 | + if agent_type in {"unified_source_analysis", "source_analysis"} and normalized_language: |
| 86 | + if normalized_language == "java": |
| 87 | + # Java: 使用 ripgrep + language-server + filesystem |
| 88 | + profile = ["ripgrep", "language-server"] |
| 89 | + elif normalized_language == "python": |
| 90 | + # Python: 使用 tree_sitter + language-server + filesystem |
| 91 | + profile = ["language-server", "ripgrep", "tree_sitter"] |
| 92 | + elif normalized_language in {"c", "cpp", "c++"}: |
| 93 | + # C/C++: 使用 tree_sitter + filesystem |
| 94 | + profile = ["tree_sitter", "ripgrep"] |
| 95 | + |
| 96 | + # 构建完整的MCP服务器配置 |
| 97 | + mcp_servers: Dict[str, Dict] = {} |
| 98 | + |
| 99 | + # 添加文件系统MCP |
| 100 | + if "filesystem" in profile: |
| 101 | + if not workspace_path: |
| 102 | + logger.warning("⚠️ filesystem MCP 需要 workspace_path 参数") |
| 103 | + else: |
| 104 | + workspace_path_str = str(workspace_path).replace("source_code", "") |
| 105 | + workspace_path_obj = Path(workspace_path_str) |
| 106 | + parents = workspace_path_obj.parents |
| 107 | + repo_root = parents[1] if len(parents) > 1 else workspace_path_obj.parent |
| 108 | + |
| 109 | + # 添加对于错误整理Agent的MCP配置 |
| 110 | + if agent_type == "template_refinement": |
| 111 | + prompts_path = prompts_dir() |
| 112 | + mcp_servers["filesystem"] = { |
| 113 | + "command": "npx", |
| 114 | + "args": [ |
| 115 | + "-y", |
| 116 | + "@modelcontextprotocol/server-filesystem", |
| 117 | + str(prompts_path), |
| 118 | + ], |
| 119 | + "transport": "stdio", |
| 120 | + } |
| 121 | + else: |
| 122 | + temp_codeql_path = repo_root / "temp" / "codeql_temp" |
| 123 | + prompts_path = prompts_dir() |
| 124 | + mcp_servers["filesystem"] = { |
| 125 | + "command": "npx", |
| 126 | + "args": [ |
| 127 | + "-y", |
| 128 | + "@modelcontextprotocol/server-filesystem", |
| 129 | + workspace_path_str, |
| 130 | + str(temp_codeql_path), |
| 131 | + str(prompts_path), |
| 132 | + ], |
| 133 | + "transport": "stdio", |
| 134 | + } |
| 135 | + |
| 136 | + # 添加ripgrep MCP |
| 137 | + if "ripgrep" in profile: |
| 138 | + mcp_servers["ripgrep"] = { |
| 139 | + "command": "node", |
| 140 | + "args": [ |
| 141 | + str( |
| 142 | + get_repo_root() |
| 143 | + / "tools" |
| 144 | + / "mcp_ripgrep" |
| 145 | + / "dist" |
| 146 | + / "index.js" |
| 147 | + ) |
| 148 | + ], |
| 149 | + "transport": "stdio", |
| 150 | + } |
| 151 | + |
| 152 | + # 添加 tree_sitter MCP |
| 153 | + if "tree_sitter" in profile: |
| 154 | + if not workspace_path: |
| 155 | + logger.warning("⚠️ tree_sitter MCP 需要 workspace_path 参数") |
| 156 | + else: |
| 157 | + mcp_servers["tree_sitter"] = { |
| 158 | + "command": "uv", |
| 159 | + "args": [ |
| 160 | + "--directory", |
| 161 | + str(workspace_path), |
| 162 | + "run", |
| 163 | + "-m", |
| 164 | + "mcp_server_tree_sitter.server", |
| 165 | + ], |
| 166 | + "transport": "stdio", |
| 167 | + } |
| 168 | + |
| 169 | + # 添加语言服务器MCP |
| 170 | + if "language-server" in profile and language and workspace_path: |
| 171 | + try: |
| 172 | + language_config = self._language_config_service |
| 173 | + if language_config.is_language_supported(language): |
| 174 | + lsp_config = language_config.get_language_server_config(language, workspace_path) |
| 175 | + mcp_servers["language-server"] = lsp_config |
| 176 | + logger.info(f"✓ 已添加 {language} LSP MCP 配置") |
| 177 | + else: |
| 178 | + logger.info(f"ℹ️ 语言 {language} 不支持 LSP MCP") |
| 179 | + except Exception as e: # pragma: no cover - 防御性日志记录 |
| 180 | + logger.warning(f"⚠️ LSP MCP 配置失败,继续使用基础 MCP: {e}") |
| 181 | + elif "language-server" in profile: |
| 182 | + logger.warning("⚠️ language-server MCP 需要 language 和 workspace_path 参数") |
| 183 | + |
| 184 | + return mcp_servers |
0 commit comments