Add log level setting in UI

* This fixes #188
* In App.tsx
  - import LoggingLevel from sdk
  - add [logLevel, setLogLevel] useState with value of type LoggingLevel initialized to "debug"
  - add useEffect that stores the new logLevel in localStorage as "logLevel"
  - added sendLogLevelRequest function that takes a level argument of type LoggingLevel and sends the appropriate request. It calls setLogLevel when done, to update the local UI
  - pass logLevel and sendLogLevelRequest to Sidebar component as props
* In Sidebar.tsx
  - Import LoggingLevel and LoggingLevelSchema from sdk
  - add props and prop types for  logLevel and sendLogLevelRequest and loggingSupported
  - add Select component populated with the enum values of LoggingLevelSchema, shown only if loggingSupported is true and connectionStatus is "connected"
*
This commit is contained in:
cliffhall
2025-03-15 16:37:10 -04:00
parent cedf02d152
commit 5104952239
2 changed files with 50 additions and 0 deletions

View File

@@ -15,6 +15,7 @@ import {
Root,
ServerNotification,
Tool,
LoggingLevel,
} from "@modelcontextprotocol/sdk/types.js";
import React, { Suspense, useEffect, useRef, useState } from "react";
import { useConnection } from "./lib/hooks/useConnection";
@@ -91,6 +92,7 @@ const App = () => {
(localStorage.getItem("lastTransportType") as "stdio" | "sse") || "stdio"
);
});
const [logLevel, setLogLevel] = useState<LoggingLevel>("debug");
const [notifications, setNotifications] = useState<ServerNotification[]>([]);
const [stdErrNotifications, setStdErrNotifications] = useState<
StdErrNotification[]
@@ -412,6 +414,17 @@ const App = () => {
await sendNotification({ method: "notifications/roots/list_changed" });
};
const sendLogLevelRequest = async (level: LoggingLevel) => {
await makeRequest(
{
method: "logging/setLevel" as const,
params: { level },
},
z.object({}),
);
setLogLevel(level);
};
return (
<div className="flex h-screen bg-background">
<Sidebar
@@ -430,6 +443,11 @@ const App = () => {
setBearerToken={setBearerToken}
onConnect={connectMcpServer}
stdErrNotifications={stdErrNotifications}
logLevel={logLevel}
sendLogLevelRequest={sendLogLevelRequest}
loggingSupported={
!!serverCapabilities?.logging || false
}
/>
<div className="flex-1 flex flex-col overflow-hidden">
<div className="flex-1 overflow-auto">