Merge pull request #11 from modelcontextprotocol/ashwin/tools

get tools working
This commit is contained in:
ashwin-ant
2024-10-09 09:05:25 -07:00
committed by GitHub
8 changed files with 92 additions and 21 deletions

View File

@@ -11,6 +11,7 @@
},
"dependencies": {
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-tabs": "^1.1.1",
"class-variance-authority": "^0.7.0",

View File

@@ -201,7 +201,7 @@ const App = () => {
<Bell className="w-4 h-4 mr-2" />
Notifications
</TabsTrigger>
<TabsTrigger value="tools" disabled>
<TabsTrigger value="tools">
<Hammer className="w-4 h-4 mr-2" />
Tools
</TabsTrigger>
@@ -237,7 +237,10 @@ const App = () => {
listTools={listTools}
callTool={callTool}
selectedTool={selectedTool}
setSelectedTool={setSelectedTool}
setSelectedTool={(tool) => {
setSelectedTool(tool);
setToolResult("");
}}
toolResult={toolResult}
error={error}
/>

View File

@@ -5,6 +5,7 @@ import { TabsContent } from "@/components/ui/tabs";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { useState } from "react";
import { Label } from "@/components/ui/label";
export type Prompt = {
name: string;
@@ -98,7 +99,9 @@ const PromptsTab = ({
)}
{selectedPrompt.arguments?.map((arg) => (
<div key={arg.name}>
<Label htmlFor={arg.name}>{arg.name}</Label>
<Input
id={arg.name}
placeholder={`Enter ${arg.name}`}
value={promptArgs[arg.name] || ""}
onChange={(e) =>

View File

@@ -1,12 +1,18 @@
import { TabsContent } from "@/components/ui/tabs";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { Input } from "@/components/ui/input";
import { Send, AlertCircle } from "lucide-react";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { useState } from "react";
import { Label } from "@/components/ui/label";
export type Tool = {
name: string;
description: string;
inputSchema: {
type: string;
properties: Record<string, { type: string; description: string }>;
};
};
const ToolsTab = ({
@@ -26,7 +32,7 @@ const ToolsTab = ({
toolResult: string;
error: string | null;
}) => {
const [params, setParams] = useState("");
const [params, setParams] = useState<Record<string, unknown>>({});
return (
<TabsContent value="tools" className="grid grid-cols-2 gap-4">
@@ -46,6 +52,9 @@ const ToolsTab = ({
onClick={() => setSelectedTool(tool)}
>
<span className="flex-1">{tool.name}</span>
<span className="text-sm text-gray-500">
{tool.description}
</span>
</div>
))}
</div>
@@ -67,22 +76,47 @@ const ToolsTab = ({
</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))}
>
<p className="text-sm text-gray-600">
{selectedTool.description}
</p>
{Object.entries(selectedTool.inputSchema.properties).map(
([key, value]) => (
<div key={key}>
<Label
htmlFor={key}
className="block text-sm font-medium text-gray-700"
>
{key}
</Label>
<Input
type={value.type === "number" ? "number" : "text"}
id={key}
name={key}
placeholder={value.description}
onChange={(e) =>
setParams({
...params,
[key]:
value.type === "number"
? Number(e.target.value)
: e.target.value,
})
}
/>
</div>
),
)}
<Button onClick={() => callTool(selectedTool.name, 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>
<>
<h4 className="font-semibold mb-2">Tool Result:</h4>
<pre className="bg-gray-50 p-4 rounded text-sm overflow-auto max-h-64">
{toolResult}
</pre>
</>
)}
</div>
) : (

View File

@@ -0,0 +1,24 @@
import * as React from "react"
import * as LabelPrimitive from "@radix-ui/react-label"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
const labelVariants = cva(
"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
)
const Label = React.forwardRef<
React.ElementRef<typeof LabelPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> &
VariantProps<typeof labelVariants>
>(({ className, ...props }, ref) => (
<LabelPrimitive.Root
ref={ref}
className={cn(labelVariants(), className)}
{...props}
/>
))
Label.displayName = LabelPrimitive.Root.displayName
export { Label }

7
client/yarn.lock generated
View File

@@ -499,6 +499,13 @@
dependencies:
"@radix-ui/react-use-layout-effect" "1.1.0"
"@radix-ui/react-label@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@radix-ui/react-label/-/react-label-2.1.0.tgz#3aa2418d70bb242be37c51ff5e51a2adcbc372e3"
integrity sha512-peLblDlFw/ngk3UWq0VnYaOLy6agTZZ+MUO/WhVfm14vJGML+xH4FAl2XQGLqdefjNb7ApRg6Yn7U42ZhmYXdw==
dependencies:
"@radix-ui/react-primitive" "2.0.0"
"@radix-ui/react-presence@1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@radix-ui/react-presence/-/react-presence-1.1.1.tgz#98aba423dba5e0c687a782c0669dcd99de17f9b1"

View File

@@ -109,7 +109,7 @@ export class McpClient {
return await this.client.request(
{
method: "tools/call",
params: { name, ...params },
params: { name, arguments: params },
},
CallToolResultSchema,
);

View File

@@ -45,11 +45,10 @@ wss.on("connection", (ws: WebSocket) => {
command.name &&
command.params
) {
const result = await mcpClient.callTool(
command.name + "asdf",
command.params,
const result = await mcpClient.callTool(command.name, command.params);
ws.send(
JSON.stringify({ type: "toolResult", data: result.toolResult }),
);
ws.send(JSON.stringify({ type: "toolResult", data: result }));
}
} catch (error) {
console.error("Error:", error);