add user index

This commit is contained in:
Сергей Маринкевич
2026-06-05 17:19:12 +07:00
parent 70ee7c6b81
commit b83d4f8b9b
6 changed files with 519 additions and 19 deletions
+82 -4
View File
@@ -28,6 +28,7 @@ from typing import Any, Literal, Optional
from fastmcp import FastMCP
import mm_search as mm_search_lib
import mm_users
mcp = FastMCP("mattermost-search")
@@ -61,7 +62,7 @@ def _collect_matches(
regex: bool,
search_mode: SearchMode,
ignore_case: bool,
limit: int,
limit: int = 10,
db: str,
no_index: bool,
) -> list[dict[str, Any]]:
@@ -167,7 +168,7 @@ def mm_search(
regex: bool = False,
search_mode: SearchMode = "message",
ignore_case: bool = None,
limit: int = None,
limit: int = 10,
db: str = None,
no_index: bool = False,
output: Literal["json", "human"] = "json",
@@ -188,7 +189,7 @@ def mm_search(
regex: Treat query as regular expression (message scan only).
search_mode: "message" (default), "file_name", or "file_content".
ignore_case: Case-insensitive search. Default: True for substring, False for regex.
limit: Maximum number of matches to return.
limit: Maximum number of matches to return (default: 10).
db: SQLite index path (default: MM_SEARCH_DB or "mattermost_index.sqlite").
no_index: Do not use sqlite index even if DB exists.
output: "json" returns match objects; "human" returns CLI-style text lines.
@@ -234,9 +235,73 @@ def mm_search(
@mcp.tool
def mm_search_index_status(
def mm_resolve_user(
ref: str,
db: str = None,
) -> dict[str, Any]:
"""Resolve display name, FIO, nickname, or username to Mattermost username.
Args:
ref: User reference — username, first/last name, nickname, or FIO
(e.g. "Цисык", "vladislav.tsisyk").
db: SQLite index path (default: MM_SEARCH_DB or "mattermost_index.sqlite").
"""
resolved_db = db or _env_db()
return mm_users.resolve_user(resolved_db, ref)
@mcp.tool
def mm_search_users(
query: str,
limit: int = 10,
db: str = None,
) -> dict[str, Any]:
"""Search users in the local directory by name or username.
Args:
query: Substring to match against username, first/last name, nickname, full name.
limit: Maximum matches (default: 10).
db: SQLite index path (default: MM_SEARCH_DB or "mattermost_index.sqlite").
"""
resolved_db = db or _env_db()
matches = mm_users.search_users(resolved_db, query, limit=limit)
return {
"query": query,
"count": len(matches),
"db": resolved_db,
"matches": matches,
}
@mcp.tool
def mm_user_directory_refresh(
root: str = None,
db: str = None,
from_api: bool = False,
config_path: str = None,
) -> dict[str, Any]:
"""Rebuild the local user directory from users.json, Mattermost API, and post usernames.
Args:
root: Export root (default: MM_SEARCH_ROOT or "results").
db: SQLite index path (default: MM_SEARCH_DB).
from_api: Also fetch profiles from Mattermost API (needs config.json with token).
config_path: Path to Mattermost config (default: MM_CONFIG_PATH or "config.json").
"""
resolved_root = root or _env_root()
resolved_db = db or _env_db()
return mm_users.refresh_user_directory(
resolved_db,
resolved_root,
config_path=config_path,
from_api=from_api,
)
@mcp.tool
def mm_search_index_status(
db: Optional[str] = None,
root: Optional[str] = None,
) -> dict[str, Any]:
"""Report whether the sqlite index and export root are available."""
resolved_db = db or _env_db()
@@ -248,12 +313,25 @@ def mm_search_index_status(
if root_exists:
json_count = sum(1 for _ in mm_search_lib.iter_result_files(resolved_root))
user_count = 0
if db_exists:
try:
import sqlite3
conn = sqlite3.connect(resolved_db)
mm_users.ensure_users_schema(conn)
user_count = conn.execute("SELECT COUNT(*) FROM users").fetchone()[0]
conn.close()
except sqlite3.Error:
user_count = None
return {
"db": resolved_db,
"db_exists": db_exists,
"root": resolved_root,
"root_exists": root_exists,
"json_files": json_count,
"users": user_count,
"index_required_for": ["file_name", "file_content"],
}