Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

test(mcp): detect the installed optional SDK reliably (#6921)

LearningCircuit341e99d8055c
diff --git a/.github/workflows/mcp-tests.yml b/.github/workflows/mcp-tests.yml
index 7b59995090..378ed0926a 100644
--- a/.github/workflows/mcp-tests.yml
+++ b/.github/workflows/mcp-tests.yml
@@ -6,6 +6,9 @@ on:
paths:
- 'src/local_deep_research/mcp/**'
- 'tests/mcp/**'
+ - 'tests/mcp_sdk.py'
+ - 'tests/test_cli_and_mcp_entrypoints.py'
+ - 'tests/security/test_mcp_stdio_only_tripwire.py'
- 'scripts/mcp_smoke_test.sh'
- '.github/workflows/mcp-tests.yml'
pull_request:
@@ -13,6 +16,9 @@ on:
paths:
- 'src/local_deep_research/mcp/**'
- 'tests/mcp/**'
+ - 'tests/mcp_sdk.py'
+ - 'tests/test_cli_and_mcp_entrypoints.py'
+ - 'tests/security/test_mcp_stdio_only_tripwire.py'
- 'scripts/mcp_smoke_test.sh'
- '.github/workflows/mcp-tests.yml'
workflow_dispatch:
@@ -72,7 +78,9 @@ jobs:
- name: Run MCP unit tests
if: steps.check-mcp.outputs.mcp_exists == 'true'
run: |
- pdm run pytest tests/mcp/ -v --tb=short -n 4
+ pdm run pytest tests/mcp/ \
+ tests/test_cli_and_mcp_entrypoints.py \
+ tests/security/test_mcp_stdio_only_tripwire.py -v --tb=short -n 4
env:
LDR_TESTING_WITH_MOCKS: "true"
diff --git a/tests/mcp/test_additional_coverage.py b/tests/mcp/test_additional_coverage.py
index 5d1a385e21..8cddf2f1cc 100644
--- a/tests/mcp/test_additional_coverage.py
+++ b/tests/mcp/test_additional_coverage.py
@@ -15,12 +15,9 @@ from unittest.mock import patch
import pytest
# Skip all tests if MCP is not available
-try:
- import mcp # noqa: F401
+from tests.mcp_sdk import is_mcp_installed
- MCP_AVAILABLE = True
-except ImportError:
- MCP_AVAILABLE = False
+MCP_AVAILABLE = is_mcp_installed()
pytestmark = pytest.mark.skipif(
not MCP_AVAILABLE, reason="MCP package not installed"
diff --git a/tests/mcp/test_edge_cases.py b/tests/mcp/test_edge_cases.py
index 84596679e5..9e6ead116f 100644
--- a/tests/mcp/test_edge_cases.py
+++ b/tests/mcp/test_edge_cases.py
@@ -9,12 +9,9 @@ from unittest.mock import patch
import pytest
# Skip all tests if MCP is not available
-try:
- import mcp # noqa: F401
+from tests.mcp_sdk import is_mcp_installed
- MCP_AVAILABLE = True
-except ImportError:
- MCP_AVAILABLE = False
+MCP_AVAILABLE = is_mcp_installed()
pytestmark = pytest.mark.skipif(
not MCP_AVAILABLE, reason="MCP package not installed"
diff --git a/tests/mcp/test_egress_audit_net.py b/tests/mcp/test_egress_audit_net.py
index 04b29ad60b..00da0a8329 100644
--- a/tests/mcp/test_egress_audit_net.py
+++ b/tests/mcp/test_egress_audit_net.py
@@ -16,12 +16,9 @@ from local_deep_research.security.egress.policy import (
context_from_snapshot,
)
-try:
- import mcp # noqa: F401
+from tests.mcp_sdk import is_mcp_installed
- MCP_AVAILABLE = True
-except ImportError:
- MCP_AVAILABLE = False
+MCP_AVAILABLE = is_mcp_installed()
pytestmark = pytest.mark.skipif(
not MCP_AVAILABLE, reason="MCP package not installed"
diff --git a/tests/mcp/test_integration.py b/tests/mcp/test_integration.py
index 3bcbb73d66..3684323228 100644
--- a/tests/mcp/test_integration.py
+++ b/tests/mcp/test_integration.py
@@ -9,12 +9,9 @@ from unittest.mock import patch
import pytest
# Skip all tests if MCP is not available
-try:
- import mcp # noqa: F401
+from tests.mcp_sdk import is_mcp_installed
- MCP_AVAILABLE = True
-except ImportError:
- MCP_AVAILABLE = False
+MCP_AVAILABLE = is_mcp_installed()
pytestmark = pytest.mark.skipif(
not MCP_AVAILABLE, reason="MCP package not installed"
diff --git a/tests/mcp/test_mcp_protocol.py b/tests/mcp/test_mcp_protocol.py
index deb321af32..7f3313139c 100644
--- a/tests/mcp/test_mcp_protocol.py
+++ b/tests/mcp/test_mcp_protocol.py
@@ -13,12 +13,9 @@ import pytest
from loguru import logger
# Skip all tests if MCP is not available
-try:
- import mcp # noqa: F401
+from tests.mcp_sdk import is_mcp_installed
- MCP_AVAILABLE = True
-except ImportError:
- MCP_AVAILABLE = False
+MCP_AVAILABLE = is_mcp_installed()
pytestmark = pytest.mark.skipif(
not MCP_AVAILABLE, reason="MCP package not installed"
diff --git a/tests/mcp/test_policy_denied.py b/tests/mcp/test_policy_denied.py
index e7c4393366..6921020352 100644
--- a/tests/mcp/test_policy_denied.py
+++ b/tests/mcp/test_policy_denied.py
@@ -32,12 +32,9 @@ from local_deep_research.security.egress.policy import (
# MCP is an optional dependency. Skip the full module when it is absent so
# the suite still collects in minimal environments; the import-only checks
# below still run unconditionally to catch syntax/import regressions.
-try:
- import mcp # noqa: F401
+from tests.mcp_sdk import is_mcp_installed
- MCP_AVAILABLE = True
-except ImportError:
- MCP_AVAILABLE = False
+MCP_AVAILABLE = is_mcp_installed()
pytestmark = pytest.mark.skipif(
not MCP_AVAILABLE, reason="MCP package not installed"
diff --git a/tests/mcp/test_server.py b/tests/mcp/test_server.py
index c7f66ab77e..ff1cacafb3 100644
--- a/tests/mcp/test_server.py
+++ b/tests/mcp/test_server.py
@@ -10,12 +10,9 @@ from unittest.mock import patch
import pytest
# Skip all tests if MCP is not available
-try:
- import mcp # noqa: F401
+from tests.mcp_sdk import is_mcp_installed
- MCP_AVAILABLE = True
-except ImportError:
- MCP_AVAILABLE = False
+MCP_AVAILABLE = is_mcp_installed()
pytestmark = pytest.mark.skipif(
not MCP_AVAILABLE, reason="MCP package not installed"
diff --git a/tests/mcp/test_server_coverage.py b/tests/mcp/test_server_coverage.py
index f3e40bee80..ea5604cc1c 100644
--- a/tests/mcp/test_server_coverage.py
+++ b/tests/mcp/test_server_coverage.py
@@ -16,12 +16,9 @@ tests/mcp/test_validation.py:
import pytest
from loguru import logger
-try:
- import mcp # noqa: F401
+from tests.mcp_sdk import is_mcp_installed
- MCP_AVAILABLE = True
-except ImportError:
- MCP_AVAILABLE = False
+MCP_AVAILABLE = is_mcp_installed()
pytestmark = pytest.mark.skipif(
not MCP_AVAILABLE, reason="MCP package not installed"
diff --git a/tests/mcp/test_server_validators.py b/tests/mcp/test_server_validators.py
index 074f6c7f86..6238898d80 100644
--- a/tests/mcp/test_server_validators.py
+++ b/tests/mcp/test_server_validators.py
@@ -5,12 +5,9 @@ from unittest.mock import patch
import pytest
# Skip all tests if MCP package is not installed
-try:
- import mcp # noqa: F401
+from tests.mcp_sdk import is_mcp_installed
- MCP_AVAILABLE = True
-except ImportError:
- MCP_AVAILABLE = False
+MCP_AVAILABLE = is_mcp_installed()
pytestmark = pytest.mark.skipif(
not MCP_AVAILABLE, reason="MCP package not installed"
diff --git a/tests/mcp/test_stderr_control_chars.py b/tests/mcp/test_stderr_control_chars.py
index 3d0f15f0d6..a62578f13a 100644
--- a/tests/mcp/test_stderr_control_chars.py
+++ b/tests/mcp/test_stderr_control_chars.py
@@ -16,13 +16,17 @@ import io
import pytest
from loguru import logger
+from tests.mcp_sdk import is_mcp_installed
+from tests.test_utils import restored_loguru_state
+
+if not is_mcp_installed():
+ pytest.skip("MCP package not installed", allow_module_level=True)
+
from local_deep_research.mcp.server import configure_mcp_logging
from local_deep_research.utilities.url_utils import (
is_safe_custom_llm_endpoint,
)
-from tests.test_utils import restored_loguru_state
-
@pytest.fixture
def emit():
diff --git a/tests/mcp/test_validation.py b/tests/mcp/test_validation.py
index 5d9ebbab15..53ac4806c0 100644
--- a/tests/mcp/test_validation.py
+++ b/tests/mcp/test_validation.py
@@ -7,12 +7,9 @@ These tests verify input validation for all MCP tool parameters.
import pytest
# Skip all tests if MCP is not available
-try:
- import mcp # noqa: F401
+from tests.mcp_sdk import is_mcp_installed
- MCP_AVAILABLE = True
-except ImportError:
- MCP_AVAILABLE = False
+MCP_AVAILABLE = is_mcp_installed()
pytestmark = pytest.mark.skipif(
not MCP_AVAILABLE, reason="MCP package not installed"
diff --git a/tests/mcp_sdk.py b/tests/mcp_sdk.py
new file mode 100644
index 0000000000..29e89650f9
--- /dev/null
+++ b/tests/mcp_sdk.py
@@ -0,0 +1,16 @@
+"""Availability of the optional installed MCP SDK, independent of sys.path."""
+
+from importlib.metadata import PackageNotFoundError, distribution
+
+
+def is_mcp_installed() -> bool:
+ """A namespace directory or a test stub is not an installed SDK.
+
+ An installed SDK with broken imports must still enter the real tests,
+ where the import fails visibly instead of silently skipping coverage.
+ """
+ try:
+ distribution("mcp")
+ except PackageNotFoundError:
+ return False
+ return True
diff --git a/tests/security/test_mcp_stdio_only_tripwire.py b/tests/security/test_mcp_stdio_only_tripwire.py
index b357c0bcb5..d15013b735 100644
--- a/tests/security/test_mcp_stdio_only_tripwire.py
+++ b/tests/security/test_mcp_stdio_only_tripwire.py
@@ -17,9 +17,11 @@ from __future__ import annotations
import re
from pathlib import Path
-import local_deep_research.mcp.server as mcp_server
+import local_deep_research
-MODULE_PATH = Path(mcp_server.__file__)
+# This is a source scan, so locating the installed package is enough. Loading
+# the server would unnecessarily require the optional MCP SDK at collection.
+MODULE_PATH = Path(local_deep_research.__file__).parent / "mcp" / "server.py"
def _code_lines() -> list[str]:
diff --git a/tests/test_cli_and_mcp_entrypoints.py b/tests/test_cli_and_mcp_entrypoints.py
index dc6953901a..e5304f275d 100644
--- a/tests/test_cli_and_mcp_entrypoints.py
+++ b/tests/test_cli_and_mcp_entrypoints.py
@@ -9,20 +9,19 @@ egress / auth checks the web routers apply.
Why this file exists at ``tests/`` root rather than under ``tests/mcp/``:
- * every test under ``tests/mcp/`` is ``skipif``-ed on ``import mcp``
+ * SDK-dependent tests under ``tests/mcp/`` require the installed extra
(see ``tests/mcp/test_server_validators.py``), and the default unit
- suite does NOT install the ``[mcp]`` extra -- so all ~4.5k lines of
- ``tests/mcp/`` are skipped there;
- * ``.github/workflows/mcp-tests.yml`` (the job that *does* install the
- extra) only triggers on ``src/local_deep_research/mcp/**`` and
- ``tests/mcp/**``.
-
-Net effect: a change *outside* ``mcp/`` -- the Flask->FastAPI port
-rewriting ``utilities/db_utils.py``, ``settings/``, or
-``api/research_functions.py`` -- is never exercised against the MCP
-entry point by CI at all. The tests here therefore stub the ``mcp``
-package boundary when it is absent so they run in the default suite,
-which is exactly where the blind spot is.
+ suite does NOT install the ``[mcp]`` extra, so those tests are skipped
+ there;
+ * ``.github/workflows/mcp-tests.yml`` installs the extra and also runs
+ this file, but only triggers on changes to the MCP code, tests, or
+ workflow.
+
+These root-level tests also cover changes to ``utilities/db_utils.py``,
+``settings/``, or ``api/research_functions.py`` that do not trigger the
+dedicated MCP job. They stub the ``mcp`` package boundary when it is absent
+so they run in the default suite; the dedicated job verifies them against
+the installed SDK too.
No server is started, no LLM is called, and no network request is made:
the FastMCP boundary is stubbed, ``run_server``'s transport call is
@@ -47,6 +46,8 @@ import anyio.to_thread
import pytest
from loguru import logger
+from tests.mcp_sdk import is_mcp_installed
+
REPO_ROOT = Path(__file__).resolve().parents[1]
SRC_ROOT = REPO_ROOT / "src"
PYPROJECT = REPO_ROOT / "pyproject.toml"
@@ -60,19 +61,39 @@ ANYIO_WORKER_THREAD_NAME = "AnyIO worker thread"
# Resolved at import time, BEFORE the fixture below may install a stub
# ``mcp`` in sys.modules -- otherwise the two real-SDK tests would happily
# run against the stub and assert nothing.
-try: # pragma: no cover - environment dependent
- import mcp as _real_mcp
-
- REAL_MCP_INSTALLED = True
-except ImportError: # pragma: no cover - the default unit suite
- _real_mcp = None
- REAL_MCP_INSTALLED = False
+REAL_MCP_INSTALLED = is_mcp_installed()
requires_real_mcp = pytest.mark.skipif(
not REAL_MCP_INSTALLED, reason="[mcp] extra not installed"
)
+def test_mcp_availability_does_not_accept_a_namespace_or_stub(monkeypatch):
+ from importlib.metadata import PackageNotFoundError
+
+ from tests import mcp_sdk
+
+ namespace = types.ModuleType("mcp")
+ namespace.__path__ = [str(REPO_ROOT / "tests" / "mcp")]
+ monkeypatch.setitem(sys.modules, "mcp", namespace)
+
+ def absent(_name):
+ raise PackageNotFoundError("mcp")
+
+ monkeypatch.setattr(mcp_sdk, "distribution", absent)
+ assert not mcp_sdk.is_mcp_installed()
+
+
+def test_installed_but_broken_mcp_is_not_silently_skipped(monkeypatch):
+ from tests import mcp_sdk
+
+ monkeypatch.setattr(mcp_sdk, "distribution", lambda _name: object())
+ monkeypatch.setitem(sys.modules, "mcp", None)
+ assert mcp_sdk.is_mcp_installed()
+ with pytest.raises(ModuleNotFoundError):
+ __import__("mcp")
+
+
# ---------------------------------------------------------------------------
# Loading local_deep_research.mcp.server without the [mcp] extra
# ---------------------------------------------------------------------------
@@ -121,9 +142,8 @@ def _install_mcp_stub():
def mcp_server():
"""Import ``local_deep_research.mcp.server``, stubbing ``mcp`` if needed.
- ``sys.modules`` is restored afterwards so that the stub can never make
- ``tests/mcp/``'s ``import mcp`` availability probe report a false
- positive when both files land on the same xdist worker.
+ ``sys.modules`` is restored afterwards so later tests see the original
+ SDK/module state on the same worker.
"""
saved = {
name: sys.modules.get(name)
@@ -135,14 +155,13 @@ def mcp_server():
"local_deep_research.mcp.server",
)
}
- real_mcp_present = "mcp" in sys.modules
- if not real_mcp_present:
- try:
- import mcp # noqa: F401
-
- real_mcp_present = True
- except ImportError:
- _install_mcp_stub()
+ if not REAL_MCP_INSTALLED:
+ _install_mcp_stub()
+ # A preceding classifier test may have loaded these tools using a
+ # different SDK stub. Rebind the tools to this fixture's faithful
+ # decorator instead of reusing functions wrapped in MagicMock.
+ sys.modules.pop("local_deep_research.mcp.server", None)
+ sys.modules.pop("local_deep_research.mcp", None)
try:
import local_deep_research.mcp.server as server_module
@@ -160,6 +179,8 @@ def mcp_server():
# Drop the attribute the import bound on the parent package too.
if getattr(local_deep_research, "mcp", None) is not None:
delattr(local_deep_research, "mcp")
+ else:
+ local_deep_research.mcp = saved["local_deep_research.mcp"]
@pytest.fixture