~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""The MCP stderr sink emits package logs, with control characters stripped.
A research query reaches the MCP log verbatim (query[:80] in the
citation-handler warnings), so a query containing a newline used to split one
WARNING into two stderr lines. The web path has stripped control characters
since config_logger grew its patcher; the MCP subprocess builds its own sink
and did not.
``local_deep_research/__init__.py`` disables the package's own loguru
namespace, and only ``config_logger`` re-enabled it, so the MCP sink also has
to enable it or it sees nothing the package logs.
"""
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,
)
@pytest.fixture
def emit():
"""Emit through a real MCP sink and return what it wrote."""
# configure_mcp_logging's patcher/namespace-activation/handler changes are
# process-wide (loguru holds one of each per process); restored_loguru_state
# snapshots and restores them so they don't leak into later tests. See its
# docstring in tests/test_utils.py for why logger.configure(patcher=None)
# can't be used for this instead.
with restored_loguru_state():
sink = io.StringIO()
configure_mcp_logging(sink=sink)
def _emit(call):
call()
# configure_mcp_logging's stderr sink now runs with enqueue=True
# (a back-pressure protection -- see the logger.add() comment in
# mcp/server.py), so the write happens on loguru's background
# queue-writer thread rather than inline in call() above.
# logger.complete() blocks until every enqueued message has been
# handed to the sink, so the read below stays deterministic
# instead of racing that thread.
logger.complete()
return sink.getvalue()
yield _emit
def test_a_newline_in_the_message_does_not_start_a_second_line(emit):
out = emit(
lambda: logger.warning(
"Citation handler failed (query 'a\nWARNING | forged')"
)
)
assert out.count("\n") == 1, out
assert "forged" in out
def test_carriage_return_and_escape_are_stripped(emit):
out = emit(lambda: logger.warning("query 'a\rb\x1b[31mRED'"))
assert "\r" not in out
assert "\x1b" not in out
assert "RED" in out
def test_an_ordinary_message_is_unchanged(emit):
out = emit(
lambda: logger.warning("Starting Local Deep Research MCP server...")
)
assert "Starting Local Deep Research MCP server..." in out
def test_a_log_call_inside_the_package_reaches_the_sink(emit):
"""The tests above log from the test module, which the package's own
``logger.disable`` does not cover. Everything the MCP server actually
logs comes from inside ``local_deep_research``."""
out = emit(lambda: is_safe_custom_llm_endpoint(123))
assert "rejected non-string custom_endpoint" in out
assert "local_deep_research.utilities.url_utils" in out
def test_credential_in_message_is_redacted(emit):
out = emit(
lambda: logger.warning("auth failed api_key=plainvalue1234567890ab")
)
# The value is replaced, the field name kept, so the line stays useful.
assert "plainvalue1234567890ab" not in out
assert "api_key=" in out
def test_bound_secret_is_redacted(emit):
records = []
def _bound_warning():
logger.add(
lambda m: records.append(m.record), level="INFO", diagnose=False
)
logger.bind(user_password="BoundSecretPw999").warning("connecting")
out = emit(_bound_warning)
# The stderr format renders only {message}, so a bound secret never
# shows in the formatted line on its own -- it rides on the record
# every sink formats from. The second sink above captures that record
# under the same process-wide patcher, like the sink tests in
# tests/security/test_log_sink_redaction.py.
assert "BoundSecretPw999" not in out
assert records[-1]["extra"].get("user_password") == "[REDACTED]"
def test_credential_in_exception_is_redacted(emit):
"""The MCP patcher scrubs rendered exception text, not just the message.
The ``logger.exception()`` calls in mcp/server.py render the exception
value to this sink; a credential inside it must not reach the MCP
client's stderr log.
"""
# Assembled at runtime: the traceback renders the source line of the
# raise, which must not itself carry the value the assertion looks for.
secret = "McpException" + "Secret739"
error = ValueError(f"upstream refused token={secret}")
def _log_exception():
try:
raise error
except ValueError:
logger.exception("research failed")
out = emit(_log_exception)
assert secret not in out
assert "ValueError" in out
assert "token=" in out
_ENQUEUE_SECRET = "McpEnqueueDump" + "Secret739"
def _local_class_with_secret_text():
class ProviderError(Exception):
pass
return ProviderError(f"request failed: api_key=sk-live-{_ENQUEUE_SECRET}")
def _clean_local_class_with_secret_cause():
class LocalError(Exception):
pass
exc = LocalError("clean text")
exc.__cause__ = ValueError(f"token=ghp_{_ENQUEUE_SECRET}")
return exc
def _secret_named_class():
return type(f"password={_ENQUEUE_SECRET}", (ValueError,), {})("benign")
def _builtin_with_secret_text():
return ValueError(f"token=ghp_{_ENQUEUE_SECRET}")
@pytest.mark.parametrize(
"make, bind_lock, with_exception",
[
(_local_class_with_secret_text, False, True),
(_clean_local_class_with_secret_cause, False, True),
(_secret_named_class, False, True),
(_builtin_with_secret_text, True, True),
(_builtin_with_secret_text, True, False),
],
ids=[
"local_class",
"local_class_cause",
"secret_class_name",
"unpicklable_extra",
"unpicklable_extra_no_exception",
],
)
def test_enqueue_sink_never_dumps_the_unredacted_exception(
monkeypatch, make, bind_lock, with_exception
):
"""The real sink pickles each record; a failure must not dump the original.
loguru's error handler writes the record and the pickling traceback to
``sys.stderr``, and that traceback's ``__context__`` is the exception
being handled, unredacted. ``sys.stderr`` is replaced before the sink is
added, so it captures both the sink and that handler.
"""
import sys
import threading
stderr = io.StringIO()
monkeypatch.setattr(sys, "stderr", stderr)
bound = {"lock": threading.Lock()} if bind_lock else {}
with restored_loguru_state():
configure_mcp_logging()
exc = make()
try:
raise exc
except BaseException:
logger.bind(**bound).opt(exception=with_exception).error(
"handled failure"
)
logger.complete()
out = stderr.getvalue()
assert "handled failure" in out
assert "Logging error in Loguru Handler" not in out
assert "McpEnqueueDumpSecret739" not in out