Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 | 2x 2x 2x 2x 2x 2x 2x 2x 2x | /**
* MCP server management — the Inference → Toolkits → MCP Servers subpage.
*
* Split out of what was `Tools.tsx`, which held two unrelated halves: a `Tool`
* resource whose rows never reached an invocation (see the tools router), and
* this. Only this half survived — reaching an external system is MCP's job, and
* built-in tools need no management surface because every agent has them.
*/
import {
CollectionView,
CreateButton,
EmptyState,
MultiSelectFilter,
SectionHeading,
SkeletonCard,
StatsBar,
Toolbar,
} from "@/components/collection";
import ResourceHistorySection from "@/components/diff/ResourceHistorySection/ResourceHistorySection";
import {
MCPConfigField,
MCPConnectionType,
MCPServer,
MCPServerTemplate,
} from "@/objects/MCPServer";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import type { LucideIcon } from "lucide-react";
import {
AlertCircle,
Check,
Database,
ExternalLink,
FileText,
History,
Info,
Loader2,
Mail,
Plus,
Power,
Search,
Server,
Settings2,
Terminal,
Trash2,
X,
Zap,
} from "lucide-react";
import React, { useEffect, useMemo, useState } from "react";
import { parseServerDate } from "@/util/time";
/** Icon + colour for one card. Named for its origin in the tool half of the
* old module; MCP servers pick theirs by category the same way. */
interface ToolStyle {
Icon: LucideIcon;
bg: string;
iconColour: string;
badge: string;
}
// ─── MCP icon resolution ──────────────────────────────────────────────────────
const MCP_CATEGORY_STYLES: Record<string, ToolStyle> = {
filesystem: {
Icon: FileText,
bg: "bg-sky-100 dark:bg-sky-900/30",
iconColour: "text-sky-600 dark:text-sky-400",
badge:
"bg-sky-50 dark:bg-sky-950/40 text-sky-700 dark:text-sky-300 border-sky-200 dark:border-sky-800",
},
search: {
Icon: Search,
bg: "bg-emerald-100 dark:bg-emerald-900/30",
iconColour: "text-emerald-600 dark:text-emerald-400",
badge:
"bg-emerald-50 dark:bg-emerald-950/40 text-emerald-700 dark:text-emerald-300 border-emerald-200 dark:border-emerald-800",
},
data: {
Icon: Database,
bg: "bg-violet-100 dark:bg-violet-900/30",
iconColour: "text-violet-600 dark:text-violet-400",
badge:
"bg-violet-50 dark:bg-violet-950/40 text-violet-700 dark:text-violet-300 border-violet-200 dark:border-violet-800",
},
dev: {
Icon: Terminal,
bg: "bg-slate-100 dark:bg-slate-800",
iconColour: "text-slate-600 dark:text-slate-300",
badge:
"bg-slate-50 dark:bg-slate-950/40 text-slate-600 dark:text-slate-300 border-slate-200 dark:border-slate-700",
},
communication: {
Icon: Mail,
bg: "bg-blue-100 dark:bg-blue-900/30",
iconColour: "text-blue-600 dark:text-blue-400",
badge:
"bg-blue-50 dark:bg-blue-950/40 text-blue-700 dark:text-blue-300 border-blue-200 dark:border-blue-800",
},
custom: {
Icon: Server,
bg: "bg-gray-100 dark:bg-gray-700",
iconColour: "text-gray-500 dark:text-gray-400",
badge:
"bg-gray-50 dark:bg-gray-800 text-gray-600 dark:text-gray-300 border-gray-200 dark:border-gray-600",
},
};
function mcpStyle(category: string): ToolStyle {
return MCP_CATEGORY_STYLES[category] ?? MCP_CATEGORY_STYLES.custom;
}
const CONNECTION_TYPE_BADGE: Record<MCPConnectionType, string> = {
stdio:
"bg-slate-100 dark:bg-slate-800 text-slate-600 dark:text-slate-300 border-slate-200 dark:border-slate-700",
http: "bg-emerald-50 dark:bg-emerald-950/40 text-emerald-700 dark:text-emerald-300 border-emerald-200 dark:border-emerald-800",
sse: "bg-blue-50 dark:bg-blue-950/40 text-blue-700 dark:text-blue-300 border-blue-200 dark:border-blue-800",
};
// ─── Shared primitives ────────────────────────────────────────────────────────
const inputBase =
"w-full px-3 py-2 text-sm rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-200 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-300 dark:focus:ring-gray-500 transition-colors";
// ─── MCP Server template card ─────────────────────────────────────────────────
const MCPTemplateCard: React.FC<{
template: MCPServerTemplate;
onAdd: () => void;
}> = ({ template, onAdd }) => {
const { Icon, bg, iconColour } = mcpStyle(template.category);
return (
<div className="flex flex-col rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm hover:shadow-md hover:border-gray-300 dark:hover:border-gray-500 transition-all overflow-hidden">
<div className="flex-1 p-5 flex flex-col gap-3">
<div className="flex items-start justify-between gap-3">
<div className="flex items-center gap-3 min-w-0">
<div className={`p-2.5 rounded-lg ${bg} shrink-0`}>
<Icon className={`size-5 ${iconColour}`} />
</div>
<div className="min-w-0">
<div className="text-sm font-semibold text-gray-900 dark:text-gray-100 truncate">
{template.name}
</div>
<span
className={`inline-flex mt-0.5 text-[10px] font-medium px-1.5 py-0.5 rounded border ${CONNECTION_TYPE_BADGE[template.connection_type]}`}
>
{template.connection_type}
</span>
</div>
</div>
</div>
<p className="text-xs text-gray-500 dark:text-gray-400 line-clamp-2 leading-relaxed">
{template.description}
</p>
{template.tools_preview.length > 0 && (
<div className="flex flex-wrap gap-1">
{template.tools_preview.slice(0, 4).map((t) => (
<span
key={t}
className="text-[10px] font-mono px-1.5 py-0.5 rounded bg-gray-100 dark:bg-gray-700 text-gray-500 dark:text-gray-400"
>
{t}
</span>
))}
{template.tools_preview.length > 4 && (
<span className="text-[10px] px-1.5 py-0.5 rounded bg-gray-100 dark:bg-gray-700 text-gray-400">
+{template.tools_preview.length - 4} more
</span>
)}
</div>
)}
</div>
<div className="flex items-center justify-between px-5 py-3 border-t border-gray-100 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/50">
{template.docs_url ? (
<a
href={template.docs_url}
target="_blank"
rel="noopener noreferrer"
onClick={(e) => e.stopPropagation()}
className="flex items-center gap-1 text-[10px] text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors"
>
<ExternalLink className="size-3" />
Docs
</a>
) : (
<span />
)}
<button
onClick={onAdd}
className="flex items-center gap-1.5 text-xs font-medium text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-gray-100 transition-colors"
>
<Plus className="size-3.5" />
Add Server
</button>
</div>
</div>
);
};
// ─── User MCP server card ─────────────────────────────────────────────────────
const MCPServerCard: React.FC<{
server: MCPServer;
onDelete: () => void;
deleting: boolean;
onViewHistory: () => void;
}> = ({ server, onDelete, deleting, onViewHistory }) => {
const { bg, iconColour } = server.template_key
? (mcpStyle(server.template_key) ?? mcpStyle("custom"))
: mcpStyle("custom");
return (
<div className="flex flex-col rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm overflow-hidden">
<div className="flex-1 p-5 flex flex-col gap-3">
<div className="flex items-start justify-between gap-3">
<div className="flex items-center gap-3 min-w-0">
<div className={`p-2.5 rounded-lg ${bg} shrink-0`}>
<Server className={`size-5 ${iconColour}`} />
</div>
<div className="min-w-0">
<div className="text-sm font-semibold text-gray-900 dark:text-gray-100 truncate">
{server.name}
</div>
<div className="flex items-center gap-1.5 mt-0.5">
<span
className={`inline-flex text-[10px] font-medium px-1.5 py-0.5 rounded border ${CONNECTION_TYPE_BADGE[server.connection_type]}`}
>
{server.connection_type}
</span>
{server.template_key && (
<span className="text-[10px] text-gray-400 dark:text-gray-500 font-mono">
{server.template_key}
</span>
)}
</div>
</div>
</div>
<div
className={`flex items-center gap-1 shrink-0 ${server.is_active ? "text-emerald-500" : "text-gray-300 dark:text-gray-600"}`}
>
<Power className="size-3.5" />
<span className="text-[10px] font-medium">
{server.is_active ? "Active" : "Inactive"}
</span>
</div>
</div>
{server.description && (
<p className="text-xs text-gray-500 dark:text-gray-400 line-clamp-2 leading-relaxed">
{server.description}
</p>
)}
{server.tools_cache && server.tools_cache.length > 0 && (
<div>
<div className="text-[10px] font-medium text-gray-400 dark:text-gray-500 uppercase tracking-wide mb-1.5">
Discovered Tools
</div>
<div className="flex flex-wrap gap-1">
{server.tools_cache.slice(0, 5).map((t) => (
<span
key={t}
className="text-[10px] font-mono px-1.5 py-0.5 rounded bg-gray-100 dark:bg-gray-700 text-gray-500 dark:text-gray-400"
>
{t}
</span>
))}
{server.tools_cache.length > 5 && (
<span className="text-[10px] px-1.5 py-0.5 rounded bg-gray-100 dark:bg-gray-700 text-gray-400">
+{server.tools_cache.length - 5} more
</span>
)}
</div>
</div>
)}
{(!server.tools_cache || server.tools_cache.length === 0) && (
<div className="flex items-center gap-1.5 text-[10px] text-gray-400 dark:text-gray-500">
<Info className="size-3" />
No tools discovered yet. Connect the server to populate.
</div>
)}
</div>
<div className="flex items-center justify-between px-5 py-3 border-t border-gray-100 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/50">
<span className="text-[10px] text-gray-400 dark:text-gray-500">
{server.created_at
? parseServerDate(server.created_at).toLocaleDateString(undefined, {
year: "numeric",
month: "short",
day: "numeric",
})
: "—"}
</span>
<div className="flex items-center gap-3">
<button
onClick={onViewHistory}
className="flex items-center gap-1 text-xs font-medium text-gray-400 dark:text-gray-500 hover:text-gray-700 dark:hover:text-gray-300 transition-colors"
>
<History className="size-3.5" />
History
</button>
<button
onClick={onDelete}
disabled={deleting}
className="flex items-center gap-1 text-xs font-medium text-gray-400 dark:text-gray-500 hover:text-red-600 dark:hover:text-red-400 transition-colors disabled:opacity-40"
>
{deleting ? (
<Loader2 className="size-3.5 animate-spin" />
) : (
<Trash2 className="size-3.5" />
)}
Remove
</button>
</div>
</div>
</div>
);
};
// ─── MCP Server history modal ──────────────────────────────────────────────────
//
// MCP servers have no dedicated detail page (they're managed as cards in
// this grid), so edit history is surfaced in a lightweight modal instead of
// a page section — same ResourceHistoryPanel as every other resource.
const MCPServerHistoryModal: React.FC<{
server: MCPServer;
onClose: () => void;
}> = ({ server, onClose }) => {
const { getApiHandler } = useApiHandler();
return (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/40">
<div className="w-full max-w-2xl max-h-[85vh] overflow-y-auto rounded-2xl bg-white dark:bg-gray-900 shadow-xl">
<div className="flex items-center justify-between px-6 py-4 border-b border-gray-100 dark:border-gray-700 sticky top-0 bg-white dark:bg-gray-900 z-10">
<div className="flex items-center gap-2">
<History className="size-4 text-gray-500 dark:text-gray-400" />
<h2 className="text-sm font-semibold text-gray-800 dark:text-gray-200">
Edit History — {server.name}
</h2>
</div>
<button
onClick={onClose}
className="p-1 rounded text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 transition-colors"
>
<X className="size-4" />
</button>
</div>
<div className="p-6">
<ResourceHistorySection
bare
source={getApiHandler().mcp_server_router}
resourceId={server.id}
queryKey={QUERY_KEYS.GET.MCP_SERVER_HISTORY}
fieldLabels={{
name: "Name",
description: "Description",
connection_type: "Connection Type",
template_key: "Template",
is_active: "Active",
}}
hiddenFields={[
// connection_config is redacted server-side too (may hold
// credentials) — hidden here since it never carries content.
"connection_config",
]}
/>
</div>
</div>
</div>
);
};
// ─── Add MCP Server panel ─────────────────────────────────────────────────────
interface AddMCPServerPanelProps {
template: MCPServerTemplate | null;
onClose: () => void;
onCreated: () => void;
}
const AddMCPServerPanel: React.FC<AddMCPServerPanelProps> = ({
template,
onClose,
onCreated,
}) => {
const { getApiHandler } = useApiHandler();
const [name, setName] = useState(template ? `My ${template.name}` : "");
const [description, setDescription] = useState("");
const [connectionType, setConnectionType] = useState<MCPConnectionType>(
template?.connection_type ?? "http",
);
const [fieldValues, setFieldValues] = useState<Record<string, string>>(() => {
if (!template) return {};
const init: Record<string, string> = {};
for (const f of template.config_fields) {
if (f.default !== null && f.default !== undefined) {
init[f.key] = Array.isArray(f.default)
? (f.default as string[]).join(", ")
: typeof f.default === "object"
? JSON.stringify(f.default, null, 2)
: String(f.default);
}
}
return init;
});
const [nameError, setNameError] = useState<string | null>(null);
const [fieldErrors, setFieldErrors] = useState<Record<string, string>>({});
const [submitError, setSubmitError] = useState<string | null>(null);
const [submitting, setSubmitting] = useState(false);
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
};
document.addEventListener("keydown", handler);
return () => document.removeEventListener("keydown", handler);
}, [onClose]);
const fields: MCPConfigField[] = template?.config_fields ?? [];
const handleSubmit = async () => {
let hasError = false;
if (!name.trim()) {
setNameError("Name is required.");
hasError = true;
}
const newFieldErrors: Record<string, string> = {};
for (const f of fields) {
if (f.required && !fieldValues[f.key]?.trim()) {
newFieldErrors[f.key] = `${f.label} is required.`;
hasError = true;
}
}
setFieldErrors(newFieldErrors);
if (hasError) return;
// Build connection_config from field values
const connection_config: Record<string, unknown> = {};
for (const f of fields) {
const raw = fieldValues[f.key] ?? "";
if (!raw.trim()) continue;
if (f.type === "array") {
connection_config[f.key] = raw
.split(",")
.map((s) => s.trim())
.filter(Boolean);
} else if (f.type === "object") {
try {
connection_config[f.key] = JSON.parse(raw);
} catch {
connection_config[f.key] = raw;
}
} else {
connection_config[f.key] = raw.trim();
}
}
setSubmitting(true);
setSubmitError(null);
try {
await getApiHandler().mcp_server_router.create({
name: name.trim(),
description: description.trim() || undefined,
template_key: template?.key ?? undefined,
connection_type: connectionType,
connection_config,
is_active: true,
});
onCreated();
} catch (e: unknown) {
const err = e as { message?: string };
setSubmitError(err?.message ?? "Something went wrong. Please try again.");
} finally {
setSubmitting(false);
}
};
const { bg: panelBg, iconColour: panelIconColour } = template
? mcpStyle(template.category)
: mcpStyle("custom");
return (
<div className="fixed inset-0 z-50 flex justify-end">
<div
className="absolute inset-0 bg-black/30 dark:bg-black/50 backdrop-blur-sm"
onClick={onClose}
/>
<div className="relative z-10 w-full max-w-md bg-white dark:bg-gray-900 h-full flex flex-col shadow-2xl border-l border-gray-200 dark:border-gray-700">
{/* Header */}
<div className="flex items-center justify-between px-5 py-4 border-b border-gray-100 dark:border-gray-800">
<div className="flex items-center gap-2.5">
<div className={`p-1.5 rounded-md ${panelBg}`}>
<Server className={`size-4 ${panelIconColour}`} />
</div>
<div>
<div className="text-sm font-semibold text-gray-900 dark:text-gray-100">
{template ? `Add ${template.name}` : "Add MCP Server"}
</div>
{template && (
<div className="text-[10px] text-gray-400 dark:text-gray-500">
{template.connection_type} • {template.category}
</div>
)}
</div>
</div>
<button
onClick={onClose}
className="p-1.5 rounded-lg text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors"
>
<X className="size-4" />
</button>
</div>
{/* Body */}
<div className="flex-1 overflow-y-auto px-5 py-5 space-y-5">
{/* Identity */}
<div className="space-y-4">
<div>
<label className="text-sm font-medium text-gray-700 dark:text-gray-300">
Name <span className="text-red-500">*</span>
</label>
<p className="text-xs text-gray-400 dark:text-gray-500 mt-0.5 mb-1.5">
A display name for this server connection.
</p>
<input
type="text"
value={name}
onChange={(e) => {
setName(e.target.value);
setNameError(null);
}}
placeholder="e.g. My GitHub Connector"
className={
nameError
? "w-full px-3 py-2 text-sm rounded-lg border border-red-300 dark:border-red-700 bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-200 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-red-200 dark:focus:ring-red-900 transition-colors"
: inputBase
}
/>
{nameError && (
<p className="text-xs text-red-500 mt-1">{nameError}</p>
)}
</div>
<div>
<label className="text-sm font-medium text-gray-700 dark:text-gray-300">
Description
</label>
<textarea
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="Optional description of what this server provides…"
rows={2}
className={inputBase + " resize-none mt-1.5"}
/>
</div>
{!template && (
<div>
<label className="text-sm font-medium text-gray-700 dark:text-gray-300">
Connection Type <span className="text-red-500">*</span>
</label>
<div className="mt-1.5 grid grid-cols-3 gap-2">
{(["stdio", "http", "sse"] as MCPConnectionType[]).map(
(t) => (
<button
key={t}
onClick={() => setConnectionType(t)}
className={`px-3 py-2 rounded-lg text-xs font-medium border transition-colors ${
connectionType === t
? "bg-gray-900 dark:bg-white text-white dark:text-gray-900 border-gray-900 dark:border-white"
: "bg-white dark:bg-gray-800 text-gray-600 dark:text-gray-400 border-gray-200 dark:border-gray-600 hover:border-gray-400 dark:hover:border-gray-400"
}`}
>
{t}
</button>
),
)}
</div>
</div>
)}
</div>
{/* Config fields */}
{fields.length > 0 && (
<div className="space-y-4">
<div className="flex items-center gap-2">
<Settings2 className="size-4 text-gray-400" />
<span className="text-sm font-medium text-gray-700 dark:text-gray-300">
Connection Configuration
</span>
</div>
{fields.map((f) => (
<div key={f.key}>
<div className="flex items-center gap-1.5 mb-1">
<label className="text-sm font-medium text-gray-700 dark:text-gray-300">
{f.label}
</label>
{f.required && (
<span className="text-red-500 text-xs">*</span>
)}
{!f.required && (
<span className="text-[10px] text-gray-400 dark:text-gray-500">
(optional)
</span>
)}
</div>
{f.description && (
<p className="text-xs text-gray-400 dark:text-gray-500 mb-1.5">
{f.description}
</p>
)}
{f.type === "object" ? (
<textarea
value={fieldValues[f.key] ?? ""}
onChange={(e) => {
setFieldValues((p) => ({
...p,
[f.key]: e.target.value,
}));
setFieldErrors((p) => {
const n = { ...p };
delete n[f.key];
return n;
});
}}
placeholder={f.placeholder || "{}"}
rows={3}
className={`${inputBase} resize-none font-mono text-xs`}
/>
) : (
<input
type={f.type === "password" ? "password" : "text"}
value={fieldValues[f.key] ?? ""}
onChange={(e) => {
setFieldValues((p) => ({
...p,
[f.key]: e.target.value,
}));
setFieldErrors((p) => {
const n = { ...p };
delete n[f.key];
return n;
});
}}
placeholder={
f.type === "array"
? f.placeholder || "item1, item2, item3"
: f.placeholder
}
className={
fieldErrors[f.key]
? "w-full px-3 py-2 text-sm rounded-lg border border-red-300 dark:border-red-700 bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-200 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-red-200 dark:focus:ring-red-900 transition-colors"
: inputBase
}
/>
)}
{f.type === "array" && (
<p className="text-[10px] text-gray-400 dark:text-gray-500 mt-1">
Comma-separated values.
</p>
)}
{fieldErrors[f.key] && (
<p className="text-xs text-red-500 mt-1">
{fieldErrors[f.key]}
</p>
)}
</div>
))}
</div>
)}
{/* Tools preview */}
{template && template.tools_preview.length > 0 && (
<div className="rounded-lg bg-gray-50 dark:bg-gray-800/60 border border-gray-100 dark:border-gray-700 p-4">
<div className="flex items-center gap-1.5 text-xs font-medium text-gray-600 dark:text-gray-400 mb-2">
<Zap className="size-3.5" />
Tools exposed by this server
</div>
<div className="flex flex-wrap gap-1">
{template.tools_preview.map((t) => (
<span
key={t}
className="text-[10px] font-mono px-1.5 py-0.5 rounded bg-white dark:bg-gray-700 border border-gray-200 dark:border-gray-600 text-gray-500 dark:text-gray-400"
>
{t}
</span>
))}
</div>
</div>
)}
{/* Info callout */}
<div className="rounded-lg bg-blue-50 dark:bg-blue-950/30 border border-blue-200 dark:border-blue-800 px-4 py-3 space-y-1.5">
<div className="flex items-center gap-1.5 text-xs font-medium text-blue-700 dark:text-blue-300">
<Info className="size-3.5" />
About MCP Servers
</div>
<ul className="space-y-1">
{[
"MCP servers expose tools that agents can use during task execution.",
"Connection credentials are stored securely and never shared between accounts.",
"After adding a server, assign its tools to agents from the agent configuration page.",
].map((tip) => (
<li
key={tip}
className="flex items-start gap-1.5 text-xs text-blue-600 dark:text-blue-400"
>
<span className="mt-1.5 size-1 rounded-full bg-blue-400 dark:bg-blue-500 shrink-0" />
{tip}
</li>
))}
</ul>
</div>
{submitError && (
<div className="flex items-center gap-2 px-4 py-3 rounded-lg bg-red-50 dark:bg-red-950/40 border border-red-200 dark:border-red-800 text-red-700 dark:text-red-300 text-sm">
<AlertCircle className="size-4 shrink-0" />
{submitError}
</div>
)}
</div>
{/* Footer */}
<div className="flex items-center justify-end gap-3 px-5 py-4 border-t border-gray-100 dark:border-gray-800">
<button
onClick={onClose}
className="px-4 py-2 rounded-lg text-sm font-medium text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
>
Cancel
</button>
<button
onClick={() => void handleSubmit()}
disabled={submitting}
className="flex items-center gap-1.5 px-4 py-2 rounded-lg bg-gray-900 dark:bg-white text-white dark:text-gray-900 text-sm font-medium hover:opacity-80 disabled:opacity-50 transition-opacity"
>
{submitting ? (
<>
<Loader2 className="size-4 animate-spin" />
Adding…
</>
) : (
<>
<Check className="size-4" />
Add Server
</>
)}
</button>
</div>
</div>
</div>
);
};
// ─── MCP Servers tab ──────────────────────────────────────────────────────────
const MCPServersTab: React.FC<{
templates: MCPServerTemplate[];
templatesLoading: boolean;
userServers: MCPServer[];
serversLoading: boolean;
onDelete: (id: string) => Promise<void>;
deletingId: string | null;
}> = ({
templates,
templatesLoading,
userServers,
serversLoading,
onDelete,
deletingId,
}) => {
const [addPanel, setAddPanel] = useState<MCPServerTemplate | null | "custom">(
null,
);
const [historyServer, setHistoryServer] = useState<MCPServer | null>(null);
const [tmplSearch, setTmplSearch] = useState("");
const [selectedCategories, setSelectedCategories] = useState<string[]>([]);
const [selectedConnTypes, setSelectedConnTypes] = useState<string[]>([]);
const queryClient = useQueryClient();
const CATEGORY_LABELS: Record<string, string> = {
filesystem: "Filesystem",
search: "Search & Web",
data: "Data & Databases",
dev: "Developer Tools",
communication: "Communication",
custom: "Custom",
};
const allCategories = useMemo(
() => [...new Set(templates.map((t) => t.category ?? "custom"))].sort(),
[templates],
);
const allConnTypes = useMemo(
() => [...new Set(templates.map((t) => t.connection_type))].sort(),
[templates],
);
const filteredTemplates = useMemo(() => {
const q = tmplSearch.trim().toLowerCase();
return templates.filter((t) => {
const matchesSearch =
!q ||
t.name.toLowerCase().includes(q) ||
t.description.toLowerCase().includes(q) ||
t.tools_preview.some((tp) => tp.toLowerCase().includes(q));
const matchesCat =
selectedCategories.length === 0 ||
selectedCategories.includes(t.category ?? "custom");
const matchesConn =
selectedConnTypes.length === 0 ||
selectedConnTypes.includes(t.connection_type);
return matchesSearch && matchesCat && matchesConn;
});
}, [templates, tmplSearch, selectedCategories, selectedConnTypes]);
const groupedFiltered = useMemo(() => {
const map: Record<string, MCPServerTemplate[]> = {};
for (const t of filteredTemplates) {
const cat = t.category ?? "custom";
if (!map[cat]) map[cat] = [];
map[cat].push(t);
}
return map;
}, [filteredTemplates]);
const tmplIsFiltered = !!(
tmplSearch ||
selectedCategories.length ||
selectedConnTypes.length
);
const clearTmplFilters = () => {
setTmplSearch("");
setSelectedCategories([]);
setSelectedConnTypes([]);
};
return (
<div className="space-y-8">
{/* User's configured servers */}
<div>
<SectionHeading
label="Your MCP Servers"
count={userServers.length}
action={
<CreateButton
label="Add Custom"
onClick={() => setAddPanel("custom")}
/>
}
/>
<CollectionView
items={userServers}
itemKey={(s) => s.id}
renderCard={(s) => (
<MCPServerCard
server={s}
onDelete={() => void onDelete(s.id)}
deleting={deletingId === s.id}
onViewHistory={() => setHistoryServer(s)}
/>
)}
view="grid"
loading={serversLoading}
skeletonCount={2}
empty={{
Icon: Server,
title: "No MCP servers configured yet.",
hint: "Add a server from the templates below, or connect a custom server.",
dashed: true,
}}
/>
</div>
{/* Built-in templates */}
<div>
<SectionHeading
label="Server Templates"
count={tmplIsFiltered ? filteredTemplates.length : templates.length}
/>
<div className="mb-4">
<Toolbar
search={{
value: tmplSearch,
onChange: setTmplSearch,
placeholder: "Search templates…",
className: "w-full sm:w-48",
}}
activeFilterCount={
(tmplSearch ? 1 : 0) +
(selectedCategories.length ? 1 : 0) +
(selectedConnTypes.length ? 1 : 0)
}
onClear={clearTmplFilters}
>
{allCategories.length > 1 && (
<MultiSelectFilter
label="Category"
options={allCategories.map((c) => CATEGORY_LABELS[c] ?? c)}
selected={selectedCategories.map(
(c) => CATEGORY_LABELS[c] ?? c,
)}
onChange={(labels) =>
setSelectedCategories(
labels.map(
(l) =>
Object.entries(CATEGORY_LABELS).find(
([, v]) => v === l,
)?.[0] ?? l,
),
)
}
/>
)}
{allConnTypes.length > 1 && (
<MultiSelectFilter
label="Transport"
options={allConnTypes}
selected={selectedConnTypes}
onChange={setSelectedConnTypes}
capitalizeOptions
/>
)}
</Toolbar>
</div>
{templatesLoading ? (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
{Array.from({ length: 6 }).map((_, i) => (
<SkeletonCard key={i} />
))}
</div>
) : filteredTemplates.length === 0 ? (
<EmptyState
Icon={Server}
title="No templates match your filters."
dashed
/>
) : tmplIsFiltered ? (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
{filteredTemplates.map((t) => (
<MCPTemplateCard
key={t.key}
template={t}
onAdd={() => setAddPanel(t)}
/>
))}
</div>
) : (
<div className="space-y-6">
{Object.entries(groupedFiltered).map(([cat, items]) => (
<div key={cat}>
<div className="flex items-center gap-2 mb-3">
<span className="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wide">
{CATEGORY_LABELS[cat] ?? cat}
</span>
<div className="flex-1 h-px bg-gray-100 dark:bg-gray-700" />
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
{items.map((t) => (
<MCPTemplateCard
key={t.key}
template={t}
onAdd={() => setAddPanel(t)}
/>
))}
</div>
</div>
))}
</div>
)}
</div>
{/* Add server panel */}
{addPanel !== null && (
<AddMCPServerPanel
template={addPanel === "custom" ? null : addPanel}
onClose={() => setAddPanel(null)}
onCreated={() => {
setAddPanel(null);
void queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.USER_MCP_SERVERS],
});
}}
/>
)}
{/* Edit history modal */}
{historyServer && (
<MCPServerHistoryModal
server={historyServer}
onClose={() => setHistoryServer(null)}
/>
)}
</div>
);
};
// ─── MCP servers content (rendered as a subpage of the Inference → Agents tab) ─
export const MCPServersContent: React.FC = () => {
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const [deletingServerId, setDeletingServerId] = useState<string | null>(null);
const { data: mcpTemplates = [], isLoading: templatesLoading } = useQuery({
queryKey: [QUERY_KEYS.GET.MCP_SERVER_TEMPLATES],
queryFn: () => getApiHandler().mcp_server_router.templates(),
});
const { data: userMCPServers = [], isLoading: serversLoading } = useQuery({
queryKey: [QUERY_KEYS.GET.USER_MCP_SERVERS],
queryFn: () => getApiHandler().mcp_server_router.list(),
});
const handleDeleteServer = async (serverId: string) => {
setDeletingServerId(serverId);
try {
await getApiHandler().mcp_server_router.delete(serverId);
await queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.USER_MCP_SERVERS],
});
} catch {
/* silently fail */
} finally {
setDeletingServerId(null);
}
};
return (
<div>
<p className="mb-4 text-sm text-gray-500 dark:text-gray-400 leading-relaxed">
MCP servers connect your agents to external systems — each one offers
a set of tools. Start from a template or connect your own. To give an
agent a server's tools, bundle servers into a toolkit and attach it to
the agent.
</p>
<div className="mb-5">
<StatsBar
stats={[
{
label: "Templates",
value: mcpTemplates.length,
loading: templatesLoading,
},
{
label: "Your Servers",
value: userMCPServers.length,
loading: serversLoading,
},
]}
/>
</div>
<MCPServersTab
templates={mcpTemplates}
templatesLoading={templatesLoading}
userServers={userMCPServers}
serversLoading={serversLoading}
onDelete={handleDeleteServer}
deletingId={deletingServerId}
/>
</div>
);
};
|