Skip to content

Commit bbff835

Browse files
committed
fix(web): copy the local environment's MCP URL
Settings → Sharing & access only offered Copy MCP URL for saved remote environments, so a host could not find the T3 Connect address a hosted agent needs. The server now reports the live tunnel hostname its connector receives, and the environment card offers an MCP URL row that prefers it and falls back to an https or loopback route.
1 parent 6577c6b commit bbff835

10 files changed

Lines changed: 181 additions & 28 deletions

File tree

‎apps/server/src/cloud/CloudLink.lifecycle.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ describe("releaseManagedTunnelOnShutdown", () => {
142142
readonly respondEffect?: Effect.Effect<Response>;
143143
readonly getExisting?: CliTokenManager.CloudCliTokenManager["Service"]["getExisting"];
144144
readonly latestReachability?: ConnectReachability.ConnectReachability["Service"]["latest"];
145+
readonly connector?: ManagedEndpointRuntime.ConnectorFacts | null;
145146
}
146147

147148
const writeLauncherState = (update: ServiceUpdateRecord) =>
@@ -208,7 +209,7 @@ describe("releaseManagedTunnelOnShutdown", () => {
208209
requestRecovery: () => Effect.void,
209210
restartConnector: () => Effect.succeed(false),
210211
tunnelConnected: Stream.empty,
211-
inspectConnector: Effect.succeed(null),
212+
inspectConnector: Effect.succeed(harness.connector ?? null),
212213
readinessChanged: Stream.empty,
213214
withLinkStateLock: (effect) => effect,
214215
}),
@@ -1111,4 +1112,26 @@ describe("releaseManagedTunnelOnShutdown", () => {
11111112
}),
11121113
);
11131114
});
1115+
1116+
it.effect("reports the live tunnel address for agents that cannot discover the host", () => {
1117+
const { store } = makeMemorySecretStore(managedLinkSecrets);
1118+
1119+
return Effect.gen(function* () {
1120+
const state = yield* (yield* CloudLink.CloudLink).linkState();
1121+
1122+
expect(state.managedTunnelHttpBaseUrl).toBe("https://prod-abc.t3coderelay.com");
1123+
}).pipe(
1124+
provideReleaseHarness({
1125+
store,
1126+
applyConfigCalls: [],
1127+
requests: [],
1128+
connector: {
1129+
pid: 123,
1130+
readyConnections: 4,
1131+
hostname: "prod-abc.t3coderelay.com",
1132+
protocol: "quic",
1133+
},
1134+
}),
1135+
);
1136+
});
11141137
});

