feat: restore WORKSPACE_ROOT, add fastmcp server (App over SQLiteStore)

This commit is contained in:
lda
2026-09-01 03:04:25 +07:00 Verified
parent 41652c3a16
commit e6071994c7
5 changed files with 1270 additions and 1 deletions
+3 -1
View File
@@ -7,7 +7,9 @@ authors = [
{ name = "lda", email = "[email protected]" } { name = "lda", email = "[email protected]" }
] ]
requires-python = ">=3.14" requires-python = ">=3.14"
dependencies = [] dependencies = [
"fastmcp>=4.0.0",
]
[tool.uv] [tool.uv]
package = true package = true
View File
+102
View File
@@ -0,0 +1,102 @@
from __future__ import annotations
from fastmcp import FastMCP
from agentmsgs.core.app import App
from agentmsgs.stores.sqlite import SQLiteStore
from agentmsgs.utils import WORKSPACE_ROOT
mcp = FastMCP("agentmsgs")
# Shared App — file-backed so opencode ↔ codex share the same DB even as separate processes
_app = App(store=SQLiteStore(WORKSPACE_ROOT / ".agentmsgs.db"))
@mcp.tool
def create_agent(name: str) -> dict:
"""Create or get an ephemeral agent (id is key, name soft-unique)."""
agent = _app.get_or_create_agent(name)
return {"id": str(agent.id), "name": agent.name}
@mcp.tool
def delete_account(agent_id: str) -> dict:
"""Delete an agent by id."""
from uuid import UUID
_app.delete_account(UUID(agent_id))
return {"deleted": agent_id}
@mcp.tool
def create_thread(participant_names: list[str]) -> dict:
"""Create a thread with 2+ agents by name."""
agents = [_app.get_or_create_agent(n) for n in participant_names]
thread = _app.create_thread(*agents)
return {
"id": str(thread.id),
"participants": [
{"id": str(a.id), "name": a.name} for a in thread.participants
],
}
@mcp.tool
def join_thread(thread_id: str, agent_name: str) -> dict:
"""Add an agent to an existing thread."""
from uuid import UUID
agent = _app.get_or_create_agent(agent_name)
thread = _app.join_thread(UUID(thread_id), agent)
return {"id": str(thread.id), "participants": [a.name for a in thread.participants]}
@mcp.tool
def append(thread_id: str, sender_name: str, content: str) -> dict:
"""Append a message to a thread."""
from uuid import UUID
sender = _app.get_or_create_agent(sender_name)
msg = _app.append(UUID(thread_id), sender, content)
return {
"id": str(msg.id),
"seq": msg.seq,
"sender": sender.name,
"content": msg.content,
}
@mcp.tool
def poll(thread_id: str, after_seq: int = 0) -> list[dict]:
"""Poll messages in a thread after seq."""
from uuid import UUID
# poll doesn't need agent for cursor; use raw store list for simplicity
msgs = _app.poll(UUID(thread_id), _app.get_or_create_agent("__poll__"), after_seq)
# filter out the dummy poll agent if it ever sent (it never does)
return [
{
"seq": m.seq,
"sender": m.sender.name,
"content": m.content,
"ts": m.ts.isoformat(),
}
for m in msgs
]
@mcp.tool
def has_unread(thread_id: str, agent_name: str) -> dict:
"""Check if agent has unread messages in thread."""
from uuid import UUID
agent = _app.get_or_create_agent(agent_name)
return {"has_unread": _app.has_unread(UUID(thread_id), agent)}
def main() -> None:
mcp.run()
if __name__ == "__main__":
main()
+6
View File
@@ -0,0 +1,6 @@
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"
)
Generated
+1159
View File
File diff suppressed because it is too large Load Diff