@blackbelt-technology/pi-dashboard-mcp-client-plugin 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,212 @@
1
+ /**
2
+ * FolderMcpSection — `sidebar-folder-section` + `worktree-card-section` claim.
3
+ *
4
+ * ONE compact pill per folder cwd, counted from `/effective`: the active
5
+ * server count, the off count when non-zero (warning colour; off = disabled
6
+ * OR inactive), an untrusted state, and an error marker (error colour) whose
7
+ * accessible name names the failing layer path. While the effective view
8
+ * loads it renders a muted placeholder of the same height rather than an
9
+ * empty slot. Activating it opens `/folder/<encodedCwd>/mcp` through the same
10
+ * wouter navigation the kb-plugin pill uses; `placement` picks the raised
11
+ * (sidebar) or flat (worktree card) surface.
12
+ *
13
+ * A cwd outside the host's known-folder set is refused by the server (403) and
14
+ * rendered as a muted "not tracked" state. That refusal is cached per cwd in
15
+ * the effective-config store, so a remount never re-asks; the cache is dropped
16
+ * when the host's session list changes (a folder can become known), which is
17
+ * also the moment the pill retries. See change: migrate-mcp-to-pi-builtin.
18
+ */
19
+ import {
20
+ SlotPill,
21
+ useAllSessions,
22
+ useT,
23
+ } from "@blackbelt-technology/dashboard-plugin-runtime";
24
+ import type {
25
+ FolderDescriptor,
26
+ SlotPlacement,
27
+ } from "@blackbelt-technology/pi-dashboard-shared/dashboard-plugin/slot-props.js";
28
+ import { mdiServerOutline } from "@mdi/js";
29
+ import type React from "react";
30
+ import { useEffect, useRef } from "react";
31
+ import { useLocation } from "wouter";
32
+ import { ApiError } from "./api.js";
33
+ import { invalidateEffective, useEffectiveConfig } from "./hooks.js";
34
+
35
+ export interface FolderMcpSectionProps {
36
+ folder?: FolderDescriptor;
37
+ placement?: SlotPlacement;
38
+ }
39
+
40
+ type Translate = ReturnType<typeof useT>;
41
+
42
+ export function folderMcpUrl(cwd: string): string {
43
+ return `/folder/${encodeURIComponent(cwd)}/mcp`;
44
+ }
45
+
46
+ function ErrorMarker({ name }: { name: string }): React.ReactElement {
47
+ return (
48
+ <span
49
+ data-testid="mcp-folder-pill-error"
50
+ role="img"
51
+ aria-label={name}
52
+ className="text-red-400 flex-none"
53
+ >
54
+ ⚠
55
+ </span>
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Retry hook for a cached 403: the session list is the signal that a folder
61
+ * may have become known, so a change drops the refusal and forces one request.
62
+ */
63
+ function useSessionRetry(cwd: string | undefined, error: unknown, reload: () => void): void {
64
+ const sessions = useAllSessions();
65
+ const signature = sessions.map((s) => s.id).join("|");
66
+ const lastSignature = useRef(signature);
67
+ useEffect(() => {
68
+ if (lastSignature.current === signature) return;
69
+ lastSignature.current = signature;
70
+ if (error instanceof ApiError && error.isNotAllowed) {
71
+ invalidateEffective(cwd);
72
+ reload();
73
+ }
74
+ }, [signature, cwd, error, reload]);
75
+ }
76
+
77
+ interface PillContent {
78
+ accent: "indigo" | "red";
79
+ body: React.ReactNode;
80
+ }
81
+
82
+ interface PillState {
83
+ parseErrorPath: string | undefined;
84
+ otherError: Error | null;
85
+ notAllowed: boolean;
86
+ loading: boolean;
87
+ hasView: boolean;
88
+ untrusted: boolean;
89
+ count: number;
90
+ off: number;
91
+ }
92
+
93
+ function pillContent(t: Translate, state: PillState): PillContent {
94
+ if (state.parseErrorPath !== undefined) {
95
+ const path = state.parseErrorPath;
96
+ return {
97
+ accent: "red",
98
+ body: (
99
+ <ErrorMarker
100
+ name={t("mcpFolderParseErrorAria", { path }, `MCP config failed to parse: ${path}`)}
101
+ />
102
+ ),
103
+ };
104
+ }
105
+ if (state.otherError) {
106
+ const message = state.otherError.message;
107
+ return {
108
+ accent: "red",
109
+ body: (
110
+ <ErrorMarker name={t("mcpFolderErrorAria", { message }, `MCP config error: ${message}`)} />
111
+ ),
112
+ };
113
+ }
114
+ if (state.notAllowed) {
115
+ return {
116
+ accent: "indigo",
117
+ body: (
118
+ <span
119
+ data-testid="mcp-folder-pill-not-tracked"
120
+ className="text-[var(--text-tertiary)] font-medium"
121
+ >
122
+ {t("mcpFolderNotTracked", undefined, "not tracked")}
123
+ </span>
124
+ ),
125
+ };
126
+ }
127
+ if (state.loading && !state.hasView) {
128
+ return {
129
+ accent: "indigo",
130
+ body: (
131
+ <span
132
+ data-testid="mcp-folder-pill-loading"
133
+ className="inline-block h-[15px] w-16 rounded bg-[var(--bg-tertiary,#27272a)] animate-pulse"
134
+ />
135
+ ),
136
+ };
137
+ }
138
+ if (state.untrusted) {
139
+ return {
140
+ accent: "indigo",
141
+ body: (
142
+ <span
143
+ data-testid="mcp-folder-pill-untrusted"
144
+ className="text-[var(--text-tertiary)] font-medium"
145
+ >
146
+ {t("mcpFolderUntrustedPill", undefined, "untrusted")}
147
+ </span>
148
+ ),
149
+ };
150
+ }
151
+ return {
152
+ accent: "indigo",
153
+ body: (
154
+ <>
155
+ <span data-testid="mcp-folder-pill-count" className="tabular-nums">
156
+ {state.count === 1
157
+ ? t("mcpFolderServerOne", undefined, "1 server")
158
+ : t("mcpFolderServerCount", { count: state.count }, `${state.count} servers`)}
159
+ </span>
160
+ {state.off > 0 && (
161
+ <>
162
+ <span className="text-[var(--text-tertiary)]">·</span>
163
+ <span data-testid="mcp-folder-pill-off" className="text-amber-400">
164
+ {t("mcpFolderOffCount", { count: state.off }, `${state.off} off`)}
165
+ </span>
166
+ </>
167
+ )}
168
+ </>
169
+ ),
170
+ };
171
+ }
172
+
173
+ export function FolderMcpSection({
174
+ folder,
175
+ placement = "sidebar",
176
+ }: FolderMcpSectionProps): React.ReactElement | null {
177
+ const t = useT();
178
+ const cwd = folder?.cwd;
179
+ const [, navigate] = useLocation();
180
+ const { view, loading, error, reload } = useEffectiveConfig(cwd);
181
+ useSessionRetry(cwd, error, reload);
182
+
183
+ if (!cwd) return null;
184
+
185
+ const notAllowed = error instanceof ApiError && error.isNotAllowed;
186
+ const otherError = error && !notAllowed ? (error as Error) : null;
187
+ const servers = view?.servers ?? [];
188
+ const { accent, body } = pillContent(t, {
189
+ parseErrorPath: view?.layers.find((l) => !l.ok)?.path,
190
+ otherError,
191
+ notAllowed,
192
+ loading,
193
+ hasView: view !== null,
194
+ untrusted: view?.trusted === false,
195
+ count: servers.length,
196
+ off: servers.filter((s) => !s.enabled || !s.active).length,
197
+ });
198
+
199
+ return (
200
+ <SlotPill
201
+ surface={placement === "card" ? "flat" : "raised"}
202
+ glyph={mdiServerOutline}
203
+ accent={accent}
204
+ label={t("mcpFolderPillLabel", undefined, "MCP")}
205
+ activateTestId="mcp-folder-pill"
206
+ activateTitle={t("mcpFolderPillTitle", undefined, "MCP servers for this folder")}
207
+ onActivate={() => navigate(folderMcpUrl(cwd))}
208
+ >
209
+ {body}
210
+ </SlotPill>
211
+ );
212
+ }
@@ -0,0 +1,122 @@
1
+ /**
2
+ * mcp-client · settings-section root (`McpSettingsClaim`).
3
+ *
4
+ * The Pi-global server list (rows carry pi's live state + tool count from
5
+ * `/live`, fetched on page view / explicit refresh), error states, and the
6
+ * schema-driven server editor. No adapter status, no global settings form —
7
+ * the section is always editable.
8
+ *
9
+ * See change: migrate-mcp-to-pi-builtin.
10
+ */
11
+ import { useT } from "@blackbelt-technology/dashboard-plugin-runtime";
12
+ import type React from "react";
13
+ import { useState } from "react";
14
+ import { ApiError } from "./api.js";
15
+ import { useEffectiveConfig, useLiveState } from "./hooks.js";
16
+ import { ServerEditor } from "./ServerEditor.js";
17
+ import { ServerList } from "./ServerList.js";
18
+
19
+ export function McpSettingsClaim(): React.ReactElement {
20
+ return <McpSettings />;
21
+ }
22
+
23
+ /** The editor for the open `editing` key: "" = add, a name = that server, null = closed. */
24
+ function EditorForEditing({
25
+ editing,
26
+ entryOf,
27
+ onClose,
28
+ onChanged,
29
+ }: {
30
+ editing: string | null;
31
+ entryOf: (name: string) => Record<string, unknown> | null;
32
+ onClose: () => void;
33
+ onChanged: () => void;
34
+ }): React.ReactElement | null {
35
+ if (editing === null) return null;
36
+ const entry = editing === "" ? {} : (entryOf(editing) ?? {});
37
+ return (
38
+ <ServerEditor
39
+ key={editing}
40
+ name={editing === "" ? null : editing}
41
+ entry={entry}
42
+ onClose={onClose}
43
+ onChanged={onChanged}
44
+ />
45
+ );
46
+ }
47
+
48
+ export function McpSettings(): React.ReactElement {
49
+ const t = useT();
50
+ const { view, loading, error, reload } = useEffectiveConfig();
51
+ const { live, loading: liveLoading, refresh } = useLiveState();
52
+ const [editing, setEditing] = useState<string | null>(null);
53
+
54
+ const notAllowed = error instanceof ApiError && error.isNotAllowed ? error : null;
55
+ const otherError = error && !notAllowed ? error : null;
56
+
57
+ const refreshAll = (): void => {
58
+ reload();
59
+ refresh();
60
+ };
61
+
62
+ return (
63
+ <section data-testid="mcp-settings" className="space-y-2">
64
+ <div className="flex items-center gap-2 flex-wrap">
65
+ <h3 className="text-sm font-semibold m-0 text-[var(--text-primary)]">
66
+ {t("mcpServersHeading", undefined, "MCP servers")}
67
+ </h3>
68
+ <button
69
+ type="button"
70
+ onClick={refreshAll}
71
+ data-testid="mcp-refresh"
72
+ title={t("mcpRefreshTitle", undefined, "Re-read the config files and pi's live state")}
73
+ className="text-[11px] px-2 py-1 min-h-11 min-w-11 sm:min-h-0 sm:min-w-0 rounded border border-[var(--border-secondary)] text-[var(--text-secondary)] hover:text-[var(--text-primary)]"
74
+ >
75
+ {t("mcpRefresh", undefined, "Refresh")}
76
+ </button>
77
+ <button
78
+ type="button"
79
+ onClick={() => setEditing("")}
80
+ data-testid="mcp-add-server"
81
+ className="ml-auto text-[11px] px-2 py-1 min-h-11 min-w-11 sm:min-h-0 sm:min-w-0 rounded border border-[var(--border-secondary)] text-[var(--text-secondary)] hover:text-[var(--text-primary)]"
82
+ >
83
+ {t("mcpAddServer", undefined, "Add server")}
84
+ </button>
85
+ </div>
86
+
87
+ {notAllowed && (
88
+ <p data-testid="mcp-not-allowed" role="alert" className="text-[11px] m-0">
89
+ {t("mcpNotAllowed", undefined, "This folder is not tracked by the dashboard.")}
90
+ </p>
91
+ )}
92
+
93
+ {otherError && (
94
+ <p
95
+ data-testid="mcp-error"
96
+ role="alert"
97
+ className="text-[11px] text-[var(--status-error,#f87171)] m-0"
98
+ >
99
+ {t("mcpLoadError", { message: (otherError as Error).message }, `Could not load MCP configuration: ${(otherError as Error).message}`)}
100
+ </p>
101
+ )}
102
+
103
+ <ServerList
104
+ servers={view?.servers ?? []}
105
+ layers={view?.layers ?? []}
106
+ loading={loading}
107
+ live={live}
108
+ liveLoading={liveLoading}
109
+ onOpen={setEditing}
110
+ onAdd={() => setEditing("")}
111
+ onChanged={refreshAll}
112
+ />
113
+
114
+ <EditorForEditing
115
+ editing={editing}
116
+ entryOf={(name) => view?.servers.find((s) => s.name === name)?.entry ?? null}
117
+ onClose={() => setEditing(null)}
118
+ onChanged={refreshAll}
119
+ />
120
+ </section>
121
+ );
122
+ }