‎apps/server/src/cloud/CloudLink.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,10 @@ const make = Effect.gen(function* () {
12831283
],
12841284
{ concurrency: 8 },
12851285
);
1286+
// Only a running connector knows the tunnel hostname the relay assigned.
1287+
const connector = Option.isSome(endpointRuntimeConfig)
1288+
? yield* endpointRuntime.inspectConnector
1289+
: null;
12861290
// A not-linked check predates the stored tunnel config; the next check replaces it.
12871291
const currentReachability = Option.filter(
12881292
latestReachability,
@@ -1297,6 +1301,10 @@ const make = Effect.gen(function* () {
12971301
relayUrl: Option.isSome(relayUrl) ? bytesToString(relayUrl.value) : null,
12981302
relayIssuer: Option.isSome(relayIssuer) ? bytesToString(relayIssuer.value) : null,
12991303
managedTunnelActive: Option.isSome(endpointRuntimeConfig),
1304+
managedTunnelHttpBaseUrl:
1305+
connector?.hostname === undefined || connector.hostname === null
1306+
? null
1307+
: `https://${connector.hostname}`,
13001308
publishAgentActivity: Option.isSome(publishAgentActivity)
13011309
? bytesToString(publishAgentActivity.value) === "true"
13021310
: false,

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ import { ITEM_ROW_CLASSNAME } from "./itemRows";
135135
import {
136136
CloudLinkRow,
137137
CloudRemoteEnvironmentRows,
138+
LocalEnvironmentMcpUrlRow,
138139
SavedBackendListRow,
139140
} from "./SavedEnvironmentRows";
140141
import { AddEnvironmentDialog } from "./AddEnvironmentDialog";
@@ -1244,6 +1245,9 @@ function ConnectionsSettingsContent({
12441245
canManageRelay={canManageRelay}
12451246
/>
12461247
) : null}
1248+
{isPrimaryAccess && accessEnvironment ? (
1249+
<LocalEnvironmentMcpUrlRow entry={accessEnvironment.entry} />
1250+
) : null}
12471251
{accessEnvironment ? (
12481252
<details className="group px-3 sm:px-4">
12491253
<summary className="flex min-h-11 cursor-pointer list-none items-center gap-2 rounded-md text-sm font-medium outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring [&::-webkit-details-marker]:hidden">

‎apps/web/src/components/settings/SavedEnvironmentRows.tsx‎

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
hasSavedBearerRoute,
2121
connectionStatusText,
2222
environmentMcpUrl,
23+
localEnvironmentMcpUrl,
24+
type ConnectionCatalogEntry,
2325
} from "@t3tools/client-runtime/connection";
2426
import * as Option from "effect/Option";
2527

@@ -50,6 +52,7 @@ import {
5052
import { hasCloudPublicConfig } from "~/cloud/publicConfig";
5153
import { isElectron } from "~/env";
5254
import { useCloudLinkController } from "~/cloud/useCloudLinkController";
55+
import { usePrimaryCloudLinkState } from "~/cloud/cloudLinkState";
5356
import { useManagedTunnelUsage } from "~/cloud/managedRelayState";
5457
import { useT3ConnectAccountPage } from "../clerk/T3ConnectAccountPages";
5558
import { TunnelLimitAlert, TunnelUsageMeter } from "../cloud/TunnelUsage";
@@ -107,6 +110,28 @@ function savedBackendStatus(environment: EnvironmentPresentation): {
107110
}
108111
}
109112

113+
function useCopyMcpUrl() {
114+
return useCopyToClipboard<{ url: string }>({
115+
target: "MCP URL",
116+
onCopy: ({ url }) => {
117+
toastManager.add({
118+
type: "success",
119+
title: "MCP URL copied",
120+
description: `Add it to an agent, e.g. claude mcp add --transport http t3 ${url}`,
121+
});
122+
},
123+
onError: (error) => {
124+
toastManager.add(
125+
stackedThreadToast({
126+
type: "error",
127+
title: "Could not copy MCP URL",
128+
description: error.message,
129+
}),
130+
);
131+
},
132+
});
133+
}
134+
110135
export function SavedBackendListRow({
111136
environment,
112137
removingEnvironmentId,
@@ -146,25 +171,7 @@ export function SavedBackendListRow({
146171
const copyTraceId = (traceId: string) => {
147172
copyTraceIdToClipboard(traceId, { traceId });
148173
};
149-
const { copyToClipboard: copyMcpUrl } = useCopyToClipboard<{ url: string }>({
150-
target: "MCP URL",
151-
onCopy: ({ url }) => {
152-
toastManager.add({
153-
type: "success",
154-
title: "MCP URL copied",
155-
description: `Add it to an agent, e.g. claude mcp add --transport http t3 ${url}`,
156-
});
157-
},
158-
onError: (error) => {
159-
toastManager.add(
160-
stackedThreadToast({
161-
type: "error",
162-
title: "Could not copy MCP URL",
163-
description: error.message,
164-
}),
165-
);
166-
},
167-
});
174+
const { copyToClipboard: copyMcpUrl } = useCopyMcpUrl();
168175
const versionMismatch = resolveServerConfigVersionMismatch(environment.serverConfig);
169176
const serverUpdateState = useAtomValue(serverEnvironment.updateStateAtom(environmentId));
170177
const resumingServerUpdate =
@@ -433,6 +440,33 @@ export function SavedBackendListRow({
433440
);
434441
}
435442

443+
/** Copying the host's own MCP address, for hosted agents or agents on this machine. */
444+
export function LocalEnvironmentMcpUrlRow({ entry }: { readonly entry: ConnectionCatalogEntry }) {
445+
const cloudLink = usePrimaryCloudLinkState();
446+
const { copyToClipboard: copyMcpUrl } = useCopyMcpUrl();
447+
const mcpUrl = localEnvironmentMcpUrl({
448+
entry,
449+
tunnelHttpBaseUrl: cloudLink.data?.managedTunnelHttpBaseUrl,
450+
});
451+
if (mcpUrl === null) return null;
452+
const hosted = cloudLink.data?.managedTunnelHttpBaseUrl != null;
453+
return (
454+
<SettingsRow
455+
{...searchableSetting("mcp-url")}
456+
description={
457+
hosted
458+
? `Give this URL to a hosted agent, such as ChatGPT: ${mcpUrl}`
459+
: `Give this URL to an agent outside T3 Code: ${mcpUrl}`
460+
}
461+
control={
462+
<Button size="sm" variant="outline" onClick={() => copyMcpUrl(mcpUrl, { url: mcpUrl })}>
463+
Copy
464+
</Button>
465+
}
466+
/>
467+
);
468+
}
469+
436470
function CloudLinkSwitch({
437471
checked,
438472
disabled,

‎apps/web/src/components/settings/settingsSearch.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,14 @@ export const SETTINGS_SEARCH_ITEMS = [
12211221
desktopOnly: true,
12221222
cloudOnly: true,
12231223
},
1224+
{
1225+
id: "mcp-url",
1226+
localEnvironmentOnly: true,
1227+
title: "MCP URL",
1228+
to: "/settings/access",
1229+
targetId: "connections-environment",
1230+
searchTerms: ["outside agents chatgpt claude codex hosted bot oauth server address copy"],
1231+
},
12241232
{
12251233
id: "hold-webhooks-while-offline",
12261234
localEnvironmentOnly: true,

‎docs/user/outside-agents.md‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,24 @@ in once, and you choose what it may do.
88

99
## Get the MCP URL
1010

11-
In **Settings → Environments**, open a saved environment's menu and choose
12-
**Copy MCP URL**. The URL is the environment's address followed by `/mcp`, for
13-
example:
11+
For the environment on this machine, open **Settings → Sharing & access** and
12+
find **MCP URL** on its card. With T3 Connect on and its tunnel connected, this
13+
is the environment's public address followed by `/mcp`:
1414

1515
```text
1616
https://<environment-address>/mcp
1717
```
1818

19-
The copied URL uses the route this device is connected over, so an agent on the
20-
same device can reach it.
19+
That is the URL a hosted agent, such as ChatGPT, needs. With T3 Connect off, the
20+
same setting shows a loopback address, which only an agent on the machine
21+
running T3 Code can reach.
22+
23+
For a saved remote environment, open **Settings → Environments**, open the
24+
environment's menu, and choose **Copy MCP URL**.
25+
26+
The copied URL uses the environment's T3 Connect address when it has one, and
27+
otherwise the route this device is connected over, so an agent on the same
28+
device can reach it.
2129

2230
- **An agent on your own computers** can use `localhost` on the host, T3
2331
Connect, or any `https` address, such as

‎docs/user/remote-access.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,10 @@ environment of an already running T3 server.
316316
## Connect an outside agent
317317

318318
Claude Code, Codex, ChatGPT and other agents T3 Code did not start can drive
319-
threads on an environment through its MCP server. See
320-
[outside agents](./outside-agents.md) for setup.
319+
threads on an environment through its MCP server. On the host, **Settings →
320+
Sharing & access** shows the MCP URL to give the agent, including its T3 Connect
321+
address when the tunnel is on. See [outside agents](./outside-agents.md) for
322+
setup.
321323

322324
## Link another environment
323325

‎packages/client-runtime/src/connection/presentation.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ import {
1010
import {
1111
BearerConnectionTarget,
1212
ConnectionTransientError,
13+
PrimaryConnectionTarget,
1314
RelayConnectionTarget,
1415
type SupervisorConnectionState,
1516
} from "./model.ts";
16-
import { environmentMcpUrl, presentConnectionState } from "./presentation.ts";
17+
import {
18+
environmentMcpUrl,
19+
localEnvironmentMcpUrl,
20+
presentConnectionState,
21+
} from "./presentation.ts";
1722

1823
const TARGET = new BearerConnectionTarget({
1924
environmentId: EnvironmentId.make("environment-1"),
@@ -107,6 +112,45 @@ describe("connection presentation", () => {
107112
).toBe("https://tunnel.example.test/mcp");
108113
});
109114

115+
it("offers the local environment's live tunnel address, then loopback, and never plain http", () => {
116+
const primary = new PrimaryConnectionTarget({
117+
environmentId: TARGET.environmentId,
118+
label: "This machine",
119+
httpBaseUrl: "http://127.0.0.1:3773",
120+
wsBaseUrl: "ws://127.0.0.1:3773",
121+
});
122+
const entry: ConnectionCatalogEntry = {
123+
...ENTRY,
124+
target: primary,
125+
profile: Option.none(),
126+
};
127+
128+
expect(
129+
localEnvironmentMcpUrl({
130+
entry,
131+
tunnelHttpBaseUrl: "https://prod-abc.t3coderelay.com",
132+
}),
133+
).toBe("https://prod-abc.t3coderelay.com/mcp");
134+
expect(localEnvironmentMcpUrl({ entry })).toBe("http://127.0.0.1:3773/mcp");
135+
136+
const lan: ConnectionCatalogEntry = {
137+
...entry,
138+
target: new PrimaryConnectionTarget({
139+
environmentId: TARGET.environmentId,
140+
label: "This machine",
141+
httpBaseUrl: "http://192.168.4.53:3773",
142+
wsBaseUrl: "ws://192.168.4.53:3773",
143+
}),
144+
};
145+
expect(localEnvironmentMcpUrl({ entry: lan })).toBeNull();
146+
expect(
147+
localEnvironmentMcpUrl({
148+
entry: lan,
149+
tunnelHttpBaseUrl: "https://prod-abc.t3coderelay.com",
150+
}),
151+
).toBe("https://prod-abc.t3coderelay.com/mcp");
152+
});
153+
110154
it("distinguishes initial connection, reconnect, and retry errors", () => {
111155
expect(presentConnectionState(supervisorState({ phase: "connecting", attempt: 1 }))).toEqual({
112156
phase: "connecting",

‎packages/client-runtime/src/connection/presentation.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ServerConfig } from "@t3tools/contracts";
2+
import { isLocalLoopbackHost } from "@t3tools/shared/hostClassification";
23
import * as Option from "effect/Option";
34

45
import type { ConnectionCatalogEntry } from "./catalog.ts";
@@ -119,6 +120,25 @@ export function environmentMcpUrl(input: {
119120
return null;
120121
}
121122

123+
/**
124+
* The MCP address for the environment this app runs. Agents only sign in over https or
125+
* loopback, so a plain-http LAN address is not offered.
126+
*/
127+
export function localEnvironmentMcpUrl(input: {
128+
readonly entry: ConnectionCatalogEntry;
129+
readonly tunnelHttpBaseUrl?: string | null | undefined;
130+
}): string | null {
131+
const tunnelUrl = input.tunnelHttpBaseUrl ? mcpUrlFromBase(input.tunnelHttpBaseUrl) : null;
132+
if (tunnelUrl !== null && isAgentSignInMcpUrl(tunnelUrl)) return tunnelUrl;
133+
const mcpUrl = environmentMcpUrl({ entry: input.entry });
134+
return mcpUrl !== null && isAgentSignInMcpUrl(mcpUrl) ? mcpUrl : null;
135+
}
136+
137+
function isAgentSignInMcpUrl(mcpUrl: string): boolean {
138+
const url = new URL(mcpUrl);
139+
return url.protocol === "https:" || isLocalLoopbackHost(url.hostname);
140+
}
141+
122142
function mcpUrlFromBase(httpBaseUrl: string): string | null {
123143
let url: URL;
124144
try {

‎packages/contracts/src/environmentHttp.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ export const EnvironmentCloudLinkStateResult = Schema.Struct({
511511
relayUrl: Schema.NullOr(Schema.String),
512512
relayIssuer: Schema.NullOr(Schema.String),
513513
managedTunnelActive: Schema.optional(Schema.Boolean),
514+
/** The live public address of the host's managed tunnel, when its connector reports one. */
515+
managedTunnelHttpBaseUrl: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
514516
publishAgentActivity: Schema.Boolean,
515517
holdWebhooksWhileOffline: Schema.optional(Schema.Boolean),
516518
linkRevoked: Schema.optional(Schema.Boolean),

0 commit comments

Comments
 (0)