feat: add collapsible MCP JSON field-mapping helper (#1136)

* feat(web): add collapsible MCP JSON helper component

* feat(web): add collapsible MCP JSON field-mapping helper

* test(web): add McpJsonHelper component tests for toggle behavior

* fix(web): scope helper id per row and show helper

* test(web): rewrite McpJsonHelper tests to use row-scoped ids

* feat(mcp): use stable _localId for McpRow keys and aria-controls\n\n- Add _localId to DraftRow and genLocalId()\n- Use _localId as React key and helper id to avoid duplicate DOM ids\n- Move helper outside transport branches so helper is visible for all transports\n- Fix malformed template.homepage anchor

* fix(web): restore _localId-scoped helperId and helper visibility for all transports

* test(web): replace integration test with _localId-scoped helper tests

* test(web): exercise McpJsonHelper via production McpClientSection in jsdom

* fix(web): resolve typecheck errors

* test(web):expand rows before querying helper toggles to fix timeout
This commit is contained in:
Priyanshu Kayarkar 2026-05-10 17:07:46 +05:30 committed by GitHub
parent 1f625cff77
commit eabf3a6e86
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 355 additions and 9 deletions

View file

@ -51,6 +51,16 @@ interface DraftRow extends McpServerConfig {
// map when the user steps away from the field.
_envText?: string;
_headersText?: string;
// Per-instance local id to use as a stable React `key` independent of
// the editable `id` field (avoids remounts & focus loss while editing).
_localId: string;
}
// Simple incrementing local id generator for row keys. Kept module-scoped
// and deterministic for the lifetime of this UI instance.
let NEXT_LOCAL_ID = 1;
function genLocalId(): string {
return `mcp-row-${NEXT_LOCAL_ID++}`;
}
function rowsFromServers(servers: McpServerConfig[]): DraftRow[] {
@ -58,6 +68,7 @@ function rowsFromServers(servers: McpServerConfig[]): DraftRow[] {
...s,
_envText: s.env ? mapToText(s.env) : '',
_headersText: s.headers ? mapToText(s.headers) : '',
_localId: genLocalId(),
}));
}
@ -127,6 +138,7 @@ function rowFromTemplate(
_envText: Object.keys(env).length > 0 ? mapToText(env) : '',
_headersText: Object.keys(headers).length > 0 ? mapToText(headers) : '',
_isNew: true,
_localId: genLocalId(),
};
}
@ -141,6 +153,7 @@ function rowFromBlank(taken: ReadonlySet<string>): DraftRow {
_envText: '',
_headersText: '',
_isNew: true,
_localId: genLocalId(),
};
}
@ -397,7 +410,7 @@ export const McpClientSection = forwardRef<McpClientSectionHandle, Props>(
<div className="mcp-rows">
{rows.map((row, idx) => (
<McpRow
key={`${row.id}-${idx}`}
key={row._localId}
row={row}
idx={idx}
total={rows.length}
@ -637,13 +650,11 @@ interface RowProps {
function McpRow({ row, idx, total, template, onChange, onRemove, onMoveUp, onMoveDown }: RowProps) {
const isHttpLike = row.transport === 'http' || row.transport === 'sse';
// Every row starts collapsed. The Settings page is meant to be scannable
// — the user expands a row only when they need to edit fields or run the
// OAuth dance. Newly-added rows from the picker are no exception: their
// fields are already pre-filled from the template, so the user can save
// immediately if they don't need to customize anything.
const [expanded, setExpanded] = useState<boolean>(false);
const summaryTitle = row.label?.trim() || row.id || 'Unnamed MCP server';
const [showMcpExample, setShowMcpExample] = useState<boolean>(false);
const helperId = `mcp-json-helper-panel-${row._localId}`;
return (
<div
className={`mcp-row${row.enabled ? '' : ' mcp-row-disabled'}${
@ -668,9 +679,6 @@ function McpRow({ row, idx, total, template, onChange, onRemove, onMoveUp, onMov
onChange={(e) => onChange({ label: e.target.value })}
/>
) : (
// When collapsed, the label is read-only — keeps the row compact
// and avoids accidental edits while scanning. The user can click
// the expand caret to reveal the full editable label input.
<button
type="button"
className="mcp-row-summary-title"
@ -859,6 +867,96 @@ function McpRow({ row, idx, total, template, onChange, onRemove, onMoveUp, onMov
</label>
</>
)}
<div className={`mcp-json-helper ${showMcpExample ? 'is-open' : ''}`}>
<button
type="button"
className="mcp-json-helper-toggle"
aria-expanded={showMcpExample}
aria-controls={helperId}
onClick={() => setShowMcpExample((prev) => !prev)}
>
<span className="mcp-json-helper-toggle-content">
<span className="mcp-json-helper-eye">
<Icon name="eye" />
</span>
<span className="mcp-json-helper-toggle-text">
Need help? Map your MCP server's JSON config using the example below.
</span>
</span>
<span className="mcp-json-helper-toggle-icon">
{showMcpExample ? (
<Icon name="arrow-up" />
) : (
<Icon name="chevron-down" />
)}
</span>
</button>
{showMcpExample && (
<div className="mcp-json-helper-example" id={helperId}>
<div className="mcp-json-helper-example-head">
Example MCP JSON
</div>
<pre className="mcp-json-helper-code">
<code>
<span className="json-punctuation">{"{"}</span>
{"\n "}
<span className="json-key">"mcpServers"</span>
<span className="json-punctuation">: {"{"}</span>
{"\n "}
<span className="json-key">"tdesign"</span>
<span className="json-punctuation">: {"{"}</span>
{"\n "}
<span className="json-key">"command"</span>
<span className="json-punctuation">:</span>{" "}
<span className="json-string">"npx"</span>
<span className="json-punctuation">,</span>
{"\n "}
<span className="json-key">"args"</span>
<span className="json-punctuation">: [</span>
<span className="json-string">"-y"</span>
<span className="json-punctuation">, </span>
<span className="json-string">"tdesign-mcp-server@latest"</span>
<span className="json-punctuation">],</span>
{"\n "}
<span className="json-key">"env"</span>
<span className="json-punctuation">: {"{"}</span>
{"\n "}
<span className="json-key">"API_KEY"</span>
<span className="json-punctuation">:</span>{" "}
<span className="json-string">"your-key-here"</span>
{"\n "}
<span className="json-punctuation">{"}"}</span>
{"\n "}
<span className="json-punctuation">{"}"}</span>
{"\n "}
<span className="json-punctuation">{"}"}</span>
{"\n"}
<span className="json-punctuation">{"}"}</span>
</code>
</pre>
<div className="mcp-json-helper-conversion">
<div>
<strong>Command</strong>
<code>npx</code>
</div>
<div>
<strong>Args</strong>
<code>-y tdesign-mcp-server@latest</code>
</div>
<div>
<strong>Env</strong>
<code>API_KEY = your-key-here</code>
</div>
<div>
<strong>HTTP / SSE</strong>
<code>use url + headers instead of command / args</code>
</div>
</div>
</div>
)}
</div>
</>
) : null}
</div>

View file

@ -15434,6 +15434,196 @@ body.entry-resizing { cursor: col-resize; user-select: none; }
text-decoration-thickness: 2px;
}
.mcp-json-helper {
margin-top: 10px;
border: 1px solid var(--border);
border-radius: var(--radius);
background: var(--bg-subtle);
overflow: hidden;
}
.mcp-json-helper-toggle {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 10px 12px;
border: none;
border-radius: calc(var(--radius) - 2px);
background: transparent;
font-size: 12px;
font-weight: 500;
line-height: 1.5;
color: var(--text-muted);
cursor: pointer;
transition:
color 120ms ease,
background 120ms ease,
box-shadow 120ms ease;
}
.mcp-json-helper-toggle:hover {
color: var(--text);
background: transparent;
box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent-soft) 30%, transparent);
}
.mcp-json-helper.is-open .mcp-json-helper-toggle {
border: none;
border-radius: 0;
background: transparent;
box-shadow: inset 0 -1px 0 var(--border);
}
.mcp-json-helper.is-open .mcp-json-helper-toggle:hover {
box-shadow: inset 0 -1px 0 var(--border);
}
.mcp-json-helper-toggle span:first-child {
text-align: left;
}
.mcp-json-helper-toggle-icon {
display: inline-flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
color: var(--text-soft);
}
.mcp-json-helper-example {
display: flex;
flex-direction: column;
}
.mcp-json-helper-example-head {
padding: 10px 12px 0;
font-size: 11px;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
color: var(--text-soft);
}
.mcp-json-helper-code {
margin: 0 12px 12px;
padding: 14px;
border: 1px solid var(--border);
border-radius: calc(var(--radius) - 4px);
background:
linear-gradient(
180deg,
color-mix(in srgb, var(--bg) 92%, black) 0%,
color-mix(in srgb, var(--bg) 86%, black) 100%
);
font-family: var(--mono);
font-size: 12px;
line-height: 1.7;
color: var(--text);
overflow-x: auto;
}
.mcp-json-helper-code code {
background: transparent;
padding: 0;
border-radius: 0;
}
.json-key {
color: var(--accent);
}
.json-string {
color: var(--green);
}
.json-punctuation {
color: var(--text-soft);
}
.mcp-json-helper-conversion {
display: flex;
flex-direction: column;
gap: 10px;
padding: 12px;
border-top: 1px solid var(--border);
background: transparent;
}
.mcp-json-helper-conversion div {
display: flex;
flex-direction: column;
gap: 4px;
}
.mcp-json-helper-conversion strong {
font-size: 11px;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
color: var(--text-soft);
}
.mcp-json-helper-conversion code {
width: fit-content;
}
.mcp-json-helper-toggle-content {
display: flex;
align-items: flex-start;
gap: 10px;
min-width: 0;
flex: 1;
}
.mcp-json-helper-eye {
width: 22px;
height: 22px;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 999px;
background: var(--accent-tint);
color: var(--accent);
flex-shrink: 0;
}
.mcp-json-helper-toggle-text {
font-size: 12px;
font-weight: 500;
line-height: 1.5;
color: inherit;
text-align: left;
}
/* ----- Project actions toolbar (#451) ---------------------------------- */
/* Project-scoped actions live in their own bar between the chrome header
* and the chat/workspace split. File-scoped actions stay inside

View file

@ -0,0 +1,58 @@
// @vitest-environment jsdom
import { fireEvent, render, screen } from '@testing-library/react';
import { beforeAll, describe, expect, it } from 'vitest';
import { McpClientSection } from '../../src/components/McpClientSection';
beforeAll(() => {
(global as any).fetch = (input: RequestInfo) => {
const url = typeof input === 'string' ? input : String(input);
if (url.endsWith('/api/mcp/servers')) {
return Promise.resolve(new Response(
JSON.stringify({
servers: [
{ id: 'srv-1', transport: 'stdio', enabled: true },
{ id: 'srv-2', transport: 'http', enabled: true },
],
templates: [],
}),
)) as any;
}
return Promise.resolve(new Response('{}')) as any;
};
});
describe('McpJsonHelper (production)', () => {
it('renders helper toggles and opens the per-row panel with a unique id', async () => {
render(<McpClientSection />);
const expandButtons = await screen.findAllByRole('button', {
name: /Expand this MCP server/i,
});
expect(expandButtons.length).toBeGreaterThanOrEqual(2);
fireEvent.click(expandButtons[0]!);
fireEvent.click(expandButtons[1]!);
const toggles = await screen.findAllByRole('button', { name: /Need help\?/i });
expect(toggles.length).toBeGreaterThanOrEqual(2);
const firstToggle = toggles[0];
const secondToggle = toggles[1];
if (!firstToggle || !secondToggle) {
throw new Error('Expected at least two MCP helper toggle buttons');
}
expect(firstToggle.getAttribute('aria-expanded')).toBe('false');
fireEvent.click(secondToggle);
const ariaControls = secondToggle.getAttribute('aria-controls');
expect(ariaControls).toBeTruthy();
expect(ariaControls).not.toBe('mcp-json-helper-panel');
expect(ariaControls).toMatch(/^mcp-json-helper-panel-/);
const panel = document.getElementById(ariaControls!);
expect(panel).toBeTruthy();
expect(panel?.textContent).toContain('Example MCP JSON');
});
});