app-sidebar-mcp.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import type { McpServerStatus, McpServerStatusKind } from "@zcode/contracts";
  2. import type { TuiCopy } from "@zcode/i18n";
  3. import React from "react";
  4. import type { McpSidebarState } from "./app-model.js";
  5. import { palette } from "./app-model.js";
  6. import { SIDEBAR_CONTENT_WIDTH } from "./app-sidebar-layout.js";
  7. import { SidebarSectionHeader } from "./app-sidebar-section-header.js";
  8. import { displayWidth, truncateDisplay } from "./app-terminal-width.js";
  9. const h = React.createElement as (
  10. type: React.ElementType | string,
  11. props?: Record<string, unknown> | null,
  12. ...children: React.ReactNode[]
  13. ) => React.ReactElement;
  14. const MCP_SERVER_ROW_LIMIT = 5;
  15. const MCP_STATUS_WIDTH = 12;
  16. const MCP_META_WIDTH = 13;
  17. const MCP_NAME_WIDTH = SIDEBAR_CONTENT_WIDTH - MCP_STATUS_WIDTH - MCP_META_WIDTH - 2;
  18. const SIDEBAR_LABEL_WIDTH = 9;
  19. const SIDEBAR_ROW_VALUE_WIDTH = SIDEBAR_CONTENT_WIDTH - SIDEBAR_LABEL_WIDTH - 1;
  20. const SIDEBAR_TEXT_ROW_STYLE = { flexShrink: 0, height: 1, truncate: true, wrapMode: "none" };
  21. export function McpSection(props: {
  22. copy: TuiCopy;
  23. expanded: boolean;
  24. mcpStatus: McpSidebarState;
  25. onToggle?: () => void;
  26. }): React.ReactElement {
  27. return h(
  28. "box",
  29. {
  30. style: {
  31. flexDirection: "column",
  32. flexShrink: 0,
  33. marginTop: 1,
  34. },
  35. },
  36. SidebarSectionHeader({
  37. expanded: props.expanded,
  38. onToggle: props.onToggle,
  39. title: props.copy.sidebar.sections.mcp,
  40. }),
  41. ...(props.expanded ? mcpLines(props.copy, props.mcpStatus) : []),
  42. );
  43. }
  44. function mcpLines(copy: TuiCopy, state: McpSidebarState): React.ReactNode[] {
  45. const entries = Object.entries(state.servers).sort(([left], [right]) =>
  46. left.localeCompare(right),
  47. );
  48. const connected = entries.filter(([, server]) => server.status === "connected").length;
  49. const lines = [
  50. rowLine(
  51. copy.sidebar.mcp.servers,
  52. copy.sidebar.mcp.summary({ connected, total: entries.length }),
  53. "mcp-summary",
  54. ),
  55. ];
  56. if (state.loading && entries.length === 0) {
  57. lines.push(textLine(copy.sidebar.mcp.loading, palette.muted, "mcp-loading"));
  58. return lines;
  59. }
  60. if (entries.length === 0) {
  61. lines.push(
  62. textLine(
  63. state.error ? copy.sidebar.mcp.loadFailed : copy.sidebar.mcp.empty,
  64. errorColor(state),
  65. "mcp-empty",
  66. ),
  67. );
  68. if (state.error) lines.push(textLine(state.error, palette.danger, "mcp-error"));
  69. return lines;
  70. }
  71. for (const [name, status] of entries.slice(0, MCP_SERVER_ROW_LIMIT)) {
  72. lines.push(mcpServerLine(copy, name, status));
  73. if (status.error) {
  74. lines.push(
  75. textLine(
  76. truncateDisplay(status.error, SIDEBAR_CONTENT_WIDTH),
  77. palette.warning,
  78. `mcp-${name}-error`,
  79. ),
  80. );
  81. }
  82. }
  83. if (entries.length > MCP_SERVER_ROW_LIMIT) {
  84. lines.push(
  85. textLine(
  86. copy.sidebar.mcp.more(entries.length - MCP_SERVER_ROW_LIMIT),
  87. palette.muted,
  88. "mcp-more",
  89. ),
  90. );
  91. }
  92. if (state.error) {
  93. lines.push(textLine(copy.sidebar.mcp.loadFailed, palette.warning, "mcp-refresh-error"));
  94. }
  95. return lines;
  96. }
  97. function mcpServerLine(copy: TuiCopy, name: string, status: McpServerStatus): React.ReactElement {
  98. const statusLabel = copy.sidebar.mcp.status[status.status];
  99. const meta = `${status.transport} ${copy.sidebar.mcp.tools(status.toolCount)}`;
  100. return textLine(
  101. `${padEndDisplay(statusLabel, MCP_STATUS_WIDTH)} ${truncateDisplay(name, MCP_NAME_WIDTH)} ${truncateDisplay(meta, MCP_META_WIDTH)}`,
  102. serverStatusColor(status.status),
  103. `mcp-${name}`,
  104. );
  105. }
  106. function rowLine(label: string, value: string, key: string): React.ReactElement {
  107. return textLine(
  108. `${padEndDisplay(label, SIDEBAR_LABEL_WIDTH)} ${truncateDisplay(value, SIDEBAR_ROW_VALUE_WIDTH)}`,
  109. palette.text,
  110. key,
  111. );
  112. }
  113. function textLine(value: string, color: string, key: string): React.ReactElement {
  114. return h("text", { key, style: { ...SIDEBAR_TEXT_ROW_STYLE, fg: color } }, value);
  115. }
  116. function padEndDisplay(value: string, targetCells: number): string {
  117. const visible = truncateDisplay(value, targetCells);
  118. return `${visible}${" ".repeat(Math.max(0, targetCells - displayWidth(visible)))}`;
  119. }
  120. function serverStatusColor(status: McpServerStatusKind): string {
  121. if (status === "connected") return palette.success;
  122. if (status === "connecting") return palette.accent;
  123. if (status === "failed") return palette.danger;
  124. if (status === "untrusted") return palette.warning;
  125. return palette.muted;
  126. }
  127. function errorColor(state: McpSidebarState): string {
  128. return state.error ? palette.warning : palette.muted;
  129. }