Skip to content

Commit 0d8532c

Browse files
t3dotggclaude
andcommitted
fix(web): council round 10 fixes for the MCP wizard
- The automatic check waits until the environment is connected. - A disconnected saved machine shows checking while T3 Connect's list refreshes, keeps Check again when that list cannot be read, and the wizard asks for the list on open. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f3b9c14 commit 0d8532c

3 files changed

Lines changed: 73 additions & 28 deletions

File tree

‎apps/web/src/components/mcp/UseInAnotherAppDialog.tsx‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,11 @@ function useGate(environment: EnvironmentPresentation) {
251251
const linkState = controller.linkState.data;
252252
const reported = linkState?.reachability;
253253
const serverConfig = environment.serverConfig;
254+
const connected = environment.connection.phase === "connected";
255+
const relayEntry = discovery.environments.get(environmentId);
254256
const gate = resolveMcpGate({
255257
signedIn: isSignedIn,
256-
connected: environment.connection.phase === "connected",
257-
relayOffline:
258-
!thisMachine && discovery.environments.get(environmentId)?.availability === "offline",
258+
connected,
259259
approvalCodes:
260260
serverConfig === null
261261
? undefined
@@ -270,7 +270,8 @@ function useGate(environment: EnvironmentPresentation) {
270270
: discovery.listed
271271
? {
272272
status: "listed",
273-
endpoint: discovery.environments.get(environmentId)?.environment.endpoint,
273+
endpoint: relayEntry?.environment.endpoint,
274+
availability: relayEntry?.availability,
274275
}
275276
: { status: "loading" },
276277
reachability:
@@ -302,14 +303,24 @@ function useGate(environment: EnvironmentPresentation) {
302303
});
303304
if (!ok) setConnecting(false);
304305
};
305-
const tunnelActive = linkState !== null && isManagedTunnelActive(linkState, undefined);
306+
// A cached link state can name a tunnel on a disconnected machine; checking it would fail and
307+
// spend the one automatic check.
308+
const canCheck = connected && linkState !== null && isManagedTunnelActive(linkState, undefined);
306309
const checkOnce = useEffectEvent(() => {
307310
if (check === "idle") void runReachabilityCheck();
308311
});
309312
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- asks the server to check its tunnel once one exists
310313
useEffect(() => {
311-
if (tunnelActive) queueMicrotask(checkOnce);
312-
}, [tunnelActive]);
314+
if (canCheck) queueMicrotask(checkOnce);
315+
}, [canCheck]);
316+
// T3 Connect lists machines only once something asks, and a saved machine's gate reads that list.
317+
const discoverIfUnasked = useEffectEvent(() => {
318+
if (!thisMachine && !discovery.listed && !discovery.refreshing) void refreshDiscovery();
319+
});
320+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- asks T3 Connect for its machine list once the wizard opens signed in
321+
useEffect(() => {
322+
if (isSignedIn) discoverIfUnasked();
323+
}, [isSignedIn]);
313324
const waiting = gate.kind === "offline" || (connecting && gate.kind === "turn-on");
314325
const refreshLinkState = useEffectEvent(() => controller.linkState.refresh());
315326
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- polls the server's link state while the tunnel starts; the server has no push for it
@@ -348,11 +359,12 @@ function GateScreen({
348359
const name = environment.label;
349360
const turnOnHere = useT3ConnectTurnOn(environment.environmentId);
350361
const { openAuthPrompt } = useT3ConnectAuthPrompt();
351-
const checkAgain = (
352-
<Button variant="outline" onClick={() => void check()}>
362+
const retry = (onClick: () => void) => (
363+
<Button variant="outline" onClick={onClick}>
353364
Check again
354365
</Button>
355366
);
367+
const checkAgain = retry(() => void check());
356368
switch (gate.kind) {
357369
case "checking":
358370
return (
@@ -382,14 +394,18 @@ function GateScreen({
382394
</GateMessage>
383395
);
384396
case "disconnected":
385-
return gate.offline ? (
386-
<GateMessage title={`${name}'s T3 Connect is offline`}>
387-
<Button variant="outline" onClick={refreshList}>
388-
Check again
389-
</Button>
390-
</GateMessage>
391-
) : (
397+
return gate.relay === null ? (
392398
<GateMessage title={`Connect to ${name} first.`} />
399+
) : (
400+
<GateMessage
401+
title={
402+
gate.relay === "offline"
403+
? `${name}'s T3 Connect is offline`
404+
: `Can't check T3 Connect on ${name}`
405+
}
406+
>
407+
{retry(refreshList)}
408+
</GateMessage>
393409
);
394410
case "update":
395411
return <GateMessage title={`Update T3 Code on ${name}`} />;

‎apps/web/src/components/mcp/mcpGate.test.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const savedTunnel = {
2929
const open = {
3030
signedIn: true,
3131
connected: true,
32-
relayOffline: false,
3332
approvalCodes: true,
3433
canReadRelay: true,
3534
linkState: tunnel,
@@ -56,11 +55,20 @@ describe("resolveMcpGate", () => {
5655
kind: "open",
5756
mcpUrl: "https://prod-b2.t3coderelay.com/mcp",
5857
});
59-
// A saved machine T3 Connect reports offline says so instead of asking to reconnect.
60-
expect(resolveMcpGate({ ...saved, connected: false, relayOffline: true })).toEqual({
58+
// A disconnected saved machine follows T3 Connect's list. Offline or an unreadable list keeps
59+
// a retry; only a list showing nothing wrong asks to reconnect.
60+
const away = { ...saved, connected: false };
61+
const listed = (availability: "online" | "offline" | "error") => ({
62+
...away,
63+
relay: { ...saved.relay, availability },
64+
});
65+
expect(resolveMcpGate(listed("offline"))).toEqual({ kind: "disconnected", relay: "offline" });
66+
expect(resolveMcpGate(listed("error"))).toEqual({ kind: "disconnected", relay: "failed" });
67+
expect(resolveMcpGate({ ...away, relay: { status: "failed" } })).toEqual({
6168
kind: "disconnected",
62-
offline: true,
69+
relay: "failed",
6370
});
71+
expect(resolveMcpGate(listed("online"))).toEqual({ kind: "disconnected", relay: null });
6472
// This machine's probe outranks a stale publish-only record in that list.
6573
expect(
6674
resolveMcpGate({
@@ -97,6 +105,11 @@ describe("resolveMcpGate", () => {
97105
"turn-on",
98106
],
99107
["T3 Connect's list still loading", { ...saved, relay: { status: "loading" } }, "checking"],
108+
[
109+
"a disconnected saved machine while T3 Connect's list refreshes",
110+
{ ...saved, connected: false, relay: { status: "loading" } },
111+
"checking",
112+
],
100113
[
101114
"no tunnel, before T3 Connect's list loads",
102115
{

‎apps/web/src/components/mcp/mcpGate.ts‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
EnvironmentCloudLinkStateResult,
33
EnvironmentConnectReachability,
44
} from "@t3tools/contracts";
5+
import type { Discovery } from "@t3tools/client-runtime/relay";
56
import type { RelayManagedEndpoint } from "@t3tools/contracts/relay";
67

78
import { isManagedTunnelActive, t3ConnectMcpUrl } from "~/cloud/t3ConnectState";
@@ -15,8 +16,11 @@ type Unreachable = Extract<EnvironmentConnectReachability, { readonly status: "u
1516
export type McpGate =
1617
| { readonly kind: "checking" }
1718
| { readonly kind: "sign-in" }
18-
/** `offline`: T3 Connect reports the saved machine offline, so reconnecting cannot help. */
19-
| { readonly kind: "disconnected"; readonly offline: boolean }
19+
/**
20+
* `relay`: T3 Connect reports the saved machine offline, or its list could not be read; both
21+
* keep a retry. Null when that list shows nothing wrong, so the user should reconnect.
22+
*/
23+
| { readonly kind: "disconnected"; readonly relay: "offline" | "failed" | null }
2024
| { readonly kind: "update" }
2125
| { readonly kind: "no-relay-read" }
2226
| { readonly kind: "error"; readonly message: string }
@@ -28,30 +32,42 @@ export function resolveMcpGate(input: {
2832
/** Undefined until T3 Connect sign-in has loaded. */
2933
readonly signedIn: boolean | undefined;
3034
readonly connected: boolean;
31-
/** T3 Connect's list reports this saved machine offline. */
32-
readonly relayOffline: boolean;
3335
/** The environment's `mcpApprovalCodes` capability; undefined until its config loads. */
3436
readonly approvalCodes: boolean | undefined;
3537
readonly canReadRelay: boolean;
3638
readonly linkState: EnvironmentCloudLinkStateResult | null;
3739
readonly linkStateError: string | null;
3840
/** A backend this device runs; its link state names its own tunnel. */
3941
readonly thisMachine: boolean;
40-
/** The account's machines on T3 Connect, where a saved machine's address comes from. */
42+
/** The account's machines on T3 Connect, where a saved machine's address and health come from. */
4143
readonly relay:
4244
| { readonly status: "loading" | "failed" }
43-
| { readonly status: "listed"; readonly endpoint: RelayManagedEndpoint | undefined };
45+
| {
46+
readonly status: "listed";
47+
readonly endpoint: RelayManagedEndpoint | undefined;
48+
readonly availability?: Discovery.RelayEnvironmentAvailability | undefined;
49+
};
4450
/** A check run for this gate: its answer, "checking" until it has one, or null if it failed. */
4551
readonly reachability: EnvironmentConnectReachability | "checking" | null;
4652
}): McpGate {
4753
if (input.signedIn === undefined) return { kind: "checking" };
4854
if (!input.signedIn) return { kind: "sign-in" };
49-
if (!input.connected) return { kind: "disconnected", offline: input.relayOffline };
55+
const { relay } = input;
56+
if (!input.connected) {
57+
if (input.thisMachine) return { kind: "disconnected", relay: null };
58+
const availability = relay.status === "listed" ? relay.availability : undefined;
59+
// A refresh empties the list first, so wait for it instead of flashing "Connect first".
60+
if (relay.status === "loading" || availability === "checking") return { kind: "checking" };
61+
if (relay.status === "failed" || availability === "error") {
62+
return { kind: "disconnected", relay: "failed" };
63+
}
64+
return { kind: "disconnected", relay: availability === "offline" ? "offline" : null };
65+
}
5066
if (input.approvalCodes === undefined) return { kind: "checking" };
5167
if (!input.approvalCodes) return { kind: "update" };
5268
if (!input.canReadRelay) return { kind: "no-relay-read" };
5369
if (input.linkStateError !== null) return { kind: "error", message: input.linkStateError };
54-
const { linkState, relay } = input;
70+
const { linkState } = input;
5571
if (linkState === null) return { kind: "checking" };
5672
// This machine reports its own tunnel and the probe proves it; the list speaks for saved ones.
5773
const endpoint = !input.thisMachine && relay.status === "listed" ? relay.endpoint : undefined;

0 commit comments

Comments
 (0)