refactor: require explicit sqlite paths

This commit is contained in:
lda committed 2026-09-24 05:39:30 +07:00 Verified
1 parent aa316d0864
commit 66cdb38408
5 files changed
+20 -18

No files matched your search

+4 -4
View File
@@ -57,16 +57,16 @@ app = App(store=SQLiteStore(Path(".agentmsgs.db")))
### MCP server ### MCP server
Start the MCP server over stdio: Start the MCP server over stdio with an explicit SQLite path:
```bash ```bash
uv run agentmsgs-mcp --transport stdio uv run agentmsgs-mcp --transport stdio --db-path ./agentmsgs.db
``` ```
Or use HTTP: Or use HTTP:
```bash ```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: 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 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: See all server options with:
+8 -6
View File
@@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import argparse import argparse
from pathlib import Path
from fastmcp import FastMCP from fastmcp import FastMCP
from fastmcp.utilities.logging import get_logger 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.core.types import Agent, Message
from agentmsgs.stores.memory import InMemoryStore from agentmsgs.stores.memory import InMemoryStore
from agentmsgs.stores.sqlite import SQLiteStore from agentmsgs.stores.sqlite import SQLiteStore
from agentmsgs.utils import WORKSPACE_ROOT
logger = get_logger(__name__) # WHY logger = get_logger(__name__) # WHY
mcp = FastMCP("agentmsgs") mcp = FastMCP("agentmsgs")
@@ -162,7 +162,7 @@ def main() -> None:
description="Run the AgentMsgs MCP server.", description="Run the AgentMsgs MCP server.",
epilog=( epilog=(
"Examples:\n" "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 --transport stdio --db-path ./shared-agentmsgs.db\n"
" agentmsgs-mcp --in-memory --transport stdio" " agentmsgs-mcp --in-memory --transport stdio"
), ),
@@ -180,11 +180,11 @@ def main() -> None:
parser.add_argument( parser.add_argument(
"--db-path", "--db-path",
type=str, type=Path,
default=str(WORKSPACE_ROOT / ".agentmsgs.db"), default=None,
help=( help=(
"SQLite database path. Used unless --in-memory is set. " "SQLite database path. Required unless --in-memory is set. "
"Defaults to .agentmsgs.db in the workspace root." "The path is used exactly as supplied."
), ),
) )
parser.add_argument( parser.add_argument(
@@ -203,6 +203,8 @@ def main() -> None:
_app = App(store=InMemoryStore()) _app = App(store=InMemoryStore())
logger.info("Using in-memory store for MCP server.") logger.info("Using in-memory store for MCP server.")
else: 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)) _app = App(store=SQLiteStore(args.db_path))
logger.info("Using SQLite store at %s for MCP server.", args.db_path) logger.info("Using SQLite store at %s for MCP server.", args.db_path)
+1 -2
View File
@@ -8,7 +8,6 @@ from contextlib import contextmanager
from pathlib import Path from pathlib import Path
from agentmsgs.core.types import Agent, Message, Thread, ThreadInfo, ThreadReadState from agentmsgs.core.types import Agent, Message, Thread, ThreadInfo, ThreadReadState
from agentmsgs.utils import WORKSPACE_ROOT
_SCHEMA = """ _SCHEMA = """
PRAGMA journal_mode=WAL; 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: class SQLiteStore:
def __init__(self, path: Path = WORKSPACE_ROOT / ".agentmsgs.db") -> None: def __init__(self, path: Path) -> None:
self.path = Path(path) self.path = Path(path)
# ensure parent exists # ensure parent exists
if self.path.parent and not self.path.parent.exists(): if self.path.parent and not self.path.parent.exists():
-6
View File
@@ -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"
)
+7
View File
@@ -2,9 +2,16 @@ import pathlib
import tempfile import tempfile
import uuid import uuid
import pytest
from agentmsgs.stores.sqlite import SQLiteStore from agentmsgs.stores.sqlite import SQLiteStore
def test_sqlite_store_requires_explicit_path():
with pytest.raises(TypeError):
SQLiteStore()
def test_sqlite_persists_across_handles(): def test_sqlite_persists_across_handles():
with tempfile.TemporaryDirectory() as d: with tempfile.TemporaryDirectory() as d:
p = pathlib.Path(d) / "test.db" p = pathlib.Path(d) / "test.db"