agent: wire MCP connectors into the loop (rmcp v3, Initialize lifecycle)
- src/mcp: streamable-HTTP client (rmcp v3.12) negotiating Initialize +
LATEST_WITH_INITIALIZE; namespaced mcp__{connector}__{tool} surface, write
badges from ToolAnnotations, persisted discovery (tools_json) so the agent
tool declarations stay byte-stable for KV-cache reuse.
- store: mcp_connectors catalog + tools_json column (idempotent migration) and
set_mcp_tools; per-session per-tool allowlist (SessionSettings.mcp_enabled).
- agent: execute_mcp routes namespaced calls, gated on the session allowlist
before any network call; Bearer token comes from the DB catalog, never echoed.
- api/sessions: build the MCP surface into Ambient + the agent tool declarations
on both the turn and prewarm paths (identical rebuild keeps the prefix stable).
- api/mcp + GUI: connector CRUD, tool-discovery picker, per-session enable.
- examples/mcp_probe: read-only CLI discovery spike.
Live-verified: 22 gmail tools discovered, gmail_labels returned 25 labels, and a
full model-driven GUI turn (7 enabled tools) with a 9197-token RESUME cache hit.
This commit is contained in:
1 parent
2f5b263d4e
commit
0d335b808c
18 files changed
+2266
-17
No files matched your search
+36
-1
@@ -307,7 +307,7 @@ details.thinking .reasoning {
|
||||
.queue-chip button:hover { color: var(--danger); }
|
||||
|
||||
/* ---- Stats modal ---- */
|
||||
#stats-modal, #dialog, #settings-modal, #notepad-modal {
|
||||
#stats-modal, #dialog, #settings-modal, #notepad-modal, #mcp-picker-modal {
|
||||
position: fixed; inset: 0; z-index: 40;
|
||||
background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center;
|
||||
padding: 20px;
|
||||
@@ -317,6 +317,8 @@ details.thinking .reasoning {
|
||||
padding: 20px; max-width: 420px; width: 100%;
|
||||
}
|
||||
.modal-card h2 { margin: 0 0 12px; font-size: 1.1rem; }
|
||||
/* The MCP tool picker lists many tools with descriptions: give it room to scroll. */
|
||||
#mcp-picker-modal .modal-card { width: min(560px, 100%); max-width: 560px; max-height: 85dvh; overflow-y: auto; }
|
||||
|
||||
/* ---- Stats dashboard ---- */
|
||||
.stats-card {
|
||||
@@ -454,4 +456,37 @@ details.thinking .reasoning {
|
||||
background: var(--accent);
|
||||
}
|
||||
#splitter:focus-visible { outline: none; }
|
||||
}
|
||||
|
||||
/* ---- MCP connectors / tool picker (settings) ---- */
|
||||
.mcp-block { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--border); }
|
||||
.mcp-block h3 { margin: 0 0 4px; font-size: 0.95rem; }
|
||||
.mcp-list { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
|
||||
.mcp-row {
|
||||
display: flex; align-items: center; justify-content: space-between; gap: 8px;
|
||||
padding: 8px 10px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
|
||||
}
|
||||
.mcp-row-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
|
||||
.mcp-name { font-weight: 600; }
|
||||
.mcp-url, .mcp-token { font-size: 0.75rem; color: var(--muted); overflow-wrap: anywhere; }
|
||||
.mcp-count { font-size: 0.75rem; color: var(--muted); }
|
||||
.mcp-row-actions { display: flex; gap: 6px; flex-shrink: 0; }
|
||||
.mcp-add { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
|
||||
.mcp-add input {
|
||||
flex: 1 1 140px; min-width: 0; padding: 8px; font-size: 0.85rem;
|
||||
background: var(--panel); border: 1px solid var(--border); border-radius: 6px; color: var(--text);
|
||||
}
|
||||
.mcp-picker-list { display: flex; flex-direction: column; gap: 6px; max-height: 50vh; overflow-y: auto; margin: 8px 0; }
|
||||
.mcp-tool {
|
||||
display: flex; align-items: flex-start; gap: 8px; padding: 8px;
|
||||
background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; cursor: pointer;
|
||||
}
|
||||
.mcp-tool input { margin-top: 3px; flex-shrink: 0; }
|
||||
.mcp-tool-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
|
||||
.mcp-tool-name { font-weight: 600; font-size: 0.85rem; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
|
||||
.mcp-tool-desc { font-size: 0.75rem; color: var(--muted); overflow-wrap: anywhere; }
|
||||
.mcp-write-badge {
|
||||
font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.03em;
|
||||
padding: 1px 6px; border-radius: 999px; color: var(--danger);
|
||||
border: 1px solid var(--danger); background: transparent;
|
||||
}
|
||||
+163
-1
@@ -49,7 +49,11 @@ async function sendJSON(url, method, body) {
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
});
|
||||
if (!r.ok) throw new Error(await r.text());
|
||||
return r.status === 204 ? null : r.json();
|
||||
// Several endpoints (PUT/PATCH/DELETE) reply 200/204 with an EMPTY body;
|
||||
// parse only when there is content, else r.json() throws "Unexpected end of
|
||||
// JSON input". Treat any bodyless success as null.
|
||||
const text = await r.text();
|
||||
return text ? JSON.parse(text) : null;
|
||||
}
|
||||
|
||||
// ---- DOM builders ---------------------------------------------------------
|
||||
@@ -793,6 +797,9 @@ function fillSettingsForm(data, scope) {
|
||||
$("#set-maxtokens").value = data.max_tokens ?? "";
|
||||
// Display preference is device-local and scope-independent.
|
||||
$("#set-fontsize").value = String(loadFontSize());
|
||||
// MCP: catalog editor in Generali, per-session tool allowlist in Sessione.
|
||||
$("#set-mcp-catalog").hidden = scope !== "general";
|
||||
$("#set-mcp-tools").hidden = scope !== "session";
|
||||
}
|
||||
|
||||
function readSettingsForm() {
|
||||
@@ -834,6 +841,11 @@ async function openSettings(scope) {
|
||||
settingsBase = await getJSON("/api/settings/defaults");
|
||||
}
|
||||
fillSettingsForm(settingsBase, scope);
|
||||
// Load the global connector catalog for both scopes (Generali edits it;
|
||||
// Sessione picks tools from it). Best-effort: a failure leaves it empty.
|
||||
await loadMcpCatalog();
|
||||
if (scope === "general") renderMcpCatalog();
|
||||
else renderMcpTools();
|
||||
$("#settings-modal").hidden = false;
|
||||
}
|
||||
|
||||
@@ -864,6 +876,152 @@ async function saveSettings() {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- MCP connectors (settings) -------------------------------------------
|
||||
// Two levels: the global catalog (Generali) holds connection params only; the
|
||||
// per-session allowlist (Sessione) curates which tools the model may use. The
|
||||
// picker fetches tools/list live and writes the selection into settingsBase,
|
||||
// persisted by the main Salva (readSettingsForm leaves mcp_enabled untouched).
|
||||
let mcpCatalog = [];
|
||||
let mcpPickerConnector = null;
|
||||
|
||||
async function loadMcpCatalog() {
|
||||
try { mcpCatalog = await getJSON("/api/mcp/connectors"); }
|
||||
catch { mcpCatalog = []; }
|
||||
}
|
||||
|
||||
function renderMcpCatalog() {
|
||||
const list = $("#mcp-connector-list");
|
||||
list.textContent = "";
|
||||
if (!mcpCatalog.length) {
|
||||
list.appendChild(el("p", "muted small", t("settings.mcp.empty")));
|
||||
return;
|
||||
}
|
||||
for (const c of mcpCatalog) {
|
||||
const row = el("div", "mcp-row");
|
||||
const info = el("div", "mcp-row-info");
|
||||
info.appendChild(el("span", "mcp-name", c.name));
|
||||
info.appendChild(el("span", "mcp-url", c.url));
|
||||
info.appendChild(el("span", "mcp-token", "••••••••"));
|
||||
row.appendChild(info);
|
||||
const actions = el("div", "mcp-row-actions");
|
||||
const edit = el("button", "btn ghost", t("settings.mcp.edit"));
|
||||
edit.addEventListener("click", () => {
|
||||
$("#mcp-new-name").value = c.name;
|
||||
$("#mcp-new-url").value = c.url;
|
||||
$("#mcp-new-token").value = c.token;
|
||||
});
|
||||
const del = el("button", "btn ghost danger", t("settings.mcp.delete"));
|
||||
del.addEventListener("click", async () => {
|
||||
const ok = await showDialog({
|
||||
title: t("settings.mcp.delete"),
|
||||
message: t("settings.mcp.delete.confirm", { name: c.name }),
|
||||
});
|
||||
if (!ok) return;
|
||||
try {
|
||||
await sendJSON(`/api/mcp/connectors/${encodeURIComponent(c.name)}`, "DELETE");
|
||||
await loadMcpCatalog();
|
||||
renderMcpCatalog();
|
||||
} catch (e) {
|
||||
$("#mcp-catalog-hint").textContent = t("settings.mcp.error", { msg: e.message });
|
||||
}
|
||||
});
|
||||
actions.appendChild(edit);
|
||||
actions.appendChild(del);
|
||||
row.appendChild(actions);
|
||||
list.appendChild(row);
|
||||
}
|
||||
}
|
||||
|
||||
async function addMcpConnector() {
|
||||
const name = $("#mcp-new-name").value.trim();
|
||||
const url = $("#mcp-new-url").value.trim();
|
||||
const token = $("#mcp-new-token").value.trim();
|
||||
const hint = $("#mcp-catalog-hint");
|
||||
if (!name || !url || !token) {
|
||||
hint.textContent = t("settings.mcp.fields.required");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await sendJSON(`/api/mcp/connectors/${encodeURIComponent(name)}`, "PUT", { url, token });
|
||||
$("#mcp-new-name").value = "";
|
||||
$("#mcp-new-url").value = "";
|
||||
$("#mcp-new-token").value = "";
|
||||
hint.textContent = t("settings.mcp.saved");
|
||||
await loadMcpCatalog();
|
||||
renderMcpCatalog();
|
||||
} catch (e) {
|
||||
hint.textContent = t("settings.mcp.error", { msg: e.message });
|
||||
}
|
||||
}
|
||||
|
||||
function renderMcpTools() {
|
||||
const list = $("#mcp-tools-list");
|
||||
list.textContent = "";
|
||||
if (!mcpCatalog.length) {
|
||||
list.appendChild(el("p", "muted small", t("settings.mcp.noconnectors")));
|
||||
return;
|
||||
}
|
||||
const enabled = settingsBase.mcp_enabled || {};
|
||||
for (const c of mcpCatalog) {
|
||||
const row = el("div", "mcp-row");
|
||||
const info = el("div", "mcp-row-info");
|
||||
info.appendChild(el("span", "mcp-name", c.name));
|
||||
const n = (enabled[c.name] || []).length;
|
||||
info.appendChild(el("span", "mcp-count", t("settings.mcp.enabled.count", { n })));
|
||||
row.appendChild(info);
|
||||
const pick = el("button", "btn ghost", t("settings.mcp.choose"));
|
||||
pick.addEventListener("click", () => openMcpPicker(c));
|
||||
row.appendChild(pick);
|
||||
list.appendChild(row);
|
||||
}
|
||||
}
|
||||
|
||||
async function openMcpPicker(connector) {
|
||||
mcpPickerConnector = connector.name;
|
||||
$("#mcp-picker-title").textContent = t("settings.mcp.picker.title", { name: connector.name });
|
||||
const list = $("#mcp-picker-list");
|
||||
const hint = $("#mcp-picker-hint");
|
||||
list.textContent = "";
|
||||
hint.textContent = t("settings.mcp.loading");
|
||||
$("#mcp-picker-modal").hidden = false;
|
||||
let tools;
|
||||
try {
|
||||
tools = await sendJSON(`/api/mcp/connectors/${encodeURIComponent(connector.name)}/tools`, "POST");
|
||||
} catch (e) {
|
||||
hint.textContent = t("settings.mcp.error", { msg: e.message });
|
||||
return;
|
||||
}
|
||||
hint.textContent = t("settings.mcp.picker.hint", { n: tools.length });
|
||||
const enabled = new Set((settingsBase.mcp_enabled || {})[connector.name] || []);
|
||||
list.textContent = "";
|
||||
for (const tool of tools) {
|
||||
const label = el("label", "mcp-tool");
|
||||
const cb = el("input");
|
||||
cb.type = "checkbox";
|
||||
cb.value = tool.name;
|
||||
cb.checked = enabled.has(tool.name);
|
||||
label.appendChild(cb);
|
||||
const txt = el("span", "mcp-tool-text");
|
||||
const head = el("span", "mcp-tool-name", tool.name);
|
||||
if (tool.write) head.appendChild(el("span", "mcp-write-badge", t("settings.mcp.write")));
|
||||
txt.appendChild(head);
|
||||
if (tool.description) txt.appendChild(el("span", "mcp-tool-desc", tool.description));
|
||||
label.appendChild(txt);
|
||||
list.appendChild(label);
|
||||
}
|
||||
}
|
||||
|
||||
function confirmMcpPicker() {
|
||||
if (!mcpPickerConnector) return;
|
||||
const checked = [...$("#mcp-picker-list").querySelectorAll("input[type=checkbox]:checked")]
|
||||
.map((c) => c.value);
|
||||
settingsBase.mcp_enabled = settingsBase.mcp_enabled || {};
|
||||
if (checked.length) settingsBase.mcp_enabled[mcpPickerConnector] = checked;
|
||||
else delete settingsBase.mcp_enabled[mcpPickerConnector];
|
||||
$("#mcp-picker-modal").hidden = true;
|
||||
renderMcpTools();
|
||||
}
|
||||
|
||||
// A custom modal dialog (replaces native prompt/confirm: styled, mobile-friendly,
|
||||
// and inspectable by automation). Resolves with the input string (or null on
|
||||
// cancel) when `input` is given, otherwise true/false.
|
||||
@@ -1003,6 +1161,10 @@ function wire() {
|
||||
$("#set-scope-general").addEventListener("click", () => openSettings("general"));
|
||||
$("#set-save").addEventListener("click", saveSettings);
|
||||
$("#set-cancel").addEventListener("click", () => { $("#settings-modal").hidden = true; });
|
||||
$("#mcp-add-btn").addEventListener("click", addMcpConnector);
|
||||
$("#mcp-picker-ok").addEventListener("click", confirmMcpPicker);
|
||||
$("#mcp-picker-cancel").addEventListener("click", () => { $("#mcp-picker-modal").hidden = true; });
|
||||
$("#mcp-picker-modal").addEventListener("click", (e) => { if (e.target.id === "mcp-picker-modal") $("#mcp-picker-modal").hidden = true; });
|
||||
const input = $("#input");
|
||||
input.addEventListener("input", () => autoGrow(input));
|
||||
input.addEventListener("keydown", (e) => {
|
||||
|
||||
@@ -15,6 +15,29 @@
|
||||
"settings.maxtokens": "Max tokens",
|
||||
"settings.fontsize": "Font size",
|
||||
"settings.fontsize.hint": "This device's preference (does not affect the model).",
|
||||
"settings.mcp.catalog": "MCP connectors",
|
||||
"settings.mcp.catalog.hint": "External MCP servers. Paste the connector's Bearer token here: it is stored in the local database, like everything else.",
|
||||
"settings.mcp.tools": "MCP tools",
|
||||
"settings.mcp.tools.hint": "Choose which tools of each connector the model may use in this chat. Save to apply.",
|
||||
"settings.mcp.name.ph": "name (e.g. gmail)",
|
||||
"settings.mcp.url.ph": "http://host:port/",
|
||||
"settings.mcp.token.ph": "Bearer token",
|
||||
"settings.mcp.add": "Add",
|
||||
"settings.mcp.edit": "Edit",
|
||||
"settings.mcp.delete": "Delete",
|
||||
"settings.mcp.delete.confirm": "Remove connector '{name}' from the catalog?",
|
||||
"settings.mcp.empty": "No connectors. Add one below.",
|
||||
"settings.mcp.noconnectors": "No connectors in the catalog. Add them in General settings.",
|
||||
"settings.mcp.choose": "Choose tools",
|
||||
"settings.mcp.enabled.count": "{n} enabled",
|
||||
"settings.mcp.write": "write",
|
||||
"settings.mcp.ok": "OK",
|
||||
"settings.mcp.loading": "Connecting and listing tools…",
|
||||
"settings.mcp.picker.title": "Tools of {name}",
|
||||
"settings.mcp.picker.hint": "{n} tools available. Check the ones to enable for this chat.",
|
||||
"settings.mcp.fields.required": "Name, URL and token are required.",
|
||||
"settings.mcp.saved": "Connector saved.",
|
||||
"settings.mcp.error": "MCP error: {msg}",
|
||||
"settings.save": "Save",
|
||||
"settings.cancel": "Cancel",
|
||||
"settings.hint.session": "Changes apply only to this session.",
|
||||
|
||||
@@ -15,6 +15,29 @@
|
||||
"settings.maxtokens": "Max token",
|
||||
"settings.fontsize": "Dimensione del font",
|
||||
"settings.fontsize.hint": "Preferenza di questo dispositivo (non influisce sul modello).",
|
||||
"settings.mcp.catalog": "Connettori MCP",
|
||||
"settings.mcp.catalog.hint": "Server MCP esterni. Incolla qui il token Bearer del connettore: viene salvato nel database locale, come tutto il resto.",
|
||||
"settings.mcp.tools": "Strumenti MCP",
|
||||
"settings.mcp.tools.hint": "Scegli quali tool di ogni connettore il modello può usare in questa chat. Salva per applicare.",
|
||||
"settings.mcp.name.ph": "nome (es. gmail)",
|
||||
"settings.mcp.url.ph": "http://host:porta/",
|
||||
"settings.mcp.token.ph": "Token Bearer",
|
||||
"settings.mcp.add": "Aggiungi",
|
||||
"settings.mcp.edit": "Modifica",
|
||||
"settings.mcp.delete": "Elimina",
|
||||
"settings.mcp.delete.confirm": "Eliminare il connettore '{name}' dal catalogo?",
|
||||
"settings.mcp.empty": "Nessun connettore. Aggiungine uno qui sotto.",
|
||||
"settings.mcp.noconnectors": "Nessun connettore nel catalogo. Aggiungili in Impostazioni generali.",
|
||||
"settings.mcp.choose": "Scegli strumenti",
|
||||
"settings.mcp.enabled.count": "{n} abilitati",
|
||||
"settings.mcp.write": "scrittura",
|
||||
"settings.mcp.ok": "OK",
|
||||
"settings.mcp.loading": "Connessione e lettura dei tool…",
|
||||
"settings.mcp.picker.title": "Strumenti di {name}",
|
||||
"settings.mcp.picker.hint": "{n} tool disponibili. Spunta quelli da abilitare per questa chat.",
|
||||
"settings.mcp.fields.required": "Nome, URL e token sono obbligatori.",
|
||||
"settings.mcp.saved": "Connettore salvato.",
|
||||
"settings.mcp.error": "Errore MCP: {msg}",
|
||||
"settings.save": "Salva",
|
||||
"settings.cancel": "Annulla",
|
||||
"settings.hint.session": "Modifiche valide solo per questa sessione.",
|
||||
|
||||
@@ -126,6 +126,25 @@
|
||||
</select>
|
||||
</label>
|
||||
<p id="set-fontsize-hint" class="muted small" data-i18n="settings.fontsize.hint">Preferenza di questo dispositivo.</p>
|
||||
<!-- MCP: global connector catalog (Generali scope) -->
|
||||
<div id="set-mcp-catalog" class="mcp-block" hidden>
|
||||
<h3 data-i18n="settings.mcp.catalog">Connettori MCP</h3>
|
||||
<p class="muted small" data-i18n="settings.mcp.catalog.hint">Server MCP esterni. Incolla qui il token Bearer del connettore: viene salvato nel database locale, come tutto il resto.</p>
|
||||
<div id="mcp-connector-list" class="mcp-list"></div>
|
||||
<div class="mcp-add">
|
||||
<input id="mcp-new-name" type="text" autocomplete="off" data-i18n-ph="settings.mcp.name.ph" placeholder="nome (es. gmail)" />
|
||||
<input id="mcp-new-url" type="text" autocomplete="off" data-i18n-ph="settings.mcp.url.ph" placeholder="http://host:porta/" />
|
||||
<input id="mcp-new-token" type="password" autocomplete="off" data-i18n-ph="settings.mcp.token.ph" placeholder="Token Bearer" />
|
||||
<button id="mcp-add-btn" class="btn" data-i18n="settings.mcp.add">Aggiungi</button>
|
||||
</div>
|
||||
<p id="mcp-catalog-hint" class="muted small"></p>
|
||||
</div>
|
||||
<!-- MCP: per-session tool allowlist (Sessione scope) -->
|
||||
<div id="set-mcp-tools" class="mcp-block" hidden>
|
||||
<h3 data-i18n="settings.mcp.tools">Strumenti MCP</h3>
|
||||
<p class="muted small" data-i18n="settings.mcp.tools.hint">Scegli quali tool di ogni connettore il modello può usare in questa chat. Salva per applicare.</p>
|
||||
<div id="mcp-tools-list" class="mcp-list"></div>
|
||||
</div>
|
||||
<div class="dialog-actions">
|
||||
<button id="set-cancel" class="btn" data-i18n="settings.cancel">Annulla</button>
|
||||
<button id="set-save" class="btn primary" data-i18n="settings.save">Salva</button>
|
||||
@@ -133,6 +152,19 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- MCP per-tool picker: live tools/list + checkboxes (write badge) -->
|
||||
<div id="mcp-picker-modal" hidden>
|
||||
<div class="modal-card">
|
||||
<h2 id="mcp-picker-title">Strumenti</h2>
|
||||
<p id="mcp-picker-hint" class="muted small"></p>
|
||||
<div id="mcp-picker-list" class="mcp-picker-list"></div>
|
||||
<div class="dialog-actions">
|
||||
<button id="mcp-picker-cancel" class="btn" data-i18n="settings.cancel">Annulla</button>
|
||||
<button id="mcp-picker-ok" class="btn primary" data-i18n="settings.mcp.ok">OK</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script type="module" src="/assets/app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user