Skip to content

Commit b73f8fe

Browse files
committed
fix(server): only a calling thread's modes carry over a link
eee5e3b started any linked launch that omitted runtimeMode at the forwarded T3-Mode-Limit. For a calling thread that limit is its own running mode, which is what should carry over. For an MCP client outside a thread it is only an approval ceiling, so a full-access client launching through a full-access link got a full-access thread there. The same client launching locally gets the project default. The forwarder now marks the limit as inheritable with T3-Mode-Inherit: 1, and only when the caller is a thread. The receiving /mcp records that beside narrowedTo. t3_thread_launch skips the project default only for a caller that sent it. A client outside a thread starts at the far side's project default, within its ceiling and the link, as it does here. Mixed versions keep today's default: an old receiver ignores the header, and an old sender never sends it. docs/internals/remote.md states the rule. Fails before: the new client case in PeerForwarding "launches in a linked environment with the caller's modes" received full-access, expected approval-required. After: PeerForwarding, RemoteDelegation, project handlers, linkOrigin and McpHttpServer tests 66/66; tsc, fmt and lint clean on the changed files. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01A1D6J2UMPo7YBWQFvbg1br
1 parent eee5e3b commit b73f8fe

7 files changed

Lines changed: 57 additions & 15 deletions

File tree

‎apps/server/src/mcp/McpHttpServer.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,16 @@ const withModeLimit = (
180180
const limit = parseModeLimit(header);
181181
return Option.isNone(limit)
182182
? undefined
183-
: { ...invocation, client: { ...invocation.client, narrowedTo: limit.value } };
183+
: {
184+
...invocation,
185+
client: {
186+
...invocation.client,
187+
narrowedTo: limit.value,
188+
...(request.headers[McpInvocationContext.MODE_INHERIT_HEADER] === "1"
189+
? { inheritsModes: true }
190+
: {}),
191+
},
192+
};
184193
};
185194

186195
// The Linked environments beta is off here: links wait until it is on again.

‎apps/server/src/mcp/McpInvocationContext.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export interface McpClientCaller {
4545
* another environment passing on the limits of the agent it calls for.
4646
*/
4747
readonly narrowedTo?: ClientModes | undefined;
48+
/** `narrowedTo` is the calling agent's own running modes (`T3-Mode-Inherit`), not only a ceiling. */
49+
readonly inheritsModes?: boolean | undefined;
4850
/** The origin an outside client reached; its thread links open the web app there. */
4951
readonly webOrigin?: string | undefined;
5052
}
@@ -57,6 +59,13 @@ export interface McpClientCaller {
5759
*/
5860
export const MODE_LIMIT_HEADER = "t3-mode-limit";
5961

62+
/**
63+
* `T3-Mode-Inherit: 1` beside `T3-Mode-Limit` marks that limit as the calling agent's own
64+
* modes, so a launch naming no mode starts there instead of at the project default. An
65+
* environment sends it only for a calling thread: a client outside one has a ceiling, not a mode.
66+
*/
67+
export const MODE_INHERIT_HEADER = "t3-mode-inherit";
68+
6069
export interface ClientModes {
6170
readonly runtimeMode: RuntimeMode;
6271
readonly interactionMode: ProviderInteractionMode;

‎apps/server/src/mcp/toolkits/project/handlers.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
162162
);
163163
// A caller outside a thread has nothing to inherit, so it starts with the
164164
// project's effective defaults, as the composer does. Its mode stays
165-
// within the ceiling it was approved with. A linked session carries the
166-
// remote caller's modes, capped by the link, so it starts there instead.
165+
// within the ceiling it was approved with. A linked thread's own modes
166+
// arrive as T3-Mode-Inherit, capped by the link, so it starts there instead.
167167
const projectDefaults =
168168
caller === undefined && !replaying
169169
? yield* ServerSettings.ServerSettingsService.pipe(
@@ -192,7 +192,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
192192
const startedRuntimeMode =
193193
projectDefaults !== undefined &&
194194
input.runtimeMode === undefined &&
195-
linkOrigin === undefined
195+
scope.client?.inheritsModes !== true
196196
? yield* resolveRuntimeMode(runtimeMode, projectDefaults.defaultRuntimeMode).pipe(
197197
Effect.orElseSucceed(() => runtimeMode),
198198
)

‎apps/server/src/peer/PeerForwarding.test.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,23 @@ const makeLaptop = Effect.gen(function* () {
317317
);
318318
const here = yield* Layer.build(layerHere);
319319
const server = Context.get(here, McpServer.McpServer);
320+
// A `client:` caller is an OAuth client outside any thread, approved for full access.
320321
const scopeOf = (
321322
caller: string,
322323
capabilities: ReadonlySet<McpInvocationContext.McpCapability> = new Set(["orchestration"]),
323324
): McpInvocationContext.McpInvocationScope => ({
324325
environmentId: laptop.environmentId,
325326
requestNamespace: `provider-session:${caller}`,
326-
thread: {
327-
threadId: ThreadId.make(caller),
328-
providerSessionId: `provider-session:${caller}`,
329-
providerInstanceId: ProviderInstanceId.make("codex"),
330-
},
331-
client: undefined,
327+
thread: caller.startsWith("client:")
328+
? undefined
329+
: {
330+
threadId: ThreadId.make(caller),
331+
providerSessionId: `provider-session:${caller}`,
332+
providerInstanceId: ProviderInstanceId.make("codex"),
333+
},
334+
client: caller.startsWith("client:")
335+
? { sessionId: caller, label: "Outside client", access: "full-access" }
336+
: undefined,
332337
capabilities,
333338
issuedAt: 1,
334339
});
@@ -526,6 +531,14 @@ it.effect("launches in a linked environment with the caller's modes, retry-safe"
526531
expect(failureOf(withUpload)).toMatchObject({ code: "invalid_request" });
527532
expect(yield* Ref.get(b.bearers)).toEqual([]);
528533
expect((yield* Ref.get(seen)).launches).toHaveLength(2);
534+
535+
// A client outside a thread has a ceiling, not modes to inherit, so its
536+
// launch there starts at that project's default, as it would here.
537+
const fromClient = yield* launch("client:outside", { clientRequestId: "launch-3" });
538+
expect(fromClient.isError).toBe(false);
539+
expect((yield* Ref.get(seen)).launches[2]).toMatchObject({
540+
runtimeMode: "approval-required",
541+
});
529542
}),
530543
).pipe(Effect.provide(NodeHttpServer.layerTest)),
531544
);

