Skip to content

Commit 227ada0

Browse files
t3dotggclaude
andcommitted
fix(server): outside MCP clients get clickable thread links
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9826dbb commit 227ada0

7 files changed

Lines changed: 51 additions & 18 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ export const layerMcpClientAuthenticator = Layer.effect(
573573
label: client.label,
574574
access: client.access,
575575
linked: client.peerLink,
576+
...(client.peerLink ? {} : { webOrigin: requestUrls(request).issuer }),
576577
},
577578
capabilities: new Set<McpInvocationContext.McpCapability>([
578579
"orchestration",

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import * as Context from "effect/Context";
1313
import * as Effect from "effect/Effect";
1414
import * as Fiber from "effect/Fiber";
15+
import { formatThreadLink } from "@t3tools/shared/threadLinks";
1516

1617
const ALL_MCP_CAPABILITIES = [
1718
"preview",
@@ -44,6 +45,8 @@ export interface McpClientCaller {
4445
* another environment passing on the limits of the agent it calls for.
4546
*/
4647
readonly narrowedTo?: ClientModes | undefined;
48+
/** The origin an outside client reached; its thread links open the web app there. */
49+
readonly webOrigin?: string | undefined;
4750
}
4851

4952
/**
@@ -105,6 +108,20 @@ export interface McpInvocationScope {
105108
readonly linkedEnvironments?: boolean;
106109
}
107110

111+
/**
112+
* T3 Code renders `t3-thread://` links as thread chips. Other MCP clients cannot open them, so
113+
* they get a web URL on the origin they reached instead.
114+
*/
115+
export const threadLinkFor = (
116+
scope: Pick<McpInvocationScope, "environmentId" | "client">,
117+
threadId: ThreadId,
118+
): string => {
119+
const origin = scope.client?.webOrigin;
120+
return origin === undefined
121+
? formatThreadLink({ environmentId: scope.environmentId, threadId })
122+
: `${origin}/${encodeURIComponent(scope.environmentId)}/${encodeURIComponent(threadId)}`;
123+
};
124+
108125
export class McpInvocationContext extends Context.Service<
109126
McpInvocationContext,
110127
McpInvocationScope

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
CommandId,
3-
type EnvironmentId,
43
type RunId,
54
isProviderAvailable,
65
MessageId,
@@ -108,6 +107,7 @@ import {
108107
type McpInvocationScope,
109108
type McpThreadInvocationScope,
110109
requireThreadScope,
110+
threadLinkFor,
111111
} from "./McpInvocationContext.ts";
112112
import * as Metrics from "../observability/Metrics.ts";
113113
import {
@@ -752,15 +752,15 @@ function threadPullRequests(
752752
}
753753

754754
function listItemFromShell(
755-
environmentId: EnvironmentId,
755+
scope: McpInvocationScope,
756756
shell: OrchestrationV2ThreadShell,
757757
nowMs: number,
758758
): OrchestratorMcpThreadListItem {
759759
return {
760760
threadId: shell.id,
761761
projectId: shell.projectId,
762762
title: shell.title,
763-
link: formatThreadLink({ environmentId, threadId: shell.id }),
763+
link: threadLinkFor(scope, shell.id),
764764
createdBy: shell.createdBy,
765765
creationSource: shell.creationSource,
766766
status: shell.activityRunStatus ?? shell.status,
@@ -782,7 +782,7 @@ function listItemFromShell(
782782
}
783783

784784
function threadDetail(
785-
environmentId: EnvironmentId,
785+
scope: McpInvocationScope,
786786
projection: Pick<OrchestrationV2ThreadProjection, "thread" | "runs" | "runtimeRequests">,
787787
itemCount: number,
788788
shell: OrchestrationV2ThreadShell,
@@ -794,7 +794,7 @@ function threadDetail(
794794
threadId: projection.thread.id,
795795
projectId: projection.thread.projectId,
796796
title: projection.thread.title,
797-
link: formatThreadLink({ environmentId, threadId: projection.thread.id }),
797+
link: threadLinkFor(scope, projection.thread.id),
798798
createdBy: projection.thread.createdBy,
799799
creationSource: projection.thread.creationSource,
800800
status: active?.status ?? latest?.status ?? "idle",
@@ -2806,7 +2806,7 @@ const make = Effect.gen(function* () {
28062806
return {
28072807
projectId,
28082808
currentThreadId: parent?.thread.id ?? null,
2809-
threads: page.map((shell) => listItemFromShell(scope.environmentId, shell, nowMs)),
2809+
threads: page.map((shell) => listItemFromShell(scope, shell, nowMs)),
28102810
nextCursor,
28112811
total: filtered.length,
28122812
} satisfies OrchestratorMcpThreadListResult;
@@ -2887,7 +2887,7 @@ const make = Effect.gen(function* () {
28872887
}
28882888
return {
28892889
thread: threadDetail(
2890-
scope.environmentId,
2890+
scope,
28912891
target,
28922892
timeline.totalItems,
28932893
shell,
@@ -2951,7 +2951,7 @@ const make = Effect.gen(function* () {
29512951
);
29522952
return {
29532953
threadId: input.threadId,
2954-
link: formatThreadLink({ environmentId: scope.environmentId, threadId: input.threadId }),
2954+
link: threadLinkFor(scope, input.threadId),
29552955
messageId,
29562956
runId: result.run.id,
29572957
...(result.run.delegatedTaskId === undefined ? {} : { taskId: result.run.delegatedTaskId }),

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const clientLaunchHarness = (input: {
4242
readonly launched: Array<ThreadLaunch.ThreadLaunchInput>;
4343
readonly workspaceRoot?: string;
4444
readonly checkWorktreeBase?: ThreadLaunch.ThreadLaunchService["Service"]["checkWorktreeBase"];
45+
readonly webOrigin?: string;
4546
}) => {
4647
const projectId = ProjectId.make("project:client-target");
4748
const modelSelection = {
@@ -75,6 +76,7 @@ const clientLaunchHarness = (input: {
7576
sessionId: "session-1",
7677
label: "Claude Code",
7778
access: input.runtimeModeCeiling,
79+
webOrigin: input.webOrigin,
7880
},
7981
issuedAt: 0,
8082
capabilities: new Set(["orchestration" as const]),
@@ -155,15 +157,17 @@ it.effect(
155157
readonly runtimeMode?: RuntimeMode;
156158
readonly targeted?: false;
157159
readonly overrideModel?: ModelSelection;
160+
readonly webOrigin?: string;
158161
},
159162
) {
160-
const { targeted, overrideModel, ...input } = params;
163+
const { targeted, overrideModel, webOrigin, ...input } = params;
161164
const launched: Array<ThreadLaunch.ThreadLaunchInput> = [];
162165
const { projectId, modelSelection, dependencies } = clientLaunchHarness({
163166
runtimeModeCeiling,
164167
defaultRuntimeMode,
165168
launched,
166169
...(overrideModel === undefined ? {} : { overrideModel }),
170+
...(webOrigin === undefined ? {} : { webOrigin }),
167171
});
168172
const toolkit = yield* ProjectToolkit.pipe(
169173
Effect.provide(
@@ -190,8 +194,14 @@ it.effect(
190194
expect(belowCeiling.launched[0]?.runtimeMode).toBe("approval-required");
191195
expect(belowCeiling.launched[0]?.initialMessage?.senderThreadId).toBeUndefined();
192196

193-
const aboveCeiling = yield* launchWith("auto-accept-edits", "full-access", { title: "Fix" });
197+
const aboveCeiling = yield* launchWith("auto-accept-edits", "full-access", {
198+
title: "Fix",
199+
webOrigin: "https://box.example.ts.net",
200+
});
194201
expect(aboveCeiling.launched[0]?.runtimeMode).toBe("auto-accept-edits");
202+
expect(aboveCeiling.result).toMatchObject({
203+
link: `https://box.example.ts.net/environment/${encodeURIComponent(aboveCeiling.launched[0]?.threadId ?? "")}`,
204+
});
195205

196206
const requested = yield* launchWith("full-access", "approval-required", {
197207
title: "Fix",

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import * as Repositories from "../../../sourceControl/SourceControlRepositorySer
1919
import * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts";
2020
import * as ServerSettings from "../../../serverSettings.ts";
2121
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
22-
import { formatThreadLink } from "@t3tools/shared/threadLinks";
2322
import { resolveRuntimeMode } from "../../delegatedTaskModes.ts";
2423
import * as McpInvocationContext from "../../McpInvocationContext.ts";
2524
import * as McpToolAccess from "../../McpToolAccess.ts";
@@ -240,7 +239,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
240239
const run = result.projection.runs.find((run) => run.userMessageId === messageId);
241240
return {
242241
threadId: thread.id,
243-
link: formatThreadLink({ environmentId: scope.environmentId, threadId: thread.id }),
242+
link: McpInvocationContext.threadLinkFor(scope, thread.id),
244243
projectId: thread.projectId,
245244
modelSelection: thread.modelSelection,
246245
runId: run?.id ?? null,

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
} from "@t3tools/contracts";
1010
import * as Effect from "effect/Effect";
1111
import { modelSelectionCommandType } from "@t3tools/shared/model";
12-
import { formatThreadLink } from "@t3tools/shared/threadLinks";
1312

1413
import * as McpInvocationContext from "../../McpInvocationContext.ts";
1514
import * as McpToolAccess from "../../McpToolAccess.ts";
@@ -104,11 +103,15 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
104103
const { task, threadId } = yield* scheduler
105104
.runNow({ id: input.taskId })
106105
.pipe(Effect.mapError(unavailable));
107-
const { environmentId } = yield* McpInvocationContext.McpInvocationContext;
106+
const scope = yield* McpInvocationContext.McpInvocationContext;
108107
return {
109108
taskId: task.id,
110109
threadId,
111-
...(threadId === null ? {} : { link: formatThreadLink({ environmentId, threadId }) }),
110+
...(threadId === null
111+
? {}
112+
: {
113+
link: McpInvocationContext.threadLinkFor(scope, threadId),
114+
}),
112115
lastRunStatus: task.lastRunStatus,
113116
runCount: task.runCount,
114117
nextRunAt: task.nextRunAt,
@@ -118,15 +121,15 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
118121
t3_thread_search: McpToolAccess.reads((input) =>
119122
Effect.gen(function* () {
120123
const { caller } = yield* readCaller();
121-
const { environmentId } = yield* McpInvocationContext.McpInvocationContext;
124+
const scope = yield* McpInvocationContext.McpInvocationContext;
122125
const threadSearch = yield* ThreadSearch.ThreadSearch;
123126
const { matches } = yield* threadSearch
124127
.searchThreads({ ...input, projectId: input.projectId ?? caller?.projectId })
125128
.pipe(Effect.mapError(unavailable));
126129
return {
127130
matches: matches.map((match) => ({
128131
...match,
129-
link: formatThreadLink({ environmentId, threadId: match.threadId }),
132+
link: McpInvocationContext.threadLinkFor(scope, match.threadId),
130133
})),
131134
};
132135
}),

‎docs/orchestration-v2/orchestrator-mcp-server.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,10 @@ delegated to a linked environment name that environment. Clients open only that
398398
environment's thread and show its current title; a link to a thread they do not
399399
know renders as a label that opens nothing. Older `t3-thread://v1/<threadId>`
400400
links name no environment: clients resolve them in the message's environment,
401-
then by a unique match in another enabled environment.
401+
then by a unique match in another enabled environment. An outside OAuth client,
402+
one that is not a linked environment, cannot render `t3-thread://` links, so it
403+
gets a web URL on the origin it reached, `<origin>/<environmentId>/<threadId>`,
404+
for threads in this environment.
402405

403406
List and read results report `snoozed` and `snoozedUntil`, and
404407
`t3_thread_list` filters on `snoozed`. The server's `isSnoozed` follows the

0 commit comments

Comments
 (0)