feat(server): outside agents sign in to the T3 MCP server with OAuth (#16336)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Opus 5.5 authored and GitHub committed 2026-10-06 14:09:35 -07:00
1 parent 2f85686d9d
commit 2c8be5893e
29 files changed
+2531 -67

No files matched your search

@@ -0,0 +1,317 @@
import {
type AuthMcpApprovalDecision,
type AuthMcpApprovalDetails,
AuthMcpApprovalError,
AuthMcpClientAccess,
type AuthMcpAuthorizationRequest,
} from "@t3tools/contracts";
import { Radio as RadioPrimitive } from "@base-ui/react/radio";
import { EyeIcon, type LucideIcon } from "lucide-react";
import type * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { type ReactNode, useCallback, useEffect, useState } from "react";
import { PrimaryEnvironmentHttpClient } from "~/environments/primary/httpClient";
import { runPrimaryHttp } from "~/lib/runtime";
import { cn } from "~/lib/utils";
import { runtimeModeConfig } from "../chat/runtimeModeConfig";
import { Alert, AlertDescription } from "../ui/alert";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { RadioGroup } from "../ui/radio-group";
import { Spinner } from "../ui/spinner";
import { AuthSurfaceShell } from "./AuthSurfaceShell";
const accessConfig: Record<
AuthMcpClientAccess,
{ readonly label: string; readonly description: string; readonly icon: LucideIcon }
> = {
"read-only": {
label: "Read only",
description: "Read projects and threads. Cannot start, message or change anything.",
icon: EyeIcon,
},
...runtimeModeConfig,
};
type Loaded =
| { readonly status: "loading" }
| { readonly status: "invalid"; readonly message: string }
| { readonly status: "ready"; readonly details: AuthMcpApprovalDetails };
const UNREACHABLE = "Could not reach this environment. Try again.";
const isApprovalError = Schema.is(AuthMcpApprovalError);
type Answer<A> =
| { readonly kind: "ok"; readonly value: A }
| { readonly kind: "redirect"; readonly url: string }
| { readonly kind: "error"; readonly message: string };
/**
* Runs an approval call. The server validates the agent's request again on
* every call and answers what the page asked for, a message to show, or a URL
* the browser must follow (an approval, a denial, or a protocol error the
* agent should receive).
*/
function runApproval<A>(
call: (
client: Context.Service.Shape<typeof PrimaryEnvironmentHttpClient>,
) => Effect.Effect<A | { readonly redirectTo: string }, unknown>,
): Promise<Answer<A>> {
return runPrimaryHttp(
PrimaryEnvironmentHttpClient.pipe(
Effect.flatMap(call),
Effect.map((value): Answer<A> =>
typeof value === "object" && value !== null && "redirectTo" in value
? { kind: "redirect", url: value.redirectTo }
: { kind: "ok", value: value as A },
),
Effect.catch((error) =>
Effect.succeed<Answer<A>>({
kind: "error",
message: isApprovalError(error) ? error.message : UNREACHABLE,
}),
),
),
).catch((): Answer<A> => ({ kind: "error", message: UNREACHABLE }));
}
function readAuthorizationRequest(): AuthMcpAuthorizationRequest {
return Object.fromEntries(new URL(window.location.href).searchParams);
}
/**
* /connect-agent: where an outside agent's MCP sign-in lands after the server
* checks the request. The user picks the most the agent may allow, then
* approves with a pairing code, or in one click when this browser is already
* signed in to the environment as an administrator.
*/
/**
* Whether this browser's session may approve `access` without a pairing code.
* A session may only grant access whose scopes it holds itself.
*/
function oneClickApproves(details: AuthMcpApprovalDetails, access: AuthMcpClientAccess) {
return details.csrfToken !== undefined && (details.oneClickAccess ?? []).includes(access);
}
export function ConnectAgentSurface() {
const [authorization] = useState(readAuthorizationRequest);
const [loaded, setLoaded] = useState<Loaded>({ status: "loading" });
const [access, setAccess] = useState<AuthMcpClientAccess>("read-only");
const [pairingCode, setPairingCode] = useState("");
const [errorMessage, setErrorMessage] = useState("");
const [pending, setPending] = useState<"approve" | "deny" | null>(null);
useEffect(() => {
let cancelled = false;
void runApproval((client) => client.mcpOAuth.approval({ payload: authorization })).then(
(answer) => {
if (cancelled) return;
if (answer.kind === "redirect") {
window.location.replace(answer.url);
return;
}
setLoaded(
answer.kind === "error"
? { status: "invalid", message: answer.message }
: { status: "ready", details: answer.value },
);
},
);
return () => {
cancelled = true;
};
}, [authorization]);
const decide = useCallback(
async (choice: "approve" | "deny") => {
if (loaded.status !== "ready") return;
setPending(choice);
setErrorMessage("");
const { csrfToken } = loaded.details;
const decision: AuthMcpApprovalDecision =
choice === "deny"
? { _tag: "deny" }
: csrfToken !== undefined && oneClickApproves(loaded.details, access)
? { _tag: "browser-session", access, csrfToken }
: { _tag: "pairing-code", access, code: pairingCode.trim() };
const answer = await runApproval((client) =>
client.mcpOAuth.decision({ payload: { authorization, decision } }),
);
if (answer.kind === "redirect") {
window.location.replace(answer.url);
return;
}
setPending(null);
setErrorMessage(answer.kind === "error" ? answer.message : "The sign-in could not continue.");
},
[access, authorization, loaded, pairingCode],
);
if (loaded.status === "loading") {
return (
<AuthSurfaceShell>
<ConnectAgentHeading
title="Checking the sign-in request"
description="One moment while this environment verifies the agent's request."
/>
<Spinner className="mt-6" size="lg" tone="muted" />
</AuthSurfaceShell>
);
}
if (loaded.status === "invalid") {
return (
<AuthSurfaceShell>
<ConnectAgentHeading title="This sign-in cannot continue" description={loaded.message} />
<p className="mt-4 text-sm text-muted-foreground">
Close this page and start the sign-in again from your agent.
</p>
</AuthSurfaceShell>
);
}
const { details } = loaded;
const oneClick = oneClickApproves(details, access);
const canApprove = pending === null && (oneClick || pairingCode.trim().length > 0);
return (
<AuthSurfaceShell>
<ConnectAgentHeading
title={`Connect ${details.clientName}`}
description={
<>
This agent wants to use the threads in every project on{" "}
<span className="font-medium text-foreground">{details.environmentHost}</span>.
</>
}
/>
<p className="mt-2 text-xs text-muted-foreground">
The name is chosen by the agent. Approval returns to {details.redirectHost} on the computer
that opened this page. Only approve a sign-in you just started.
</p>
<form
className="mt-6 space-y-5"
onSubmit={(event) => {
event.preventDefault();
if (canApprove) void decide("approve");
}}
>
<div className="space-y-2">
<span id="connect-agent-access-label" className="text-sm font-medium">
What it may do
</span>
<RadioGroup
aria-labelledby="connect-agent-access-label"
value={access}
onValueChange={(value) => setAccess(value as AuthMcpClientAccess)}
>
{AuthMcpClientAccess.literals.map((option) => (
<AccessOption key={option} access={option} selected={option === access} />
))}
</RadioGroup>
<p className="text-xs text-muted-foreground">
Beyond read only, it can start, message and stop threads, and none of them can run with
more than the mode you pick.
</p>
</div>
{oneClick ? null : (
<div className="space-y-2">
<label className="text-sm font-medium" htmlFor="connect-agent-pairing-code">
Pairing code
</label>
<Input
id="connect-agent-pairing-code"
autoCapitalize="none"
autoComplete="one-time-code"
autoCorrect="off"
disabled={pending !== null}
nativeInput
onChange={(event) => setPairingCode(event.currentTarget.value)}
placeholder="Paste a one-time pairing code"
spellCheck={false}
value={pairingCode}
/>
<p className="text-xs text-muted-foreground">
Create one in Settings → Connections, or run <code>t3 auth pairing create</code> on
this machine.
</p>
</div>
)}
{errorMessage ? (
<Alert variant="error">
<AlertDescription>{errorMessage}</AlertDescription>
</Alert>
) : null}
<div className="flex flex-wrap gap-2">
<Button disabled={!canApprove} type="submit">
{pending === "approve" ? "Approving…" : "Approve"}
</Button>
<Button
disabled={pending !== null}
onClick={() => void decide("deny")}
type="button"
variant="outline"
>
{pending === "deny" ? "Denying…" : "Deny"}
</Button>
</div>
</form>
</AuthSurfaceShell>
);
}
function ConnectAgentHeading({
title,
description,
}: {
readonly title: string;
readonly description: ReactNode;
}) {
return (
<>
<p className="text-3xs font-semibold tracking-widest text-primary uppercase">Agent sign-in</p>
<h1 className="mt-2 text-2xl font-semibold tracking-tight sm:text-3xl">{title}</h1>
<p className="mt-2 text-sm leading-relaxed text-muted-foreground">{description}</p>
</>
);
}
function AccessOption({
access,
selected,
}: {
readonly access: AuthMcpClientAccess;
readonly selected: boolean;
}) {
const { label, description, icon: Icon } = accessConfig[access];
return (
<RadioPrimitive.Root
value={access}
className={cn(
"flex cursor-pointer items-start gap-3 rounded-lg border px-3 py-2.5 text-left outline-none transition-[background-color,border-color,box-shadow]",
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background",
selected
? "border-primary bg-background ring-2 ring-primary/25 dark:border-transparent dark:bg-primary/10 dark:ring-1 dark:ring-primary/30"
: "border-border bg-background hover:bg-muted/50 dark:border-transparent dark:bg-white/[0.035] dark:hover:bg-accent",
)}
>
<Icon
aria-hidden
className={cn(
"mt-0.5 size-4 shrink-0",
selected ? "text-primary" : "text-muted-foreground",
)}
/>
<span className="min-w-0">
<span className="block text-sm font-medium text-foreground">{label}</span>
<span className="block text-xs text-muted-foreground">{description}</span>
</span>
</RadioPrimitive.Root>
);
}
+21
View File
@@ -13,6 +13,7 @@ import { Route as WelcomeRouteImport } from './routes/welcome'
import { Route as UsageRouteImport } from './routes/usage'
import { Route as SettingsRouteImport } from './routes/settings'
import { Route as PairRouteImport } from './routes/pair'
import { Route as ConnectAgentRouteImport } from './routes/connect-agent'
import { Route as ConnectRouteImport } from './routes/connect'
import { Route as ChatRouteImport } from './routes/_chat'
import { Route as ChatIndexRouteImport } from './routes/_chat.index'
@@ -55,6 +56,11 @@ const PairRoute = PairRouteImport.update({
path: '/pair',
getParentRoute: () => rootRouteImport,
} as any)
const ConnectAgentRoute = ConnectAgentRouteImport.update({
id: '/connect-agent',
path: '/connect-agent',
getParentRoute: () => rootRouteImport,
} as any)
const ConnectRoute = ConnectRouteImport.update({
id: '/connect',
path: '/connect',
@@ -165,6 +171,7 @@ const ChatEnvironmentIdThreadIdRoute =
export interface FileRoutesByFullPath {
'/': typeof ChatIndexRoute
'/connect': typeof ConnectRoute
'/connect-agent': typeof ConnectAgentRoute
'/pair': typeof PairRoute
'/settings': typeof SettingsRouteWithChildren
'/usage': typeof UsageRoute
@@ -190,6 +197,7 @@ export interface FileRoutesByFullPath {
}
export interface FileRoutesByTo {
'/connect': typeof ConnectRoute
'/connect-agent': typeof ConnectAgentRoute
'/pair': typeof PairRoute
'/settings': typeof SettingsRouteWithChildren
'/usage': typeof UsageRoute
@@ -218,6 +226,7 @@ export interface FileRoutesById {
__root__: typeof rootRouteImport
'/_chat': typeof ChatRouteWithChildren
'/connect': typeof ConnectRoute
'/connect-agent': typeof ConnectAgentRoute
'/pair': typeof PairRoute
'/settings': typeof SettingsRouteWithChildren
'/usage': typeof UsageRoute
@@ -247,6 +256,7 @@ export interface FileRouteTypes {
fullPaths:
| '/'
| '/connect'
| '/connect-agent'
| '/pair'
| '/settings'
| '/usage'
@@ -272,6 +282,7 @@ export interface FileRouteTypes {
fileRoutesByTo: FileRoutesByTo
to:
| '/connect'
| '/connect-agent'
| '/pair'
| '/settings'
| '/usage'
@@ -299,6 +310,7 @@ export interface FileRouteTypes {
| '__root__'
| '/_chat'
| '/connect'
| '/connect-agent'
| '/pair'
| '/settings'
| '/usage'
@@ -327,6 +339,7 @@ export interface FileRouteTypes {
export interface RootRouteChildren {
ChatRoute: typeof ChatRouteWithChildren
ConnectRoute: typeof ConnectRoute
ConnectAgentRoute: typeof ConnectAgentRoute
PairRoute: typeof PairRoute
SettingsRoute: typeof SettingsRouteWithChildren
UsageRoute: typeof UsageRoute
@@ -364,6 +377,13 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof PairRouteImport
parentRoute: typeof rootRouteImport
}
'/connect-agent': {
id: '/connect-agent'
path: '/connect-agent'
fullPath: '/connect-agent'
preLoaderRoute: typeof ConnectAgentRouteImport
parentRoute: typeof rootRouteImport
}
'/connect': {
id: '/connect'
path: '/connect'
@@ -571,6 +591,7 @@ const SettingsRouteWithChildren = SettingsRoute._addFileChildren(
const rootRouteChildren: RootRouteChildren = {
ChatRoute: ChatRouteWithChildren,
ConnectRoute: ConnectRoute,
ConnectAgentRoute: ConnectAgentRoute,
PairRoute: PairRoute,
SettingsRoute: SettingsRouteWithChildren,
UsageRoute: UsageRoute,
+1 -1
View File
@@ -161,7 +161,7 @@ function RootRouteView() {
};
}, [pathname]);
if (pathname === "/pair" || pathname === "/connect") {
if (pathname === "/pair" || pathname === "/connect" || pathname === "/connect-agent") {
return (
<>
<DocumentTitleSync />
+7
View File
@@ -0,0 +1,7 @@
import { createFileRoute } from "@tanstack/react-router";
import { ConnectAgentSurface } from "../components/auth/ConnectAgentSurface";
export const Route = createFileRoute("/connect-agent")({
component: ConnectAgentSurface,
});