This commit is contained in:
Ashwin Bhat
2024-10-08 07:33:47 -07:00
parent 51cde09845
commit 38e1c8b2b9
5 changed files with 189 additions and 10 deletions

View File

@@ -1,5 +1,12 @@
import { useState, useEffect } from "react";
import { Send, Bell, Terminal, Files, MessageSquare } from "lucide-react";
import {
Send,
Bell,
Terminal,
Files,
MessageSquare,
Hammer,
} from "lucide-react";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import ConsoleTab from "./components/ConsoleTab";
@@ -8,6 +15,7 @@ import RequestsTab from "./components/RequestsTabs";
import ResourcesTab, { Resource } from "./components/ResourcesTab";
import NotificationsTab from "./components/NotificationsTab";
import PromptsTab, { Prompt } from "./components/PromptsTab";
import ToolsTab, { Tool as ToolType } from "./components/ToolsTab";
const App = () => {
const [socket, setSocket] = useState<WebSocket | null>(null);
@@ -18,6 +26,8 @@ const App = () => {
const [resourceContent, setResourceContent] = useState<string>("");
const [prompts, setPrompts] = useState<Prompt[]>([]);
const [promptContent, setPromptContent] = useState<string>("");
const [tools, setTools] = useState<ToolType[]>([]);
const [toolResult, setToolResult] = useState<string>("");
const [error, setError] = useState<string | null>(null);
useEffect(() => {
@@ -44,6 +54,12 @@ const App = () => {
} else if (message.type === "prompt") {
setPromptContent(JSON.stringify(message.data, null, 2));
setError(null);
} else if (message.type === "tools") {
setTools(message.data.tools);
setError(null);
} else if (message.type === "toolResult") {
setToolResult(JSON.stringify(message.data, null, 2));
setError(null);
} else if (message.type === "error") {
setError(message.message);
}
@@ -71,6 +87,7 @@ const App = () => {
null,
);
const [selectedPrompt, setSelectedPrompt] = useState<Prompt | null>(null);
const [selectedTool, setSelectedTool] = useState<ToolType | null>(null);
const listResources = () => {
sendWebSocketMessage({ type: "listResources" });
@@ -88,6 +105,14 @@ const App = () => {
sendWebSocketMessage({ type: "getPrompt", name });
};
const listTools = () => {
sendWebSocketMessage({ type: "listTools" });
};
const callTool = (name: string, params: Record<string, unknown>) => {
sendWebSocketMessage({ type: "callTool", name, params });
};
return (
<div className="flex h-screen bg-gray-100">
<Sidebar connectionStatus={connectionStatus} />
@@ -96,14 +121,6 @@ const App = () => {
<div className="flex-1 overflow-auto">
<Tabs defaultValue="requests" className="w-full p-4">
<TabsList className="mb-4">
<TabsTrigger value="requests">
<Send className="w-4 h-4 mr-2" />
Requests
</TabsTrigger>
<TabsTrigger value="notifications">
<Bell className="w-4 h-4 mr-2" />
Notifications
</TabsTrigger>
<TabsTrigger value="resources">
<Files className="w-4 h-4 mr-2" />
Resources
@@ -112,7 +129,19 @@ const App = () => {
<MessageSquare className="w-4 h-4 mr-2" />
Prompts
</TabsTrigger>
<TabsTrigger value="console">
<TabsTrigger value="requests" disabled>
<Send className="w-4 h-4 mr-2" />
Requests
</TabsTrigger>
<TabsTrigger value="notifications" disabled>
<Bell className="w-4 h-4 mr-2" />
Notifications
</TabsTrigger>
<TabsTrigger value="tools" disabled>
<Hammer className="w-4 h-4 mr-2" />
Tools
</TabsTrigger>
<TabsTrigger value="console" disabled>
<Terminal className="w-4 h-4 mr-2" />
Console
</TabsTrigger>
@@ -139,6 +168,15 @@ const App = () => {
promptContent={promptContent}
error={error}
/>
<ToolsTab
tools={tools}
listTools={listTools}
callTool={callTool}
selectedTool={selectedTool}
setSelectedTool={setSelectedTool}
toolResult={toolResult}
error={error}
/>
<ConsoleTab />
</div>
</Tabs>

View File

@@ -0,0 +1,101 @@
import { TabsContent } from "@/components/ui/tabs";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { Send, AlertCircle } from "lucide-react";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { useState } from "react";
export type Tool = {
name: string;
};
const ToolsTab = ({
tools,
listTools,
callTool,
selectedTool,
setSelectedTool,
toolResult,
error,
}: {
tools: Tool[];
listTools: () => void;
callTool: (name: string, params: Record<string, unknown>) => void;
selectedTool: Tool | null;
setSelectedTool: (tool: Tool) => void;
toolResult: string;
error: string | null;
}) => {
const [params, setParams] = useState("");
return (
<TabsContent value="tools" className="grid grid-cols-2 gap-4">
<div className="bg-white rounded-lg shadow">
<div className="p-4 border-b border-gray-200">
<h3 className="font-semibold">Tools</h3>
</div>
<div className="p-4">
<Button variant="outline" className="w-full mb-4" onClick={listTools}>
List Tools
</Button>
<div className="space-y-2">
{tools.map((tool, index) => (
<div
key={index}
className="flex items-center p-2 rounded hover:bg-gray-50 cursor-pointer"
onClick={() => setSelectedTool(tool)}
>
<span className="flex-1">{tool.name}</span>
</div>
))}
</div>
</div>
</div>
<div className="bg-white rounded-lg shadow">
<div className="p-4 border-b border-gray-200">
<h3 className="font-semibold">
{selectedTool ? selectedTool.name : "Select a tool"}
</h3>
</div>
<div className="p-4">
{error ? (
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
<AlertTitle>Error</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
) : selectedTool ? (
<div className="space-y-4">
<Textarea
placeholder="Tool parameters (JSON)"
className="h-32 font-mono"
value={params}
onChange={(e) => setParams(e.target.value)}
/>
<Button
onClick={() => callTool(selectedTool.name, JSON.parse(params))}
>
<Send className="w-4 h-4 mr-2" />
Run Tool
</Button>
{toolResult && (
<pre className="bg-gray-50 p-4 rounded text-sm overflow-auto max-h-64">
{JSON.stringify(toolResult, null, 2)}
</pre>
)}
</div>
) : (
<Alert>
<AlertDescription>
Select a tool from the list to view its details and run it
</AlertDescription>
</Alert>
)}
</div>
</div>
</TabsContent>
);
};
export default ToolsTab;

View File

@@ -10,6 +10,10 @@ import {
ListPromptsResultSchema,
GetPromptResult,
GetPromptResultSchema,
ListToolsResult,
ListToolsResultSchema,
CallToolResult,
CallToolResultSchema,
} from "mcp-typescript/types.js";
export class McpClient {
@@ -86,6 +90,29 @@ export class McpClient {
);
}
// Tool Operations
async listTools(): Promise<ListToolsResult> {
return await this.client.request(
{
method: "tools/list",
},
ListToolsResultSchema,
);
}
async callTool(
name: string,
params: Record<string, unknown>,
): Promise<CallToolResult> {
return await this.client.request(
{
method: "tools/call",
params: { name, ...params },
},
CallToolResultSchema,
);
}
getServerCapabilities() {
return this.client.getServerCapabilities();
}

View File

@@ -30,6 +30,19 @@ wss.on("connection", (ws: WebSocket) => {
} else if (command.type === "getPrompt" && command.name) {
const prompt = await mcpClient.getPrompt(command.name);
ws.send(JSON.stringify({ type: "prompt", data: prompt }));
} else if (command.type === "listTools") {
const tools = await mcpClient.listTools();
ws.send(JSON.stringify({ type: "tools", data: tools }));
} else if (
command.type === "callTool" &&
command.name &&
command.params
) {
const result = await mcpClient.callTool(
command.name + "asdf",
command.params,
);
ws.send(JSON.stringify({ type: "toolResult", data: result }));
}
} catch (error) {
console.error("Error:", error);