diff --git a/client/src/App.tsx b/client/src/App.tsx index c7d8f81..8160fa4 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -20,6 +20,7 @@ import { ServerNotification, Tool, CompatibilityCallToolResult, + ClientNotification, } from "@modelcontextprotocol/sdk/types.js"; import { useEffect, useRef, useState } from "react"; @@ -90,7 +91,7 @@ const App = () => { const [url, setUrl] = useState("http://localhost:3001/sse"); const [transportType, setTransportType] = useState<"stdio" | "sse">("stdio"); const [requestHistory, setRequestHistory] = useState< - { request: string; response: string }[] + { request: string; response?: string }[] >([]); const [mcpClient, setMcpClient] = useState(null); const [notifications, setNotifications] = useState([]); @@ -158,10 +159,13 @@ const App = () => { ); }, []); - const pushHistory = (request: object, response: object) => { + const pushHistory = (request: object, response?: object) => { setRequestHistory((prev) => [ ...prev, - { request: JSON.stringify(request), response: JSON.stringify(response) }, + { + request: JSON.stringify(request), + response: response !== undefined ? JSON.stringify(response) : undefined, + }, ]); }; @@ -183,6 +187,20 @@ const App = () => { } }; + const sendNotification = async (notification: ClientNotification) => { + if (!mcpClient) { + throw new Error("MCP client not connected"); + } + + try { + await mcpClient.notification(notification); + pushHistory(notification); + } catch (e: unknown) { + setError((e as Error).message); + throw e; + } + }; + const listResources = async () => { const response = await makeRequest( { @@ -275,13 +293,7 @@ const App = () => { }; const handleRootsChange = async () => { - if (mcpClient) { - try { - await mcpClient.sendRootsListChanged(); - } catch (e) { - console.error("Failed to send roots list changed notification:", e); - } - } + sendNotification({ method: "notifications/roots/list_changed" }); }; const connectMcpServer = async () => { diff --git a/client/src/components/History.tsx b/client/src/components/History.tsx index 732f0ab..832194b 100644 --- a/client/src/components/History.tsx +++ b/client/src/components/History.tsx @@ -6,7 +6,7 @@ const HistoryAndNotifications = ({ requestHistory, serverNotifications, }: { - requestHistory: Array<{ request: string; response: string | null }>; + requestHistory: Array<{ request: string; response?: string }>; serverNotifications: ServerNotification[]; }) => { const [expandedRequests, setExpandedRequests] = useState<{