‎apps/server/src/peer/PeerForwarding.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,17 @@ const make = Effect.gen(function* () {
9898
const threads = yield* ThreadManagement.ThreadManagementService;
9999

100100
/**
101-
* The caller's own limits, which travel with every forwarded call: the peer sees only the
102-
* link's credential, so a caller that may not control threads here is refused here. Work a
103-
* link started never uses this environment's links, since the peer would see this
104-
* environment's session instead of that link, and its fence could not hold.
101+
* The caller's limits travel with every forwarded call, since the peer sees only the link's
102+
* credential; a thread's are its own modes, which a launch there inherits. Work a link
103+
* started never uses this environment's links: the peer would see this environment's
104+
* session instead of that link, and its fence could not hold.
105105
*/
106106
const callerLimits = (scope: Scope) =>
107107
readCaller().pipe(
108108
Effect.tap((caller) => assertNotLinked(caller, "use this environment's links")),
109-
Effect.map((caller) => caller.limits),
109+
Effect.map((caller) =>
110+
caller.caller === undefined ? caller.limits : { ...caller.limits, inherit: true },
111+
),
110112
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
111113
Effect.provideService(ThreadManagement.ThreadManagementService, threads),
112114
);

‎apps/server/src/peer/PeerMcpClient.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const PROTOCOL_VERSION = "2025-06-18";
2424
export interface PeerCallLimits {
2525
readonly runtimeMode: RuntimeMode;
2626
readonly interactionMode: ProviderInteractionMode;
27+
/** These are a calling thread's own modes, which a launch there inherits (`T3-Mode-Inherit`). */
28+
readonly inherit?: boolean | undefined;
2729
}
2830

2931
/**
@@ -159,6 +161,9 @@ const make = Effect.gen(function* () {
159161
.pipe(Effect.mapError(fromLinkError));
160162
const limitHeader = {
161163
[McpInvocationContext.MODE_LIMIT_HEADER]: `${input.limits.runtimeMode}/${input.limits.interactionMode}`,
164+
...(input.limits.inherit === true
165+
? { [McpInvocationContext.MODE_INHERIT_HEADER]: "1" }
166+
: {}),
162167
};
163168
const sessionKey = `${input.environmentId} ${resolved.url}`;
164169
const callOnce = (sessionId: string) =>

‎docs/internals/remote.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ Unlinking on the linking side only forgets the token.
9494
`T3-Mode-Limit`, which the receiving `/mcp` only ever narrows the client's
9595
approved access with. A plan-mode agent therefore cannot start full-access
9696
work through a full-access link.
97+
- **A launch that names no mode inherits a calling thread's modes**, capped by
98+
the link: the forwarder adds `T3-Mode-Inherit: 1` only for a thread. A client
99+
outside a thread sends a ceiling, not modes, so the far side starts it at the
100+
project default, as here. Environments without the header keep that default.
97101
- **The token goes only to an address that proves it is the linked
98102
environment**: its descriptor must report the linked environment id. A LAN
99103
address that now belongs to another machine never receives it.

0 commit comments

Comments
 (0)