TextAutoGenerateText::TextAutoGenerateMcpToolsManager Class

class TextAutoGenerateText::TextAutoGenerateMcpToolsManager

Connects to MCP servers and provides their tools to LLM. Connection is created when a server is used (connectServer()), and closed when server is removed, disabled or modified. More...

Header: #include <TextAutoGenerateText/TextAutoGenerateMcpToolsManager>
Inherits: QObject

Public Functions

void addToolCallInProgress(const QByteArray &serverIdentifier, const QByteArray &chatId)
QList<TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool> allTools(const QByteArray &serverIdentifier) const
QByteArray chatIdOfToolCallInProgress(const QByteArray &serverIdentifier) const
void confirmToolCall(const QByteArray &chatId, const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool &tool, const QJsonObject &arguments, QObject *context, const std::function<void (bool)> &callback)
void connectServer(const QByteArray &identifier)
bool needConfirmation(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool &tool) const
void prepareServers(const QList<QByteArray> &serverIdentifiers, QObject *context, const std::function<void ()> &callback, std::chrono::milliseconds timeout = std::chrono::seconds(30))
QString replaceToolNames(const QByteArray &serverIdentifier, const QString &text) const
QList<TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool> selectedTools(const QList<QByteArray> &toolIdentifiers) const
void setConfirmationHandler(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::ConfirmationHandler &handler)
void setElicitationHandler(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::ElicitationHandler &handler)
void setSamplingHandler(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::SamplingHandler &handler)
bool showUrlElicitations(const QByteArray &chatId, const QByteArray &serverIdentifier, const QList<TextAutoGenerateTextMcpProtocolCore::McpProtocolElicitRequestURLParams> &elicitations)
std::optional<TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool> tool(const QByteArray &exposedName) const
QList<TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool> tools(const QByteArray &serverIdentifier) const
QList<QJsonObject> toolsMetaData(const QList<QByteArray> &serverIdentifiers) const

Signals

void statusChanged(const QByteArray &identifier)
void toolsChanged(const QByteArray &identifier)

Static Public Members

QList<TextAutoGenerateTextMcpProtocolCore::McpProtocolRoot> convertToRoots(const QStringList &paths)
bool isToolSelected(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool &tool, const QList<QByteArray> &toolIdentifiers)
QString replaceToolNames(const QString &text, const QList<TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool> &tools)
QByteArray sanitizeName(const QString &name)
QList<QByteArray> serverIdentifiers(const QList<QByteArray> &tools)
QByteArray toolIdentifier(const QByteArray &serverIdentifier)
QByteArray toolIdentifier(const QByteArray &serverIdentifier, const QString &toolName)
QJsonObject toolMetaData(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool &tool)
QString toolName(const QByteArray &toolIdentifier)

Detailed Description

Member Function Documentation

void TextAutoGenerateMcpToolsManager::addToolCallInProgress(const QByteArray &serverIdentifier, const QByteArray &chatId)

Tool of server serverIdentifier is called for chat chatId: requests sent by server while it answers (elicitation, sampling) are associated to this chat. Each call must be removed with removeToolCallInProgress().

QList<TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool> TextAutoGenerateMcpToolsManager::allTools(const QByteArray &serverIdentifier) const

All tools provided by server serverIdentifier, allowed or not.

QByteArray TextAutoGenerateMcpToolsManager::chatIdOfToolCallInProgress(const QByteArray &serverIdentifier) const

Returns chat of tool calls in progress on server serverIdentifier. Requests of server don't refer to a tool call: it's empty when no tool is called or when tools are called for several chats.

void TextAutoGenerateMcpToolsManager::confirmToolCall(const QByteArray &chatId, const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool &tool, const QJsonObject &arguments, QObject *context, const std::function<void (bool)> &callback)

Ask confirmation if needed and call callback with result (true: tool can be called). callback is not called if context is deleted.

void TextAutoGenerateMcpToolsManager::connectServer(const QByteArray &identifier)

Connect to server identifier if it's not done yet, its tools are loaded when connected.

[static] QList<TextAutoGenerateTextMcpProtocolCore::McpProtocolRoot> TextAutoGenerateMcpToolsManager::convertToRoots(const QStringList &paths)

