Skip to content

Commit db1916c

Browse files
cliffhallclaude
andauthored
feat(servers): persist server list to ~/.mcp-inspector/mcp.json (#1343) (#1349)
* feat(servers): persist server list to ~/.mcp-inspector/mcp.json (#1343) Replaces the hardcoded SEED_SERVERS in App.tsx with a file-backed list at ~/.mcp-inspector/mcp.json. Full CRUD round-trips through new /api/servers routes; first launch writes the two existing seeds if the file is absent. Backend (core): - store-io: getDefaultMcpConfigPath() — sibling of getDefaultStorageDir() - core/mcp/serverList.ts: pure converters MCPConfig <-> ServerEntry[], DEFAULT_SEED_CONFIG, and normalizeServerType (moved from node/config.ts to keep serverList.ts Node-free) - core/mcp/remote/node/server.ts: GET/POST/PUT/:id/DELETE/:id /api/servers routes, plus RemoteServerOptions.mcpConfigPath defaulting to getDefaultMcpConfigPath(). Ids validated with validateStoreId. Frontend: - core/react/useServers.ts: { servers, loading, error, refresh, addServer, updateServer, removeServer } over the new endpoints - App.tsx: drops SEED_SERVERS, wires useServers, disconnects on remove of the active server, follows id on rename of the active server - ServerConfigModal: Add/Edit/Clone form (stdio: command/args/env/cwd; sse/streamable-http: url/headers as one-per-line text) - ServerRemoveConfirmModal: confirm-on-remove with id + transport summary Tests (52 new): - unit: getDefaultMcpConfigPath env-var permutations, serverList round-trip, hook flows against real Hono app.fetch (no TCP), modal forms + validation - integration: /api/servers routes against a real createRemoteApp + tmp file - stories with play functions for both new modals Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(servers): address PR #1349 review (correctness + nits) Bugs: - normalizeServerType: validate against the allowed set and default unknown / non-string types to "stdio" (matches the missing-type case). Prevents a hand-edited {"type":"websocket"} from leaking past `as ServerType` and surprising downstream narrowing. - onConfirmRemove: when removing the active server, also destroy() the 9 state managers and null them + the client out so they GC immediately instead of waiting for the next server switch. - /api/servers mutating routes: serialize POST/PUT/DELETE through an in-process write-lock so concurrent read-modify-write cycles can't lose updates. New integration test fires 25 parallel POSTs and asserts every entry lands. - readMcpConfig comment: update to match actual behavior (valid-JSON file with no `mcpServers` → empty; invalid JSON → 500 from the outer try, so corruption surfaces rather than silently emptying the list). New integration tests pin the 500 path on both GET and POST. Nits: - Add --inspector-surface-subtle token, use it in ServerRemoveConfirmModal instead of the raw Mantine var. - Rename useServers buildHeaders boolean param to includeJsonBody. - Add a comment on the env-value vs header-value trim asymmetry (env values preserve trailing whitespace; header values follow RFC 7230). Tests: 1630 unit + 404 integration + 306 storybook all green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(ServerConfigModal): null currentTarget in functional setState updater React nulls SyntheticEvent.currentTarget after the synchronous handler returns. Reading e.currentTarget.value inside `setForm((f) => ...)` worked under happy-dom (which keeps currentTarget around) but threw "Cannot read properties of null (reading 'value')" on the first keystroke in a real browser. Capture the value before the functional updater closes over it. All seven onChange handlers in ServerConfigModal had the bug. Extended the AddEmpty storybook play to type into Server ID and Command — the real-Chromium runner now exercises this path so a future regression fails the test suite instead of the user. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(remote-server): surface stderr + transport_error when subprocess crashes during startup A stdio subprocess that spawns successfully but exits before the browser opens the SSE events stream used to vanish into a bare "Session not found" 404. The fix has three parts: 1. RemoteSession.markTransportDead always pushes the transport_error event (via pushEvent's queue) rather than dropping it when no consumer is attached. The queued event drains when the consumer eventually arrives. 2. /api/mcp/connect's transport.onclose handler no longer deletes the session immediately when no consumer is attached. Instead it holds the session (with queued stderr + transport_error) for a 30s grace window so the events endpoint can drain it. A timeout sweeps if no consumer ever connects. 3. /api/mcp/events detects isTransportDead after attaching the consumer (which just drained the queue), yields once to flush, then closes the stream and deletes the session. End result: the user sees the actual error (e.g. "Cannot find module" from stderr + the transport-closed reason) instead of a confusing 404. Tests: - New unit tests (clients/web/src/test/integration/mcp/remote/remote-session.test.ts) cover the queueing behavior, live delivery, and clearEventConsumer signaling under all transport-state permutations. - New integration test (connect-crash.test.ts) spawns a node subprocess that writes to stderr and exits, then verifies the events stream delivers both the stderr line and the transport_error event before closing — the exact scenario from PR #1349. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(state): gate listRequestorTasks on the server's `tasks` capability ManagedRequestorTasksState was calling tasks/list on every connect. Servers that don't advertise the `tasks` capability (the common case) reply with -32601 "Method not found", which then surfaces in the inspector's console for every connect. Skip the call when the server doesn't declare `tasks` — empty tasks list is the right semantics. Existing tests opt into capabilities so the live-path coverage is preserved; two new tests pin the gated behavior (direct refresh + connect-event-triggered refresh). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(App): clear activeServerId on disconnect so other server cards re-enable ServerCard dims any card whose id differs from `activeServer` (and marks it inert), so cards stay non-interactive after a disconnect until the user reconnects elsewhere. activeServerId was set on connect but never reset on disconnect, leaving the whole screen locked to the just- disconnected card. Subscribe to InspectorClient's `disconnect` event and clear activeServerId there. Covers all three disconnect paths uniformly: - user toggles the connected card off - user clicks the header Disconnect button - mid-session transport failure (subprocess exit / network drop) Event-based instead of effect-based on connectionStatus to avoid the first-render-clobbers-new-id trap (status starts as "disconnected" for a freshly-created client before connect() runs). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent e9ac57c commit db1916c

23 files changed

Lines changed: 3176 additions & 137 deletions

clients/web/src/App.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
--inspector-surface-body: var(--mantine-color-dark-9);
2626
--inspector-surface-card: var(--mantine-color-gray-0);
2727
--inspector-surface-code: var(--mantine-color-white);
28+
/* Subtle inset surface for modal call-outs / summary panels. Mantine's
29+
`default-hover` already adapts to color scheme, so no dark override. */
30+
--inspector-surface-subtle: var(--mantine-color-default-hover);
2831

2932
/* ── Inputs ────────────────────────────────────────────── */
3033
--inspector-input-background: var(--mantine-color-white);

clients/web/src/App.tsx

Lines changed: 223 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import type {
99
import type { AppBridge } from "@modelcontextprotocol/ext-apps/app-bridge";
1010
import { InspectorClient } from "@inspector/core/mcp/index.js";
1111
import type { JsonValue } from "@inspector/core/mcp/index.js";
12-
import type { MessageEntry, ServerEntry } from "@inspector/core/mcp/types.js";
12+
import type {
13+
MCPServerConfig,
14+
MessageEntry,
15+
ServerEntry,
16+
} from "@inspector/core/mcp/types.js";
1317
import { API_SERVER_ENV_VARS } from "@inspector/core/mcp/remote/constants.js";
1418
import { ManagedToolsState } from "@inspector/core/mcp/state/managedToolsState.js";
1519
import { ManagedPromptsState } from "@inspector/core/mcp/state/managedPromptsState.js";
@@ -22,6 +26,7 @@ import { FetchRequestLogState } from "@inspector/core/mcp/state/fetchRequestLogS
2226
import { StderrLogState } from "@inspector/core/mcp/state/stderrLogState.js";
2327
import type { RedirectUrlProvider } from "@inspector/core/auth/index.js";
2428
import { useInspectorClient } from "@inspector/core/react/useInspectorClient.js";
29+
import { useServers } from "@inspector/core/react/useServers.js";
2530
import { useManagedTools } from "@inspector/core/react/useManagedTools.js";
2631
import { useManagedPrompts } from "@inspector/core/react/useManagedPrompts.js";
2732
import { useManagedResources } from "@inspector/core/react/useManagedResources.js";
@@ -35,38 +40,13 @@ import type { GetPromptState } from "./components/screens/PromptsScreen/PromptsS
3540
import type { ReadResourceState } from "./components/screens/ResourcesScreen/ResourcesScreen";
3641
import type { BridgeFactory } from "./components/elements/AppRenderer/AppRenderer";
3742
import type { LogEntryData } from "./components/elements/LogEntry/LogEntry";
43+
import {
44+
ServerConfigModal,
45+
type ServerConfigModalMode,
46+
} from "./components/groups/ServerConfigModal/ServerConfigModal";
47+
import { ServerRemoveConfirmModal } from "./components/groups/ServerRemoveConfirmModal/ServerRemoveConfirmModal";
3848
import { createWebEnvironment } from "./lib/environmentFactory";
3949

40-
// Hardcoded seed servers so the Servers screen has something to connect to.
41-
// Persistence + an "Add server" UI are explicitly out of scope for #1244 (the
42-
// useServers v2-only hook is a separate effort); follow-up work will replace
43-
// this with a real `useServers` store. The two seeds here cover the common
44-
// shapes a developer reaches for first: a real filesystem (scoped to /tmp so
45-
// nothing destructive is possible by default) and the canonical "everything"
46-
// reference server (tools / prompts / resources / sampling / completion).
47-
const SEED_SERVERS: ServerEntry[] = [
48-
{
49-
id: "filesystem-server-default",
50-
name: "Local Filesystem (npx)",
51-
config: {
52-
type: "stdio",
53-
command: "npx",
54-
args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
55-
},
56-
connection: { status: "disconnected" },
57-
},
58-
{
59-
id: "everything-server-default",
60-
name: "Everything (npx)",
61-
config: {
62-
type: "stdio",
63-
command: "npx",
64-
args: ["-y", "@modelcontextprotocol/server-everything"],
65-
},
66-
connection: { status: "disconnected" },
67-
},
68-
];
69-
7050
// OAuth redirect URL provider — points at the dev backend's `/oauth/callback`
7151
// handler. The InspectorClient only consults this when the active server
7252
// requires OAuth; for stdio MCP servers it's never used. Created once and
@@ -150,9 +130,24 @@ function App() {
150130
setColorScheme(isDark ? "light" : "dark");
151131
}, [isDark, setColorScheme]);
152132

153-
// Server list — held locally; one seed entry per #1244's "hardcoded
154-
// sample server" scope decision. Future work: useServers hook.
155-
const [servers] = useState<ServerEntry[]>(SEED_SERVERS);
133+
// Server list — sourced from ~/.mcp-inspector/mcp.json via the backend's
134+
// `/api/servers` routes. First-launch seeds are written by the backend when
135+
// the file is absent, so this hook returns a non-empty list on first load.
136+
const { servers, addServer, updateServer, removeServer } = useServers({
137+
baseUrl:
138+
typeof window !== "undefined"
139+
? window.location.origin
140+
: "http://localhost",
141+
authToken: getAuthToken(),
142+
});
143+
144+
// CRUD-modal state. `configModal` drives Add / Edit / Clone via a single
145+
// shared form modal; `removeTarget` drives the remove-confirmation modal.
146+
const [configModal, setConfigModal] = useState<{
147+
mode: ServerConfigModalMode;
148+
targetId?: string;
149+
} | null>(null);
150+
const [removeTarget, setRemoveTarget] = useState<ServerEntry | null>(null);
156151

157152
// The active connection target. `null` between sessions; set as soon as
158153
// the user toggles a server card on. Drives state-manager lifetime.
@@ -277,6 +272,25 @@ function App() {
277272
};
278273
}, [inspectorClient]);
279274

