refactor: simplify capability handling and remove context provider

- Remove redundant useEffect for capability checking
- Remove CapabilityContext provider pattern
- Set default tab to first supported capability
- Add fallback UI for unsupported capabilities
- Delete unused contexts.ts file
This commit is contained in:
Devin AI
2024-12-09 10:11:03 +00:00
parent e96b3be159
commit d857e1462b
2 changed files with 178 additions and 159 deletions

View File

@@ -19,10 +19,11 @@ import {
Request, Request,
Resource, Resource,
ResourceTemplate, ResourceTemplate,
Result,
Root, Root,
ServerNotification, ServerNotification,
Tool, Tool,
ServerCapabilitiesSchema,
Result,
} from "@modelcontextprotocol/sdk/types.js"; } from "@modelcontextprotocol/sdk/types.js";
import { useCallback, useEffect, useRef, useState } from "react"; import { useCallback, useEffect, useRef, useState } from "react";
@@ -43,7 +44,7 @@ import {
} from "lucide-react"; } from "lucide-react";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
import { ZodType } from "zod"; import { z, type ZodType } from "zod";
import "./App.css"; import "./App.css";
import ConsoleTab from "./components/ConsoleTab"; import ConsoleTab from "./components/ConsoleTab";
import HistoryAndNotifications from "./components/History"; import HistoryAndNotifications from "./components/History";
@@ -55,7 +56,12 @@ import SamplingTab, { PendingRequest } from "./components/SamplingTab";
import Sidebar from "./components/Sidebar"; import Sidebar from "./components/Sidebar";
import ToolsTab from "./components/ToolsTab"; import ToolsTab from "./components/ToolsTab";
import { CapabilityContext, ServerCapabilities } from "@/lib/contexts"; type ServerCapabilities = z.infer<typeof ServerCapabilitiesSchema>;
type ServerCapabilities = z.infer<typeof ServerCapabilitiesSchema>;
type ServerCapabilities = z.infer<typeof ServerCapabilitiesSchema>;
const DEFAULT_REQUEST_TIMEOUT_MSEC = 10000; const DEFAULT_REQUEST_TIMEOUT_MSEC = 10000;
@@ -220,12 +226,7 @@ const App = () => {
rootsRef.current = roots; rootsRef.current = roots;
}, [roots]); }, [roots]);
useEffect(() => {
if (mcpClient) {
const capabilities = mcpClient.getServerCapabilities();
setServerCapabilities(capabilities ?? null);
}
}, [mcpClient]);
const pushHistory = (request: object, response?: object) => { const pushHistory = (request: object, response?: object) => {
setRequestHistory((prev) => [ setRequestHistory((prev) => [
@@ -498,18 +499,25 @@ const App = () => {
<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">
{mcpClient ? ( {mcpClient ? (
<CapabilityContext.Provider value={serverCapabilities}> <Tabs
<Tabs defaultValue="resources" className="w-full p-4"> defaultValue={
serverCapabilities?.resources ? "resources" :
serverCapabilities?.prompts ? "prompts" :
serverCapabilities?.tools ? "tools" :
"ping"
}
className="w-full p-4"
>
<TabsList className="mb-4 p-0"> <TabsList className="mb-4 p-0">
<TabsTrigger value="resources" disabled={!serverCapabilities?.resources}> <TabsTrigger value="resources">
<Files className="w-4 h-4 mr-2" /> <Files className="w-4 h-4 mr-2" />
Resources Resources
</TabsTrigger> </TabsTrigger>
<TabsTrigger value="prompts" disabled={!serverCapabilities?.prompts}> <TabsTrigger value="prompts">
<MessageSquare className="w-4 h-4 mr-2" /> <MessageSquare className="w-4 h-4 mr-2" />
Prompts Prompts
</TabsTrigger> </TabsTrigger>
<TabsTrigger value="tools" disabled={!serverCapabilities?.tools}> <TabsTrigger value="tools">
<Hammer className="w-4 h-4 mr-2" /> <Hammer className="w-4 h-4 mr-2" />
Tools Tools
</TabsTrigger> </TabsTrigger>
@@ -533,12 +541,22 @@ const App = () => {
</TabsList> </TabsList>
<div className="w-full"> <div className="w-full">
{!serverCapabilities?.resources && !serverCapabilities?.prompts && !serverCapabilities?.tools ? (
<div className="flex items-center justify-center p-4">
<p className="text-lg text-gray-500">
The connected server does not support any MCP capabilities
</p>
</div>
) : (
<>
<ResourcesTab <ResourcesTab
resources={resources} resources={resources}
resourceTemplates={resourceTemplates} resourceTemplates={resourceTemplates}
listResources={() => { listResources={() => {
clearError("resources"); clearError("resources");
if (serverCapabilities?.resources) {
listResources(); listResources();
}
}} }}
clearResources={() => { clearResources={() => {
setResources([]); setResources([]);
@@ -546,7 +564,9 @@ const App = () => {
}} }}
listResourceTemplates={() => { listResourceTemplates={() => {
clearError("resources"); clearError("resources");
if (serverCapabilities?.resources) {
listResourceTemplates(); listResourceTemplates();
}
}} }}
clearResourceTemplates={() => { clearResourceTemplates={() => {
setResourceTemplates([]); setResourceTemplates([]);
@@ -554,7 +574,9 @@ const App = () => {
}} }}
readResource={(uri) => { readResource={(uri) => {
clearError("resources"); clearError("resources");
if (serverCapabilities?.resources) {
readResource(uri); readResource(uri);
}
}} }}
selectedResource={selectedResource} selectedResource={selectedResource}
setSelectedResource={(resource) => { setSelectedResource={(resource) => {
@@ -570,7 +592,9 @@ const App = () => {
prompts={prompts} prompts={prompts}
listPrompts={() => { listPrompts={() => {
clearError("prompts"); clearError("prompts");
if (serverCapabilities?.prompts) {
listPrompts(); listPrompts();
}
}} }}
clearPrompts={() => { clearPrompts={() => {
setPrompts([]); setPrompts([]);
@@ -578,7 +602,9 @@ const App = () => {
}} }}
getPrompt={(name, args) => { getPrompt={(name, args) => {
clearError("prompts"); clearError("prompts");
if (serverCapabilities?.prompts) {
getPrompt(name, args); getPrompt(name, args);
}
}} }}
selectedPrompt={selectedPrompt} selectedPrompt={selectedPrompt}
setSelectedPrompt={(prompt) => { setSelectedPrompt={(prompt) => {
@@ -593,7 +619,9 @@ const App = () => {
tools={tools} tools={tools}
listTools={() => { listTools={() => {
clearError("tools"); clearError("tools");
if (serverCapabilities?.tools) {
listTools(); listTools();
}
}} }}
clearTools={() => { clearTools={() => {
setTools([]); setTools([]);
@@ -601,7 +629,9 @@ const App = () => {
}} }}
callTool={(name, params) => { callTool={(name, params) => {
clearError("tools"); clearError("tools");
if (serverCapabilities?.tools) {
callTool(name, params); callTool(name, params);
}
}} }}
selectedTool={selectedTool} selectedTool={selectedTool}
setSelectedTool={(tool) => { setSelectedTool={(tool) => {
@@ -634,9 +664,10 @@ const App = () => {
setRoots={setRoots} setRoots={setRoots}
onRootsChange={handleRootsChange} onRootsChange={handleRootsChange}
/> />
</>
)}
</div> </div>
</Tabs> </Tabs>
</CapabilityContext.Provider>
) : ( ) : (
<div className="flex items-center justify-center h-full"> <div className="flex items-center justify-center h-full">
<p className="text-lg text-gray-500"> <p className="text-lg text-gray-500">

View File

@@ -1,12 +0,0 @@
import { createContext, useContext } from "react";
import { ServerCapabilitiesSchema } from "@modelcontextprotocol/sdk/types.js";
import type { z } from "zod";
export type ServerCapabilities = z.infer<typeof ServerCapabilitiesSchema>;
export const CapabilityContext = createContext<ServerCapabilities | null>(null);
export function useServerCapability(capability: keyof ServerCapabilities): boolean {
const capabilities = useContext(CapabilityContext);
return !!capabilities?.[capability];
}