show server stderr in inspector UI

This commit is contained in:
Ashwin Bhat
2024-11-20 18:11:37 -08:00
parent 9196c1ddaf
commit 22bf78720b
4 changed files with 85 additions and 4 deletions

View File

@@ -12,17 +12,26 @@ import {
ListPromptsResultSchema, ListPromptsResultSchema,
ListResourcesResultSchema, ListResourcesResultSchema,
ListResourceTemplatesResultSchema, ListResourceTemplatesResultSchema,
Request,
ListRootsRequestSchema, ListRootsRequestSchema,
ListToolsResultSchema, ListToolsResultSchema,
ProgressNotificationSchema, ProgressNotificationSchema,
ReadResourceResultSchema, ReadResourceResultSchema,
Resource, Resource,
ResourceTemplate, ResourceTemplate,
Result,
Root, Root,
ServerNotification, ServerNotification,
Tool, Tool,
} from "@modelcontextprotocol/sdk/types.js"; } from "@modelcontextprotocol/sdk/types.js";
import { useCallback, useEffect, useRef, useState } from "react"; import { useCallback, useEffect, useRef, useState } from "react";
import {
StderrNotificationSchema,
StdErrNotification,
Notification,
} from "./lib/notificationTypes";
// Add dark mode class based on system preference // Add dark mode class based on system preference
if (window.matchMedia("(prefers-color-scheme: dark)").matches) { if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
document.documentElement.classList.add("dark"); document.documentElement.classList.add("dark");
@@ -87,6 +96,9 @@ const App = () => {
>([]); >([]);
const [mcpClient, setMcpClient] = useState<Client | null>(null); const [mcpClient, setMcpClient] = useState<Client | null>(null);
const [notifications, setNotifications] = useState<ServerNotification[]>([]); const [notifications, setNotifications] = useState<ServerNotification[]>([]);
const [stdErrNotifications, setStdErrNotifications] = useState<
StdErrNotification[]
>([]);
const [roots, setRoots] = useState<Root[]>([]); const [roots, setRoots] = useState<Root[]>([]);
const [env, setEnv] = useState<Record<string, string>>({}); const [env, setEnv] = useState<Record<string, string>>({});
@@ -383,7 +395,7 @@ const App = () => {
const connectMcpServer = async () => { const connectMcpServer = async () => {
try { try {
const client = new Client( const client = new Client<Request, Notification, Result>(
{ {
name: "mcp-inspector", name: "mcp-inspector",
version: "0.0.1", version: "0.0.1",
@@ -411,8 +423,6 @@ const App = () => {
} }
const clientTransport = new SSEClientTransport(backendUrl); const clientTransport = new SSEClientTransport(backendUrl);
await client.connect(clientTransport);
client.setNotificationHandler( client.setNotificationHandler(
ProgressNotificationSchema, ProgressNotificationSchema,
(notification) => { (notification) => {
@@ -423,6 +433,18 @@ const App = () => {
}, },
); );
client.setNotificationHandler(
StderrNotificationSchema,
(notification) => {
setStdErrNotifications((prevErrorNotifications) => [
...prevErrorNotifications,
notification,
]);
},
);
await client.connect(clientTransport);
client.setRequestHandler(CreateMessageRequestSchema, (request) => { client.setRequestHandler(CreateMessageRequestSchema, (request) => {
return new Promise<CreateMessageResult>((resolve, reject) => { return new Promise<CreateMessageResult>((resolve, reject) => {
setPendingSampleRequests((prev) => [ setPendingSampleRequests((prev) => [
@@ -459,6 +481,7 @@ const App = () => {
env={env} env={env}
setEnv={setEnv} setEnv={setEnv}
onConnect={connectMcpServer} onConnect={connectMcpServer}
stdErrNotifications={stdErrNotifications}
/> />
<div className="flex-1 flex flex-col overflow-hidden"> <div className="flex-1 flex flex-col overflow-hidden">
<div className="flex-1 overflow-auto"> <div className="flex-1 overflow-auto">

View File

@@ -10,6 +10,7 @@ import {
SelectTrigger, SelectTrigger,
SelectValue, SelectValue,
} from "@/components/ui/select"; } from "@/components/ui/select";
import { StdErrNotification } from "@/lib/notificationTypes";
interface SidebarProps { interface SidebarProps {
connectionStatus: "disconnected" | "connected" | "error"; connectionStatus: "disconnected" | "connected" | "error";
@@ -24,6 +25,7 @@ interface SidebarProps {
env: Record<string, string>; env: Record<string, string>;
setEnv: (env: Record<string, string>) => void; setEnv: (env: Record<string, string>) => void;
onConnect: () => void; onConnect: () => void;
stdErrNotifications: StdErrNotification[];
} }
const Sidebar = ({ const Sidebar = ({
@@ -39,6 +41,7 @@ const Sidebar = ({
env, env,
setEnv, setEnv,
onConnect, onConnect,
stdErrNotifications,
}: SidebarProps) => { }: SidebarProps) => {
const [showEnvVars, setShowEnvVars] = useState(false); const [showEnvVars, setShowEnvVars] = useState(false);
@@ -187,6 +190,25 @@ const Sidebar = ({
: "Disconnected"} : "Disconnected"}
</span> </span>
</div> </div>
{stdErrNotifications.length > 0 && (
<>
<div className="mt-4 border-t border-gray-200 pt-4">
<h3 className="text-sm font-medium">
Error output from MCP server
</h3>
<div className="mt-2 max-h-80 overflow-y-auto">
{stdErrNotifications.map((notification, index) => (
<div
key={index}
className="text-sm text-red-500 font-mono py-2 border-b border-gray-200 last:border-b-0"
>
{notification.params.content}
</div>
))}
</div>
</div>
</>
)}
</div> </div>
</div> </div>
</div> </div>

View File

@@ -0,0 +1,19 @@
import {
ClientNotificationSchema,
NotificationSchema as BaseNotificationSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
export const StderrNotificationSchema = BaseNotificationSchema.extend({
method: z.literal("notifications/stderr"),
params: z.object({
content: z.string(),
}),
});
export const NotificationSchema = ClientNotificationSchema.or(
StderrNotificationSchema,
);
export type StdErrNotification = z.infer<typeof StderrNotificationSchema>;
export type Notification = z.infer<typeof NotificationSchema>;

View File

@@ -42,7 +42,12 @@ const createTransport = async (query: express.Request["query"]) => {
console.log( console.log(
`Stdio transport: command=${command}, args=${args}, env=${JSON.stringify(env)}`, `Stdio transport: command=${command}, args=${args}, env=${JSON.stringify(env)}`,
); );
const transport = new StdioClientTransport({ command, args, env }); const transport = new StdioClientTransport({
command,
args,
env,
stderr: "pipe",
});
await transport.start(); await transport.start();
console.log("Spawned stdio transport"); console.log("Spawned stdio transport");
return transport; return transport;
@@ -75,6 +80,18 @@ app.get("/sse", async (req, res) => {
await webAppTransport.start(); await webAppTransport.start();
if (backingServerTransport.stderr) {
backingServerTransport.stderr.on("data", (chunk) => {
webAppTransport.send({
jsonrpc: "2.0",
method: "notifications/stderr",
params: {
content: chunk.toString(),
},
});
});
}
mcpProxy({ mcpProxy({
transportToClient: webAppTransport, transportToClient: webAppTransport,
transportToServer: backingServerTransport, transportToServer: backingServerTransport,