Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

fix(mcp): separate topic from engine flags (#1162)

Ilia Alshanetsky3635fa5f99fe
diff --git a/changelog.d/+mcp-arg-plumbing.fixed.md b/changelog.d/+mcp-arg-plumbing.fixed.md
new file mode 100644
index 00000000..106b2588
--- /dev/null
+++ b/changelog.d/+mcp-arg-plumbing.fixed.md
@@ -0,0 +1 @@
+MCP research tool now separates the topic from engine flags and no longer saves a report after an explicit decline.
diff --git a/mcp/internal/engine/run_test.go b/mcp/internal/engine/run_test.go
index 97b532e5..af3771b9 100644
--- a/mcp/internal/engine/run_test.go
+++ b/mcp/internal/engine/run_test.go
@@ -22,6 +22,9 @@ import (
// STUB_SLEEP_SECS - sleep before exiting (for timeout tests)
// STUB_ECHO_ENV - name of an env var; the stub prints "<NAME>=<VALUE>"
// STUB_ECHO_ARG - integer index; the stub prints "ARG<i>=<args[i]>"
+// STUB_ECHO_ARGS - when set (any value), the stub prints its full argv
+// after the script path, one argument per line, so tests
+// can assert the exact argv the engine receives
//
// The stub ignores its first argument (the script path), matching how a
// real python3 invocation treats `python3 last30days.py ...`.
@@ -38,6 +41,7 @@ if [ -n "${STUB_STDOUT:-}" ]; then printf "%s" "$STUB_STDOUT"; fi
if [ -n "${STUB_STDERR:-}" ]; then printf "%s" "$STUB_STDERR" >&2; fi
if [ -n "${STUB_ECHO_ENV:-}" ]; then echo "${STUB_ECHO_ENV}=${!STUB_ECHO_ENV:-<unset>}"; fi
if [ -n "${STUB_ECHO_ARG:-}" ]; then echo "ARG${STUB_ECHO_ARG}=${!STUB_ECHO_ARG:-<unset>}"; fi
+if [ -n "${STUB_ECHO_ARGS:-}" ]; then shift; printf "%s\n" "$@"; fi
exit "${STUB_EXIT_CODE:-0}"
`
if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
@@ -82,6 +86,30 @@ func TestRunHappyPath(t *testing.T) {
}
}
+// TestRunForwardsExactArgv pins the full argv the engine subprocess
+// receives: options first, explicit empty --save-dir on decline, `--`
+// separator, then the positional topic. The default-deny
+// --no-browser-cookies flag rides along in this shape too.
+func TestRunForwardsExactArgv(t *testing.T) {
+ stub := makeStubPython(t)
+ cache := stageCache(t)
+ t.Setenv("STUB_ECHO_ARGS", "1")
+
+ want := []string{"--emit=compact", "--no-browser-cookies", "--save-dir", "", "--", "--mock"}
+ res, err := Run(context.Background(), RunOptions{
+ PythonPath: stub,
+ CacheDir: cache,
+ Args: want,
+ })
+ if err != nil {
+ t.Fatalf("Run: %v", err)
+ }
+ got := strings.Split(strings.TrimSuffix(string(res.Stdout), "\n"), "\n")
+ if strings.Join(got, "\x00") != strings.Join(want, "\x00") {
+ t.Fatalf("engine argv = %#v, want %#v", got, want)
+ }
+}
+
func TestRunForwardsEnv(t *testing.T) {
stub := makeStubPython(t)
cache := stageCache(t)
diff --git a/mcp/internal/tools/research.go b/mcp/internal/tools/research.go
index d461e887..53deda8f 100644
--- a/mcp/internal/tools/research.go
+++ b/mcp/internal/tools/research.go
@@ -92,14 +92,21 @@ func makeResearchHandler(cfg Config) server.ToolHandlerFunc {
}
func researchRunArgs(topic, emit string, save bool) []string {
- runArgs := []string{topic, "--emit=" + emit}
+ // Options first, positional topic last behind a `--` separator: a
+ // dash-prefixed topic must never be parsed as engine flags.
+ runArgs := []string{"--emit=" + emit}
if !browserCookiesAllowed() {
runArgs = append(runArgs, "--no-browser-cookies")
}
+ // Always pass --save-dir explicitly. An empty value skips the engine's
+ // `is None` LAST30DAYS_MEMORY_DIR fallback and is falsy at the save
+ // gate, so declining to save writes nothing even when the env var set.
if save {
runArgs = append(runArgs, "--save-dir", mcpSaveDir())
+ } else {
+ runArgs = append(runArgs, "--save-dir", "")
}
- return runArgs
+ return append(runArgs, "--", topic)
}
func browserCookiesAllowed() bool {
diff --git a/mcp/internal/tools/research_test.go b/mcp/internal/tools/research_test.go
index 6c44873a..b4371c04 100644
--- a/mcp/internal/tools/research_test.go
+++ b/mcp/internal/tools/research_test.go
@@ -104,7 +104,7 @@ func TestResearchRunArgsIncludesNoBrowserCookies(t *testing.T) {
t.Fatalf("unset %s: %v", BrowserCookiesEnvOverride, err)
}
args := researchRunArgs("OpenAI", "compact", false)
- want := []string{"OpenAI", "--emit=compact", "--no-browser-cookies"}
+ want := []string{"--emit=compact", "--no-browser-cookies", "--save-dir", "", "--", "OpenAI"}
if strings.Join(args, "\x00") != strings.Join(want, "\x00") {
t.Fatalf("args = %#v, want %#v", args, want)
}
@@ -115,7 +115,7 @@ func TestResearchRunArgsAllowsBrowserCookiesForTruthyOptIn(t *testing.T) {
t.Run(value, func(t *testing.T) {
t.Setenv(BrowserCookiesEnvOverride, value)
args := researchRunArgs("OpenAI", "compact", false)
- want := []string{"OpenAI", "--emit=compact"}
+ want := []string{"--emit=compact", "--save-dir", "", "--", "OpenAI"}
if strings.Join(args, "\x00") != strings.Join(want, "\x00") {
t.Fatalf("%s=%q: args = %#v, want %#v", BrowserCookiesEnvOverride, value, args, want)
}
@@ -132,7 +132,7 @@ func TestResearchRunArgsDeniesBrowserCookiesForFalseAndUnrecognizedValues(t *tes
t.Run(name, func(t *testing.T) {
t.Setenv(BrowserCookiesEnvOverride, value)
args := researchRunArgs("OpenAI", "compact", false)
- want := []string{"OpenAI", "--emit=compact", "--no-browser-cookies"}
+ want := []string{"--emit=compact", "--no-browser-cookies", "--save-dir", "", "--", "OpenAI"}
if strings.Join(args, "\x00") != strings.Join(want, "\x00") {
t.Fatalf("%s=%q: args = %#v, want %#v", BrowserCookiesEnvOverride, value, args, want)
}
@@ -148,7 +148,7 @@ func TestResearchRunArgsSaveUsesSupportedSaveDir(t *testing.T) {
if strings.Contains(got, "--save\x00") || strings.HasSuffix(got, "--save") {
t.Fatalf("args still include unsupported --save: %#v", args)
}
- want := []string{"OpenAI", "--emit=html", "--no-browser-cookies", "--save-dir", "~/Documents/Last30Days"}
+ want := []string{"--emit=html", "--no-browser-cookies", "--save-dir", "~/Documents/Last30Days", "--", "OpenAI"}
if got != strings.Join(want, "\x00") {
t.Fatalf("args = %#v, want %#v", args, want)
}
@@ -158,7 +158,49 @@ func TestResearchRunArgsSaveUsesMemoryDirEnvOverride(t *testing.T) {
t.Setenv(BrowserCookiesEnvOverride, "")
t.Setenv("LAST30DAYS_MEMORY_DIR", "/tmp/last30days-reports")
args := researchRunArgs("OpenAI", "html", true)
- want := []string{"OpenAI", "--emit=html", "--no-browser-cookies", "--save-dir", "/tmp/last30days-reports"}
+ want := []string{"--emit=html", "--no-browser-cookies", "--save-dir", "/tmp/last30days-reports", "--", "OpenAI"}
+ if strings.Join(args, "\x00") != strings.Join(want, "\x00") {
+ t.Fatalf("args = %#v, want %#v", args, want)
+ }
+}
+
+// TestResearchRunArgsSeparatesTopicWithDoubleDash pins CR-002: options come
+// first and `--` precedes the topic, so a dash-prefixed topic is parsed as
+// the positional topic instead of engine flags (--mock/--save-dir collide).
+func TestResearchRunArgsSeparatesTopicWithDoubleDash(t *testing.T) {
+ t.Setenv(BrowserCookiesEnvOverride, "")
+ t.Setenv("LAST30DAYS_MEMORY_DIR", "")
+ args := researchRunArgs("--mock", "compact", false)
+ want := []string{"--emit=compact", "--no-browser-cookies", "--save-dir", "", "--", "--mock"}
+ if strings.Join(args, "\x00") != strings.Join(want, "\x00") {
+ t.Fatalf("args = %#v, want %#v", args, want)
+ }
+ sep := -1
+ for i, a := range args {
+ if a == "--" {
+ sep = i
+ break
+ }
+ }
+ if sep < 0 || args[len(args)-1] != "--mock" || sep != len(args)-2 {
+ t.Fatalf("topic must follow a `--` separator as the final argv: %#v", args)
+ }
+ for _, a := range args[:sep] {
+ if a == "--mock" {
+ t.Fatalf("topic leaked into option position: %#v", args)
+ }
+ }
+}
+
+// TestResearchRunArgsSaveFalsePassesExplicitEmptySaveDir pins CR-003: when
+// save is false the argv carries an explicit empty --save-dir, which skips
+// the engine's `is None` MEMORY_DIR fallback and is falsy at the save gate,
+// so declining to save writes nothing even with LAST30DAYS_MEMORY_DIR set.
+func TestResearchRunArgsSaveFalsePassesExplicitEmptySaveDir(t *testing.T) {
+ t.Setenv(BrowserCookiesEnvOverride, "")
+ t.Setenv("LAST30DAYS_MEMORY_DIR", "/tmp/last30days-reports")
+ args := researchRunArgs("OpenAI", "compact", false)
+ want := []string{"--emit=compact", "--no-browser-cookies", "--save-dir", "", "--", "OpenAI"}
if strings.Join(args, "\x00") != strings.Join(want, "\x00") {
t.Fatalf("args = %#v, want %#v", args, want)
}
diff --git a/tests/test_env_doc_contract.py b/tests/test_env_doc_contract.py
index 04b6f6d8..939e72b4 100644
--- a/tests/test_env_doc_contract.py
+++ b/tests/test_env_doc_contract.py
@@ -30,6 +30,7 @@ DOC_ONLY_KEYS = {
"LAST30DAYS_MCP_ALLOW_BROWSER_COOKIES",
"LAST30DAYS_MCP_TIMEOUT",
"LAST30DAYS_PYTHON",
+ "LAST30DAYS_MCP_ALLOW_BROWSER_COOKIES",
# Read from the process environment before or outside get_config
# (config-dir override, Keychain/pass source switches), so they are
# documented in CONFIGURATION.md without being config keys.
diff --git a/tests/test_mcp_save_decline.py b/tests/test_mcp_save_decline.py
new file mode 100644
index 00000000..04af00c8
--- /dev/null
+++ b/tests/test_mcp_save_decline.py
@@ -0,0 +1,62 @@
+# ruff: noqa: E402
+"""MCP save=false decline contract (CR-003).
+
+The MCP research tool translates save=false into an explicit empty
+`--save-dir ""` (see mcp/internal/tools/research.go::researchRunArgs).
+The empty value must skip the `LAST30DAYS_MEMORY_DIR` fallback (`is None`
+check) and stay falsy at the save gate, so declining to save writes
+nothing even when the env var is set. Omitting the flag instead would
+fall back to the env var and write a file after an explicit decline.
+"""
+
+from __future__ import annotations
+
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+REPO_ROOT = Path(__file__).resolve().parents[1]
+
+
+def _run_engine(topic: str, extra_argv: list[str], env_overrides: dict[str, str]) -> subprocess.CompletedProcess:
+ cmd = [sys.executable, str(REPO_ROOT / "skills" / "last30days" / "scripts" / "last30days.py"), *extra_argv, "--", topic]
+ base = {k: v for k, v in os.environ.items() if k != "LAST30DAYS_MEMORY_DIR"}
+ env = {**base, "LAST30DAYS_SKIP_PREFLIGHT": "1", **env_overrides}
+ return subprocess.run(cmd, capture_output=True, text=True, env=env, encoding="utf-8", errors="replace", check=False)
+
+
+class McpSaveDeclineTests(unittest.TestCase):
+ """save=false (explicit empty --save-dir) writes nothing with MEMORY_DIR set."""
+
+ def setUp(self) -> None:
+ self.tmp = Path(tempfile.mkdtemp(prefix="l30d-mcp-decline-"))
+ self.memory = self.tmp / "Last30Days"
+ self.memory.mkdir()
+
+ def tearDown(self) -> None:
+ shutil.rmtree(self.tmp, ignore_errors=True)
+
+ def test_decline_writes_nothing_despite_memory_dir(self) -> None:
+ # Exact argv shape the MCP layer sends on save=false (options first,
+ # `--` separator, positional topic last).
+ result = _run_engine(
+ topic="OpenAI",
+ extra_argv=["--mock", "--emit=md", "--save-dir", ""],
+ env_overrides={
+ "LAST30DAYS_CONFIG_DIR": "",
+ "LAST30DAYS_MEMORY_DIR": str(self.memory),
+ },
+ )
+ self.assertEqual(result.returncode, 0, msg=result.stderr)
+ self.assertEqual(
+ sorted(self.memory.glob("*.md")), [],
+ msg=f"save=false wrote files after explicit decline. stderr: {result.stderr}",
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()