275+
// Reset activeServerId whenever the live session ends. Without this the
276+
// other ServerCards stay `inert` after disconnect — ServerCard dims any
277+
// card whose id differs from `activeServer`. Subscribing to
278+
// InspectorClient's own `disconnect` event covers all three paths
279+
// (explicit toggle, header Disconnect button, mid-session transport
280+
// failure / process exit) and avoids the first-render-clobbers-new-id
281+
// trap that watching connectionStatus has (status starts as
282+
// "disconnected" for the new client before connect() runs).
283+
useEffect(() => {
284+
if (!inspectorClient) return;
285+
const onDisconnect = () => {
286+
setActiveServerId(undefined);
287+
};
288+
inspectorClient.addEventListener("disconnect", onDisconnect);
289+
return () => {
290+
inspectorClient.removeEventListener("disconnect", onDisconnect);
291+
};
292+
}, [inspectorClient]);
293+
280294
// Build the InitializeResult the connected ViewHeader expects from the
281295
// hook's split fields. `protocolVersion` is hard-coded for now — the
282296
// useInspectorClient hook doesn't expose it. TODO(#1324): consume the
@@ -591,6 +605,92 @@ function App() {
591605
/* TODO: not wired yet */
592606
}, []);
593607

608+
// Remove handler — runs after the user confirms in the modal. When removing
609+
// the active server, also tear down the session in-place so the client and
610+
// its 9 state managers can be GC'd now instead of lingering until the next
611+
// server switch. Mirrors the destroy sequence at the top of
612+
// `setupClientForServer` (lines ~304-312) but additionally nulls every ref.
613+
const onConfirmRemove = useCallback(async () => {
614+
if (!removeTarget) return;
615+
const id = removeTarget.id;
616+
if (id === activeServerId) {
617+
if (inspectorClient) {
618+
await inspectorClient.disconnect();
619+
}
620+
managedToolsState?.destroy();
621+
managedPromptsState?.destroy();
622+
managedResourcesState?.destroy();
623+
managedResourceTemplatesState?.destroy();
624+
managedRequestorTasksState?.destroy();
625+
resourceSubscriptionsState?.destroy();
626+
messageLogState?.destroy();
627+
fetchRequestLogState?.destroy();
628+
stderrLogState?.destroy();
629+
setInspectorClient(null);
630+
setManagedToolsState(null);
631+
setManagedPromptsState(null);
632+
setManagedResourcesState(null);
633+
setManagedResourceTemplatesState(null);
634+
setManagedRequestorTasksState(null);
635+
setResourceSubscriptionsState(null);
636+
setMessageLogState(null);
637+
setFetchRequestLogState(null);
638+
setStderrLogState(null);
639+
setActiveServerId(undefined);
640+
}
641+
await removeServer(id);
642+
setRemoveTarget(null);
643+
}, [
644+
removeTarget,
645+
activeServerId,
646+
inspectorClient,
647+
managedToolsState,
648+
managedPromptsState,
649+
managedResourcesState,
650+
managedResourceTemplatesState,
651+
managedRequestorTasksState,
652+
resourceSubscriptionsState,
653+
messageLogState,
654+
fetchRequestLogState,
655+
stderrLogState,
656+
removeServer,
657+
]);
658+
659+
// Submit handler for the Add / Edit / Clone modal. Add and Clone both go
660+
// through addServer; Edit uses updateServer (which supports id rename).
661+
// On rename of the active server, keep activeServerId pointed at the new id.
662+
const onConfigSubmit = useCallback(
663+
async (id: string, config: MCPServerConfig) => {
664+
if (configModal?.mode === "edit" && configModal.targetId) {
665+
const originalId = configModal.targetId;
666+
await updateServer(originalId, id, config);
667+
if (originalId === activeServerId && id !== originalId) {
668+
setActiveServerId(id);
669+
}
670+
return;
671+
}
672+
// add or clone
673+
await addServer(id, config);
674+
},
675+
[configModal, addServer, updateServer, activeServerId],
676+
);
677+
678+
// Derive the existingIds list the modal uses for uniqueness validation.
679+
// In edit mode the target's own id must be excluded so saving without
680+
// renaming doesn't trip the "already exists" check.
681+
const existingIds = useMemo(() => {
682+
const ids = servers.map((s) => s.id);
683+
if (configModal?.mode === "edit" && configModal.targetId) {
684+
return ids.filter((id) => id !== configModal.targetId);
685+
}
686+
return ids;
687+
}, [servers, configModal]);
688+
689+
const configModalTarget = useMemo(() => {
690+
if (!configModal?.targetId) return undefined;
691+
return servers.find((s) => s.id === configModal.targetId);
692+
}, [configModal, servers]);
693+
594694
// The Resources screen needs `isSubscribed` to flip the Subscribe button
595695
// label to "Unsubscribe". Derive it from the live subscriptions list rather
596696
// than threading it through every setReadResourceState site — that way the
@@ -608,75 +708,95 @@ function App() {
608708
}, [readResourceState, subscriptions]);
609709

