list_tools.py

Code Hygiene Score: 87

Issues 1

Zeile Typ Beschreibung
37 magic_number Magic Number gefunden: 100

Dependencies 10

Funktionen 1

Code

"""Task listing and statistics tools."""
import json
import time

import sys
sys.path.insert(0, "/var/www/mcp-servers/mcp_tasks")
sys.path.insert(0, "/var/www/mcp-servers/shared")

from constants import MS_PER_SECOND

from config import Config
from .base import get_repo, get_task_logger, validate_status, validate_type, log_tool_call


def register_list_tools(mcp):
    """Register task list and statistics tools."""

    repo = get_repo()
    logger = get_task_logger()

    @mcp.tool()
    def tasks_list(
        status: str | None = None,
        type: str | None = None,
        search: str | None = None,
        limit: int = 10,
        offset: int = 0,
        compact: bool = True,
    ) -> dict:
        """
        Listet Tasks mit optionalen Filtern auf.

        Args:
            status: Filter nach Status (pending, in_progress, completed, failed, cancelled)
            type: Filter nach Typ (human_task, ai_task, mixed)
            search: Volltextsuche in Titel/Beschreibung
            limit: Maximale Anzahl Ergebnisse (1-100, default: 10)
            offset: Offset für Pagination
            compact: True = nur id/title/status/type (Token-sparend), False = alle Felder

        Returns:
            Dict mit tasks, total, limit, offset
        """
        start = time.time()
        request_str = json.dumps({"status": status, "type": type, "search": search, "limit": limit})

        try:
            limit = max(1, min(limit, Config.MAX_RESULTS))

            valid, error = validate_status(status)
            if not valid:
                log_tool_call(logger, "tasks_list", request_str, "denied", error_message=error)
                return {"success": False, "error": error}

            valid, error = validate_type(type)
            if not valid:
                log_tool_call(logger, "tasks_list", request_str, "denied", error_message=error)
                return {"success": False, "error": error}

            tasks = repo.find_all(status=status, task_type=type, search=search, limit=limit, offset=offset)
            total = repo.count(status=status, task_type=type, search=search)

            duration = int((time.time() - start) * MS_PER_SECOND)
            log_tool_call(logger, "tasks_list", request_str, "success", duration)

            return {
                "success": True,
                "tasks": [t.to_dict_compact() if compact else t.to_dict() for t in tasks],
                "total": total,
                "limit": limit,
                "offset": offset,
                "compact": compact,
            }

        except Exception as e:
            duration = int((time.time() - start) * MS_PER_SECOND)
            log_tool_call(logger, "tasks_list", request_str, "error", duration, error_message=str(e))
            return {"success": False, "error": str(e)}

    @mcp.tool()
    def tasks_statistics() -> dict:
        """
        Holt Statistiken über alle Tasks.

        Returns:
            Statistiken: Tasks nach Status/Typ, Token-Verbrauch, Modell-Nutzung
        """
        start = time.time()

        try:
            stats = repo.get_statistics()

            duration = int((time.time() - start) * MS_PER_SECOND)
            log_tool_call(logger, "tasks_statistics", "", "success", duration)

            return {"success": True, "statistics": stats}

        except Exception as e:
            duration = int((time.time() - start) * MS_PER_SECOND)
            log_tool_call(logger, "tasks_statistics", "", "error", duration, error_message=str(e))
            return {"success": False, "error": str(e)}
← Übersicht