From 66cdb384082166893114e58ca822135142e0bda7 Mon Sep 17 00:00:00 2001 From: lda Date: Thu, 24 Sep 2026 05:39:30 +0700 Subject: [PATCH] refactor: require explicit sqlite paths --- README.md | 8 ++++---- src/agentmsgs/mcp/server.py | 14 ++++++++------ src/agentmsgs/stores/sqlite.py | 3 +-- src/agentmsgs/utils.py | 6 ------ tests/test_store_sqlite.py | 7 +++++++ 5 files changed, 20 insertions(+), 18 deletions(-) delete mode 100644 src/agentmsgs/utils.py diff --git a/README.md b/README.md index e2164d8..c444e68 100644 --- a/README.md +++ b/README.md @@ -57,16 +57,16 @@ app = App(store=SQLiteStore(Path(".agentmsgs.db"))) ### MCP server -Start the MCP server over stdio: +Start the MCP server over stdio with an explicit SQLite path: ```bash -uv run agentmsgs-mcp --transport stdio +uv run agentmsgs-mcp --transport stdio --db-path ./agentmsgs.db ``` Or use HTTP: ```bash -uv run agentmsgs-mcp --transport http +uv run agentmsgs-mcp --transport http --db-path ./agentmsgs.db ``` Choose a shared database file when separate clients or processes should communicate: @@ -81,7 +81,7 @@ For a temporary, process-local server, use the in-memory store: uv run agentmsgs-mcp --in-memory --transport stdio ``` -The in-memory mode is synchronized for concurrent calls within that server, but its data disappears on restart and is not shared with another server process. The default database is `.agentmsgs.db` in the workspace root. +The in-memory mode is synchronized for concurrent calls within that server, but its data disappears on restart and is not shared with another server process. SQLite mode requires an explicit `--db-path`. See all server options with: diff --git a/src/agentmsgs/mcp/server.py b/src/agentmsgs/mcp/server.py index d13a824..4631ce3 100644 --- a/src/agentmsgs/mcp/server.py +++ b/src/agentmsgs/mcp/server.py @@ -1,6 +1,7 @@ from __future__ import annotations import argparse +from pathlib import Path from fastmcp import FastMCP from fastmcp.utilities.logging import get_logger @@ -9,7 +10,6 @@ from agentmsgs.core.app import App from agentmsgs.core.types import Agent, Message from agentmsgs.stores.memory import InMemoryStore from agentmsgs.stores.sqlite import SQLiteStore -from agentmsgs.utils import WORKSPACE_ROOT logger = get_logger(__name__) # WHY mcp = FastMCP("agentmsgs") @@ -162,7 +162,7 @@ def main() -> None: description="Run the AgentMsgs MCP server.", epilog=( "Examples:\n" - " agentmsgs-mcp --transport stdio\n" + " agentmsgs-mcp --transport stdio --db-path ./agentmsgs.db\n" " agentmsgs-mcp --transport stdio --db-path ./shared-agentmsgs.db\n" " agentmsgs-mcp --in-memory --transport stdio" ), @@ -180,11 +180,11 @@ def main() -> None: parser.add_argument( "--db-path", - type=str, - default=str(WORKSPACE_ROOT / ".agentmsgs.db"), + type=Path, + default=None, help=( - "SQLite database path. Used unless --in-memory is set. " - "Defaults to .agentmsgs.db in the workspace root." + "SQLite database path. Required unless --in-memory is set. " + "The path is used exactly as supplied." ), ) parser.add_argument( @@ -203,6 +203,8 @@ def main() -> None: _app = App(store=InMemoryStore()) logger.info("Using in-memory store for MCP server.") else: + if args.db_path is None: + parser.error("--db-path is required unless --in-memory is set") _app = App(store=SQLiteStore(args.db_path)) logger.info("Using SQLite store at %s for MCP server.", args.db_path) diff --git a/src/agentmsgs/stores/sqlite.py b/src/agentmsgs/stores/sqlite.py index fc29663..8ea4a4c 100644 --- a/src/agentmsgs/stores/sqlite.py +++ b/src/agentmsgs/stores/sqlite.py @@ -8,7 +8,6 @@ from contextlib import contextmanager from pathlib import Path from agentmsgs.core.types import Agent, Message, Thread, ThreadInfo, ThreadReadState -from agentmsgs.utils import WORKSPACE_ROOT _SCHEMA = """ PRAGMA journal_mode=WAL; @@ -21,7 +20,7 @@ CREATE TABLE IF NOT EXISTS cursors (thread_id TEXT NOT NULL, agent_id TEXT NOT N class SQLiteStore: - def __init__(self, path: Path = WORKSPACE_ROOT / ".agentmsgs.db") -> None: + def __init__(self, path: Path) -> None: self.path = Path(path) # ensure parent exists if self.path.parent and not self.path.parent.exists(): diff --git a/src/agentmsgs/utils.py b/src/agentmsgs/utils.py deleted file mode 100644 index 63861d5..0000000 --- a/src/agentmsgs/utils.py +++ /dev/null @@ -1,6 +0,0 @@ -from pathlib import Path - -WORKSPACE_ROOT = Path(__file__).parent.parent.parent -assert WORKSPACE_ROOT / "src" / "agentmsgs" / "utils.py" == Path(__file__), ( - "this file should be located at src/agentmsgs/utils.py" -) diff --git a/tests/test_store_sqlite.py b/tests/test_store_sqlite.py index 19d52e8..b857e43 100644 --- a/tests/test_store_sqlite.py +++ b/tests/test_store_sqlite.py @@ -2,9 +2,16 @@ import pathlib import tempfile import uuid +import pytest + from agentmsgs.stores.sqlite import SQLiteStore +def test_sqlite_store_requires_explicit_path(): + with pytest.raises(TypeError): + SQLiteStore() + + def test_sqlite_persists_across_handles(): with tempfile.TemporaryDirectory() as d: p = pathlib.Path(d) / "test.db"