|
| 1 | +"""Tests for project-level .mcp.json discovery and merge behavior.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | +from pydantic import SecretStr |
| 8 | + |
| 9 | +from openhands.sdk import Agent, LLM |
| 10 | +from openhands.sdk.conversation.impl.local_conversation import LocalConversation |
| 11 | +from openhands.sdk.mcp.project_config import find_project_mcp_json, try_load_project_mcp_config |
| 12 | +from openhands.sdk.plugin import PluginSource, merge_mcp_configs |
| 13 | + |
| 14 | + |
| 15 | +def _minimal_mcp_file() -> dict: |
| 16 | + return {"mcpServers": {"proj": {"command": "echo", "args": ["mcp"]}}} |
| 17 | + |
| 18 | + |
| 19 | +def test_find_project_mcp_json_prefers_openhands_dir(tmp_path: Path) -> None: |
| 20 | + root = tmp_path / "ws" |
| 21 | + root.mkdir() |
| 22 | + (root / ".mcp.json").write_text(json.dumps(_minimal_mcp_file())) |
| 23 | + oh = root / ".openhands" |
| 24 | + oh.mkdir() |
| 25 | + preferred = _minimal_mcp_file() |
| 26 | + preferred["mcpServers"]["proj"]["args"] = ["preferred"] |
| 27 | + (oh / ".mcp.json").write_text(json.dumps(preferred)) |
| 28 | + |
| 29 | + found = find_project_mcp_json(root) |
| 30 | + assert found == oh / ".mcp.json" |
| 31 | + |
| 32 | + |
| 33 | +def test_find_project_mcp_json_falls_back_to_root(tmp_path: Path) -> None: |
| 34 | + root = tmp_path / "ws" |
| 35 | + root.mkdir() |
| 36 | + (root / ".mcp.json").write_text(json.dumps(_minimal_mcp_file())) |
| 37 | + |
| 38 | + assert find_project_mcp_json(root) == root / ".mcp.json" |
| 39 | + |
| 40 | + |
| 41 | +def test_merge_mcp_configs_overlay_wins() -> None: |
| 42 | + base = {"mcpServers": {"a": {"command": "x"}, "b": {"command": "y"}}} |
| 43 | + overlay = {"mcpServers": {"a": {"command": "z"}}} |
| 44 | + merged = merge_mcp_configs(base, overlay) |
| 45 | + assert merged["mcpServers"]["a"]["command"] == "z" |
| 46 | + assert merged["mcpServers"]["b"]["command"] == "y" |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture |
| 50 | +def mock_llm(): |
| 51 | + return LLM(model="test/model", api_key=SecretStr("test-key")) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def basic_agent(mock_llm): |
| 56 | + return Agent(llm=mock_llm, tools=[]) |
| 57 | + |
| 58 | + |
| 59 | +def test_trust_project_mcp_merges_under_user_config( |
| 60 | + tmp_path: Path, basic_agent: Agent, monkeypatch: pytest.MonkeyPatch |
| 61 | +) -> None: |
| 62 | + ws = tmp_path / "ws" |
| 63 | + ws.mkdir() |
| 64 | + (ws / ".mcp.json").write_text(json.dumps(_minimal_mcp_file())) |
| 65 | + |
| 66 | + agent = basic_agent.model_copy( |
| 67 | + update={ |
| 68 | + "mcp_config": { |
| 69 | + "mcpServers": {"proj": {"command": "user-wins", "args": []}}, |
| 70 | + } |
| 71 | + } |
| 72 | + ) |
| 73 | + |
| 74 | + monkeypatch.setattr( |
| 75 | + "openhands.sdk.agent.base.create_mcp_tools", |
| 76 | + lambda config, timeout: [], |
| 77 | + ) |
| 78 | + |
| 79 | + conv = LocalConversation( |
| 80 | + agent=agent, |
| 81 | + workspace=ws, |
| 82 | + visualizer=None, |
| 83 | + trust_project_mcp=True, |
| 84 | + ) |
| 85 | + conv._ensure_agent_ready() |
| 86 | + |
| 87 | + assert conv.agent.mcp_config["mcpServers"]["proj"]["command"] == "user-wins" |
| 88 | + conv.close() |
| 89 | + |
| 90 | + |
| 91 | +def test_project_mcp_skipped_without_trust(tmp_path: Path, basic_agent: Agent) -> None: |
| 92 | + ws = tmp_path / "ws" |
| 93 | + ws.mkdir() |
| 94 | + (ws / ".mcp.json").write_text(json.dumps(_minimal_mcp_file())) |
| 95 | + |
| 96 | + conv = LocalConversation( |
| 97 | + agent=basic_agent, |
| 98 | + workspace=ws, |
| 99 | + visualizer=None, |
| 100 | + trust_project_mcp=False, |
| 101 | + ) |
| 102 | + conv._ensure_plugins_loaded() |
| 103 | + |
| 104 | + assert conv.agent.mcp_config == {} |
| 105 | + conv.close() |
| 106 | + |
| 107 | + |
| 108 | +def test_project_mcp_layer_before_plugin( |
| 109 | + tmp_path: Path, basic_agent: Agent, monkeypatch: pytest.MonkeyPatch |
| 110 | +) -> None: |
| 111 | + ws = tmp_path / "ws" |
| 112 | + ws.mkdir() |
| 113 | + (ws / ".mcp.json").write_text( |
| 114 | + json.dumps({"mcpServers": {"shared": {"command": "from-project"}}}) |
| 115 | + ) |
| 116 | + |
| 117 | + plugin_dir = tmp_path / "plugin" |
| 118 | + manifest_dir = plugin_dir / ".plugin" |
| 119 | + manifest_dir.mkdir(parents=True) |
| 120 | + (manifest_dir / "plugin.json").write_text( |
| 121 | + json.dumps( |
| 122 | + { |
| 123 | + "name": "t", |
| 124 | + "version": "1.0.0", |
| 125 | + "description": "d", |
| 126 | + } |
| 127 | + ) |
| 128 | + ) |
| 129 | + (plugin_dir / ".mcp.json").write_text( |
| 130 | + json.dumps({"mcpServers": {"shared": {"command": "from-plugin"}}}) |
| 131 | + ) |
| 132 | + |
| 133 | + monkeypatch.setattr( |
| 134 | + "openhands.sdk.agent.base.create_mcp_tools", |
| 135 | + lambda config, timeout: [], |
| 136 | + ) |
| 137 | + |
| 138 | + conv = LocalConversation( |
| 139 | + agent=basic_agent, |
| 140 | + workspace=ws, |
| 141 | + plugins=[PluginSource(source=str(plugin_dir))], |
| 142 | + visualizer=None, |
| 143 | + trust_project_mcp=True, |
| 144 | + ) |
| 145 | + conv._ensure_agent_ready() |
| 146 | + |
| 147 | + assert conv.agent.mcp_config["mcpServers"]["shared"]["command"] == "from-plugin" |
| 148 | + conv.close() |
| 149 | + |
| 150 | + |
| 151 | +def test_try_load_project_mcp_config_invalid_json_logs( |
| 152 | + tmp_path: Path, caplog: pytest.LogCaptureFixture |
| 153 | +) -> None: |
| 154 | + ws = tmp_path / "ws" |
| 155 | + ws.mkdir() |
| 156 | + (ws / ".mcp.json").write_text("not json") |
| 157 | + |
| 158 | + import logging |
| 159 | + |
| 160 | + with caplog.at_level(logging.WARNING): |
| 161 | + assert try_load_project_mcp_config(ws) is None |
| 162 | + assert "Ignoring invalid project MCP config" in caplog.text |
0 commit comments