File size: 5,436 Bytes
c40c75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React, { useState } from 'react';
import { useQuery, useMutation } from '@tanstack/react-query';
import { DataTable } from '../view_logs/table';
import { columns, ToolTestPanel } from './columns';
import { MCPTool, MCPToolsViewerProps, CallMCPToolResponse } from './types';
import { listMCPTools, callMCPTool } from '../networking';

// Wrapper to handle the type mismatch between MCPTool and DataTable's expected type
function DataTableWrapper({
  columns,
  data,
  isLoading,
}: {
  columns: any;
  data: MCPTool[];
  isLoading: boolean;
}) {
  // Create a dummy renderSubComponent and getRowCanExpand function
  const renderSubComponent = () => <div />;
  const getRowCanExpand = () => false;

  return (
    <DataTable
      columns={columns as any}
      data={data as any}
      isLoading={isLoading}
      renderSubComponent={renderSubComponent}
      getRowCanExpand={getRowCanExpand}
      loadingMessage="πŸš… Loading tools..."
      noDataMessage="No tools found"
    />
  );
}

export default function MCPToolsViewer({
  accessToken,
  userRole,
  userID,
}: MCPToolsViewerProps) {
  const [searchTerm, setSearchTerm] = useState('');
  const [selectedTool, setSelectedTool] = useState<MCPTool | null>(null);
  const [toolResult, setToolResult] = useState<CallMCPToolResponse | null>(null);
  const [toolError, setToolError] = useState<Error | null>(null);

  // Query to fetch MCP tools
  const { data: mcpTools, isLoading: isLoadingTools } = useQuery({
    queryKey: ['mcpTools'],
    queryFn: () => {
      if (!accessToken) throw new Error('Access Token required');
      return listMCPTools(accessToken);
    },
    enabled: !!accessToken,
  });

  // Mutation for calling a tool
  const { mutate: executeTool, isPending: isCallingTool } = useMutation({
    mutationFn: (args: { tool: MCPTool; arguments: Record<string, any> }) => {
      if (!accessToken) throw new Error('Access Token required');
      return callMCPTool(
        accessToken,
        args.tool.name,
        args.arguments
      );
    },
    onSuccess: (data) => {
      setToolResult(data);
      setToolError(null);
    },
    onError: (error: Error) => {
      setToolError(error);
      setToolResult(null);
    },
  });

  // Add onToolSelect handler to each tool
  const toolsData = React.useMemo(() => {
    if (!mcpTools) return [];
    
    return mcpTools.map((tool: MCPTool) => ({
      ...tool,
      onToolSelect: (tool: MCPTool) => {
        setSelectedTool(tool);
        setToolResult(null);
        setToolError(null);
      }
    }));
  }, [mcpTools]);

  // Filter tools based on search term
  const filteredTools = React.useMemo(() => {
    return toolsData.filter((tool: MCPTool) => {
      const searchLower = searchTerm.toLowerCase();
      return (
        tool.name.toLowerCase().includes(searchLower) ||
        tool.description.toLowerCase().includes(searchLower) ||
        tool.mcp_info.server_name.toLowerCase().includes(searchLower)
      );
    });
  }, [toolsData, searchTerm]);

  // Handle tool call submission
  const handleToolSubmit = (args: Record<string, any>) => {
    if (!selectedTool) return;
    
    executeTool({
      tool: selectedTool,
      arguments: args,
    });
  };

  if (!accessToken || !userRole || !userID) {
    return <div className="p-6 text-center text-gray-500">Missing required authentication parameters.</div>;
  }

  return (
    <div className="w-full p-6">
      <div className="flex items-center justify-between mb-4">
        <h1 className="text-xl font-semibold">MCP Tools</h1>
      </div>

      <div className="bg-white rounded-lg shadow">
        <div className="border-b px-6 py-4">
          <div className="flex items-center justify-between">
            <div className="relative w-64">
              <input
                type="text"
                placeholder="Search tools..."
                className="w-full px-3 py-2 pl-8 border rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                value={searchTerm}
                onChange={(e) => setSearchTerm(e.target.value)}
              />
              <svg
                className="absolute left-2.5 top-2.5 h-4 w-4 text-gray-500"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
                />
              </svg>
            </div>
            <div className="text-sm text-gray-500">
              {filteredTools.length} tool{filteredTools.length !== 1 ? "s" : ""} available
            </div>
          </div>
        </div>

        <DataTableWrapper
          columns={columns}
          data={filteredTools}
          isLoading={isLoadingTools}
        />
      </div>

      {/* Tool Test Panel - Show when a tool is selected */}
      {selectedTool && (
        <div className="fixed inset-0 bg-gray-800 bg-opacity-75 flex items-center justify-center z-50 p-4">
          <ToolTestPanel
            tool={selectedTool}
            onSubmit={handleToolSubmit}
            isLoading={isCallingTool}
            result={toolResult}
            error={toolError}
            onClose={() => setSelectedTool(null)}
          />
        </div>
      )}
    </div>
  );
}