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
+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) => {
|
||||
|
||||
Reference in new issue
Block a user