Elffuss BraidHub

kikocis/continue public

4e263ad457a7 · 9.0 KiBRawPermalink
1import { ConfigYaml, parseConfigYaml } from "@continuedev/config-yaml";
2import {
3 ArrowPathIcon,
4 CircleStackIcon,
5 CommandLineIcon,
6 GlobeAltIcon,
7 InformationCircleIcon,
8 ShieldCheckIcon,
9 ShieldExclamationIcon,
10 WrenchScrewdriverIcon,
11} from "@heroicons/react/24/outline";
12import { MCPServerStatus } from "core";
13import { Fragment, useContext, useMemo } from "react";
14import { useAuth } from "../../../../../context/Auth";
15import { IdeMessengerContext } from "../../../../../context/IdeMessenger";
16import { useAppDispatch, useAppSelector } from "../../../../../redux/hooks";
17import { updateConfig } from "../../../../../redux/slices/configSlice";
18import { fontSize } from "../../../../../util";
19import { ToolTip } from "../../../../gui/Tooltip";
20import { Button } from "../../../../ui";
21import EditBlockButton from "../../EditBlockButton";
22import { ExploreBlocksButton } from "../ExploreBlocksButton";
23
24interface MCPServerStatusProps {
25 server: MCPServerStatus;
26 serverFromYaml?: NonNullable<ConfigYaml["mcpServers"]>[number];
27}
28function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) {
29 const ideMessenger = useContext(IdeMessengerContext);
30 const config = useAppSelector((store) => store.config.config);
31 const dispatch = useAppDispatch();
32 const toolsTooltipId = `${server.id}-tools`;
33 const promptsTooltipId = `${server.id}-prompts`;
34 const resourcesTooltipId = `${server.id}-resources`;
35 const errorsTooltipId = `${server.id}-errors`;
36 const mcpAuthTooltipId = `${server.id}-auth`;
37
38 const updateMCPServerStatus = (status: MCPServerStatus["status"]) => {
39 // optimistic config update
40 dispatch(
41 updateConfig({
42 ...config,
43 mcpServerStatuses: config.mcpServerStatuses.map((s) =>
44 s.id === server.id
45 ? {
46 ...s,
47 status,
48 }
49 : s,
50 ),
51 }),
52 );
53 };
54
55 const onAuthenticate = async () => {
56 updateMCPServerStatus("authenticating");
57 await ideMessenger.request("mcp/startAuthentication", server);
58 };
59
60 const onRemoveAuth = async () => {
61 updateMCPServerStatus("authenticating");
62 await ideMessenger.request("mcp/removeAuthentication", server);
63 };
64
65 const onRefresh = async () => {
66 updateMCPServerStatus("connecting");
67 ideMessenger.post("mcp/reloadServer", {
68 id: server.id,
69 });
70 };
71
72 return (
73 <div
74 style={{
75 fontSize: fontSize(-2),
76 }}
77 className={`flex flex-row items-center justify-between gap-3 ${server.status === "authenticating" ? "my-0.5" : ""}`}
78 >
79 <div className="flex flex-row items-center gap-3">
80 {/* Name and Status */}
81 <span className="m-0 font-semibold">{server.name}</span>
82
83 {/* Error indicator if any */}
84 {server.errors.length ? (
85 <>
86 <InformationCircleIcon
87 className={`h-3 w-3 ${server.status === "error" ? "text-red-500" : "text-yellow-500"}`}
88 data-tooltip-id={errorsTooltipId}
89 />
90 <ToolTip
91 clickable
92 id={errorsTooltipId}
93 delayHide={
94 server.errors.some((error) => error.length > 150) ? 1500 : 0
95 }
96 className="flex flex-col gap-0.5"
97 >
98 {server.errors.map((error, idx) => (
99 <Fragment key={idx}>
100 <div>
101 {error.length > 150
102 ? error.substring(0, 150) + "..."
103 : error}
104 </div>
105 {error.length > 150 && (
106 <Button
107 className="my-0"
108 size="sm"
109 variant="ghost"
110 onClick={() =>
111 void ideMessenger.ide.showVirtualFile(
112 server.name,
113 error,
114 )
115 }
116 >
117 View full error
118 </Button>
119 )}
120 </Fragment>
121 ))}
122 </ToolTip>
123 </>
124 ) : null}
125
126 {/* Tools, Prompts, Resources with counts */}
127 <div className="flex flex-row items-center gap-3">
128 <div
129 className="flex cursor-zoom-in items-center gap-1 hover:opacity-80"
130 data-tooltip-id={toolsTooltipId}
131 >
132 <WrenchScrewdriverIcon className="h-3 w-3" />
133 <span className="text-xs">{server.tools.length}</span>
134 <ToolTip id={toolsTooltipId} className="flex flex-col gap-0.5">
135 {server.tools.map((tool, idx) => (
136 <code key={idx}>{tool.name}</code>
137 ))}
138 {server.tools.length === 0 && (
139 <span className="text-lightgray">No tools</span>
140 )}
141 </ToolTip>
142 </div>
143 <div
144 className="flex cursor-zoom-in items-center gap-1 hover:opacity-80"
145 data-tooltip-id={promptsTooltipId}
146 >
147 <CommandLineIcon className="h-3 w-3" />
148 <span className="text-xs">{server.prompts.length}</span>
149 <ToolTip id={promptsTooltipId} className="flex flex-col gap-0.5">
150 {server.prompts.map((prompt, idx) => (
151 <code key={idx}>{prompt.name}</code>
152 ))}
153 {server.prompts.length === 0 && (
154 <span className="text-lightgray">No prompts</span>
155 )}
156 </ToolTip>
157 </div>
158 <div
159 className="flex cursor-zoom-in items-center gap-1 hover:opacity-80"
160 data-tooltip-id={resourcesTooltipId}
161 >
162 <CircleStackIcon className="h-3 w-3" />
163 <span className="text-xs">
164 {server.resources.length + server.resourceTemplates.length}
165 </span>
166 <ToolTip id={resourcesTooltipId} className="flex flex-col gap-0.5">
167 {[...server.resources, ...server.resourceTemplates].map(
168 (resource, idx) => (
169 <code key={idx}>{resource.name}</code>
170 ),
171 )}
172 {server.resources.length === 0 && (
173 <span className="text-lightgray">No resources</span>
174 )}
175 </ToolTip>
176 </div>
177 </div>
178 </div>
179
180 <div className="flex items-center gap-2">
181 {server.isProtectedResource && (
182 <>
183 <div
184 className="text-lightgray flex cursor-pointer items-center hover:text-white hover:opacity-80"
185 data-tooltip-id={
186 server.status !== "authenticating" ? mcpAuthTooltipId : ""
187 }
188 >
189 {server.status === "error" ? (
190 <ShieldExclamationIcon
191 className="h-3 w-3"
192 onClick={onAuthenticate}
193 />
194 ) : server.status === "authenticating" ? (
195 <GlobeAltIcon className="animate-spin-slow h-3 w-3" />
196 ) : (
197 <ShieldCheckIcon className="h-3 w-3" onClick={onRemoveAuth} />
198 )}
199 </div>
200 {server.status !== "authenticating" && (
201 <ToolTip place="left" id={mcpAuthTooltipId}>
202 {server.status === "error" ? "Authenticate" : "Logout"}
203 </ToolTip>
204 )}
205 </>
206 )}
207 <EditBlockButton
208 blockType={"mcpServers"}
209 block={serverFromYaml}
210 sourceFile={server.sourceFile}
211 />
212 <div
213 className="text-lightgray flex cursor-pointer items-center hover:opacity-80"
214 onClick={onRefresh}
215 >
216 <ArrowPathIcon className="h-3 w-3" />
217 </div>
218
219 <div
220 className="hidden h-2 w-2 rounded-full sm:flex"
221 style={{
222 backgroundColor:
223 server.status === "connected"
224 ? "#22c55e" // green-500
225 : server.status === "connecting"
226 ? "#eab308" // yellow-500
227 : server.status === "not-connected"
228 ? "#78716c" // stone-500
229 : "#ef4444", // red-500 for error
230 }}
231 />
232 </div>
233 </div>
234 );
235}
236
237function MCPSection() {
238 const servers = useAppSelector(
239 (store) => store.config.config.mcpServerStatuses,
240 );
241 const { selectedProfile } = useAuth();
242
243 const mergedBlocks = useMemo(() => {
244 const parsed = selectedProfile?.rawYaml
245 ? parseConfigYaml(selectedProfile?.rawYaml ?? "")
246 : undefined;
247 return (servers ?? []).map((doc, index) => ({
248 block: doc,
249 blockFromYaml: parsed?.mcpServers?.[index],
250 }));
251 }, [servers, selectedProfile]);
252
253 return (
254 <div className="flex flex-col gap-1">
255 <div className="flex max-h-[170px] flex-col gap-1 overflow-y-auto overflow-x-hidden pr-2">
256 {mergedBlocks.map(({ block, blockFromYaml }, idx) => (
257 <MCPServerPreview
258 key={idx}
259 server={block}
260 serverFromYaml={blockFromYaml}
261 />
262 ))}
263 </div>
264 <ExploreBlocksButton blockType="mcpServers" />
265 </div>
266 );
267}
268
269export default MCPSection;