Skip to content

Commit 6a0375b

Browse files
feat(server): link to another environment
An environment can now sign in to another one's /mcp as an outside agent, with a pairing code from that environment, without a browser. It drives the peer's existing MCP OAuth flow server-side, keeps the token in the server secret store and the rest in peer_environment_links (migration 059). The peer lists the link as "T3 Code · <label>" in Connections, where it can be revoked. The token is only ever sent to an address whose descriptor answers as the linked environment. PeerMcpClient calls the peer's tools and carries the caller's limits in T3-Mode-Limit on every request. Linking refuses this environment itself and peers that cannot honour that header. `t3 environment link|list|unlink` and the peerLinks.* RPCs manage links. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d2e6ce8 commit 6a0375b

18 files changed

Lines changed: 1414 additions & 1 deletion

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
authScopeRequiredResponse,
66
AssetCreateUrlInput,
77
AuthAccessReadScope,
8+
AuthAccessWriteScope,
89
ServerSettingsPatch,
910
ProviderInstanceMutation,
1011
requiredScopesForServerSettingsPatch,
@@ -80,6 +81,9 @@ export const RPC_REQUIRED_SCOPES = {
8081
[WS_METHODS.serverUpsertKeybinding]: AuthSettingsWriteScope,
8182
[WS_METHODS.serverRemoveKeybinding]: AuthSettingsWriteScope,
8283
[WS_METHODS.serverGetSettings]: AuthOrchestrationReadScope,
84+
[WS_METHODS.peerLinksList]: AuthAccessReadScope,
85+
[WS_METHODS.peerLinksLink]: AuthAccessWriteScope,
86+
[WS_METHODS.peerLinksUnlink]: AuthAccessWriteScope,
8387
[WS_METHODS.serverUpdateSettings]: AuthSettingsWriteScope,
8488
[WS_METHODS.serverSearchAcpRegistry]: AuthOrchestrationReadScope,
8589
[WS_METHODS.serverPrepareAcpRegistryAgent]: AuthProvidersManageScope,

‎apps/server/src/binCli.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as NetService from "@t3tools/shared/Net";
99
import packageJson from "../package.json" with { type: "json" };
1010
import { acpMcpBridgeCommand, acpMcpCallCommand } from "./cli/acpMcpBridge.ts";
1111
import { authCommand } from "./cli/auth.ts";
12+
import { environmentCommand } from "./cli/environment.ts";
1213
import { appCommand } from "./cli/app.ts";
1314
import { browserCommand } from "./cli/browser.ts";
1415
import { connectCommand } from "./cli/connect.ts";
@@ -72,6 +73,7 @@ export const makeCli = ({ cloudEnabled = hasCloudPublicConfig } = {}) =>
7273
appCommand,
7374
pairCommand,
7475
authCommand,
76+
environmentCommand,
7577
browserCommand,
7678
projectCommand,
7779
serviceCommand,

‎apps/server/src/cli/environment.ts‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import { AuthMcpClientAccess, EnvironmentId, type PeerLinkSummary } from "@t3tools/contracts";
2+
import * as Console from "effect/Console";
3+
import * as DateTime from "effect/DateTime";
4+
import * as Effect from "effect/Effect";
5+
import * as Layer from "effect/Layer";
6+
import * as References from "effect/References";
7+
import * as Schema from "effect/Schema";
8+
import { Argument, Command, Flag, GlobalFlag } from "effect/cli";
9+
import { FetchHttpClient } from "effect/http";
10+
11+
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
12+
import * as ServerConfig from "../config.ts";
13+
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
14+
import * as PeerLinks from "../peer/PeerLinks.ts";
15+
import * as SqlitePersistence from "../persistence/Sqlite.ts";
16+
import { authLocationFlags, type CliAuthLocationFlags, resolveCliAuthConfig } from "./config.ts";
17+
18+
/**
19+
* Links live in this environment's database and secret store, so these
20+
* commands work whether or not its server is running.
21+
*/
22+
const runWithPeerLinks = <A, E>(
23+
flags: CliAuthLocationFlags,
24+
run: (links: PeerLinks.PeerLinks["Service"]) => Effect.Effect<A, E>,
25+
options?: { readonly quietLogs?: boolean },
26+
) =>
27+
Effect.gen(function* () {
28+
const logLevel = yield* GlobalFlag.LogLevel;
29+
const config = yield* resolveCliAuthConfig(flags, logLevel);
30+
return yield* PeerLinks.PeerLinks.pipe(
31+
Effect.flatMap(run),
32+
Effect.provide(
33+
PeerLinks.layer.pipe(
34+
Layer.provide(
35+
Layer.mergeAll(
36+
ServerSecretStore.layer,
37+
SqlitePersistence.layerConfig,
38+
FetchHttpClient.layer,
39+
),
40+
),
41+
Layer.provide(ServerEnvironment.layer.pipe(Layer.provide(ServerSecretStore.layer))),
42+
Layer.provide(ServerConfig.layer(config)),
43+
Layer.provide(
44+
Layer.succeed(
45+
References.MinimumLogLevel,
46+
options?.quietLogs ? "Error" : config.logLevel,
47+
),
48+
),
49+
),
50+
),
51+
);
52+
});
53+
54+
const jsonFlag = Flag.Boolean("json").pipe(
55+
Flag.withDescription("Emit JSON instead of human-readable output."),
56+
Flag.withDefault(false),
57+
);
58+
59+
const encodeLinks = Schema.encodeEffect(
60+
Schema.fromJsonString(Schema.Array(Schema.toEncoded(Schema.Unknown))),
61+
);
62+
63+
const formatLinks = (links: ReadonlyArray<PeerLinkSummary>) =>
64+
links.length === 0
65+
? "No linked environments.\n"
66+
: `${links
67+
.map(
68+
(link) =>
69+
`${link.label} (${link.environmentId})\n ${link.status}, access ${link.access}, expires ${DateTime.formatIso(link.expiresAt)}\n ${link.urls.join(", ")}${link.lastError === null ? "" : `\n last error: ${link.lastError}`}`,
70+
)
71+
.join("\n\n")}\n`;
72+
73+
const environmentLinkCommand = Command.make("link", {
74+
...authLocationFlags,
75+
url: Argument.String("url").pipe(
76+
Argument.withDescription(
77+
"Where the other environment answers: its T3 Connect, Tailscale, LAN or loopback address.",
78+
),
79+
),
80+
alternateUrl: Flag.String("url").pipe(
81+
Flag.withDescription("Another address for the same environment, tried after the first."),
82+
Flag.atLeast(0),
83+
),
84+
pairingCode: Flag.String("pairing-code").pipe(
85+
Flag.withDescription("A pairing code from the other environment (`t3 pair` there)."),
86+
),
87+
access: Flag.Literals("access", AuthMcpClientAccess.literals).pipe(
88+
Flag.withDescription(
89+
"What this environment's agents may do there: read-only, or the broadest runtime mode.",
90+
),
91+
Flag.withDefault("approval-required" as const),
92+
),
93+
}).pipe(
94+
Command.withDescription(
95+
"Link another T3 Code environment, so agents here can work there. It shows up there under Settings → Connections, where it can be revoked.",
96+
),
97+
Command.withHandler((flags) =>
98+
runWithPeerLinks(flags, (links) =>
99+
Effect.gen(function* () {
100+
const linked = yield* links.link({
101+
url: flags.url,
102+
alternateUrls: flags.alternateUrl,
103+
pairingCode: flags.pairingCode,
104+
access: flags.access,
105+
});
106+
yield* Console.log(
107+
`Linked ${linked.label} (${linked.environmentId}) with ${linked.access} access, until ${DateTime.formatIso(linked.expiresAt)}.\n`,
108+
);
109+
}),
110+
),
111+
),
112+
);
113+
114+
const environmentListCommand = Command.make("list", { ...authLocationFlags, json: jsonFlag }).pipe(
115+
Command.withDescription("List linked environments and whether each answers now."),
116+
Command.withHandler((flags) =>
117+
runWithPeerLinks(
118+
flags,
119+
(links) =>
120+
Effect.gen(function* () {
121+
const listed = yield* links.list;
122+
yield* Console.log(flags.json ? `${yield* encodeLinks(listed)}\n` : formatLinks(listed));
123+
}),
124+
{ quietLogs: flags.json },
125+
),
126+
),
127+
);
128+
129+
const environmentUnlinkCommand = Command.make("unlink", {
130+
...authLocationFlags,
131+
environmentId: Argument.String("environment-id").pipe(
132+
Argument.withDescription("The linked environment's id, from `t3 environment list`."),
133+
),
134+
}).pipe(
135+
Command.withDescription(
136+
"Forget a linked environment here. Revoke it in that environment's Connections to end its session there too.",
137+
),
138+
Command.withHandler((flags) =>
139+
runWithPeerLinks(flags, (links) =>
140+
Effect.gen(function* () {
141+
const removed = yield* links.unlink(EnvironmentId.make(flags.environmentId));
142+
yield* Console.log(
143+
removed
144+
? `Forgot ${flags.environmentId}. Revoke it in that environment's Connections to end its session there.\n`
145+
: `No linked environment ${flags.environmentId}.\n`,
146+
);
147+
}),
148+
),
149+
),
150+
);
151+
152+
export const environmentCommand = Command.make("environment").pipe(
153+
Command.withDescription("Link other T3 Code environments, so agents here can work there."),
154+
Command.withSubcommands([
155+
environmentLinkCommand,
156+
environmentListCommand,
157+
environmentUnlinkCommand,
158+
]),
159+
);

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ export const make = Effect.gen(function* () {
259259
: {}),
260260
...(desktopAppUpdate ? { desktopAppUpdate: true } : {}),
261261
serverBrowser: true,
262+
peerLinks: true,
263+
mcpModeLimitHeader: true,
262264
},
263265
};
264266

‎apps/server/src/observability/RpcInstrumentation.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const RPC_AGGREGATES = {
5353
[WS_METHODS.serverUpsertKeybinding]: "server",
5454
[WS_METHODS.serverRemoveKeybinding]: "server",
5555
[WS_METHODS.serverGetSettings]: "server",
56+
[WS_METHODS.peerLinksList]: "peerLinks",
57+
[WS_METHODS.peerLinksLink]: "peerLinks",
58+
[WS_METHODS.peerLinksUnlink]: "peerLinks",
5659
[WS_METHODS.serverUpdateSettings]: "server",
5760
[WS_METHODS.serverSearchAcpRegistry]: "server",
5861
[WS_METHODS.serverPrepareAcpRegistryAgent]: "server",

0 commit comments

Comments
 (0)