add command history

This commit is contained in:
Ashwin Bhat
2024-10-08 13:06:08 -07:00
parent d2a7231344
commit 58e796a5a8
2 changed files with 213 additions and 85 deletions

View File

@@ -19,6 +19,7 @@ import ResourcesTab, { Resource } from "./components/ResourcesTab";
import NotificationsTab from "./components/NotificationsTab";
import PromptsTab, { Prompt } from "./components/PromptsTab";
import ToolsTab, { Tool as ToolType } from "./components/ToolsTab";
import CommandHistory from "./components/CommandHistory";
const App = () => {
const [socket, setSocket] = useState<WebSocket | null>(null);
@@ -39,6 +40,9 @@ const App = () => {
"/Users/ashwin/code/example-servers/build/everything/index.js",
);
const [mcpConnected, setMcpConnected] = useState<boolean>(false);
const [commandHistory, setCommandHistory] = useState<
Array<{ command: string; response: string | null }>
>([]);
useEffect(() => {
const ws = new WebSocket("ws://localhost:3000");
@@ -55,25 +59,33 @@ const App = () => {
if (message.type === "resources") {
setResources(message.data.resources);
setError(null);
updateCommandHistory(message);
} else if (message.type === "resource") {
setResourceContent(JSON.stringify(message.data, null, 2));
setError(null);
updateCommandHistory(message);
} else if (message.type === "prompts") {
setPrompts(message.data.prompts);
setError(null);
updateCommandHistory(message);
} else if (message.type === "prompt") {
setPromptContent(JSON.stringify(message.data, null, 2));
setError(null);
updateCommandHistory(message);
} else if (message.type === "tools") {
setTools(message.data.tools);
setError(null);
updateCommandHistory(message);
} else if (message.type === "toolResult") {
setToolResult(JSON.stringify(message.data, null, 2));
setError(null);
updateCommandHistory(message);
} else if (message.type === "error") {
setError(message.message);
updateCommandHistory(message);
} else if (message.type === "connected") {
setMcpConnected(true);
updateCommandHistory(message);
}
};
@@ -89,10 +101,29 @@ const App = () => {
return () => ws.close();
}, []);
const updateCommandHistory = (response: unknown) => {
setCommandHistory((prev) => {
const lastCommand = prev[prev.length - 1];
if (lastCommand && lastCommand.response === null) {
const updatedHistory = [...prev];
updatedHistory[updatedHistory.length - 1] = {
...lastCommand,
response: JSON.stringify(response),
};
return updatedHistory;
}
return prev;
});
};
const sendWebSocketMessage = (message: object) => {
if (socket) {
console.log("Sending WebSocket message:", message);
socket.send(JSON.stringify(message));
setCommandHistory((prev) => [
...prev,
{ command: JSON.stringify(message), response: null },
]);
}
};
@@ -136,97 +167,100 @@ const App = () => {
<Sidebar connectionStatus={connectionStatus} />
<div className="flex-1 flex flex-col overflow-hidden">
<h1 className="text-2xl font-bold p-4">MCP Inspector</h1>
<div className="flex-1 overflow-auto">
<div className="p-4 bg-white shadow-md m-4 rounded-md">
<h2 className="text-lg font-semibold mb-2">Connect MCP Server</h2>
<div className="flex space-x-2 mb-2">
<Input
placeholder="Command"
value={command}
onChange={(e) => setCommand(e.target.value)}
/>
<Input
placeholder="Arguments (space-separated)"
value={args}
onChange={(e) => setArgs(e.target.value)}
/>
<Button onClick={connectMcpServer}>
<Play className="w-4 h-4 mr-2" />
Connect
</Button>
</div>
</div>
{mcpConnected ? (
<Tabs defaultValue="resources" className="w-full p-4">
<TabsList className="mb-4 p-0">
<TabsTrigger value="resources">
<Files className="w-4 h-4 mr-2" />
Resources
</TabsTrigger>
<TabsTrigger value="prompts">
<MessageSquare className="w-4 h-4 mr-2" />
Prompts
</TabsTrigger>
<TabsTrigger value="requests" disabled>
<Send className="w-4 h-4 mr-2" />
Requests
</TabsTrigger>
<TabsTrigger value="notifications" disabled>
<Bell className="w-4 h-4 mr-2" />
Notifications
</TabsTrigger>
<TabsTrigger value="tools" disabled>
<Hammer className="w-4 h-4 mr-2" />
Tools
</TabsTrigger>
<TabsTrigger value="console" disabled>
<Terminal className="w-4 h-4 mr-2" />
Console
</TabsTrigger>
</TabsList>
<div className="w-full">
<ResourcesTab
resources={resources}
listResources={listResources}
readResource={readResource}
selectedResource={selectedResource}
setSelectedResource={setSelectedResource}
resourceContent={resourceContent}
error={error}
<div className="flex-1 overflow-auto flex">
<div className="flex-1">
<div className="p-4 bg-white shadow-md m-4 rounded-md">
<h2 className="text-lg font-semibold mb-2">Connect MCP Server</h2>
<div className="flex space-x-2 mb-2">
<Input
placeholder="Command"
value={command}
onChange={(e) => setCommand(e.target.value)}
/>
<NotificationsTab />
<PromptsTab
prompts={prompts}
listPrompts={listPrompts}
getPrompt={getPrompt}
selectedPrompt={selectedPrompt}
setSelectedPrompt={setSelectedPrompt}
promptContent={promptContent}
error={error}
<Input
placeholder="Arguments (space-separated)"
value={args}
onChange={(e) => setArgs(e.target.value)}
/>
<RequestsTab />
<ToolsTab
tools={tools}
listTools={listTools}
callTool={callTool}
selectedTool={selectedTool}
setSelectedTool={setSelectedTool}
toolResult={toolResult}
error={error}
/>
<ConsoleTab />
<Button onClick={connectMcpServer}>
<Play className="w-4 h-4 mr-2" />
Connect
</Button>
</div>
</Tabs>
) : (
<div className="flex items-center justify-center h-full">
<p className="text-lg text-gray-500">
Connect to an MCP server to start inspecting
</p>
</div>
)}
{mcpConnected ? (
<Tabs defaultValue="resources" className="w-full p-4">
<TabsList className="mb-4 p-0">
<TabsTrigger value="resources">
<Files className="w-4 h-4 mr-2" />
Resources
</TabsTrigger>
<TabsTrigger value="prompts">
<MessageSquare className="w-4 h-4 mr-2" />
Prompts
</TabsTrigger>
<TabsTrigger value="requests" disabled>
<Send className="w-4 h-4 mr-2" />
Requests
</TabsTrigger>
<TabsTrigger value="notifications" disabled>
<Bell className="w-4 h-4 mr-2" />
Notifications
</TabsTrigger>
<TabsTrigger value="tools" disabled>
<Hammer className="w-4 h-4 mr-2" />
Tools
</TabsTrigger>
<TabsTrigger value="console" disabled>
<Terminal className="w-4 h-4 mr-2" />
Console
</TabsTrigger>
</TabsList>
<div className="w-full">
<ResourcesTab
resources={resources}
listResources={listResources}
readResource={readResource}
selectedResource={selectedResource}
setSelectedResource={setSelectedResource}
resourceContent={resourceContent}
error={error}
/>
<NotificationsTab />
<PromptsTab
prompts={prompts}
listPrompts={listPrompts}
getPrompt={getPrompt}
selectedPrompt={selectedPrompt}
setSelectedPrompt={setSelectedPrompt}
promptContent={promptContent}
error={error}
/>
<RequestsTab />
<ToolsTab
tools={tools}
listTools={listTools}
callTool={callTool}
selectedTool={selectedTool}
setSelectedTool={setSelectedTool}
toolResult={toolResult}
error={error}
/>
<ConsoleTab />
</div>
</Tabs>
) : (
<div className="flex items-center justify-center h-full">
<p className="text-lg text-gray-500">
Connect to an MCP server to start inspecting
</p>
</div>
)}
</div>
</div>
</div>
<CommandHistory commandHistory={commandHistory} />
</div>
);
};

View File

@@ -0,0 +1,94 @@
import { useState } from "react";
import { Copy } from "lucide-react";
const CommandHistory = ({
commandHistory,
}: {
commandHistory: Array<{ command: string; response: string | null }>;
}) => {
const [expandedCommands, setExpandedCommands] = useState<{
[key: number]: boolean;
}>({});
const toggleCommandExpansion = (index: number) => {
setExpandedCommands((prev) => ({ ...prev, [index]: !prev[index] }));
};
const copyToClipboard = (text: string) => {
navigator.clipboard.writeText(text);
};
return (
<div className="w-64 bg-white shadow-md p-4 overflow-y-auto">
<h2 className="text-lg font-semibold mb-4">Command History</h2>
<ul className="space-y-3">
{commandHistory
.slice()
.reverse()
.map((cmd, index) => (
<li
key={index}
className="text-sm text-gray-600 bg-gray-100 p-2 rounded"
>
<div
className="flex justify-between items-center cursor-pointer"
onClick={() =>
toggleCommandExpansion(commandHistory.length - 1 - index)
}
>
<span className="font-mono">
{commandHistory.length - index}.{" "}
{JSON.parse(cmd.command).type}
</span>
<span>
{expandedCommands[commandHistory.length - 1 - index]
? "▼"
: "▶"}
</span>
</div>
{expandedCommands[commandHistory.length - 1 - index] && (
<>
<div className="mt-2">
<div className="flex justify-between items-center mb-1">
<span className="font-semibold text-blue-600">
Command:
</span>
<button
onClick={() => copyToClipboard(cmd.command)}
className="text-blue-500 hover:text-blue-700"
>
<Copy size={16} />
</button>
</div>
<pre className="whitespace-pre-wrap break-words bg-blue-50 p-2 rounded">
{JSON.stringify(JSON.parse(cmd.command), null, 2)}
</pre>
</div>
{cmd.response && (
<div className="mt-2">
<div className="flex justify-between items-center mb-1">
<span className="font-semibold text-green-600">
Response:
</span>
<button
onClick={() => copyToClipboard(cmd.response!)}
className="text-blue-500 hover:text-blue-700"
>
<Copy size={16} />
</button>
</div>
<pre className="whitespace-pre-wrap break-words bg-green-50 p-2 rounded">
{JSON.stringify(JSON.parse(cmd.response), null, 2)}
</pre>
</div>
)}
</>
)}
</li>
))}
</ul>
</div>
);
};
export default CommandHistory;