From 1095e48c448e23403d413da03db06cad3fadfb7f Mon Sep 17 00:00:00 2001 From: Ashwin Bhat Date: Tue, 8 Oct 2024 08:20:18 -0700 Subject: [PATCH] add ability to connect manually --- client/src/App.tsx | 161 ++++++++++++++++++++++++++++---------------- server/src/index.ts | 23 +++++-- 2 files changed, 119 insertions(+), 65 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index 6768047..01d5552 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -6,8 +6,11 @@ import { Files, MessageSquare, Hammer, + Play, } from "lucide-react"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { Input } from "@/components/ui/input"; +import { Button } from "@/components/ui/button"; import ConsoleTab from "./components/ConsoleTab"; import Sidebar from "./components/Sidebar"; @@ -29,6 +32,13 @@ const App = () => { const [tools, setTools] = useState([]); const [toolResult, setToolResult] = useState(""); const [error, setError] = useState(null); + const [command, setCommand] = useState( + "/Users/ashwin/.nvm/versions/node/v18.20.4/bin/node", + ); + const [args, setArgs] = useState( + "/Users/ashwin/code/example-servers/build/everything/index.js", + ); + const [mcpConnected, setMcpConnected] = useState(false); useEffect(() => { const ws = new WebSocket("ws://localhost:3000"); @@ -62,6 +72,8 @@ const App = () => { setError(null); } else if (message.type === "error") { setError(message.message); + } else if (message.type === "connected") { + setMcpConnected(true); } }; @@ -71,6 +83,7 @@ const App = () => { ws.onclose = () => { setConnectionStatus("disconnected"); + setMcpConnected(false); }; return () => ws.close(); @@ -113,73 +126,105 @@ const App = () => { sendWebSocketMessage({ type: "callTool", name, params }); }; + const connectMcpServer = () => { + const argsArray = args.split(" ").filter((arg) => arg.trim() !== ""); + sendWebSocketMessage({ type: "connect", command, args: argsArray }); + }; + return (

MCP Inspector

- - - - - Resources - - - - Prompts - - - - Requests - - - - Notifications - - - - Tools - - - - Console - - - -
- - +

Connect MCP Server

+
+ setCommand(e.target.value)} /> - - setArgs(e.target.value)} /> - - +
- +
+ {mcpConnected ? ( + + + + + Resources + + + + Prompts + + + + Requests + + + + Notifications + + + + Tools + + + + Console + + + +
+ + + + + + +
+
+ ) : ( +
+

+ Connect to an MCP server to start inspecting +

+
+ )}
diff --git a/server/src/index.ts b/server/src/index.ts index ae6ed06..2376494 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -7,18 +7,25 @@ const app = express(); const server = http.createServer(app); const wss = new WebSocketServer({ server }); -const mcpClient = new McpClient("MyApp", "1.0.0"); -await mcpClient.connectStdio( - "/Users/ashwin/.nvm/versions/node/v18.20.4/bin/node", - ["/Users/ashwin/code/example-servers/build/everything/index.js"], -); +let mcpClient: McpClient | null = null; wss.on("connection", (ws: WebSocket) => { ws.on("message", async (message: string) => { try { const command = JSON.parse(message); - if (command.type === "listResources") { + if (command.type === "connect" && command.command && command.args) { + mcpClient = new McpClient("MyApp", "1.0.0"); + await mcpClient.connectStdio(command.command, command.args); + ws.send(JSON.stringify({ type: "connected" })); + } else if (!mcpClient) { + ws.send( + JSON.stringify({ + type: "error", + message: "Not connected to MCP server", + }), + ); + } else if (command.type === "listResources") { const resources = await mcpClient.listResources(); ws.send(JSON.stringify({ type: "resources", data: resources })); } else if (command.type === "readResource" && command.uri) { @@ -58,6 +65,8 @@ server.listen(PORT, () => { // Close the client when the server is shutting down process.on("SIGINT", async () => { - await mcpClient.close(); + if (mcpClient) { + await mcpClient.close(); + } process.exit(); });