Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

MCP UI follow-ups: lazy-load Tools and kwant editors, stop persisting test error text

Lin (kwant bot)64e868bc5d24
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 68ec7f2e..a6d60b7c 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -1,15 +1,19 @@
-import React, { useState, useEffect, useRef } from 'react';
+import React, { Suspense, lazy, useState, useEffect, useRef } from 'react';
import { AppHeader } from './components/AppHeader';
import { Sidebar, Kwant, Channel, Team, memberIdsOf } from './components/Sidebar';
import { ChatView, ChatMessage, DiscussionState, RoutingState } from './components/ChatView';
import { PendingApproval } from './components/ModernApprovalCard';
import { SettingsModal } from './components/SettingsModal';
-import { McpServersModal } from './components/McpServersModal';
-import { NewAgentModal, AgentFormData } from './components/NewAgentModal';
+import type { AgentFormData } from './components/NewAgentModal';
import { NewTeamModal } from './components/NewTeamModal';
import { ManageTeamModal } from './components/ManageTeamModal';
import { playSfx } from './audio/chiptune';
+// Kwant and Tools editors are their own chunks. They stay mounted (rendering nothing while closed),
+// so both load right after start-up and opening them never waits.
+const NewAgentModal = lazy(() => import('./components/NewAgentModal').then((m) => ({ default: m.NewAgentModal })));
+const McpServersModal = lazy(() => import('./components/McpServersModal').then((m) => ({ default: m.McpServersModal })));
+
export const App: React.FC = () => {
const [kwants, setKwants] = useState<Kwant[]>([]);
const [channels, setChannels] = useState<Channel[]>([]);
@@ -1120,27 +1124,29 @@ export const App: React.FC = () => {
</div>
{/* New Agent Modal */}
- <NewAgentModal
- isOpen={isNewAgentOpen}
- onClose={() => {
- setIsNewAgentOpen(false);
- setNewAgentTeam(null);
- }}
- joinTeamName={newAgentTeam?.name}
- takenNames={kwants.map((k) => k.name)}
- onManageServers={() => setIsMcpOpen(true)}
- onSubmit={handleCreateAgent}
- />
+ <Suspense fallback={null}>
+ <NewAgentModal
+ isOpen={isNewAgentOpen}
+ onClose={() => {
+ setIsNewAgentOpen(false);
+ setNewAgentTeam(null);
+ }}
+ joinTeamName={newAgentTeam?.name}
+ takenNames={kwants.map((k) => k.name)}
+ onManageServers={() => setIsMcpOpen(true)}
+ onSubmit={handleCreateAgent}
+ />
- {/* Edit Agent Modal */}
- <NewAgentModal
- isOpen={!!editingKwant}
- initial={editingKwant}
- kwantId={editingKwant?.id}
- onManageServers={() => setIsMcpOpen(true)}
- onClose={() => setEditingKwant(null)}
- onSubmit={(data) => (editingKwant ? handleUpdateAgent(editingKwant, data) : undefined)}
- />
+ {/* Edit Agent Modal */}
+ <NewAgentModal
+ isOpen={!!editingKwant}
+ initial={editingKwant}
+ kwantId={editingKwant?.id}
+ onManageServers={() => setIsMcpOpen(true)}
+ onClose={() => setEditingKwant(null)}
+ onSubmit={(data) => (editingKwant ? handleUpdateAgent(editingKwant, data) : undefined)}
+ />
+ </Suspense>
{/* New Team Modal */}
<NewTeamModal
@@ -1171,7 +1177,9 @@ export const App: React.FC = () => {
/>
{/* MCP tool servers (opened last so it sits above the agent modal) */}
- <McpServersModal isOpen={isMcpOpen} onClose={() => setIsMcpOpen(false)} />
+ <Suspense fallback={null}>
+ <McpServersModal isOpen={isMcpOpen} onClose={() => setIsMcpOpen(false)} />
+ </Suspense>
</div>
);
};
diff --git a/frontend/src/components/McpServersModal.tsx b/frontend/src/components/McpServersModal.tsx
index 1d4e97c4..85a8f00b 100644
--- a/frontend/src/components/McpServersModal.tsx
+++ b/frontend/src/components/McpServersModal.tsx
@@ -37,6 +37,7 @@ import {
validateMcpForm,
loadStatuses,
saveStatuses,
+ toStoredStatus,
humanizeToolName,
} from '../mcp';
@@ -244,7 +245,7 @@ export const McpServersModal: React.FC<McpServersModalProps> = ({ isOpen, onClos
}
setTesting((t) => ({ ...t, [id]: false }));
setTestResults((r) => ({ ...r, [id]: result }));
- setStatus(id, { ok: result.ok, tools: (result.tools ?? []).map((t) => t.name), error: result.error, at: new Date().toISOString() });
+ setStatus(id, toStoredStatus(result, new Date().toISOString()));
playSfx(result.ok ? 'coin' : 'alert');
};
diff --git a/frontend/src/mcp.ts b/frontend/src/mcp.ts
index e54f3939..e2f00132 100644
--- a/frontend/src/mcp.ts
+++ b/frontend/src/mcp.ts
@@ -302,28 +302,64 @@ export const mcpSupportFor = (preferredModel: string, keys: EngineKeys): { suppo
};
};
-/** Last test result per server, kept in the browser so cards can show a status. */
+/**
+ * Last test result per server, kept in the browser so cards can show a status.
+ * Only ok, tool names and time are kept: a server's error text can echo back
+ * secrets, so the full error lives in memory for the open screen only.
+ */
const STATUS_KEY = 'kwnt.mcp.lastTest';
export interface StoredStatus {
ok: boolean;
tools: string[];
- error?: string;
at: string;
}
-export const loadStatuses = (): Record<string, StoredStatus> => {
- try {
- return JSON.parse(localStorage.getItem(STATUS_KEY) || '{}') ?? {};
- } catch {
- return {};
+export const toStoredStatus = (result: Pick<McpTestResult, 'ok' | 'tools'>, at: string): StoredStatus => ({
+ ok: result.ok === true,
+ tools: (result.tools ?? []).map((t) => t.name).filter((n): n is string => typeof n === 'string'),
+ at,
+});
+
+/** Keeps only the safe fields of each entry; drops anything malformed (and old `error` text). */
+export const sanitizeStatuses = (raw: unknown): Record<string, StoredStatus> => {
+ const out: Record<string, StoredStatus> = {};
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return out;
+ for (const [id, entry] of Object.entries(raw as Record<string, unknown>)) {
+ if (!entry || typeof entry !== 'object') continue;
+ const e = entry as Record<string, unknown>;
+ if (typeof e.ok !== 'boolean') continue;
+ out[id] = {
+ ok: e.ok,
+ tools: Array.isArray(e.tools) ? e.tools.filter((n): n is string => typeof n === 'string') : [],
+ at: typeof e.at === 'string' ? e.at : '',
+ };
}
+ return out;
};
export const saveStatuses = (statuses: Record<string, StoredStatus>) => {
try {
- localStorage.setItem(STATUS_KEY, JSON.stringify(statuses));
+ localStorage.setItem(STATUS_KEY, JSON.stringify(sanitizeStatuses(statuses)));
} catch {
// storage full or blocked: status is a nicety
}
};
+
+export const loadStatuses = (): Record<string, StoredStatus> => {
+ try {
+ const stored = localStorage.getItem(STATUS_KEY);
+ if (!stored) return {};
+ const clean = sanitizeStatuses(JSON.parse(stored));
+ // Rewrite older entries that still carry error text.
+ if (JSON.stringify(clean) !== stored) saveStatuses(clean);
+ return clean;
+ } catch {
+ try {
+ localStorage.removeItem(STATUS_KEY);
+ } catch {
+ // blocked
+ }
+ return {};
+ }
+};
diff --git a/frontend/tests/mcp.test.mjs b/frontend/tests/mcp.test.mjs
index 8e20ce13..7f2d3fb1 100644
--- a/frontend/tests/mcp.test.mjs
+++ b/frontend/tests/mcp.test.mjs
@@ -22,6 +22,10 @@ const {
parseToolCalls,
formatArgs,
mcpSupportFor,
+ toStoredStatus,
+ sanitizeStatuses,
+ loadStatuses,
+ saveStatuses,
} = mcp;
test('splits command lines like a README shows them', () => {
@@ -150,3 +154,47 @@ test('says honestly which engines can use MCP tools', () => {
assert.equal(auto.supported, true);
assert.match(auto.note, /Laya/);
});
+
+const memoryStorage = (initial = {}) => {
+ const data = { ...initial };
+ return {
+ data,
+ getItem: (k) => (k in data ? data[k] : null),
+ setItem: (k, v) => { data[k] = String(v); },
+ removeItem: (k) => { delete data[k]; },
+ };
+};
+
+test('stored test status keeps only ok, tool names and time, never error text', () => {
+ const failed = toStoredStatus({ ok: false, error: 'bad token SECRET-123', stderr_tail: 'SECRET-123', tools: undefined }, '2026-10-16T00:00:00Z');
+ assert.deepEqual(failed, { ok: false, tools: [], at: '2026-10-16T00:00:00Z' });
+ const ok = toStoredStatus({ ok: true, tools: [{ name: 'echo', description: 'x' }, { name: 'add' }] }, 't');
+ assert.deepEqual(ok, { ok: true, tools: ['echo', 'add'], at: 't' });
+
+ globalThis.localStorage = memoryStorage();
+ saveStatuses({ a: { ok: false, tools: [], at: 't', error: 'SECRET-123' } });
+ assert.deepEqual(JSON.parse(localStorage.data['kwnt.mcp.lastTest']), { a: { ok: false, tools: [], at: 't' } });
+ assert.ok(!localStorage.data['kwnt.mcp.lastTest'].includes('SECRET'));
+});
+
+test('old stored statuses with error text are cleaned on load and rewritten', () => {
+ const old = {
+ a: { ok: false, tools: [], error: 'Authorization: Bearer SECRET-456', at: 't1' },
+ b: { ok: true, tools: ['echo', 7], at: 't2' },
+ junk: 'nope',
+ noOk: { tools: ['x'] },
+ };
+ globalThis.localStorage = memoryStorage({ 'kwnt.mcp.lastTest': JSON.stringify(old) });
+ const loaded = loadStatuses();
+ assert.deepEqual(loaded, { a: { ok: false, tools: [], at: 't1' }, b: { ok: true, tools: ['echo'], at: 't2' } });
+ assert.ok(!localStorage.data['kwnt.mcp.lastTest'].includes('SECRET'));
+ assert.deepEqual(JSON.parse(localStorage.data['kwnt.mcp.lastTest']), loaded);
+
+ globalThis.localStorage = memoryStorage({ 'kwnt.mcp.lastTest': '{not json' });
+ assert.deepEqual(loadStatuses(), {});
+ assert.equal(localStorage.data['kwnt.mcp.lastTest'], undefined);
+
+ assert.deepEqual(sanitizeStatuses(null), {});
+ assert.deepEqual(sanitizeStatuses([{ ok: true }]), {});
+ delete globalThis.localStorage;
+});