TypeScript
fix(server): outside MCP clients get thread links they can open (#26642)
Theo Browne26caea23c0fa
diff --git a/apps/server/src/auth/McpOAuth.ts b/apps/server/src/auth/McpOAuth.tsindex 8f664f5bc7c..10aa0e430d7 100644--- a/apps/server/src/auth/McpOAuth.ts+++ b/apps/server/src/auth/McpOAuth.ts@@ -573,6 +573,7 @@ export const layerMcpClientAuthenticator = Layer.effect(label: client.label,access: client.access,linked: client.peerLink,+ ...(client.peerLink ? {} : { webOrigin: requestUrls(request).issuer }),},capabilities: new Set<McpInvocationContext.McpCapability>(["orchestration",diff --git a/apps/server/src/mcp/McpInvocationContext.ts b/apps/server/src/mcp/McpInvocationContext.tsindex 26795c51696..5ce9669bc4d 100644--- a/apps/server/src/mcp/McpInvocationContext.ts+++ b/apps/server/src/mcp/McpInvocationContext.ts@@ -12,6 +12,7 @@ import {import * as Context from "effect/Context";import * as Effect from "effect/Effect";import * as Fiber from "effect/Fiber";+import { formatThreadLink } from "@t3tools/shared/threadLinks";const ALL_MCP_CAPABILITIES = ["preview",@@ -44,6 +45,8 @@ export interface McpClientCaller {* another environment passing on the limits of the agent it calls for.*/readonly narrowedTo?: ClientModes | undefined;+ /** The origin an outside client reached; its thread links open the web app there. */+ readonly webOrigin?: string | undefined;}/**@@ -105,6 +108,20 @@ export interface McpInvocationScope {readonly linkedEnvironments?: boolean;}+/**+ * T3 Code renders `t3-thread://` links as thread chips. Other MCP clients cannot open them, so+ * they get a web URL on the origin they reached instead.+ */+export const threadLinkFor = (+ scope: Pick<McpInvocationScope, "environmentId" | "client">,+ threadId: ThreadId,+): string => {+ const origin = scope.client?.webOrigin;+ return origin === undefined+ ? formatThreadLink({ environmentId: scope.environmentId, threadId })+ : `${origin}/${encodeURIComponent(scope.environmentId)}/${encodeURIComponent(threadId)}`;+};+export class McpInvocationContext extends Context.Service<McpInvocationContext,McpInvocationScopediff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.tsindex 8b32603a719..d8dacb35a06 100644--- a/apps/server/src/mcp/OrchestratorMcpService.ts+++ b/apps/server/src/mcp/OrchestratorMcpService.ts@@ -1,6 +1,5 @@import {CommandId,- type EnvironmentId,type RunId,isProviderAvailable,MessageId,@@ -108,6 +107,7 @@ import {type McpInvocationScope,type McpThreadInvocationScope,requireThreadScope,+ threadLinkFor,} from "./McpInvocationContext.ts";import * as Metrics from "../observability/Metrics.ts";import {@@ -752,7 +752,7 @@ function threadPullRequests(}function listItemFromShell(- environmentId: EnvironmentId,+ scope: McpInvocationScope,shell: OrchestrationV2ThreadShell,nowMs: number,): OrchestratorMcpThreadListItem {@@ -760,7 +760,7 @@ function listItemFromShell(threadId: shell.id,projectId: shell.projectId,title: shell.title,- link: formatThreadLink({ environmentId, threadId: shell.id }),+ link: threadLinkFor(scope, shell.id),createdBy: shell.createdBy,creationSource: shell.creationSource,status: shell.activityRunStatus ?? shell.status,@@ -782,7 +782,7 @@ function listItemFromShell(}function threadDetail(- environmentId: EnvironmentId,+ scope: McpInvocationScope,projection: Pick<OrchestrationV2ThreadProjection, "thread" | "runs" | "runtimeRequests">,itemCount: number,shell: OrchestrationV2ThreadShell,@@ -794,7 +794,7 @@ function threadDetail(threadId: projection.thread.id,projectId: projection.thread.projectId,title: projection.thread.title,- link: formatThreadLink({ environmentId, threadId: projection.thread.id }),+ link: threadLinkFor(scope, projection.thread.id),createdBy: projection.thread.createdBy,creationSource: projection.thread.creationSource,status: active?.status ?? latest?.status ?? "idle",@@ -2806,7 +2806,7 @@ const make = Effect.gen(function* () {return {projectId,currentThreadId: parent?.thread.id ?? null,- threads: page.map((shell) => listItemFromShell(scope.environmentId, shell, nowMs)),+ threads: page.map((shell) => listItemFromShell(scope, shell, nowMs)),nextCursor,total: filtered.length,} satisfies OrchestratorMcpThreadListResult;@@ -2887,7 +2887,7 @@ const make = Effect.gen(function* () {}return {thread: threadDetail(- scope.environmentId,+ scope,target,timeline.totalItems,shell,@@ -2951,7 +2951,7 @@ const make = Effect.gen(function* () {);return {threadId: input.threadId,- link: formatThreadLink({ environmentId: scope.environmentId, threadId: input.threadId }),+ link: threadLinkFor(scope, input.threadId),messageId,runId: result.run.id,...(result.run.delegatedTaskId === undefined ? {} : { taskId: result.run.delegatedTaskId }),diff --git a/apps/server/src/mcp/toolkits/project/handlers.test.ts b/apps/server/src/mcp/toolkits/project/handlers.test.tsindex 4031654a8c8..0d7ca3e1c9d 100644--- a/apps/server/src/mcp/toolkits/project/handlers.test.ts+++ b/apps/server/src/mcp/toolkits/project/handlers.test.ts@@ -42,6 +42,7 @@ const clientLaunchHarness = (input: {readonly launched: Array<ThreadLaunch.ThreadLaunchInput>;readonly workspaceRoot?: string;readonly checkWorktreeBase?: ThreadLaunch.ThreadLaunchService["Service"]["checkWorktreeBase"];+ readonly webOrigin?: string;}) => {const projectId = ProjectId.make("project:client-target");const modelSelection = {@@ -75,6 +76,7 @@ const clientLaunchHarness = (input: {sessionId: "session-1",label: "Claude Code",access: input.runtimeModeCeiling,+ webOrigin: input.webOrigin,},issuedAt: 0,capabilities: new Set(["orchestration" as const]),@@ -155,15 +157,17 @@ it.effect(readonly runtimeMode?: RuntimeMode;readonly targeted?: false;readonly overrideModel?: ModelSelection;+ readonly webOrigin?: string;},) {- const { targeted, overrideModel, ...input } = params;+ const { targeted, overrideModel, webOrigin, ...input } = params;const launched: Array<ThreadLaunch.ThreadLaunchInput> = [];const { projectId, modelSelection, dependencies } = clientLaunchHarness({runtimeModeCeiling,defaultRuntimeMode,launched,...(overrideModel === undefined ? {} : { overrideModel }),+ ...(webOrigin === undefined ? {} : { webOrigin }),});const toolkit = yield* ProjectToolkit.pipe(Effect.provide(@@ -190,8 +194,14 @@ it.effect(expect(belowCeiling.launched[0]?.runtimeMode).toBe("approval-required");expect(belowCeiling.launched[0]?.initialMessage?.senderThreadId).toBeUndefined();- const aboveCeiling = yield* launchWith("auto-accept-edits", "full-access", { title: "Fix" });+ const aboveCeiling = yield* launchWith("auto-accept-edits", "full-access", {+ title: "Fix",+ webOrigin: "https://box.example.ts.net",+ });expect(aboveCeiling.launched[0]?.runtimeMode).toBe("auto-accept-edits");+ expect(aboveCeiling.result).toMatchObject({+ link: `https://box.example.ts.net/environment/${encodeURIComponent(aboveCeiling.launched[0]?.threadId ?? "")}`,+ });const requested = yield* launchWith("full-access", "approval-required", {title: "Fix",diff --git a/apps/server/src/mcp/toolkits/project/handlers.ts b/apps/server/src/mcp/toolkits/project/handlers.tsindex 950cb38c6c4..79f18acbc2d 100644--- a/apps/server/src/mcp/toolkits/project/handlers.ts+++ b/apps/server/src/mcp/toolkits/project/handlers.ts@@ -19,7 +19,6 @@ import * as Repositories from "../../../sourceControl/SourceControlRepositorySerimport * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts";import * as ServerSettings from "../../../serverSettings.ts";import { resolveProjectSettings } from "@t3tools/shared/projectSettings";-import { formatThreadLink } from "@t3tools/shared/threadLinks";import { resolveRuntimeMode } from "../../delegatedTaskModes.ts";import * as McpInvocationContext from "../../McpInvocationContext.ts";import * as McpToolAccess from "../../McpToolAccess.ts";@@ -240,7 +239,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {const run = result.projection.runs.find((run) => run.userMessageId === messageId);return {threadId: thread.id,- link: formatThreadLink({ environmentId: scope.environmentId, threadId: thread.id }),+ link: McpInvocationContext.threadLinkFor(scope, thread.id),projectId: thread.projectId,modelSelection: thread.modelSelection,runId: run?.id ?? null,diff --git a/apps/server/src/mcp/toolkits/thread/handlers.ts b/apps/server/src/mcp/toolkits/thread/handlers.tsindex 8ad9afa4351..b1d34c91be8 100644--- a/apps/server/src/mcp/toolkits/thread/handlers.ts+++ b/apps/server/src/mcp/toolkits/thread/handlers.ts@@ -9,7 +9,6 @@ import {} from "@t3tools/contracts";import * as Effect from "effect/Effect";import { modelSelectionCommandType } from "@t3tools/shared/model";-import { formatThreadLink } from "@t3tools/shared/threadLinks";import * as McpInvocationContext from "../../McpInvocationContext.ts";import * as McpToolAccess from "../../McpToolAccess.ts";@@ -104,11 +103,15 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {const { task, threadId } = yield* scheduler.runNow({ id: input.taskId }).pipe(Effect.mapError(unavailable));- const { environmentId } = yield* McpInvocationContext.McpInvocationContext;+ const scope = yield* McpInvocationContext.McpInvocationContext;return {taskId: task.id,threadId,- ...(threadId === null ? {} : { link: formatThreadLink({ environmentId, threadId }) }),+ ...(threadId === null+ ? {}+ : {+ link: McpInvocationContext.threadLinkFor(scope, threadId),+ }),lastRunStatus: task.lastRunStatus,runCount: task.runCount,nextRunAt: task.nextRunAt,@@ -118,7 +121,7 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {t3_thread_search: McpToolAccess.reads((input) =>Effect.gen(function* () {const { caller } = yield* readCaller();- const { environmentId } = yield* McpInvocationContext.McpInvocationContext;+ const scope = yield* McpInvocationContext.McpInvocationContext;const threadSearch = yield* ThreadSearch.ThreadSearch;const { matches } = yield* threadSearch.searchThreads({ ...input, projectId: input.projectId ?? caller?.projectId })@@ -126,7 +129,7 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {return {matches: matches.map((match) => ({...match,- link: formatThreadLink({ environmentId, threadId: match.threadId }),+ link: McpInvocationContext.threadLinkFor(scope, match.threadId),})),};}),diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.mdindex befaca4c077..6bd4dea35d7 100644--- a/docs/orchestration-v2/orchestrator-mcp-server.md+++ b/docs/orchestration-v2/orchestrator-mcp-server.md@@ -398,7 +398,10 @@ delegated to a linked environment name that environment. Clients open only thatenvironment's thread and show its current title; a link to a thread they do notknow renders as a label that opens nothing. Older `t3-thread://v1/<threadId>`links name no environment: clients resolve them in the message's environment,-then by a unique match in another enabled environment.+then by a unique match in another enabled environment. An outside OAuth client,+one that is not a linked environment, cannot render `t3-thread://` links, so it+gets a web URL on the origin it reached, `<origin>/<environmentId>/<threadId>`,+for threads in this environment.List and read results report `snoozed` and `snoozedUntil`, and`t3_thread_list` filters on `snoozed`. The server's `isSnoozed` follows the