Convert local directories paths to MCP roots (file:// uri, name of directory).

[static] bool TextAutoGenerateMcpToolsManager::isToolSelected(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool &tool, const QList<QByteArray> &toolIdentifiers)

Tool tool is selected by toolIdentifiers.

bool TextAutoGenerateMcpToolsManager::needConfirmation(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool &tool) const

Tool needs a confirmation: handler is defined, tool is not read only and server is not always allowed.

void TextAutoGenerateMcpToolsManager::prepareServers(const QList<QByteArray> &serverIdentifiers, QObject *context, const std::function<void ()> &callback, std::chrono::milliseconds timeout = std::chrono::seconds(30))

Connect to serverIdentifiers and call callback when their tools are loaded (or when connection failed, or after timeout). callback is not called if context is deleted.

[static] QString TextAutoGenerateMcpToolsManager::replaceToolNames(const QString &text, const QList<TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool> &tools)

Replace in text the names of tools by their exposed names: servers refer to their tools by name in descriptions and results, LLM must see the exposed name to call them. Only whole words are replaced, and only names which can't be a common word (they contain '_', '-', a digit or an uppercase letter after first character).

QString TextAutoGenerateMcpToolsManager::replaceToolNames(const QByteArray &serverIdentifier, const QString &text) const

Replace in text the names of tools of server serverIdentifier by their exposed names, see replaceToolNames().

[static] QByteArray TextAutoGenerateMcpToolsManager::sanitizeName(const QString &name)

Convert name to a name accepted by LLM API: [a-zA-Z0-9_-]

QList<TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool> TextAutoGenerateMcpToolsManager::selectedTools(const QList<QByteArray> &toolIdentifiers) const

Tools selected by toolIdentifiers ("mcp:<server>" for all tools of server, "mcp:<server>/<tool>" for one tool).

[static] QList<QByteArray> TextAutoGenerateMcpToolsManager::serverIdentifiers(const QList<QByteArray> &tools)

MCP servers of list of selected tools tools (without duplicate)

void TextAutoGenerateMcpToolsManager::setConfirmationHandler(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::ConfirmationHandler &handler)

Handler used to ask user before calling a tool. Without handler tools are called without confirmation.

void TextAutoGenerateMcpToolsManager::setElicitationHandler(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::ElicitationHandler &handler)

Handler used to answer elicitation requests of MCP servers. Without handler elicitation is not announced to servers. It must be defined before connecting to servers.

void TextAutoGenerateMcpToolsManager::setSamplingHandler(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::SamplingHandler &handler)

Handler used to confirm sampling requests of MCP servers before sending them to LLM returned by plugin provider. Without handler sampling is not announced to servers. It must be defined before connecting to servers.

bool TextAutoGenerateMcpToolsManager::showUrlElicitations(const QByteArray &chatId, const QByteArray &serverIdentifier, const QList<TextAutoGenerateTextMcpProtocolCore::McpProtocolElicitRequestURLParams> &elicitations)

Server serverIdentifier needs that user opens elicitations URLs (e.g. authorization) before a request can succeed. They are shown with elicitation handler, an URL is opened only when user accepts it. Return false when there is no elicitation handler.

[signal] void TextAutoGenerateMcpToolsManager::statusChanged(const QByteArray &identifier)

std::optional<TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool> TextAutoGenerateMcpToolsManager::tool(const QByteArray &exposedName) const

Returns tool named exposedName. LLM can drop server prefix of exposed name (it uses tool names found in descriptions or results of other tools), so a tool whose sanitized name is exposedName is returned when it's unique.

[static] QByteArray TextAutoGenerateMcpToolsManager::toolIdentifier(const QByteArray &serverIdentifier)

Identifier used in list of selected tools for MCP server serverIdentifier ("mcp:<identifier>").

[static] QByteArray TextAutoGenerateMcpToolsManager::toolIdentifier(const QByteArray &serverIdentifier, const QString &toolName)

Identifier used in list of selected tools for tool toolName of MCP server serverIdentifier ("mcp:<identifier>/<tool>").

[static] QJsonObject TextAutoGenerateMcpToolsManager::toolMetaData(const TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool &tool)

Tool tool in LLM format ({"type":"function","function":{...}}).

[static] QString TextAutoGenerateMcpToolsManager::toolName(const QByteArray &toolIdentifier)

Name of tool of toolIdentifier, empty when all tools of server are selected.

QList<TextAutoGenerateText::TextAutoGenerateMcpToolsManager::McpTool> TextAutoGenerateMcpToolsManager::tools(const QByteArray &serverIdentifier) const

Tools of server serverIdentifier which can be used (filtered by allowed tools of server).

[signal] void TextAutoGenerateMcpToolsManager::toolsChanged(const QByteArray &identifier)

QList<QJsonObject> TextAutoGenerateMcpToolsManager::toolsMetaData(const QList<QByteArray> &serverIdentifiers) const

Tools of servers serverIdentifiers in LLM format ({"type":"function","function":{...}}).