Files
FabledScribe/src/fabledassistant/mcp/tools/search.py
T
bvandeusen fd0431dfb6 feat(mcp): tools/ package + fable_search
Establishes the tool pattern: each tool module exposes register(mcp),
register_all() aggregates them, build_mcp_server() calls register_all.

fable_search mirrors the existing fable-mcp contract (q/content_type/limit
in; {results, total} out) but calls services.embeddings.semantic_search_notes
directly instead of going over HTTP. User comes from mcp.current_user_id().

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 20:18:36 -04:00

50 lines
1.6 KiB
Python

"""fable_search — semantic search across the user's notes and tasks.
Mirrors the existing fable-mcp contract so Claude's prior usage pattern keeps
working. Differences from fable-mcp:
- calls services.embeddings.semantic_search_notes directly instead of HTTP
- user_id comes from mcp.current_user_id() rather than a global API key
"""
from __future__ import annotations
from fabledassistant.mcp._context import current_user_id
from fabledassistant.services.embeddings import semantic_search_notes
async def fable_search(q: str, content_type: str = "all", limit: int = 10) -> dict:
"""Semantic search over the user's notes and tasks.
Args:
q: search query string.
content_type: 'all' (default), 'note' (notes only), or 'task' (tasks only).
limit: maximum number of results (1-50).
Returns:
{"results": [{"id", "title", "body", "is_task", "tags", "similarity"}],
"total": int}
"""
uid = current_user_id()
limit = max(1, min(limit, 50))
is_task = {"note": False, "task": True}.get(content_type) # None => any
raw = await semantic_search_notes(
uid, q, limit=limit, is_task=is_task,
)
return {
"results": [
{
"id": note.id,
"title": note.title,
"body": (note.body or "")[:240],
"is_task": bool(note.is_task),
"tags": list(note.tags or []),
"similarity": float(score),
}
for score, note in raw
],
"total": len(raw),
}
def register(mcp) -> None:
mcp.tool(name="fable_search")(fable_search)