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 NotificationsTab from "./components/NotificationsTab";
import PromptsTab, { Prompt } from "./components/PromptsTab"; import PromptsTab, { Prompt } from "./components/PromptsTab";
import ToolsTab, { Tool as ToolType } from "./components/ToolsTab"; import ToolsTab, { Tool as ToolType } from "./components/ToolsTab";
import CommandHistory from "./components/CommandHistory";
const App = () => { const App = () => {
const [socket, setSocket] = useState<WebSocket | null>(null); const [socket, setSocket] = useState<WebSocket | null>(null);
@@ -39,6 +40,9 @@ const App = () => {
"/Users/ashwin/code/example-servers/build/everything/index.js", "/Users/ashwin/code/example-servers/build/everything/index.js",
); );
const [mcpConnected, setMcpConnected] = useState<boolean>(false); const [mcpConnected, setMcpConnected] = useState<boolean>(false);
const [commandHistory, setCommandHistory] = useState<
Array<{ command: string; response: string | null }>
>([]);
useEffect(() => { useEffect(() => {
const ws = new WebSocket("ws://localhost:3000"); const ws = new WebSocket("ws://localhost:3000");
@@ -55,25 +59,33 @@ const App = () => {
if (message.type === "resources") { if (message.type === "resources") {
setResources(message.data.resources); setResources(message.data.resources);
setError(null); setError(null);
updateCommandHistory(message);
} else if (message.type === "resource") { } else if (message.type === "resource") {
setResourceContent(JSON.stringify(message.data, null, 2)); setResourceContent(JSON.stringify(message.data, null, 2));
setError(null); setError(null);
updateCommandHistory(message);
} else if (message.type === "prompts") { } else if (message.type === "prompts") {
setPrompts(message.data.prompts); setPrompts(message.data.prompts);
setError(null); setError(null);
updateCommandHistory(message);
} else if (message.type === "prompt") { } else if (message.type === "prompt") {
setPromptContent(JSON.stringify(message.data, null, 2)); setPromptContent(JSON.stringify(message.data, null, 2));
setError(null); setError(null);
updateCommandHistory(message);
} else if (message.type === "tools") { } else if (message.type === "tools") {
setTools(message.data.tools); setTools(message.data.tools);
setError(null); setError(null);
updateCommandHistory(message);
} else if (message.type === "toolResult") { } else if (message.type === "toolResult") {
setToolResult(JSON.stringify(message.data, null, 2)); setToolResult(JSON.stringify(message.data, null, 2));
setError(null); setError(null);
updateCommandHistory(message);
} else if (message.type === "error") { } else if (message.type === "error") {
setError(message.message); setError(message.message);
updateCommandHistory(message);
} else if (message.type === "connected") { } else if (message.type === "connected") {
setMcpConnected(true); setMcpConnected(true);
updateCommandHistory(message);
} }
}; };
@@ -89,10 +101,29 @@ const App = () => {
return () => ws.close(); 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) => { const sendWebSocketMessage = (message: object) => {
if (socket) { if (socket) {
console.log("Sending WebSocket message:", message); console.log("Sending WebSocket message:", message);
socket.send(JSON.stringify(message)); socket.send(JSON.stringify(message));
setCommandHistory((prev) => [
...prev,
{ command: JSON.stringify(message), response: null },
]);
} }
}; };
@@ -136,7 +167,8 @@ const App = () => {
<Sidebar connectionStatus={connectionStatus} /> <Sidebar connectionStatus={connectionStatus} />
<div className="flex-1 flex flex-col overflow-hidden"> <div className="flex-1 flex flex-col overflow-hidden">
<h1 className="text-2xl font-bold p-4">MCP Inspector</h1> <h1 className="text-2xl font-bold p-4">MCP Inspector</h1>
<div className="flex-1 overflow-auto"> <div className="flex-1 overflow-auto flex">
<div className="flex-1">
<div className="p-4 bg-white shadow-md m-4 rounded-md"> <div className="p-4 bg-white shadow-md m-4 rounded-md">
<h2 className="text-lg font-semibold mb-2">Connect MCP Server</h2> <h2 className="text-lg font-semibold mb-2">Connect MCP Server</h2>
<div className="flex space-x-2 mb-2"> <div className="flex space-x-2 mb-2">
@@ -228,6 +260,8 @@ const App = () => {
</div> </div>
</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;