Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

4.9 KB
Raw Blame
#pragma once
// ToolRetriever.h — BM25 semantic-ish retrieval over the registered MCP tool catalog.
//
// Background — proven pattern from Anthropic's Tool Search (Nov 2025) and
// the broader "Tool RAG" literature (Red Hat, IBM, RAG-MCP arXiv 2025/26):
// once a tool catalog exceeds ~30-50 tools, sending all definitions to the
// LLM each turn collapses tool-pick accuracy. The fix is on-demand retrieval:
// expose ONE meta-tool ("tool_list") that semantically searches the catalog
// and returns the top-K most-relevant tools as references, then the LLM
// fetches the full schema for the one it wants and invokes it.
//
// This file implements the retrieval. BM25 (Okapi) over a per-tool document
// composed of `name + description + parameter names + parameter descriptions`.
// Pure C++, no embeddings, no Python, no model files. Anthropic's own server-
// side tool_search ships in two flavours — regex and BM25 — and BM25 is the
// one recommended for general natural-language queries.
//
// Index lifecycle: lazy. The retriever pulls tools from McpProvider::list_all_tools()
// and tracks `McpProvider::generation()` so external server starts/stops or
// runtime tool registrations trigger a rebuild on the next search.
//
// Thread safety: search() is callable from any thread (the MCP tool dispatch
// path runs on QtConcurrent worker threads). Internal mutex guards the index.

#include "mcp/McpTypes.h"

#include <QHash>
#include <QMutex>
#include <QSet>
#include <QString>
#include <QStringList>
#include <QVector>

#include <vector>

namespace fincept::mcp {

/// One ranked retrieval result.
struct ToolMatch {
    QString name; // Bare tool name (no server_id prefix)
    QString category;
    QString description; // Truncated to ~200 chars for prompt-friendliness
    bool is_destructive = false;
    double score = 0.0; // BM25 score; higher = more relevant
};

class ToolRetriever {
  public:
    static ToolRetriever& instance();

    /// Search the tool catalog. Returns up to top_k most relevant tools.
    ///
    /// `query` — natural-language string. Tokenised, lowercased, stop-word
    ///           filtered, BM25-scored against per-tool documents.
    /// `top_k` — clamped to [1, 20]; defaults align with Anthropic's 3-5 band.
    /// `category_filter` — when non-empty, only tools in this category are scored.
    ///
    /// Disabled tools are excluded. Result is sorted by descending score.
    /// An empty query returns empty results (caller should fall back).
    std::vector<ToolMatch> search(const QString& query, int top_k = 5, const QString& category_filter = {});

    /// Force a rebuild on the next search. Mostly for tests; production code
    /// can rely on the generation-counter check inside search().
    void invalidate();

    ToolRetriever(const ToolRetriever&) = delete;
    ToolRetriever& operator=(const ToolRetriever&) = delete;

  private:
    ToolRetriever() = default;

    // ── Per-tool document (built once per index rebuild) ────────────────
    struct Doc {
        QString name;
        QString category;
        QString description; // Original full description (returned to caller)
        bool is_destructive = false;

        // Original raw string of the name/description for phrase matching
        QString raw_text;

        // Pre-computed TF: stemmed_token -> frequency count
        QHash<QString, int> term_tf;
        int length = 0; // |D| in BM25 formula

        // Stemmed name-tokens — used by the exact-name-match bonus in search()
        // so a query token that literally appears in the tool's name beats
        // tools that merely mention the term in their description.
        QSet<QString> name_stems;
    };

    // ── Index state — guarded by mutex_ ────────────────────────────────
    mutable QMutex mutex_;
    quint64 indexed_generation_ = static_cast<quint64>(-1); // -1 = never built
    std::vector<Doc> docs_;
    QHash<QString, int> df_; // stemmed term → document frequency
    double avg_doc_length_ = 0.0;

    // Inverted index for O(M) search performance
    QHash<QString, QVector<int>> inverted_index_;

    // All unique stemmed terms in the vocabulary (used for fuzzy matching)
    QSet<QString> vocabulary_;

    // ── BM25 hyperparameters (Okapi defaults — battle-tested) ──────────
    static constexpr double kK1 = 1.5; // term-frequency saturation
    static constexpr double kB = 0.75; // length normalisation

    // Helpers (require mutex_ held).
    void rebuild_index_locked();
    static QStringList tokenise(const QString& text);
    static bool is_stop_word(const QString& token);
    static QString stem(const QString& word);
    static int levenshtein_distance(const QString& s1, const QString& s2);
    static QString classify_category(const QStringList& query_stems);
};

} // namespace fincept::mcp