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.

2.8 KB
Raw Blame
"""Structural tripwire: the MCP server never opens a network listener.

The MCP server's security posture is "STDIO-only transport, no auth by
design" (SECURITY.md / lens 15): it is launched as a child process by
a trusted local parent and exchanges JSON-RPC over stdin/stdout. The
moment anyone adds a network listener — uvicorn, a socket bind, an
HTTP server — that posture silently becomes "unauthenticated remote
API", because none of the web tier's auth stack guards this process.

Nothing pinned this. These contracts make any listener introduction
red: the single ``mcp.run()`` transport must stay ``stdio``, and the
module must not reference any network-serving machinery.
"""

from __future__ import annotations

import re
from pathlib import Path

import local_deep_research

# 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]:
    """Module source minus comments and docstrings-approximation.

    Line comments are stripped; loguru's ``logger.bind`` is excluded
    from the bind-tripwire by the token patterns themselves.
    """
    lines = []
    for raw in MODULE_PATH.read_text(encoding="utf-8").splitlines():
        stripped = raw.strip()
        if stripped.startswith("#"):
            continue
        lines.append(raw)
    return lines


class TestMcpStaysStdioOnly:
    def test_the_only_transport_is_stdio(self):
        runs = [
            line
            for line in _code_lines()
            if re.search(r"\bmcp\.run\s*\(", line)
        ]
        assert runs, "no mcp.run() call found — transport moved elsewhere?"
        for line in runs:
            assert 'transport="stdio"' in line, (
                f"mcp.run() without stdio transport: {line.strip()!r}"
            )

    def test_no_network_listener_machinery(self):
        patterns = {
            "uvicorn": r"\buvicorn\b",
            "socket bind/listen": r"\.(?:bind|listen)\s*\(",
            "socket module": r"\bimport\s+socket\b|\bsocket\.socket\b",
            "http server": r"\b(?:HTTPServer|TCPServer|UDPServer|Flask)\b",
        }
        for label, pattern in patterns.items():
            hits = [
                line.strip()
                for line in _code_lines()
                if re.search(pattern, line)
                # loguru's logger.bind(...) is not a socket bind.
                and not re.search(r"logger\.bind\s*\(", line)
            ]
            assert hits == [], f"{label} referenced in MCP server: {hits}"

    def test_module_path_is_the_production_server(self):
        # Guard against the scan drifting onto a test double.
        assert MODULE_PATH.name == "server.py"
        assert MODULE_PATH.parent.name == "mcp"