610710
return (
611-
<InspectorView
612-
servers={servers}
613-
activeServer={activeServerId}
614-
connectionStatus={connectionStatus}
615-
initializeResult={initializeResult}
616-
latencyMs={latencyMs}
617-
errorMessage={errorMessage}
618-
tools={tools}
619-
prompts={prompts}
620-
resources={resources}
621-
resourceTemplates={resourceTemplates}
622-
subscriptions={subscriptions}
623-
logs={logs}
624-
tasks={tasks}
625-
history={messages}
626-
toolCallState={toolCallState}
627-
getPromptState={getPromptState}
628-
readResourceState={effectiveReadResourceState}
629-
currentLogLevel={currentLogLevel}
630-
sandboxPath={STUB_SANDBOX_PATH}
631-
bridgeFactory={stubBridgeFactory}
632-
onToggleTheme={onToggleTheme}
633-
onToggleConnection={(id) => {
634-
void onToggleConnection(id);
635-
}}
636-
onDisconnect={() => {
637-
void onDisconnect();
638-
}}
639-
onServerAdd={todoNoop}
640-
onServerImportConfig={todoNoop}
641-
onServerImportJson={todoNoop}
642-
onServerInfo={todoNoop}
643-
onServerSettings={todoNoop}
644-
onServerEdit={todoNoop}
645-
onServerClone={todoNoop}
646-
onServerRemove={todoNoop}
647-
onCallTool={(name, args) => {
648-
void onCallTool(name, args);
649-
}}
650-
onClearToolResult={onClearToolResult}
651-
onRefreshTools={onRefreshTools}
652-
onGetPrompt={(name, args) => {
653-
void onGetPrompt(name, args);
654-
}}
655-
onRefreshPrompts={onRefreshPrompts}
656-
onReadResource={(uri) => {
657-
void onReadResource(uri);
658-
}}
659-
onSubscribeResource={onSubscribeResource}
660-
onUnsubscribeResource={onUnsubscribeResource}
661-
onRefreshResources={onRefreshResources}
662-
onCompleteArgument={onCompleteArgument}
663-
completionsSupported={capabilities?.completions !== undefined}
664-
onCancelTask={onCancelTask}
665-
onClearCompletedTasks={todoNoop}
666-
onRefreshTasks={onRefreshTasks}
667-
onSetLogLevel={onSetLogLevel}
668-
onClearLogs={onClearLogs}
669-
onExportLogs={todoNoop}
670-
onCopyAllLogs={todoNoop}
671-
onClearHistory={onClearHistory}
672-
onExportHistory={todoNoop}
673-
onReplayHistory={todoNoop}
674-
onTogglePinHistory={todoNoop}
675-
onSelectApp={todoNoop}
676-
onOpenApp={todoNoop}
677-
onCloseApp={todoNoop}
678-
onRefreshApps={onRefreshTools}
679-
/>
711+
<>
712+
<InspectorView
713+
servers={servers}
714+
activeServer={activeServerId}
715+
connectionStatus={connectionStatus}
716+
initializeResult={initializeResult}
717+
latencyMs={latencyMs}
718+
errorMessage={errorMessage}
719+
tools={tools}
720+
prompts={prompts}
721+
resources={resources}
722+
resourceTemplates={resourceTemplates}
723+
subscriptions={subscriptions}
724+
logs={logs}
725+
tasks={tasks}
726+
history={messages}
727+
toolCallState={toolCallState}
728+
getPromptState={getPromptState}
729+
readResourceState={effectiveReadResourceState}
730+
currentLogLevel={currentLogLevel}
731+
sandboxPath={STUB_SANDBOX_PATH}
732+
bridgeFactory={stubBridgeFactory}
733+
onToggleTheme={onToggleTheme}
734+
onToggleConnection={(id) => {
735+
void onToggleConnection(id);
736+
}}
737+
onDisconnect={() => {
738+
void onDisconnect();
739+
}}
740+
onServerAdd={() => setConfigModal({ mode: "add" })}
741+
onServerImportConfig={todoNoop}
742+
onServerImportJson={todoNoop}
743+
onServerInfo={todoNoop}
744+
onServerSettings={todoNoop}
745+
onServerEdit={(id) => setConfigModal({ mode: "edit", targetId: id })}
746+
onServerClone={(id) => setConfigModal({ mode: "clone", targetId: id })}
747+
onServerRemove={(id) => {
748+
const target = servers.find((s) => s.id === id);
749+
if (target) setRemoveTarget(target);
750+
}}
751+
onCallTool={(name, args) => {
752+
void onCallTool(name, args);
753+
}}
754+
onClearToolResult={onClearToolResult}
755+
onRefreshTools={onRefreshTools}
756+
onGetPrompt={(name, args) => {
757+
void onGetPrompt(name, args);
758+
}}
759+
onRefreshPrompts={onRefreshPrompts}
760+
onReadResource={(uri) => {
761+
void onReadResource(uri);
762+
}}
763+
onSubscribeResource={onSubscribeResource}
764+
onUnsubscribeResource={onUnsubscribeResource}
765+
onRefreshResources={onRefreshResources}
766+
onCompleteArgument={onCompleteArgument}
767+
completionsSupported={capabilities?.completions !== undefined}
768+
onCancelTask={onCancelTask}
769+
onClearCompletedTasks={todoNoop}
770+
onRefreshTasks={onRefreshTasks}
771+
onSetLogLevel={onSetLogLevel}
772+
onClearLogs={onClearLogs}
773+
onExportLogs={todoNoop}
774+
onCopyAllLogs={todoNoop}
775+
onClearHistory={onClearHistory}
776+
onExportHistory={todoNoop}
777+
onReplayHistory={todoNoop}
778+
onTogglePinHistory={todoNoop}
779+
onSelectApp={todoNoop}
780+
onOpenApp={todoNoop}
781+
onCloseApp={todoNoop}
782+
onRefreshApps={onRefreshTools}
783+
/>
784+
<ServerConfigModal
785+
opened={configModal !== null}
786+
mode={configModal?.mode ?? "add"}
787+
initialId={configModalTarget?.id}
788+
initialConfig={configModalTarget?.config}
789+
existingIds={existingIds}
790+
onClose={() => setConfigModal(null)}
791+
onSubmit={onConfigSubmit}
792+
/>
793+
<ServerRemoveConfirmModal
794+
opened={removeTarget !== null}
795+
target={removeTarget}
796+
onCancel={() => setRemoveTarget(null)}
797+
onConfirm={onConfirmRemove}
798+
/>
799+
</>
680800
);
681801
}
682802

0 commit comments

Comments
 (0)