Refactor basic-host to use ServerSelect component

Extract server dropdown into ServerSelect component and simplify
ToolCallInfoPanel to read server name from toolCallInfo.serverInfo.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jonathan Hefner 2025-12-08 15:20:32 -06:00
commit d11b656d8e
2 changed files with 112 additions and 104 deletions

View file

@ -1,9 +1,3 @@
.connecting {
padding: 1rem 0;
text-align: center;
color: #666;
}
.callToolPanel, .toolCallInfoPanel { .callToolPanel, .toolCallInfoPanel {
margin: 0 auto; margin: 0 auto;
padding: 1rem; padding: 1rem;
@ -16,9 +10,6 @@
} }
.callToolPanel { .callToolPanel {
display: flex;
flex-direction: column;
gap: 1rem;
max-width: 480px; max-width: 480px;
form { form {
@ -39,12 +30,16 @@
padding: 0.5rem; padding: 0.5rem;
border: 1px solid #ccc; border: 1px solid #ccc;
border-radius: 4px; border-radius: 4px;
font-family: monospace;
font-size: inherit; font-size: inherit;
} }
textarea { .toolSelect {
font-family: monospace;
}
.toolInput {
min-height: 6rem; min-height: 6rem;
font-family: monospace;
resize: vertical; resize: vertical;
&[aria-invalid="true"] { &[aria-invalid="true"] {
@ -92,10 +87,15 @@
gap: 0.5rem; gap: 0.5rem;
min-width: 0; min-width: 0;
.toolName { h2 {
display: flex;
flex-direction: column;
margin: 0; margin: 0;
font-family: monospace;
font-size: 1.5rem; font-size: 1.5rem;
.toolName {
font-family: monospace;
}
} }
} }

View file

@ -4,84 +4,39 @@ import { callTool, connectToServer, hasAppHtml, initializeApp, loadSandboxProxy,
import styles from "./index.module.css"; import styles from "./index.module.css";
// Wrapper to track server name with each tool call // Host passes serversPromise to CallToolPanel
interface ToolCallEntry {
serverName: string;
info: ToolCallInfo;
}
// Host receives connected servers via promise, uses single use() call
interface HostProps { interface HostProps {
serversPromise: Promise<ServerInfo[]>; serversPromise: Promise<ServerInfo[]>;
} }
function Host({ serversPromise }: HostProps) { function Host({ serversPromise }: HostProps) {
const servers = use(serversPromise); const [toolCalls, setToolCalls] = useState<ToolCallInfo[]>([]);
const [toolCalls, setToolCalls] = useState<ToolCallEntry[]>([]);
if (servers.length === 0) {
return <p>No servers configured. Set SERVERS environment variable.</p>;
}
return ( return (
<> <>
{toolCalls.map((entry, i) => ( {toolCalls.map((info, i) => (
<ToolCallInfoPanel key={i} serverName={entry.serverName} toolCallInfo={entry.info} /> <ToolCallInfoPanel key={i} toolCallInfo={info} />
))} ))}
<CallToolPanel <CallToolPanel
servers={servers} serversPromise={serversPromise}
addToolCall={(serverName, info) => setToolCalls([...toolCalls, { serverName, info }])} addToolCall={(info) => setToolCalls([...toolCalls, info])}
/> />
</> </>
); );
} }
// CallToolPanel manages server selection from already-connected servers // CallToolPanel renders the unified form with Suspense around ServerSelect
interface CallToolPanelProps { interface CallToolPanelProps {
servers: ServerInfo[]; serversPromise: Promise<ServerInfo[]>;
addToolCall: (serverName: string, info: ToolCallInfo) => void; addToolCall: (info: ToolCallInfo) => void;
} }
function CallToolPanel({ servers, addToolCall }: CallToolPanelProps) { function CallToolPanel({ serversPromise, addToolCall }: CallToolPanelProps) {
const [selectedIndex, setSelectedIndex] = useState(0); const [selectedServer, setSelectedServer] = useState<ServerInfo | null>(null);
const selectedServer = servers[selectedIndex]; const [selectedTool, setSelectedTool] = useState("");
return (
<div className={styles.callToolPanel}>
<label>
Server
<select
value={selectedIndex}
onChange={(e) => setSelectedIndex(Number(e.target.value))}
>
{servers.map((server, i) => (
<option key={i} value={i}>
{server.name}
</option>
))}
</select>
</label>
<ToolCallForm
key={selectedIndex}
serverName={selectedServer.name}
serverInfo={selectedServer}
addToolCall={addToolCall}
/>
</div>
);
}
// ToolCallForm receives already-resolved serverInfo
interface ToolCallFormProps {
serverName: string;
serverInfo: ServerInfo;
addToolCall: (serverName: string, info: ToolCallInfo) => void;
}
function ToolCallForm({ serverName, serverInfo, addToolCall }: ToolCallFormProps) {
const toolNames = Array.from(serverInfo.tools.keys());
const [selectedTool, setSelectedTool] = useState(toolNames[0] ?? "");
const [inputJson, setInputJson] = useState("{}"); const [inputJson, setInputJson] = useState("{}");
const toolNames = selectedServer ? Array.from(selectedServer.tools.keys()) : [];
const isValidJson = useMemo(() => { const isValidJson = useMemo(() => {
try { try {
JSON.parse(inputJson); JSON.parse(inputJson);
@ -91,49 +46,104 @@ function ToolCallForm({ serverName, serverInfo, addToolCall }: ToolCallFormProps
} }
}, [inputJson]); }, [inputJson]);
const handleServerSelect = (server: ServerInfo) => {
setSelectedServer(server);
const [firstTool] = server.tools.keys();
setSelectedTool(firstTool ?? "");
};
const handleSubmit = () => { const handleSubmit = () => {
const toolCallInfo = callTool(serverInfo, selectedTool, JSON.parse(inputJson)); if (!selectedServer) return;
addToolCall(serverName, toolCallInfo); const toolCallInfo = callTool(selectedServer, selectedTool, JSON.parse(inputJson));
addToolCall(toolCallInfo);
}; };
return ( return (
<form onSubmit={(e) => { e.preventDefault(); handleSubmit(); }}> <div className={styles.callToolPanel}>
<label> <form onSubmit={(e) => { e.preventDefault(); handleSubmit(); }}>
Tool <label>
<select Server
value={selectedTool} <Suspense fallback={<select disabled><option>Loading...</option></select>}>
onChange={(e) => setSelectedTool(e.target.value)} <ServerSelect serversPromise={serversPromise} onSelect={handleServerSelect} />
> </Suspense>
{toolNames.map((name) => ( </label>
<option key={name} value={name}>{name}</option> <label>
))} Tool
</select> <select
</label> className={styles.toolSelect}
<label> value={selectedTool}
Input onChange={(e) => setSelectedTool(e.target.value)}
<textarea >
aria-invalid={!isValidJson} {selectedServer && toolNames.map((name) => (
value={inputJson} <option key={name} value={name}>{name}</option>
onChange={(e) => setInputJson(e.target.value)} ))}
/> </select>
</label> </label>
<button type="submit" disabled={!selectedTool || !isValidJson}> <label>
Call Tool Input
</button> <textarea
</form> className={styles.toolInput}
aria-invalid={!isValidJson}
value={inputJson}
onChange={(e) => setInputJson(e.target.value)}
/>
</label>
<button type="submit" disabled={!selectedTool || !isValidJson}>
Call Tool
</button>
</form>
</div>
);
}
// ServerSelect calls use() and renders the server <select>
interface ServerSelectProps {
serversPromise: Promise<ServerInfo[]>;
onSelect: (server: ServerInfo) => void;
}
function ServerSelect({ serversPromise, onSelect }: ServerSelectProps) {
const servers = use(serversPromise);
const [selectedIndex, setSelectedIndex] = useState(0);
useEffect(() => {
if (servers.length > selectedIndex) {
onSelect(servers[selectedIndex]);
}
}, [servers]);
if (servers.length === 0) {
return <select disabled><option>No servers configured</option></select>;
}
return (
<select
value={selectedIndex}
onChange={(e) => {
const newIndex = Number(e.target.value);
setSelectedIndex(newIndex);
onSelect(servers[newIndex]);
}}
>
{servers.map((server, i) => (
<option key={i} value={i}>{server.name}</option>
))}
</select>
); );
} }
interface ToolCallInfoPanelProps { interface ToolCallInfoPanelProps {
serverName: string;
toolCallInfo: ToolCallInfo; toolCallInfo: ToolCallInfo;
} }
function ToolCallInfoPanel({ serverName, toolCallInfo }: ToolCallInfoPanelProps) { function ToolCallInfoPanel({ toolCallInfo }: ToolCallInfoPanelProps) {
return ( return (
<div className={styles.toolCallInfoPanel}> <div className={styles.toolCallInfoPanel}>
<div className={styles.inputInfoPanel}> <div className={styles.inputInfoPanel}>
<h2 className={styles.toolName}>{serverName}:{toolCallInfo.tool.name}</h2> <h2>
<span>{toolCallInfo.serverInfo.name}</span>
<span className={styles.toolName}>{toolCallInfo.tool.name}</span>
</h2>
<JsonBlock value={toolCallInfo.input} /> <JsonBlock value={toolCallInfo.input} />
</div> </div>
<div className={styles.outputInfoPanel}> <div className={styles.outputInfoPanel}>
@ -239,9 +249,7 @@ async function connectToAllServers(): Promise<ServerInfo[]> {
createRoot(document.getElementById("root")!).render( createRoot(document.getElementById("root")!).render(
<StrictMode> <StrictMode>
<ErrorBoundary> <ErrorBoundary>
<Suspense fallback={<p className={styles.connecting}>Connecting to servers...</p>}> <Host serversPromise={connectToAllServers()} />
<Host serversPromise={connectToAllServers()} />
</Suspense>
</ErrorBoundary> </ErrorBoundary>
</StrictMode>, </StrictMode>,
); );