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.
C++ ai-agentsalgorithmic-tradingbloomberg-terminalcppfinancefinancial-marketsfintechgood-first-issueinvestmentinvestment-researchmachine-learningopensourcepythonqtquantitative-financestock-markettrading
v4.4.1
tilakpatel22935f8f6784ca
diff --git a/fincept-qt/CMakeLists.txt b/fincept-qt/CMakeLists.txtindex 3509c26f9..fa3c54261 100644--- a/fincept-qt/CMakeLists.txt+++ b/fincept-qt/CMakeLists.txt@@ -1082,6 +1082,8 @@ set(MCP_SOURCESsrc/mcp/SchemaValidator.cppsrc/mcp/ToolRetriever.cppsrc/mcp/ToolSelfTest.cpp+ src/mcp/GeminiSchema.cpp+ src/mcp/ProviderToolFormatSelfTest.cppsrc/mcp/TerminalMcpBridge.cppsrc/mcp/JobRegistry.cppsrc/mcp/ResultStore.cpp@@ -2661,6 +2663,8 @@ set_source_files_properties(src/mcp/SchemaValidator.cppsrc/mcp/ToolRetriever.cppsrc/mcp/ToolSelfTest.cpp+ src/mcp/GeminiSchema.cpp+ src/mcp/ProviderToolFormatSelfTest.cppsrc/mcp/TerminalMcpBridge.cppsrc/mcp/tools/NavigationTools.cppsrc/mcp/tools/MarketsTools.cppdiff --git a/fincept-qt/src/app/main.cpp b/fincept-qt/src/app/main.cppindex 4d19d1eef..65d1faeab 100644--- a/fincept-qt/src/app/main.cpp+++ b/fincept-qt/src/app/main.cpp@@ -30,6 +30,7 @@#include "datahub/DataHubMetaTypes.h"#include "datahub/TopicPolicy.h"#include "mcp/McpInit.h"+#include "mcp/ProviderToolFormatSelfTest.h"#include "mcp/ToolSelfTest.h"#include "network/http/HttpClient.h"#include "python/OptionGreeksWorker.h"@@ -1042,7 +1043,8 @@ int main(int argc, char* argv[]) {{bool tools_needed_synchronously = false;for (int i = 1; i < argc; ++i) {- if (qstrcmp(argv[i], "--selftest-tools") == 0 || qstrcmp(argv[i], "--dump-tools") == 0)+ if (qstrcmp(argv[i], "--selftest-tools") == 0 || qstrcmp(argv[i], "--selftest-llm-tools") == 0 ||+ qstrcmp(argv[i], "--dump-tools") == 0)tools_needed_synchronously = true;}if (tools_needed_synchronously)@@ -1073,6 +1075,7 @@ int main(int argc, char* argv[]) {};static constexpr SelftestSuite kSelftestSuites[] = {{"--selftest-tools", &fincept::mcp::run_tool_selftest},+ {"--selftest-llm-tools", &fincept::mcp::run_provider_tool_format_selftest},{"--selftest-feeds", &fincept::feeds::run_feed_selftest},{"--selftest-dock-layout", &fincept::layout::run_dock_layout_selftest},{"--selftest-live-table", &fincept::ui::run_live_table_selftest},diff --git a/fincept-qt/src/mcp/GeminiSchema.cpp b/fincept-qt/src/mcp/GeminiSchema.cppnew file mode 100644index 000000000..ab64154b3--- /dev/null+++ b/fincept-qt/src/mcp/GeminiSchema.cpp@@ -0,0 +1,223 @@+// GeminiSchema.cpp — see GeminiSchema.h.++#include "mcp/GeminiSchema.h"++#include <QJsonArray>+#include <QJsonValue>+#include <QRegularExpression>+#include <QSet>+#include <QString>++namespace fincept::mcp {++namespace {++// Field set of Gemini's `Schema` message. Anything outside this list is+// dropped rather than forwarded — the API's JSON parser rejects unknown names+// outright, and a dropped constraint only costs the model a hint whereas a+// forwarded one costs the entire request.+//+// Deliberately absent (and present in real JSON Schema): additionalProperties,+// $schema, $id, $ref, $defs, definitions, oneOf, allOf, not, const,+// exclusiveMinimum, exclusiveMaximum, multipleOf, uniqueItems, examples,+// contains, patternProperties, dependentRequired.+const QSet<QString>& gemini_allowed_schema_keys() {+ static const QSet<QString> kKeys = {+ QStringLiteral("type"), QStringLiteral("format"), QStringLiteral("title"),+ QStringLiteral("description"), QStringLiteral("nullable"), QStringLiteral("enum"),+ QStringLiteral("items"), QStringLiteral("properties"), QStringLiteral("required"),+ QStringLiteral("minItems"), QStringLiteral("maxItems"), QStringLiteral("minProperties"),+ QStringLiteral("maxProperties"), QStringLiteral("minLength"), QStringLiteral("maxLength"),+ QStringLiteral("pattern"), QStringLiteral("minimum"), QStringLiteral("maximum"),+ QStringLiteral("default"), QStringLiteral("anyOf"), QStringLiteral("example"),+ QStringLiteral("propertyOrdering"),+ };+ return kKeys;+}++// Gemini's Type enum, lower-cased. JSON Schema's "null" has no Type member —+// it is expressed as `nullable: true`, so a bare "null" type is not usable.+QString gemini_normalise_type(const QJsonValue& raw) {+ // A type UNION (`["string","null"]`) is legal JSON Schema and illegal here.+ // Take the first non-null member; nullability is carried separately.+ QString t;+ if (raw.isArray()) {+ for (const auto& v : raw.toArray()) {+ const QString s = v.toString().toLower();+ if (!s.isEmpty() && s != QLatin1String("null")) {+ t = s;+ break;+ }+ }+ } else {+ t = raw.toString().toLower();+ }+ static const QSet<QString> kTypes = {+ QStringLiteral("string"), QStringLiteral("number"), QStringLiteral("integer"),+ QStringLiteral("boolean"), QStringLiteral("array"), QStringLiteral("object"),+ };+ return kTypes.contains(t) ? t : QString();+}++// `format` is validated against a small per-type vocabulary; an unrecognised+// value ("uri", "email", "uuid", …) is a hard 400. Keep only what Gemini names.+bool gemini_format_is_valid(const QString& type, const QString& format) {+ if (type == QLatin1String("string")) {+ static const QSet<QString> kStr = {QStringLiteral("date-time"), QStringLiteral("date"),+ QStringLiteral("time"), QStringLiteral("duration"),+ QStringLiteral("enum")};+ return kStr.contains(format);+ }+ if (type == QLatin1String("integer")) {+ return format == QLatin1String("int32") || format == QLatin1String("int64");+ }+ if (type == QLatin1String("number")) {+ return format == QLatin1String("float") || format == QLatin1String("double");+ }+ return false;+}++std::optional<QJsonObject> gemini_sanitize(const QJsonObject& in, int depth);++// Sanitise the schema for one property. Returns nullopt when the property must+// be dropped (an object that ends up with no properties of its own).+std::optional<QJsonObject> gemini_sanitize_child(const QJsonValue& v, int depth) {+ if (!v.isObject())+ return std::nullopt;+ return gemini_sanitize(v.toObject(), depth);+}++std::optional<QJsonObject> gemini_sanitize(const QJsonObject& in, int depth) {+ // Guard against a self-referential schema from an external MCP server.+ // Gemini's own nesting ceiling is far below this.+ if (depth > 12)+ return std::nullopt;++ QJsonObject out;++ QString type = gemini_normalise_type(in.value(QStringLiteral("type")));+ // No usable type: infer object when it declares properties, else string —+ // a typeless leaf is more likely a free-text field than a broken object.+ if (type.isEmpty())+ type = in.contains(QStringLiteral("properties")) ? QStringLiteral("object") : QStringLiteral("string");+ out[QStringLiteral("type")] = type;++ // A `["string","null"]` union or an explicit nullable flag both map onto+ // Gemini's dedicated `nullable` field.+ const QJsonValue raw_type = in.value(QStringLiteral("type"));+ if (raw_type.isArray()) {+ for (const auto& v : raw_type.toArray()) {+ if (v.toString().toLower() == QLatin1String("null")) {+ out[QStringLiteral("nullable")] = true;+ break;+ }+ }+ }++ for (auto it = in.constBegin(); it != in.constEnd(); ++it) {+ const QString key = it.key();+ if (key == QLatin1String("type"))+ continue; // handled above+ if (!gemini_allowed_schema_keys().contains(key))+ continue;++ if (key == QLatin1String("format")) {+ const QString f = it.value().toString();+ if (gemini_format_is_valid(type, f))+ out[key] = f;+ continue;+ }++ if (key == QLatin1String("enum")) {+ // Gemini only accepts an enum on a STRING schema, and every member+ // must be a string. Numeric enums are expressed via minimum/maximum+ // instead; forwarding one is a 400.+ if (type != QLatin1String("string"))+ continue;+ QJsonArray vals;+ for (const auto& v : it.value().toArray()) {+ if (v.isString())+ vals.append(v.toString());+ }+ if (!vals.isEmpty())+ out[key] = vals;+ continue;+ }++ if (key == QLatin1String("items")) {+ if (auto child = gemini_sanitize_child(it.value(), depth + 1))+ out[key] = *child;+ continue;+ }++ if (key == QLatin1String("anyOf")) {+ QJsonArray variants;+ for (const auto& v : it.value().toArray()) {+ if (auto child = gemini_sanitize_child(v, depth + 1))+ variants.append(*child);+ }+ if (!variants.isEmpty())+ out[key] = variants;+ continue;+ }++ if (key == QLatin1String("properties")) {+ QJsonObject props;+ const QJsonObject src = it.value().toObject();+ for (auto pit = src.constBegin(); pit != src.constEnd(); ++pit) {+ if (auto child = gemini_sanitize_child(pit.value(), depth + 1))+ props[pit.key()] = *child;+ }+ if (!props.isEmpty())+ out[key] = props;+ continue;+ }++ if (key == QLatin1String("required"))+ continue; // filtered below, once `properties` is final++ out[key] = it.value();+ }++ if (type == QLatin1String("array") && !out.contains(QStringLiteral("items"))) {+ // ARRAY without `items` is rejected. A string element is the least+ // surprising default and keeps the surrounding declaration usable.+ out[QStringLiteral("items")] = QJsonObject{{QStringLiteral("type"), QStringLiteral("string")}};+ }++ if (type == QLatin1String("object")) {+ const QJsonObject props = out.value(QStringLiteral("properties")).toObject();+ // An OBJECT with no properties is rejected outright ("should be+ // non-empty for OBJECT type"). Signal "drop me" to the caller.+ if (props.isEmpty())+ return std::nullopt;++ // `required` may only name properties that survived sanitisation —+ // a required-but-absent name is itself a validation error.+ QJsonArray req;+ for (const auto& r : in.value(QStringLiteral("required")).toArray()) {+ const QString rn = r.toString();+ if (props.contains(rn))+ req.append(rn);+ }+ if (!req.isEmpty())+ out[QStringLiteral("required")] = req;+ }++ return out;+}++} // namespace++std::optional<QJsonObject> sanitize_schema_for_gemini(const QJsonObject& schema) {+ if (schema.isEmpty())+ return std::nullopt;+ return gemini_sanitize(schema, 0);+}++bool is_valid_gemini_function_name(const QString& name) {+ static const QRegularExpression rx(QStringLiteral("^[a-zA-Z_][a-zA-Z0-9_.:-]{0,63}$"));+ return rx.match(name).hasMatch();+}++} // namespace fincept::mcpdiff --git a/fincept-qt/src/mcp/GeminiSchema.h b/fincept-qt/src/mcp/GeminiSchema.hnew file mode 100644index 000000000..142078264--- /dev/null+++ b/fincept-qt/src/mcp/GeminiSchema.h@@ -0,0 +1,51 @@+#pragma once+// GeminiSchema.h — translate a JSON-Schema tool parameter object into the+// OpenAPI-3.0 subset that Gemini's `FunctionDeclaration.parameters` accepts.+//+// Gemini does NOT take arbitrary JSON Schema. Its `Schema` message is a fixed+// field set, and the JSON parser in front of it is STRICT: one unrecognised+// key anywhere in the tree fails the WHOLE generateContent request with+//+// Invalid JSON payload received. Unknown name "additionalProperties" at+// 'tools[0].function_declarations[0].parameters'+//+// — which reads to the user as "tools don't work with Gemini" even though+// every other provider accepts the identical schema. The same is true of an+// OBJECT schema with an empty `properties` map:+//+// GenerateContentRequest.tools[0].function_declarations[0].parameters.properties:+// should be non-empty for OBJECT type+//+// …so a parameterless tool must omit `parameters` entirely rather than send+// `{"type":"object","properties":{}}` (what every OpenAI-shaped builder emits).+//+// Both failures are all-or-nothing: one bad declaration in the array takes the+// whole request down, so this translation is not best-effort polish — it is+// the difference between all tools working and none.++#include <QJsonObject>+#include <QString>++#include <optional>++namespace fincept::mcp {++/// Rewrite `schema` recursively, keeping only keys Gemini's Schema message+/// defines and normalising the values it is picky about (type casing, `format`+/// vocabulary, `enum` on non-string types).+///+/// Returns std::nullopt when the result carries nothing Gemini can accept —+/// an object with no usable properties. The caller MUST then omit the field+/// this schema belongs to: `parameters` at the top level, or the property+/// itself when nested.+std::optional<QJsonObject> sanitize_schema_for_gemini(const QJsonObject& schema);++/// Gemini's `FunctionDeclaration.name` constraint: must start with a letter or+/// underscore, may contain letters, digits, underscores, dots, dashes and+/// colons, and is capped at 64 characters. Internal tool names fit today+/// (longest is 56 including the `fincept-terminal__` prefix), but an external+/// MCP server with a long id can exceed it, and Gemini rejects the entire+/// request when it does.+bool is_valid_gemini_function_name(const QString& name);++} // namespace fincept::mcpdiff --git a/fincept-qt/src/mcp/McpService.cpp b/fincept-qt/src/mcp/McpService.cppindex 5556c7115..0a7479d65 100644--- a/fincept-qt/src/mcp/McpService.cpp+++ b/fincept-qt/src/mcp/McpService.cpp@@ -4,6 +4,7 @@#include "core/config/AppConfig.h"#include "core/logging/Logger.h"+#include "mcp/GeminiSchema.h"#include "mcp/McpManager.h"#include "mcp/McpProvider.h"#include "mcp/TerminalMcpBridge.h"@@ -24,6 +25,16 @@ namespace fincept::mcp {static constexpr const char* TAG = "McpService";+// Per-request declaration ceilings — a backstop, not the primary limit.+//+// kHardMaxTools (50, below) already bounds the non-Tier-0 path, and a Tier-0+// turn ships ~12. These exist so that a caller who raises ToolFilter::max_tools+// can't silently walk a provider past a hard API limit: Gemini rejects a+// request carrying more than 128 function declarations, and does so without+// naming the offender, which is indistinguishable from "tools are broken".+static constexpr int kGeminiMaxToolsPerRequest = 128;+static constexpr int kMaxToolsPerRequest = 256;+McpService& McpService::instance() {static McpService s;return s;@@ -297,8 +308,9 @@ QJsonArray McpService::format_tools_for_openai(const ToolFilter& filter) {return format_tools_for_openai(filter, QSet<QString>{});}-QJsonArray McpService::format_tools_for_openai(const ToolFilter& filter, const QSet<QString>& extra_tool_names) {- QMutexLocker lock(&mutex_);+std::vector<UnifiedTool> McpService::select_tools_for_llm_locked(const ToolFilter& filter,+ const QSet<QString>& extra_tool_names,+ QByteArray* out_key, bool* out_used_rag) {cached_tools_locked();// ── Tool RAG Tier-0 mode ──@@ -327,15 +339,6 @@ QJsonArray McpService::format_tools_for_openai(const ToolFilter& filter, const Qkey = filter_signature(filter);}- auto cached = openai_format_cache_.constFind(key);- if (cached != openai_format_cache_.constEnd()) {- LOG_INFO(TAG, QString("format_tools_for_openai: %1 tools sent to LLM (cached, %2)")- .arg(cached.value().size())- .arg(use_rag ? "tier-0" : "filtered"));- return cached.value();- }-- const int total_seen = static_cast<int>(cached_tools_.size());std::vector<UnifiedTool> tools;if (use_rag) {const auto& tier0 = tier_0_tool_names();@@ -347,6 +350,48 @@ QJsonArray McpService::format_tools_for_openai(const ToolFilter& filter, const Qtools = apply_tool_filter(cached_tools_, filter);}+ if (out_key)+ *out_key = key;+ if (out_used_rag)+ *out_used_rag = use_rag;+ return tools;+}++// Bound any provider's tool array. In a Tier-0 turn this never binds (12-ish+// declarations); it only matters when Tool RAG is switched off, where the raw+// catalogue is ~900 tools — past every provider's declaration ceiling and+// enough prompt to crowd out the conversation. Dropping silently would read as+// "the model can't see my tool", so overflow is always logged.+static std::vector<UnifiedTool> cap_tool_list(std::vector<UnifiedTool> tools, int cap, const char* dialect) {+ if (cap <= 0 || static_cast<int>(tools.size()) <= cap)+ return tools;+ LOG_WARN(TAG, QString("%1: %2 tools selected but the dialect accepts at most %3 — sending the first %3. "+ "Enable Tool RAG (mcp/use_tool_rag) or pass a narrower ToolFilter.")+ .arg(QString::fromUtf8(dialect))+ .arg(tools.size())+ .arg(cap));+ tools.resize(static_cast<std::size_t>(cap));+ return tools;+}++QJsonArray McpService::format_tools_for_openai(const ToolFilter& filter, const QSet<QString>& extra_tool_names) {+ QMutexLocker lock(&mutex_);++ QByteArray key;+ bool use_rag = false;+ std::vector<UnifiedTool> tools = select_tools_for_llm_locked(filter, extra_tool_names, &key, &use_rag);++ auto cached = openai_format_cache_.constFind(key);+ if (cached != openai_format_cache_.constEnd()) {+ LOG_INFO(TAG, QString("format_tools_for_openai: %1 tools sent to LLM (cached, %2)")+ .arg(cached.value().size())+ .arg(use_rag ? "tier-0" : "filtered"));+ return cached.value();+ }++ const int total_seen = static_cast<int>(cached_tools_.size());+ tools = cap_tool_list(std::move(tools), kMaxToolsPerRequest, "openai");+QJsonArray result;for (const auto& tool : tools) {// Encode tool name for the wire so dotted internal names like@@ -399,6 +444,101 @@ QJsonArray McpService::format_tools_for_openai(const ToolFilter& filter, const Qreturn result;}+QJsonArray McpService::format_tools_for_anthropic(const ToolFilter& filter, const QSet<QString>& extra_tool_names) {+ QMutexLocker lock(&mutex_);++ QByteArray key;+ bool use_rag = false;+ std::vector<UnifiedTool> tools = select_tools_for_llm_locked(filter, extra_tool_names, &key, &use_rag);++ auto cached = anthropic_format_cache_.constFind(key);+ if (cached != anthropic_format_cache_.constEnd())+ return cached.value();++ tools = cap_tool_list(std::move(tools), kMaxToolsPerRequest, "anthropic");++ QJsonArray result;+ for (const auto& tool : tools) {+ QJsonObject schema = tool.input_schema;+ // Anthropic requires input_schema to be a JSON Schema object. A+ // parameterless tool is legal here (unlike Gemini) as long as the+ // object is well-formed.+ if (schema.value(QStringLiteral("type")).toString() != QLatin1String("object")) {+ schema[QStringLiteral("type")] = QStringLiteral("object");+ }+ if (!schema.contains(QStringLiteral("properties")))+ schema[QStringLiteral("properties")] = QJsonObject();++ result.append(QJsonObject{+ {QStringLiteral("name"), tool.server_id + QStringLiteral("__") + McpProvider::encode_tool_name_for_wire(tool.name)},+ {QStringLiteral("description"), tool.description},+ {QStringLiteral("input_schema"), schema},+ });+ }++ if (anthropic_format_cache_.size() >= kMaxFormatCacheEntries)+ anthropic_format_cache_.clear();+ anthropic_format_cache_.insert(key, result);++ LOG_INFO(TAG, QString("format_tools_for_anthropic: %1 tools sent to LLM (%2)")+ .arg(result.size())+ .arg(use_rag ? "tier-0" : "filtered"));+ return result;+}++QJsonArray McpService::format_tools_for_gemini(const ToolFilter& filter, const QSet<QString>& extra_tool_names) {+ QMutexLocker lock(&mutex_);++ QByteArray key;+ bool use_rag = false;+ std::vector<UnifiedTool> tools = select_tools_for_llm_locked(filter, extra_tool_names, &key, &use_rag);++ auto cached = gemini_format_cache_.constFind(key);+ if (cached != gemini_format_cache_.constEnd())+ return cached.value();++ tools = cap_tool_list(std::move(tools), kGeminiMaxToolsPerRequest, "gemini");++ QJsonArray fn_decls;+ int dropped_names = 0;+ for (const auto& tool : tools) {+ const QString fn_name =+ tool.server_id + QStringLiteral("__") + McpProvider::encode_tool_name_for_wire(tool.name);+ if (!is_valid_gemini_function_name(fn_name)) {+ // One invalid name fails the entire generateContent call, taking+ // every other tool down with it. Drop this one instead.+ ++dropped_names;+ LOG_WARN(TAG, QString("format_tools_for_gemini: dropping '%1' — not a valid Gemini function name "+ "(must match ^[a-zA-Z_][a-zA-Z0-9_.:-]{0,63}$)")+ .arg(fn_name));+ continue;+ }++ QJsonObject decl{{QStringLiteral("name"), fn_name}, {QStringLiteral("description"), tool.description}};+ // `parameters` is OMITTED for a parameterless tool. Sending+ // {"type":"object","properties":{}} — which is what the OpenAI-shaped+ // builders emit — is rejected with "properties: should be non-empty+ // for OBJECT type" and fails the whole request.+ if (auto params = sanitize_schema_for_gemini(tool.input_schema))+ decl[QStringLiteral("parameters")] = *params;+ fn_decls.append(decl);+ }++ QJsonArray result;+ if (!fn_decls.isEmpty())+ result.append(QJsonObject{{QStringLiteral("functionDeclarations"), fn_decls}});++ if (gemini_format_cache_.size() >= kMaxFormatCacheEntries)+ gemini_format_cache_.clear();+ gemini_format_cache_.insert(key, result);++ LOG_INFO(TAG, QString("format_tools_for_gemini: %1 function declarations sent to LLM (%2%3)")+ .arg(fn_decls.size())+ .arg(use_rag ? "tier-0" : "filtered")+ .arg(dropped_names > 0 ? QString(", %1 dropped on name").arg(dropped_names) : QString()));+ return result;+}+std::size_t McpService::tool_count() {return get_all_tools().size();}@@ -583,6 +723,8 @@ void McpService::refresh_cache() {// <200 KB, so simple full-clear is fine.filtered_tools_cache_.clear();openai_format_cache_.clear();+ anthropic_format_cache_.clear();+ gemini_format_cache_.clear();cache_time_ = QDateTime::currentDateTime();cached_generation_ = McpProvider::instance().generation();diff --git a/fincept-qt/src/mcp/McpService.h b/fincept-qt/src/mcp/McpService.hindex deb80df5f..c1b0f64bb 100644--- a/fincept-qt/src/mcp/McpService.h+++ b/fincept-qt/src/mcp/McpService.h@@ -44,6 +44,24 @@ class McpService {/// contains them). See the tool loop / note_tool_activations.QJsonArray format_tools_for_openai(const ToolFilter& filter, const QSet<QString>& extra_tool_names);+ /// Format for Anthropic `/v1/messages` — `[{name, description, input_schema}]`,+ /// no OpenAI-style `{"type":"function"}` wrapper.+ ///+ /// Shares the Tier-0 / filter / activation selection with+ /// format_tools_for_openai. It has to: the catalogue is ~900 tools, and the+ /// Anthropic and Gemini builders used to bypass Tool RAG and send ALL of+ /// them on every turn, which is why tool calling worked on the OpenAI path+ /// and nowhere else.+ QJsonArray format_tools_for_anthropic(const ToolFilter& filter, const QSet<QString>& extra_tool_names = {});++ /// Format for Gemini `generateContent` —+ /// `[{functionDeclarations:[{name, description, parameters}]}]`.+ ///+ /// Parameters are translated into Gemini's OpenAPI subset (see+ /// mcp/GeminiSchema.h); declarations whose name or schema cannot be made+ /// valid are dropped rather than allowed to fail the whole request.+ QJsonArray format_tools_for_gemini(const ToolFilter& filter, const QSet<QString>& extra_tool_names = {});+std::size_t tool_count();// ── Unified Tool Execution ──────────────────────────────────────────@@ -105,6 +123,8 @@ class McpService {// ~150 KB OpenAI JSON schema on every LLM turn.QHash<QByteArray, std::vector<UnifiedTool>> filtered_tools_cache_;QHash<QByteArray, QJsonArray> openai_format_cache_;+ QHash<QByteArray, QJsonArray> anthropic_format_cache_;+ QHash<QByteArray, QJsonArray> gemini_format_cache_;// In Tool RAG mode the format-cache key encodes the activated-tool set, so// it varies per round and the map would otherwise grow unbounded across a// session. Cleared wholesale on overflow — it is a memo, not a store.@@ -118,6 +138,14 @@ class McpService {// format_tools_for_openai(filter) reuse the cache without re-locking.const std::vector<UnifiedTool>& cached_tools_locked();+ /// Tool selection shared by every provider formatter: applies Tier-0 (Tool+ /// RAG) mode plus the activated-tool set when the caller passed a default+ /// filter, and the explicit ToolFilter otherwise. Caller must hold mutex_.+ /// `out_key` receives the cache key the caller should memo under.+ std::vector<UnifiedTool> select_tools_for_llm_locked(const ToolFilter& filter,+ const QSet<QString>& extra_tool_names, QByteArray* out_key,+ bool* out_used_rag);+// Stable byte signature of a ToolFilter — usable as a QHash key.static QByteArray filter_signature(const ToolFilter& filter);};diff --git a/fincept-qt/src/mcp/ProviderToolFormatSelfTest.cpp b/fincept-qt/src/mcp/ProviderToolFormatSelfTest.cppnew file mode 100644index 000000000..375dd373a--- /dev/null+++ b/fincept-qt/src/mcp/ProviderToolFormatSelfTest.cpp@@ -0,0 +1,357 @@+// ProviderToolFormatSelfTest.cpp — see ProviderToolFormatSelfTest.h.++#include "mcp/ProviderToolFormatSelfTest.h"++#include "mcp/GeminiSchema.h"+#include "mcp/McpService.h"+#include "services/llm/ProviderCatalog.h"++#include <QJsonArray>+#include <QJsonDocument>+#include <QJsonObject>+#include <QRegularExpression>+#include <QSet>+#include <QString>+#include <QStringList>++#include <cstdio>++namespace fincept::mcp {+namespace {++void ptf_out(const QString& line) {+ std::printf("%s\n", line.toUtf8().constData());+ std::fflush(stdout);+}++// Shared by OpenAI and Anthropic: both document the function/tool name as+// ^[a-zA-Z0-9_-]{1,64}$.+const QRegularExpression& ptf_openai_name_rx() {+ static const QRegularExpression rx(QStringLiteral("^[a-zA-Z0-9_-]{1,64}$"));+ return rx;+}++// Keys Gemini's Schema message defines. Kept independent of the whitelist in+// GeminiSchema.cpp on purpose: if the two disagree, the test should fail+// rather than agree with the implementation it is checking.+const QSet<QString>& ptf_gemini_schema_keys() {+ static const QSet<QString> k = {+ QStringLiteral("type"), QStringLiteral("format"), QStringLiteral("title"),+ QStringLiteral("description"), QStringLiteral("nullable"), QStringLiteral("enum"),+ QStringLiteral("items"), QStringLiteral("properties"), QStringLiteral("required"),+ QStringLiteral("minItems"), QStringLiteral("maxItems"), QStringLiteral("minProperties"),+ QStringLiteral("maxProperties"), QStringLiteral("minLength"), QStringLiteral("maxLength"),+ QStringLiteral("pattern"), QStringLiteral("minimum"), QStringLiteral("maximum"),+ QStringLiteral("default"), QStringLiteral("anyOf"), QStringLiteral("example"),+ QStringLiteral("propertyOrdering"),+ };+ return k;+}++const QSet<QString>& ptf_gemini_types() {+ static const QSet<QString> k = {+ QStringLiteral("string"), QStringLiteral("number"), QStringLiteral("integer"),+ QStringLiteral("boolean"), QStringLiteral("array"), QStringLiteral("object"),+ };+ return k;+}++// Walk a sanitised Gemini schema and record every way the API would reject it.+void ptf_check_gemini_schema(const QJsonObject& schema, const QString& path, QStringList* errs) {+ for (auto it = schema.constBegin(); it != schema.constEnd(); ++it) {+ if (!ptf_gemini_schema_keys().contains(it.key()))+ *errs << QStringLiteral("%1: unsupported key '%2'").arg(path, it.key());+ }++ const QString type = schema.value(QStringLiteral("type")).toString();+ if (type.isEmpty())+ *errs << QStringLiteral("%1: missing 'type'").arg(path);+ else if (!ptf_gemini_types().contains(type))+ *errs << QStringLiteral("%1: type '%2' is not a Gemini Type").arg(path, type);++ if (schema.contains(QStringLiteral("enum")) && type != QLatin1String("string"))+ *errs << QStringLiteral("%1: 'enum' is only valid on a STRING schema (got '%2')").arg(path, type);++ if (type == QLatin1String("object")) {+ const QJsonObject props = schema.value(QStringLiteral("properties")).toObject();+ // The failure that broke Gemini tool calling outright: an OBJECT with+ // an empty properties map is rejected with "should be non-empty for+ // OBJECT type", and it takes the whole request with it.+ if (props.isEmpty()) {+ *errs << QStringLiteral("%1: OBJECT with empty 'properties' — Gemini rejects the entire request").arg(path);+ }+ for (const auto& r : schema.value(QStringLiteral("required")).toArray()) {+ if (!props.contains(r.toString()))+ *errs << QStringLiteral("%1: required '%2' is not a declared property").arg(path, r.toString());+ }+ for (auto pit = props.constBegin(); pit != props.constEnd(); ++pit) {+ ptf_check_gemini_schema(pit.value().toObject(), path + QStringLiteral(".") + pit.key(), errs);+ }+ }++ if (type == QLatin1String("array")) {+ if (!schema.contains(QStringLiteral("items")))+ *errs << QStringLiteral("%1: ARRAY without 'items'").arg(path);+ else+ ptf_check_gemini_schema(schema.value(QStringLiteral("items")).toObject(),+ path + QStringLiteral("[]"), errs);+ }+}++void ptf_report(const QString& label, const QStringList& errs, bool* failed) {+ if (errs.isEmpty()) {+ ptf_out(QStringLiteral(" OK ") + label);+ return;+ }+ *failed = true;+ ptf_out(QStringLiteral(" FAIL ") + label + QStringLiteral(": ") + QString::number(errs.size()));+ int shown = 0;+ for (const auto& e : errs) {+ ptf_out(QStringLiteral(" - ") + e);+ if (++shown >= 20) {+ ptf_out(QStringLiteral(" … and %1 more").arg(errs.size() - shown));+ break;+ }+ }+}++} // namespace++int run_provider_tool_format_selftest() {+ ptf_out(QStringLiteral("\n=============================================================="));+ ptf_out(QStringLiteral(" LLM PROVIDER TOOL-FORMAT SELF-TEST"));+ ptf_out(QStringLiteral("=============================================================="));++ bool failed = false;+ auto& svc = McpService::instance();+ const ToolFilter default_filter;++ // Every dialect is exercised twice: once as the opening turn (no activated+ // tools) and once mid-turn with a discovered tool fed back, because the+ // activation path has its own cache key and its own history of drift.+ const QSet<QString> activated = {QStringLiteral("create_portfolio"), QStringLiteral("get_quote")};++ // ── OpenAI ──────────────────────────────────────────────────────────+ {+ const QJsonArray tools = svc.format_tools_for_openai(default_filter, activated);+ ptf_out(QStringLiteral("\n[1] OPENAI — %1 function declarations").arg(tools.size()));++ QStringList errs;+ if (tools.isEmpty())+ errs << QStringLiteral("empty tool array — the model would have no tools at all");+ QSet<QString> seen;+ for (const auto& v : tools) {+ const QJsonObject e = v.toObject();+ if (e.value(QStringLiteral("type")).toString() != QLatin1String("function"))+ errs << QStringLiteral("entry missing type=function");+ const QJsonObject fn = e.value(QStringLiteral("function")).toObject();+ const QString name = fn.value(QStringLiteral("name")).toString();+ if (!ptf_openai_name_rx().match(name).hasMatch())+ errs << QStringLiteral("'%1' violates ^[a-zA-Z0-9_-]{1,64}$").arg(name);+ if (seen.contains(name))+ errs << QStringLiteral("duplicate function name '%1'").arg(name);+ seen.insert(name);+ if (fn.value(QStringLiteral("description")).toString().trimmed().isEmpty())+ errs << QStringLiteral("'%1' has no description").arg(name);+ if (!fn.contains(QStringLiteral("parameters")))+ errs << QStringLiteral("'%1' has no parameters object").arg(name);+ }+ ptf_report(QStringLiteral("openai payload"), errs, &failed);+ }++ // ── Anthropic ───────────────────────────────────────────────────────+ {+ const QJsonArray tools = svc.format_tools_for_anthropic(default_filter, activated);+ ptf_out(QStringLiteral("\n[2] ANTHROPIC — %1 tools").arg(tools.size()));++ QStringList errs;+ if (tools.isEmpty())+ errs << QStringLiteral("empty tool array — the model would have no tools at all");+ for (const auto& v : tools) {+ const QJsonObject t = v.toObject();+ const QString name = t.value(QStringLiteral("name")).toString();+ if (!ptf_openai_name_rx().match(name).hasMatch())+ errs << QStringLiteral("'%1' violates ^[a-zA-Z0-9_-]{1,64}$").arg(name);+ // Anthropic takes a bare tool object — an OpenAI-style+ // {"type":"function","function":{…}} wrapper is a 400.+ if (t.contains(QStringLiteral("type")) || t.contains(QStringLiteral("function")))+ errs << QStringLiteral("'%1' carries an OpenAI-style wrapper").arg(name);+ const QJsonObject schema = t.value(QStringLiteral("input_schema")).toObject();+ if (schema.isEmpty())+ errs << QStringLiteral("'%1' has no input_schema").arg(name);+ else if (schema.value(QStringLiteral("type")).toString() != QLatin1String("object"))+ errs << QStringLiteral("'%1' input_schema.type != object").arg(name);+ else if (!schema.contains(QStringLiteral("properties")))+ errs << QStringLiteral("'%1' input_schema has no properties map").arg(name);+ }+ ptf_report(QStringLiteral("anthropic payload"), errs, &failed);++ // The whole point of routing Anthropic through the shared selector: it+ // must see the same tools the OpenAI path does, not a different slice.+ const QJsonArray oai = svc.format_tools_for_openai(default_filter, activated);+ QStringList parity;+ if (tools.size() != oai.size()) {+ parity << QStringLiteral("anthropic advertises %1 tools, openai %2 — the two dialects "+ "are not selecting from the same set")+ .arg(tools.size())+ .arg(oai.size());+ }+ ptf_report(QStringLiteral("anthropic/openai selection parity"), parity, &failed);+ }++ // ── Gemini ──────────────────────────────────────────────────────────+ {+ const QJsonArray tools = svc.format_tools_for_gemini(default_filter, activated);+ const QJsonArray decls =+ tools.isEmpty() ? QJsonArray{}+ : tools[0].toObject().value(QStringLiteral("functionDeclarations")).toArray();+ ptf_out(QStringLiteral("\n[3] GEMINI — %1 function declarations").arg(decls.size()));++ QStringList errs;+ if (decls.isEmpty())+ errs << QStringLiteral("empty functionDeclarations — the model would have no tools at all");+ // Gemini rejects a request carrying more than 128 declarations.+ if (decls.size() > 128)+ errs << QStringLiteral("%1 declarations exceeds Gemini's limit of 128").arg(decls.size());++ int with_params = 0;+ for (const auto& v : decls) {+ const QJsonObject d = v.toObject();+ const QString name = d.value(QStringLiteral("name")).toString();+ if (!is_valid_gemini_function_name(name))+ errs << QStringLiteral("'%1' violates ^[a-zA-Z_][a-zA-Z0-9_.:-]{0,63}$").arg(name);+ if (d.value(QStringLiteral("description")).toString().trimmed().isEmpty())+ errs << QStringLiteral("'%1' has no description").arg(name);+ if (d.contains(QStringLiteral("parameters"))) {+ ++with_params;+ ptf_check_gemini_schema(d.value(QStringLiteral("parameters")).toObject(),+ name + QStringLiteral(".parameters"), &errs);+ }+ }+ ptf_out(QStringLiteral(" %1 with parameters, %2 parameterless (parameters omitted, as Gemini requires)")+ .arg(with_params)+ .arg(decls.size() - with_params));+ ptf_report(QStringLiteral("gemini payload"), errs, &failed);+ }++ // ── Sanitiser unit checks ───────────────────────────────────────────+ // Concrete regressions rather than catalogue-wide sweeps: each of these is+ // a shape a real MCP server can register and every one of them used to be+ // forwarded to Gemini verbatim.+ {+ ptf_out(QStringLiteral("\n[4] GEMINI SCHEMA TRANSLATION"));+ QStringList errs;++ // Parameterless tool → no usable schema, caller must omit `parameters`.+ if (sanitize_schema_for_gemini(QJsonObject{{"type", "object"}, {"properties", QJsonObject{}}}).has_value())+ errs << QStringLiteral("empty-properties object should sanitise to nullopt");++ // JSON Schema keywords with no Gemini equivalent must be dropped, not forwarded.+ const QJsonObject noisy{+ {"type", "object"},+ {"$schema", "https://json-schema.org/draft/2020-12/schema"},+ {"additionalProperties", false},+ {"properties",+ QJsonObject{+ {"sym", QJsonObject{{"type", "string"}, {"format", "uri"}, {"pattern", "^[A-Z]+$"}}},+ {"n", QJsonObject{{"type", "integer"}, {"exclusiveMinimum", 0}, {"enum", QJsonArray{1, 2}}}},+ {"tags", QJsonObject{{"type", "array"}}},+ {"empty", QJsonObject{{"type", "object"}, {"properties", QJsonObject{}}}},+ }},+ {"required", QJsonArray{"sym", "empty", "ghost"}}};+ const auto cleaned = sanitize_schema_for_gemini(noisy);+ if (!cleaned) {+ errs << QStringLiteral("a schema with real properties should not sanitise away");+ } else {+ ptf_check_gemini_schema(*cleaned, QStringLiteral("noisy"), &errs);+ const QJsonObject props = cleaned->value(QStringLiteral("properties")).toObject();+ if (props.contains(QStringLiteral("empty")))+ errs << QStringLiteral("property with an unusable object schema should have been dropped");+ if (!props.value(QStringLiteral("tags")).toObject().contains(QStringLiteral("items")))+ errs << QStringLiteral("ARRAY property should have been given a default 'items'");+ if (props.value(QStringLiteral("sym")).toObject().contains(QStringLiteral("format")))+ errs << QStringLiteral("string format 'uri' is not in Gemini's vocabulary and should be dropped");+ const QStringList req = [&] {+ QStringList r;+ for (const auto& v : cleaned->value(QStringLiteral("required")).toArray())+ r << v.toString();+ return r;+ }();+ if (req.contains(QStringLiteral("ghost")) || req.contains(QStringLiteral("empty")))+ errs << QStringLiteral("'required' still names a property that is not declared: ") + req.join(',');+ }++ // A ["string","null"] union is legal JSON Schema and illegal here.+ const auto nullable = sanitize_schema_for_gemini(+ QJsonObject{{"type", "object"},+ {"properties", QJsonObject{{"x", QJsonObject{{"type", QJsonArray{"string", "null"}}}}}}});+ if (!nullable) {+ errs << QStringLiteral("nullable-union schema should survive sanitisation");+ } else {+ const QJsonObject x = nullable->value(QStringLiteral("properties")).toObject()+ .value(QStringLiteral("x")).toObject();+ if (x.value(QStringLiteral("type")).toString() != QLatin1String("string"))+ errs << QStringLiteral("type union should collapse to its non-null member");+ if (!x.value(QStringLiteral("nullable")).toBool())+ errs << QStringLiteral("type union should set nullable=true");+ }++ ptf_report(QStringLiteral("sanitiser behaviour"), errs, &failed);+ }++ // ── Endpoint composition ────────────────────────────────────────────+ // A tool payload is only half the contract; it still has to reach the+ // route that speaks that dialect. Gemini is the trap: its body is the only+ // non-OpenAI shape, so a user-supplied base_url that gets "/chat/completions"+ // appended sends a native `contents` body to an OpenAI-compat route and+ // 400s — tools included. ProviderCatalog::chat_endpoint mirrors+ // LlmService::get_endpoint_url; both must agree with these cases.+ {+ ptf_out(QStringLiteral("\n[5] ENDPOINT COMPOSITION"));+ QStringList errs;+ struct Case {+ const char* provider;+ const char* base_url;+ const char* model;+ const char* expected;+ };+ static const Case kCases[] = {+ // Defaults.+ {"openai", "", "gpt-5", "https://api.openai.com/v1/chat/completions"},+ {"anthropic", "", "claude-sonnet-5", "https://api.anthropic.com/v1/messages"},+ {"gemini", "", "gemini-2.5-pro",+ "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent"},+ // Custom OpenAI-compatible endpoints — with and without a version segment.+ {"openrouter", "https://proxy.local", "x", "https://proxy.local/v1/chat/completions"},+ {"openrouter", "https://proxy.local/v1", "x", "https://proxy.local/v1/chat/completions"},+ {"openrouter", "https://proxy.local/v1/chat/completions", "x",+ "https://proxy.local/v1/chat/completions"},+ {"anthropic", "https://proxy.local", "x", "https://proxy.local/v1/messages"},+ // Gemini behind a proxy / regional host must stay on the native path.+ {"gemini", "https://gw.example.com", "gemini-2.5-flash",+ "https://gw.example.com/v1beta/models/gemini-2.5-flash:generateContent"},+ {"gemini", "https://gw.example.com/v1beta", "gemini-2.5-flash",+ "https://gw.example.com/v1beta/models/gemini-2.5-flash:generateContent"},+ {"gemini", "https://gw.example.com/v1beta/", "gemini-2.5-flash",+ "https://gw.example.com/v1beta/models/gemini-2.5-flash:generateContent"},+ };+ for (const auto& c : kCases) {+ const QString got = ai_chat::ProviderCatalog::chat_endpoint(QString::fromUtf8(c.provider),+ QString::fromUtf8(c.base_url),+ QString::fromUtf8(c.model));+ const QString want = QString::fromUtf8(c.expected);+ if (got != want) {+ errs << QStringLiteral("%1 base='%2' -> '%3' (expected '%4')")+ .arg(QString::fromUtf8(c.provider), QString::fromUtf8(c.base_url), got, want);+ }+ }+ ptf_report(QStringLiteral("chat endpoint per provider"), errs, &failed);+ }++ ptf_out(QStringLiteral("\n--------------------------------------------------------------"));+ ptf_out(failed ? QStringLiteral(" RESULT: FAIL") : QStringLiteral(" RESULT: PASS"));+ ptf_out(QStringLiteral("--------------------------------------------------------------\n"));+ return failed ? 1 : 0;+}++} // namespace fincept::mcpdiff --git a/fincept-qt/src/mcp/ProviderToolFormatSelfTest.h b/fincept-qt/src/mcp/ProviderToolFormatSelfTest.hnew file mode 100644index 000000000..f741d3d7a--- /dev/null+++ b/fincept-qt/src/mcp/ProviderToolFormatSelfTest.h@@ -0,0 +1,30 @@+#pragma once+// ProviderToolFormatSelfTest.h — headless self-test for the per-provider tool+// payloads (OpenAI / Anthropic / Gemini).+//+// Runs WITHOUT a GUI, an LLM, or any network/API key. It builds the tool array+// each dialect would actually be sent and checks it against that provider's+// documented constraints — the class of defect that is otherwise invisible+// until a user with that provider selected finds that tool calling silently+// does nothing.+//+// Why this exists: the three dialects had drifted apart. The OpenAI path went+// through format_tools_for_openai (Tool RAG Tier-0, activation feedback,+// name encoding) while Anthropic and Gemini called get_all_tools() and hand-+// rolled their arrays, so they shipped an arbitrary 50-tool slice of a ~900+// tool catalogue; Gemini additionally forwarded raw JSON Schema into a field+// that only accepts an OpenAPI subset, where one unrecognised key or one+// empty `properties` object fails the whole request. Nothing in the build+// caught any of it.+//+// Invoked from main() via `--selftest-llm-tools`, after initialize_all_tools()+// registers the real catalog, so it tests exactly what ships.++namespace fincept::mcp {++/// Validate the OpenAI / Anthropic / Gemini tool payloads against each+/// provider's wire constraints. Prints a report to stdout. Returns a process+/// exit code: 0 = passed, non-zero = at least one payload would be rejected.+int run_provider_tool_format_selftest();++} // namespace fincept::mcpdiff --git a/fincept-qt/src/services/llm/LlmRequestBuilders.cpp b/fincept-qt/src/services/llm/LlmRequestBuilders.cppindex 6ddc0d32c..f75a710c6 100644--- a/fincept-qt/src/services/llm/LlmRequestBuilders.cpp+++ b/fincept-qt/src/services/llm/LlmRequestBuilders.cpp@@ -16,6 +16,26 @@ namespace {constexpr const char* kLlmBuildersTag = "LlmService";}+void LlmService::apply_openai_token_limit(QJsonObject& body) const {+ // OpenAI/xAI native endpoints take max_completion_tokens universally.+ // AIHubMix is a pass-through aggregator (no param translation), so an+ // OpenAI-family model routed through it ALSO needs max_completion_tokens —+ // o-series and gpt-5 hard-reject max_tokens with a 400 ("Unsupported+ // parameter: 'max_tokens' ... Use 'max_completion_tokens' instead").+ // Non-OpenAI models (claude/gemini/deepseek/qwen/…) keep max_tokens.+ //+ // Centralised because the tool-loop follow-ups did NOT do this and sent+ // max_tokens unconditionally: on a reasoning model the opening turn was+ // built correctly and every follow-up 400'd, so the model would call one+ // tool and the reply never arrived.+ const QString model_lower = model_.toLower();+ const bool wants_completion_tokens = provider_ == "openai" || provider_ == "xai" ||+ (provider_ == "aihubmix" && detail::is_openai_family_model(model_lower));+ body.remove(wants_completion_tokens ? QStringLiteral("max_tokens") : QStringLiteral("max_completion_tokens"));+ body[wants_completion_tokens ? QStringLiteral("max_completion_tokens") : QStringLiteral("max_tokens")] =+ resolved_max_tokens();+}+QJsonObject LlmService::build_openai_request(const QString& user_message,const std::vector<ConversationMessage>& history, bool stream,bool with_tools) {@@ -37,24 +57,12 @@ QJsonObject LlmService::build_openai_request(const QString& user_message,req["model"] = model_;req["messages"] = messages;// Temperature omitted — each provider's default.- // Token-limit field: OpenAI/xAI native endpoints take max_completion_tokens- // universally. AIHubMix is a pass-through aggregator (no param translation),- // so an OpenAI-family model routed through it ALSO needs max_completion_tokens —- // o-series and gpt-5 hard-reject max_tokens with a 400 ("Unsupported parameter:- // 'max_tokens' ... Use 'max_completion_tokens' instead"). Non-OpenAI models- // (claude/gemini/deepseek/qwen/…) keep the OpenAI-compat max_tokens.- const int mx = resolved_max_tokens();- const bool wants_completion_tokens = provider_ == "openai" || provider_ == "xai" ||- (provider_ == "aihubmix" && detail::is_openai_family_model(model_lower));- if (wants_completion_tokens)- req["max_completion_tokens"] = mx;- else- req["max_tokens"] = mx;+ apply_openai_token_limit(req);if (stream) {req["stream"] = true;// OpenAI/xAI (and OpenAI-family models via AIHubMix) omit usage on streamed// responses unless we opt in. Don't send stream_options to non-OpenAI routes.- if (wants_completion_tokens)+ if (req.contains("max_completion_tokens"))req["stream_options"] = QJsonObject{{"include_usage", true}};}@@ -106,21 +114,16 @@ QJsonObject LlmService::build_anthropic_request(const QString& user_message,return req;}-QJsonArray LlmService::build_anthropic_tools() {- QJsonArray ant_tools;+QJsonArray LlmService::build_anthropic_tools(const QSet<QString>& activated) {if (!detail::effective_tools_enabled(tools_enabled_))- return ant_tools;- auto all_tools = mcp::McpService::instance().get_all_tools(detail::apply_request_policy(tool_filter_));- for (const auto& tool : all_tools) {- QString fn_name = tool.server_id + "__" + mcp::McpProvider::encode_tool_name_for_wire(tool.name);- QJsonObject schema = tool.input_schema;- if (schema.isEmpty()) {- schema["type"] = "object";- schema["properties"] = QJsonObject();- }- ant_tools.append(QJsonObject{{"name", fn_name}, {"description", tool.description}, {"input_schema", schema}});- }- return ant_tools;+ return {};+ // Same selection as the OpenAI path (Tier-0 + whatever the model has+ // discovered this turn). This used to call get_all_tools(), which bypasses+ // Tool RAG entirely and hands back an arbitrary 50-tool slice of a ~900+ // tool catalogue — so `tool_list` was frequently not even declared and the+ // model had no way to reach the rest.+ return mcp::McpService::instance().format_tools_for_anthropic(detail::apply_request_policy(tool_filter_),+ activated);}QJsonObject LlmService::build_gemini_request(const QString& user_message,@@ -152,23 +155,16 @@ QJsonObject LlmService::build_gemini_request(const QString& user_message,return req;}-QJsonArray LlmService::build_gemini_tools() {+QJsonArray LlmService::build_gemini_tools(const QSet<QString>& activated) {if (!detail::effective_tools_enabled(tools_enabled_))return {};- auto all_tools = mcp::McpService::instance().get_all_tools(detail::apply_request_policy(tool_filter_));- QJsonArray fn_decls;- for (const auto& tool : all_tools) {- QString fn_name = tool.server_id + "__" + mcp::McpProvider::encode_tool_name_for_wire(tool.name);- QJsonObject schema = tool.input_schema;- if (schema.isEmpty()) {- schema["type"] = "object";- schema["properties"] = QJsonObject();- }- fn_decls.append(QJsonObject{{"name", fn_name}, {"description", tool.description}, {"parameters", schema}});- }- if (fn_decls.isEmpty())- return {};- return QJsonArray{QJsonObject{{"functionDeclarations", fn_decls}}};+ // Gemini needs more than a reshuffle of the OpenAI payload: its+ // `parameters` field is an OpenAPI subset, not JSON Schema, and one+ // unrecognised key — or one parameterless tool sent as+ // {"type":"object","properties":{}} — fails the ENTIRE request rather than+ // the offending declaration. format_tools_for_gemini does that+ // translation; see mcp/GeminiSchema.h.+ return mcp::McpService::instance().format_tools_for_gemini(detail::apply_request_policy(tool_filter_), activated);}QJsonObject LlmService::build_fincept_request(const QString& user_message,diff --git a/fincept-qt/src/services/llm/LlmService.cpp b/fincept-qt/src/services/llm/LlmService.cppindex 8a6730af0..481351afa 100644--- a/fincept-qt/src/services/llm/LlmService.cpp+++ b/fincept-qt/src/services/llm/LlmService.cpp@@ -351,6 +351,22 @@ QString LlmService::get_endpoint_url() const {QString base = base_url_;while (base.endsWith('/'))base.chop(1);++ // Gemini is the one provider whose request body is NOT OpenAI-shaped,+ // so a custom base_url has to compose Gemini's own path. Appending+ // "/chat/completions" (what every other provider gets) pointed a+ // native `contents`/`tools[].functionDeclarations` body at an+ // OpenAI-compat route, which 400s — so any user with a Gemini proxy or+ // a regional endpoint lost tool calling along with everything else.+ if (p == "gemini" || p == "google") {+ if (base.contains(QLatin1String(":generateContent")))+ return base;+ static const QRegularExpression gem_ver(QStringLiteral("/v\\d+[a-zA-Z]*$"));+ if (!gem_ver.match(base).hasMatch())+ base += QStringLiteral("/v1beta");+ return base + QStringLiteral("/models/") + model_ + QStringLiteral(":generateContent");+ }+const QString suffix = (p == "anthropic") ? QStringLiteral("/messages") : QStringLiteral("/chat/completions");// Already a full endpoint — use verbatim.@@ -490,6 +506,13 @@ LlmResponse LlmService::do_request(const QString& user_message, const std::vectoloop_msgs.append(QJsonObject{{"role", "user"}, {"content", user_message}});loop_msgs.append(QJsonObject{{"role", "assistant"}, {"content", content}});+ // Tools the model discovers this turn via tool_list / tool_describe.+ // Tool RAG only ships the Tier-0 prefix, so without feeding these+ // back into the next round's `tools` array the model stalls right+ // after tool_describe with "I don't have that tool loaded" — the+ // OpenAI path has always done this; Anthropic never did.+ detail::ActivationTracker activated;+QJsonArray tool_results;for (const auto& block_val : content) {QJsonObject block = block_val.toObject();@@ -500,11 +523,14 @@ LlmResponse LlmService::do_request(const QString& user_message, const std::vectoQJsonObject input = block["input"].toObject();LOG_INFO(kLlmSvcTag, "Executing Anthropic tool: " + tool_name);+ const QString bare = mcp::McpProvider::parse_openai_function_name(tool_name).second;+ detail::emit_tool_progress(bare, input);auto tr = mcp::McpService::instance().execute_openai_function(tool_name, input, /*allow_defer=*/true);+ detail::note_tool_activations(bare, input, tr, activated);tool_results.append(QJsonObject{{"type", "tool_result"},{"tool_use_id", tool_id},- {"content", detail::encode_tool_result_for_llm(tool_name, tr)}});+ {"content", detail::encode_tool_result_for_llm(bare, tr)}});}loop_msgs.append(QJsonObject{{"role", "user"}, {"content", tool_results}});@@ -513,13 +539,14 @@ LlmResponse LlmService::do_request(const QString& user_message, const std::vecto// Previously this was a SINGLE follow-up with NO tools attached, so// the model could execute exactly one tool and never chain a second// (multi-step flows like "create a portfolio then add assets" broke).- // We now loop, keeping the full tool set advertised each round, until- // the model returns a final text answer or we hit the round cap. All- // tools are always declared (full catalogue), so no activation step- // is needed here — unlike the Tool-RAG OpenAI path.+ // We now loop, re-advertising the tool set each round, until the+ // model returns a final text answer or we hit the round cap. The+ // set is rebuilt per round rather than hoisted: under Tool RAG it+ // grows as the model discovers tools, so a hoisted array would+ // freeze the catalogue at whatever round 1 could see.const int kMaxRounds = active_max_tool_rounds();- const QJsonArray ant_tools = build_anthropic_tools();for (int round = 0; round < kMaxRounds; ++round) {+ const QJsonArray ant_tools = build_anthropic_tools(activated.names());QJsonObject fu;fu["model"] = model_;fu["messages"] = loop_msgs;@@ -557,12 +584,16 @@ LlmResponse LlmService::do_request(const QString& user_message, const std::vectocontinue;const QString tid = block["id"].toString();const QString tname = block["name"].toString();+ const QJsonObject targs = block["input"].toObject();LOG_INFO(kLlmSvcTag, QString("Anthropic tool loop r%1: %2").arg(round).arg(tname));- auto tr = mcp::McpService::instance().execute_openai_function(tname, block["input"].toObject(),+ const QString bare = mcp::McpProvider::parse_openai_function_name(tname).second;+ detail::emit_tool_progress(bare, targs);+ auto tr = mcp::McpService::instance().execute_openai_function(tname, targs,/*allow_defer=*/true);+ detail::note_tool_activations(bare, targs, tr, activated);more_results.append(QJsonObject{{"type", "tool_result"},{"tool_use_id", tid},- {"content", detail::encode_tool_result_for_llm(tname, tr)}});+ {"content", detail::encode_tool_result_for_llm(bare, tr)}});}loop_msgs.append(QJsonObject{{"role", "user"}, {"content", more_results}});}@@ -594,7 +625,10 @@ LlmResponse LlmService::do_request(const QString& user_message, const std::vecto// tool and never chain a second. We now loop, re-attaching the tool// set each round, until a text answer or the round cap.const int kMaxRounds = active_max_tool_rounds();- const QJsonArray gem_tools = build_gemini_tools();+ // Rebuilt per round, not hoisted: under Tool RAG the declared+ // set grows as tool_list / tool_describe surface tools, and a+ // hoisted array would pin the model to what round 1 could see.+ detail::ActivationTracker activated;// Constant conversation prefix: prior history + the user turn.QJsonArray fu_contents;@@ -622,9 +656,13 @@ LlmResponse LlmService::do_request(const QString& user_message, const std::vectocontinue;const QJsonObject fc = part["functionCall"].toObject();const QString fn_name = fc["name"].toString();+ const QJsonObject fn_args = fc["args"].toObject();LOG_INFO(kLlmSvcTag, QString("Gemini tool loop r%1: %2").arg(round).arg(fn_name));- auto tr = mcp::McpService::instance().execute_openai_function(fn_name, fc["args"].toObject(),+ const QString bare = mcp::McpProvider::parse_openai_function_name(fn_name).second;+ detail::emit_tool_progress(bare, fn_args);+ auto tr = mcp::McpService::instance().execute_openai_function(fn_name, fn_args,/*allow_defer=*/true);+ detail::note_tool_activations(bare, fn_args, tr, activated);// Gemini requires response to be an object — wrap strings under "result".QJsonObject response_obj;if (!tr.data.isNull() && !tr.data.isUndefined() && tr.data.isObject())@@ -635,7 +673,7 @@ LlmResponse LlmService::do_request(const QString& user_message, const std::vectoresponse_obj = tr.to_json();// Same transcript budget as the other providers — this// path re-posts `fu_contents` wholesale every round too.- detail::fit_llm_payload(fn_name, response_obj);+ detail::fit_llm_payload(bare, response_obj);fn_response_parts.append(QJsonObject{{"functionResponse", QJsonObject{{"name", fn_name}, {"response", response_obj}}}});}@@ -650,6 +688,7 @@ LlmResponse LlmService::do_request(const QString& user_message, const std::vectoif (!system_prompt_.isEmpty())fu_body["systemInstruction"] =QJsonObject{{"parts", QJsonArray{QJsonObject{{"text", system_prompt_}}}}};+ const QJsonArray gem_tools = build_gemini_tools(activated.names());if (!gem_tools.isEmpty())fu_body["tools"] = gem_tools;diff --git a/fincept-qt/src/services/llm/LlmService.h b/fincept-qt/src/services/llm/LlmService.hindex 5161c3e75..653082000 100644--- a/fincept-qt/src/services/llm/LlmService.h+++ b/fincept-qt/src/services/llm/LlmService.h@@ -180,8 +180,26 @@ class LlmService : public QObject {// follow-up turns stay identical to the first turn (Anthropic/Gemini// previously dropped tools on the follow-up, so they could never chain a// second tool call). Honour the active ToolPolicy via apply_request_policy.- QJsonArray build_anthropic_tools(); // [{name, description, input_schema}]- QJsonArray build_gemini_tools(); // [{functionDeclarations:[{name, description, parameters}]}]+ //+ // `activated` carries the tools the model has discovered this turn via+ // tool_list / tool_describe, exactly as the OpenAI path does. Without it+ // these two providers saw a fixed slice of the catalogue and could never+ // call anything they discovered — Tool RAG is on by default, so that slice+ // is what "tools don't work on Gemini/Claude" actually looked like.+ QJsonArray build_anthropic_tools(const QSet<QString>& activated = {}); // [{name, description, input_schema}]+ QJsonArray build_gemini_tools(const QSet<QString>& activated = {}); // [{functionDeclarations:[...]}]++ /// Stamp the output-token cap onto an OpenAI-shaped request body using the+ /// field the target endpoint actually accepts.+ ///+ /// OpenAI/xAI native endpoints — and OpenAI-family models routed through a+ /// pass-through aggregator — require `max_completion_tokens`; the o-series+ /// and gpt-5 hard-reject `max_tokens` with a 400. EVERY OpenAI-shaped body+ /// must go through this, tool-loop follow-ups included: those used to send+ /// `max_tokens` unconditionally, so on a reasoning model the first turn+ /// succeeded and every follow-up 400'd — the model called a tool once and+ /// the answer never came back.+ void apply_openai_token_limit(QJsonObject& body) const;QString get_endpoint_url() const;QMap<QString, QString> get_headers() const;diff --git a/fincept-qt/src/services/llm/LlmToolLoop.cpp b/fincept-qt/src/services/llm/LlmToolLoop.cppindex b13bca10d..75da79d40 100644--- a/fincept-qt/src/services/llm/LlmToolLoop.cpp+++ b/fincept-qt/src/services/llm/LlmToolLoop.cpp@@ -48,7 +48,7 @@ LlmResponse LlmService::do_tool_loop(QJsonArray loop_messages, const QString& urfu["model"] = model_;fu["messages"] = loop_messages;// Temperature intentionally omitted — provider default.- fu["max_tokens"] = resolved_max_tokens();+ apply_openai_token_limit(fu);QJsonArray tools = mcp::McpService::instance().format_tools_for_openai(detail::apply_request_policy(tool_filter_), activated.names());@@ -163,7 +163,7 @@ LlmResponse LlmService::do_tool_loop(QJsonArray loop_messages, const QString& urQJsonObject fu;fu["model"] = model_;fu["messages"] = loop_messages;- fu["max_tokens"] = resolved_max_tokens();+ apply_openai_token_limit(fu);// Keep tools available so structured tool_calls still work. Stripping// them used to force text-markup mode (raw <minimax:tool_call> blobs)// which leaked into the chat bubble. Carry the activated set so a@@ -338,6 +338,17 @@ std::optional<LlmResponse> LlmService::try_extract_and_execute_text_tool_calls(c// Temperature intentionally omitted — Anthropic default.if (!system_prompt_.isEmpty())follow_body["system"] = system_prompt_;+ } else if (provider_ == "gemini" || provider_ == "google") {+ // `url` here is the Gemini generateContent endpoint, so the body must+ // be Gemini-shaped. It previously fell through to the OpenAI branch+ // below and posted {model, messages, max_tokens} at :generateContent,+ // which 400s — the user then saw the raw-tool-output fallback instead+ // of an answer.+ follow_body["contents"] =+ QJsonArray{QJsonObject{{"role", "user"}, {"parts", QJsonArray{QJsonObject{{"text", follow_prompt}}}}}};+ follow_body["generationConfig"] = QJsonObject{{"maxOutputTokens", resolved_max_tokens()}};+ if (!system_prompt_.isEmpty())+ follow_body["systemInstruction"] = QJsonObject{{"parts", QJsonArray{QJsonObject{{"text", system_prompt_}}}}};} else if (provider_ == "fincept") {// /research/chat uses messages arrayQJsonArray msgs;@@ -356,7 +367,7 @@ std::optional<LlmResponse> LlmService::try_extract_and_execute_text_tool_calls(cfollow_body["model"] = model_;follow_body["messages"] = msgs;// Temperature intentionally omitted — provider default.- follow_body["max_tokens"] = resolved_max_tokens();+ apply_openai_token_limit(follow_body);}// Last-resort rendering when the summarisation turn can't be obtained.@@ -383,6 +394,10 @@ std::optional<LlmResponse> LlmService::try_extract_and_execute_text_tool_calls(c// Extract text from follow-up response (provider-aware)if (provider_ == "anthropic") {resp.content = extract_anthropic_content_text(fu_rj["content"].toArray());+ } else if (provider_ == "gemini" || provider_ == "google") {+ const QJsonArray cands = fu_rj["candidates"].toArray();+ if (!cands.isEmpty())+ resp.content = extract_gemini_parts_text(cands[0].toObject()["content"].toObject()["parts"].toArray());} else if (provider_ == "fincept") {// /research/chat: {"success":true,"data":{"choices":[{"message":{"content":"..."}}]}}QJsonObject data = fu_rj.contains("data") ? fu_rj["data"].toObject() : fu_rj;diff --git a/fincept-qt/src/services/llm/ProviderCatalog.cpp b/fincept-qt/src/services/llm/ProviderCatalog.cppindex b01c1eef7..3d7a86504 100644--- a/fincept-qt/src/services/llm/ProviderCatalog.cpp+++ b/fincept-qt/src/services/llm/ProviderCatalog.cpp@@ -200,6 +200,17 @@ QString ProviderCatalog::chat_endpoint(const QString& provider, const QString& bQString base = base_url;while (base.endsWith('/'))base.chop(1);+ // Gemini's body is not OpenAI-shaped, so its custom base_url composes+ // the native path. Mirrors LlmService::get_endpoint_url — keep the two+ // in step.+ if (p == "gemini" || p == "google") {+ if (base.contains(QLatin1String(":generateContent")))+ return base;+ static const QRegularExpression gem_ver(QStringLiteral("/v\\d+[a-zA-Z]*$"));+ if (!gem_ver.match(base).hasMatch())+ base += QStringLiteral("/v1beta");+ return base + QStringLiteral("/models/") + model + QStringLiteral(":generateContent");+ }const QString suffix = (p == "anthropic") ? QStringLiteral("/messages") : QStringLiteral("/chat/completions");if (base.endsWith(suffix))return base;