Files
DeepTutor/web/hooks/useMcpServers.ts
Bingxi Zhao (Frank) b46d9b5763 fix(mcp): reconnect after consent and refresh authorization status
Local dev repairs for #2586.

OAuth owner isolation and refresh-token stability verified. Hook polls until connected, refreshes on focus and stops on unmount. Python rechecks: 56 passed; UI batch: 25 passed. Web typecheck, 1407 Node tests, eslint and i18n passed.
2026-11-15 00:50:30 +08:00

287 lines
8.8 KiB
TypeScript

"use client";
import { resourceUsage } from "@/lib/workspaces-api";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import {
type McpSurface,
deleteMcpSurfaceServer,
loadMcpSurface,
writeMcpSurface,
} from "@/components/mcp/surface";
import {
authorizeSpaceMcpServer,
type McpServerConfig,
type McpStatusRow,
type McpStoreState,
type McpUserView,
} from "@/lib/mcp-api";
import { describeMcpError } from "@/lib/mcp-store";
/**
* State + handlers behind an MCP server manager screen.
*
* Mutations (toggle, delete, save) are expressed as the registry the screen
* wants to end up with; the surface's helpers translate that into whatever
* writes its API takes and answer with fresh connection status, so one round
* trip keeps the rows and their badges in sync.
*/
export function useMcpServers(surface: McpSurface) {
const { t } = useTranslation();
const [servers, setServers] = useState<Record<
string,
McpServerConfig
> | null>(null);
const [statusRows, setStatusRows] = useState<McpStatusRow[]>([]);
/** The per-user extras (secret markers, rejects, deployment view, cap). */
const [userView, setUserView] = useState<McpUserView | null>(null);
const [loadError, setLoadError] = useState<string | null>(null);
const [saveError, setSaveError] = useState<string | null>(null);
const [saving, setSaving] = useState(false);
const [authorizingName, setAuthorizingName] = useState<string | null>(null);
const [expanded, setExpanded] = useState<Set<string>>(new Set());
// Editing key: a server name for edit, "" for the "add new" form, or null
// when no editor is open.
const [editingKey, setEditingKey] = useState<string | null>(null);
/**
* Adopt a full state snapshot — a load, a write's response, or a write made
* outside this hook (a catalog install). Every one of those endpoints answers
* with the same shape, so none of them needs a follow-up read.
*/
const applyState = useCallback((state: McpStoreState) => {
setServers(state.servers);
setStatusRows(state.status);
setUserView(state.user);
}, []);
const load = useCallback(async () => {
try {
const state = await loadMcpSurface(surface);
applyState(state);
setLoadError(null);
return state;
} catch (err) {
setLoadError(describeMcpError(err, t));
}
}, [surface, t, applyState]);
useEffect(() => {
void load();
}, [load]);
useEffect(() => {
const refresh = () => void load();
window.addEventListener("focus", refresh);
return () => window.removeEventListener("focus", refresh);
}, [load]);
useEffect(() => {
if (!authorizingName) return;
let cancelled = false;
let timer: ReturnType<typeof setTimeout>;
const deadline = Date.now() + 10 * 60_000;
const refresh = async () => {
const state = await load();
if (cancelled) return;
const status = state?.status.find((row) => row.name === authorizingName);
const authorized = state?.user?.oauth[authorizingName]?.authorized;
if (
Date.now() >= deadline ||
(state && !state.servers[authorizingName]?.enabled) ||
(authorized &&
status?.status !== "needs_auth" &&
status?.status !== "connecting")
) {
setAuthorizingName(null);
return;
}
timer = setTimeout(() => void refresh(), 1500);
};
timer = setTimeout(() => void refresh(), 1500);
return () => {
cancelled = true;
clearTimeout(timer);
};
}, [authorizingName, load]);
const statusByName = useMemo(() => {
const map = new Map<string, McpStatusRow>();
for (const row of statusRows) map.set(row.name, row);
return map;
}, [statusRows]);
const persist = useCallback(
async (next: Record<string, McpServerConfig>) => {
setSaving(true);
setSaveError(null);
try {
// Adopt what the backend reports rather than `next`: on the per-user
// surface the file is the authority, and a partially applied write must
// not leave the rows showing an intent that never landed.
applyState(await writeMcpSurface(surface, servers ?? {}, next));
return true;
} catch (err) {
setSaveError(describeMcpError(err, t));
// A refused write may still have applied an earlier step of the diff.
await load();
return false;
} finally {
setSaving(false);
}
},
[surface, servers, t, load, applyState],
);
const toggleExpanded = useCallback((name: string) => {
setExpanded((prev) => {
const next = new Set(prev);
if (next.has(name)) next.delete(name);
else next.add(name);
return next;
});
}, []);
const toggleEnabled = useCallback(
async (name: string) => {
if (!servers || saving) return;
const current = servers[name];
if (!current) return;
const next = {
...servers,
[name]: { ...current, enabled: !current.enabled },
};
await persist(next);
},
[servers, saving, persist],
);
const remove = useCallback(
async (name: string) => {
if (!servers || saving) return;
try {
const origin = surface.writes === "per-server" ? "account" : "deployment";
const workspaces = await resourceUsage("mcp", `${origin}:${name}`);
const impact = workspaces.length ? "\n\n" + t("Used by workspaces: {{names}}", { names: workspaces.join(", ") }) : "";
if (typeof window !== "undefined" && !window.confirm(t('Delete MCP server "{{name}}"?', { name }) + impact)) return;
} catch (err) {
setSaveError(describeMcpError(err, t));
return;
}
const next = { ...servers };
delete next[name];
if (editingKey === name) setEditingKey(null);
await persist(next);
},
[servers, saving, t, editingKey, persist, surface],
);
/**
* Delete an entry the store refuses to connect (a hand-edited stdio server).
*
* It is absent from `servers`, so the diff `remove()` performs would issue no
* request at all — leaving the user looking at a row they cannot get rid of.
* Only meaningful on a per-server surface; the registry surface has no such
* entries because it accepts stdio in the first place.
*/
const removeRejected = useCallback(
async (name: string) => {
if (saving || surface.writes !== "per-server") return;
if (
typeof window !== "undefined" &&
!window.confirm(t('Delete MCP server "{{name}}"?', { name }))
) {
return;
}
setSaving(true);
setSaveError(null);
try {
applyState(await deleteMcpSurfaceServer(surface, name));
} catch (err) {
setSaveError(describeMcpError(err, t));
} finally {
setSaving(false);
}
},
[surface, saving, t, applyState],
);
/**
* Grant an OAuth-backed server its authorization.
*
* The consent screen belongs to the provider, so it opens in a new tab; when it
* redirects back the app's callback route completes the exchange server-side.
* Poll while consent is pending and refresh on focus when the person returns.
*/
const authorize = useCallback(
async (name: string) => {
if (saving) return;
setSaving(true);
setSaveError(null);
try {
const url = await authorizeSpaceMcpServer(surface.basePath, name);
if (!url) throw new Error("no authorization URL");
setAuthorizingName(name);
if (typeof window !== "undefined")
window.open(url, "_blank", "noopener");
} catch (err) {
setSaveError(describeMcpError(err, t));
} finally {
setSaving(false);
}
},
[surface.basePath, saving, t],
);
const save = useCallback(
async (originalName: string | "", name: string, cfg: McpServerConfig) => {
if (!servers) return false;
const next = { ...servers };
// Rename: drop the old key when editing under a new name.
if (originalName && originalName !== name) delete next[originalName];
next[name] = cfg;
const ok = await persist(next);
if (ok) setEditingKey(null);
return ok;
},
[servers, persist],
);
const startAdding = useCallback(() => setEditingKey(""), []);
const cancelEditing = useCallback(() => setEditingKey(null), []);
const toggleEditing = useCallback((name: string) => {
setEditingKey((prev) => (prev === name ? null : name));
}, []);
const serverNames = useMemo(
() => (servers ? Object.keys(servers).sort() : []),
[servers],
);
return {
servers,
serverNames,
statusByName,
userView,
loadError,
saveError,
saving,
expanded,
editingKey,
reload: load,
applyState,
toggleExpanded,
toggleEnabled,
toggleEditing,
startAdding,
cancelEditing,
remove,
removeRejected,
save,
authorize,
};
}