feat(app): accept a pasted redirect URL for MCP sign-in (#61706)

Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
This commit is contained in:
Filip
2026-11-04 19:06:05 +01:00
committed by GitHub
Unverified
parent 0d226daccf
commit b92e085cf8
19 changed files with 581 additions and 26 deletions
@@ -0,0 +1,166 @@
import { createUniqueId, onCleanup, onMount, Show } from "solid-js"
import { createStore } from "solid-js/store"
import { Button } from "@opencode/ui/button"
import { Dialog, DialogBody, DialogHeader, DialogTitleGroup } from "@opencode/ui/dialog"
import { Loader } from "@opencode/ui/loader"
import { TextInput } from "@opencode/ui/text-input"
import { useLanguage } from "@/runtime/i18n/language"
import { usePlatform } from "@/runtime/platform/platform"
import { useServerSDK } from "@/runtime/server/client"
import { formatServerError } from "@/runtime/server/errors"
/**
* Waits for an MCP sign-in that also accepts the redirect URL pasted from the browser, for when the browser
* cannot reach the machine running the server (SSH, a remote desktop connection, a container).
*/
export function DialogMcpSignIn(props: {
name: string
integrationID: string
attemptID: string
url: string
/** The attempt's lifetime in milliseconds, from the server's `time`; the two clocks may disagree. */
lifetime: number
location: { directory: string } | undefined
onDone: () => Promise<void>
}) {
const language = useLanguage()
const platform = usePlatform()
const serverSDK = useServerSDK()
const errorID = createUniqueId()
const attempt = { integrationID: props.integrationID, attemptID: props.attemptID, location: props.location }
const [store, setStore] = createStore<{ value: string; error?: string; failed?: string; submitting: boolean }>({
value: "",
submitting: false,
})
let input: HTMLInputElement | undefined
let timer: ReturnType<typeof setTimeout> | undefined
const deadline = Date.now() + props.lifetime
let disposed = false
let settled = false
const fail = (message: string) => {
settled = true
setStore("failed", message)
}
const poll = async () => {
const result = await serverSDK.api.integration.oauth
.status(attempt)
.then((response) => ({ ok: true as const, status: response.data }))
.catch((error) => ({ ok: false as const, error }))
if (disposed) return
// A failed request says nothing about the attempt, which may still be pending, so keep polling until it expires.
if (!result.ok) {
if (Date.now() >= deadline) return fail(formatServerError(result.error, language.t))
timer = setTimeout(() => void poll(), 1_000)
return
}
if (result.status.status === "complete") {
settled = true
return props.onDone()
}
if (result.status.status === "failed") return fail(result.status.message)
if (result.status.status === "expired") return fail(language.t("provider.connect.oauth.expired"))
timer = setTimeout(() => void poll(), 1_000)
}
// A rejected paste leaves the attempt waiting; an accepted one settles through the status poll.
const submit = async (event: SubmitEvent) => {
event.preventDefault()
const code = store.value.trim()
if (!code) return setStore("error", language.t("dialog.mcp.signIn.paste.required"))
setStore({ error: undefined, submitting: true })
const error = await serverSDK.api.integration.oauth
.complete({ ...attempt, code })
.then(() => undefined)
.catch((error) => formatServerError(error, language.t))
setStore({ error, submitting: false })
}
onMount(() => {
input?.focus({ preventScroll: true })
void poll()
})
// Like the TUI, dismissing the dialog ends the attempt, so a late browser callback cannot connect the server.
onCleanup(() => {
disposed = true
clearTimeout(timer)
if (settled) return
void serverSDK.api.integration.oauth.cancel(attempt).catch(() => undefined)
})
return (
<Dialog size="normal">
<DialogHeader>
<DialogTitleGroup title={language.t("dialog.mcp.signIn.open", { name: props.name })} />
</DialogHeader>
<DialogBody>
<div class="flex flex-col gap-5 px-3 pb-3 text-[13px] font-[440] leading-5 tracking-[-0.04px] text-v2-text-text-muted">
<div>{language.t("dialog.mcp.signIn.description", { name: props.name })}</div>
<Show
when={store.failed}
fallback={
<div class="flex items-center gap-2">
<Loader class="size-4 shrink-0 text-v2-icon-icon-muted" />
<span>{language.t("provider.connect.status.waiting")}</span>
</div>
}
>
{(failed) => (
<div role="alert" class="text-v2-state-fg-danger">
{language.t("provider.connect.status.failed", { error: failed() })}
</div>
)}
</Show>
<div class="flex flex-wrap items-center gap-2">
<Button variant="neutral" icon="arrow-up-right" onClick={() => platform.openExternal(props.url)}>
{language.t("provider.connect.oauth.openBrowser")}
</Button>
</div>
<Show when={!store.failed}>
<form onSubmit={submit} class="flex flex-col items-start gap-5 self-stretch">
<label class="flex w-full flex-col gap-2 font-[530] leading-4 text-v2-text-text-base">
{language.t("dialog.mcp.signIn.paste.label")}
<TextInput
ref={input}
class="!w-full"
name="redirect"
placeholder={language.t("dialog.mcp.signIn.paste.placeholder")}
value={store.value}
invalid={store.error !== undefined}
aria-describedby={store.error ? errorID : undefined}
autocomplete="off"
spellcheck={false}
onInput={(event) => setStore({ value: event.currentTarget.value, error: undefined })}
/>
</label>
<Show when={store.error}>
{(error) => (
<div id={errorID} role="alert" class="-mt-4 text-xs text-v2-state-fg-danger">
{error()}
</div>
)}
</Show>
<Button type="submit" variant="contrast" disabled={store.submitting}>
{language.t("common.continue")}
</Button>
</form>
</Show>
</div>
</DialogBody>
</Dialog>
)
}
@@ -5,6 +5,9 @@ import { useData } from "@/runtime/server/current"
import { useServerSDK } from "@/runtime/server/client"
import { usePlatform } from "@/runtime/platform/platform"
import { showToast } from "@/shell/notifications/toast"
import { useDialog } from "@opencode/ui/context/dialog"
import { uuid } from "@opencode/util/uuid"
import { DialogMcpSignIn } from "./mcp-sign-in"
export type McpControls = {
readonly preview: boolean
@@ -18,6 +21,7 @@ export function useMcpToggle(directory?: Accessor<string | undefined>, onSuccess
const serverSDK = useServerSDK()
const platform = usePlatform()
const language = useLanguage()
const dialog = useDialog()
const location = () => {
const value = directory ? directory() : data.location.default().directory
@@ -62,6 +66,35 @@ export function useMcpToggle(directory?: Accessor<string | undefined>, onSuccess
})
platform.openExternal(attempt.data.url)
if (attempt.data.paste) {
const integrationID = current.integrationID
const dialogID = uuid()
void dialog.push(
() => (
<DialogMcpSignIn
name={name}
integrationID={integrationID}
attemptID={attempt.data.attemptID}
url={attempt.data.url}
lifetime={attempt.data.time.expires - attempt.data.time.created}
location={ref}
onDone={async () => {
data.location.mcp.server.invalidate(ref)
data.location.mcp.resource.invalidate(ref)
await Promise.all([
data.location.mcp.server.sync(ref),
data.location.mcp.resource.sync(ref),
onSuccess?.(),
])
dialog.close(dialogID)
}}
/>
),
undefined,
dialogID,
)
}
}
data.location.mcp.resource.invalidate(ref)
+5
View File
@@ -386,6 +386,11 @@ export const dict = {
"dialog.mcp.error.disable": "Disable",
"dialog.mcp.signIn": "Sign in",
"dialog.mcp.signIn.open": "Sign in to {{name}}",
"dialog.mcp.signIn.description":
"Your browser opens so you can sign in to {{name}}. If it can't reach the machine running OpenCode, paste the URL it was redirected to below.",
"dialog.mcp.signIn.paste.label": "Redirect URL",
"dialog.mcp.signIn.paste.placeholder": "http://127.0.0.1:…/callback?code=…",
"dialog.mcp.signIn.paste.required": "Paste the URL from the browser's address bar",
"dialog.mcp.error.details":
"MCP server: {{name}}\nStatus: failed\nConfiguration: mcp.servers.{{name}}\nError: {{error}}",
"dialog.fork.search.empty": "No messages found",
@@ -5,6 +5,7 @@ import { Commands } from "../../commands"
import { Runtime } from "../../../framework/runtime"
import { selectIntegration, type IntegrationChoice } from "../../../ui/integration-picker"
import { handlePromptErrors, openUrl, prompt, requireInteractive } from "../../../ui/prompt"
import { errorMessage } from "../../../util/error"
import { answerForm, secret } from "./form"
import {
createClient,
@@ -214,7 +215,14 @@ export const oauthLogin = Effect.fn("cli.auth.login.oauth")(function* (
// Clack's spinner captures Ctrl+C and exits the process directly, which would skip the finalizer that
// cancels the attempt. Waits that can last minutes use plain log lines so Ctrl+C interrupts normally.
log.step("Waiting for authorization...")
const status = yield* waitForOAuth(client, integration.id, attempt.attemptID)
const wait = waitForOAuth(client, integration.id, attempt.attemptID)
const status =
attempt.paste && process.stdin.isTTY && process.stdout.isTTY
? yield* Effect.raceFirst(
wait,
pasteRedirect(client, integration.id, attempt.attemptID).pipe(Effect.andThen(Effect.never)),
)
: yield* wait
if (status.status === "complete") {
log.success(`Connected to ${integration.name}`)
return
@@ -264,6 +272,30 @@ const commandLogin = Effect.fn("cli.auth.login.command")(function* (
yield* Effect.fail(new Error("Authentication expired"))
})
// Hands the redirect URL to the attempt, which still settles through its status; a rejected paste asks again.
const pasteRedirect = Effect.fn("cli.auth.login.oauth.paste")(function* (
client: OpenCodeClient,
integrationID: string,
attemptID: string,
) {
while (true) {
const code = yield* prompt<string>((signal) =>
text({
message: "If the browser can't reach this machine, paste the URL it was redirected to",
validate: (value) => (!value ? "Required" : undefined),
signal,
}),
)
const pasted = yield* request((signal) =>
client.integration.oauth.complete({ integrationID, attemptID, code, location }, { signal }),
).pipe(
Effect.as(true),
Effect.catch((error) => Effect.sync(() => log.error(errorMessage(error))).pipe(Effect.as(false))),
)
if (pasted) return
}
})
const waitForOAuth = Effect.fn("cli.auth.login.oauth.wait")(function* (
client: OpenCodeClient,
integrationID: string,
+1 -1
View File
@@ -4,7 +4,7 @@ import { errorMessage } from "../util/error"
const cancelled = Symbol("cancelled")
export function prompt<A>(run: () => Promise<A | symbol>) {
export function prompt<A>(run: (signal: AbortSignal) => Promise<A | symbol>) {
return Effect.tryPromise({ try: run, catch: (cause) => cause }).pipe(
Effect.flatMap((value) => (isCancel(value) ? Effect.fail(cancelled) : Effect.succeed(value))),
)
+1
View File
@@ -769,6 +769,7 @@ export type IntegrationAttempt = {
url: string
instructions: string
mode: "auto" | "code"
paste?: true
time: { created: number; expires: number }
}
+6
View File
@@ -65,6 +65,8 @@ export type OAuthAuthorization = {
| {
readonly mode: "auto"
readonly callback: Effect.Effect<Credential.OAuth, unknown>
/** Hands `callback` the redirect URL a user pasted, for when the browser cannot reach this machine. */
readonly submit?: (redirect: string) => Effect.Effect<void, unknown>
}
| {
readonly mode: "code"
@@ -605,6 +607,7 @@ const layer = Layer.effect(
url: authorization.url,
instructions: authorization.instructions,
mode: authorization.mode,
...(authorization.mode === "auto" && authorization.submit ? { paste: true } : {}),
time,
})
})
@@ -797,6 +800,9 @@ const layer = Layer.effect(
})
if (!attempt) return yield* new AttemptNotFoundError(input)
if (attempt.status !== "pending") return
// The attempt still settles through its callback; clients keep polling status for the result.
if (attempt.authorization.mode === "auto" && attempt.authorization.submit && input.code !== undefined)
return yield* authorize(attempt.authorization.submit(input.code))
if (attempt.authorization.mode === "code" && input.code === undefined) {
return yield* new CodeRequiredError({ attemptID: input.attemptID })
}
+21 -3
View File
@@ -364,7 +364,7 @@ export const authorize = (input: {
const client = clientFromCredential(previous)
if (client) yield* Effect.promise(() => store.saveClientInformation(client))
}
const code = yield* Deferred.make<{ code: string; iss: string | undefined; response: ServerResponse }, Error>()
const code = yield* Deferred.make<{ code: string; iss: string | undefined; response?: ServerResponse }, Error>()
const redirect = oauth?.redirect_uri ? new URL(oauth.redirect_uri) : undefined
const redirectPath = redirect?.pathname ?? "/callback"
const state = Buffer.from(crypto.getRandomValues(new Uint8Array(32))).toString("base64url")
@@ -502,7 +502,7 @@ export const authorize = (input: {
const respond = (error?: string) =>
Effect.sync(() =>
value.response
.writeHead(error ? 400 : 200, { "Content-Type": "text/html" })
?.writeHead(error ? 400 : 200, { "Content-Type": "text/html" })
.end(
error
? OauthCallbackPage.error(error, { provider: input.name })
@@ -523,7 +523,7 @@ export const authorize = (input: {
Effect.flatMap(() => finalize),
Effect.tap(() => respond()),
Effect.tapError((error) => respond(error.message)),
Effect.onInterrupt(() => Effect.sync(() => value.response.destroy())),
Effect.onInterrupt(() => Effect.sync(() => value.response?.destroy())),
)
}),
Effect.onErrorIf(
@@ -542,6 +542,24 @@ export const authorize = (input: {
instructions: `Authorize ${input.name} in your browser. This window will close automatically.`,
mode: "auto" as const,
callback: Deferred.await(completed),
// A pasted URL is held to the same checks as the browser's redirect, but a wrong paste keeps the login waiting.
submit: (pasted: string) =>
Effect.gen(function* () {
const text = pasted.trim()
if (!URL.canParse(text)) return yield* Effect.fail(new Error("Paste the full URL from the browser's address bar"))
const url = new URL(text)
if (url.searchParams.get("state") !== state)
return yield* Effect.fail(new Error("This URL belongs to a different sign-in"))
const error = url.searchParams.get("error_description") ?? url.searchParams.get("error")
if (error) {
yield* Deferred.fail(code, new Error(error))
return
}
const value = url.searchParams.get("code")
if (!value) return yield* Effect.fail(new Error("The URL does not contain an authorization code"))
yield* Effect.logInfo("mcp oauth redirect pasted", fields)
yield* Deferred.succeed(code, { code: value, iss: url.searchParams.get("iss") ?? undefined })
}),
}
}).pipe(
Effect.onError((cause) =>
+35
View File
@@ -192,6 +192,41 @@ describe("MCP OAuth", () => {
expect(credential.access).toBe("access")
})
test("completes from a pasted redirect URL and keeps waiting after a wrong paste", async () => {
const tokenRequests: URLSearchParams[] = []
const server = Bun.serve({
port: 0,
async fetch(request) {
if (request.method !== "POST" || new URL(request.url).pathname !== "/token")
return new Response(null, { status: 404 })
tokenRequests.push(new URLSearchParams(await request.text()))
return Response.json({ access_token: "access", token_type: "Bearer", refresh_token: "refresh", expires_in: 3600 })
},
})
const result = await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const { authorization, url: authorizationUrl } = yield* start(server, { client_id: "client" })
const redirect = new URL(authorizationUrl.searchParams.get("redirect_uri") ?? "")
redirect.searchParams.set("code", "pasted")
redirect.searchParams.set("state", "another-sign-in")
const rejected = yield* Effect.flip(authorization.submit(redirect.href))
const notURL = yield* Effect.flip(authorization.submit("pasted"))
redirect.searchParams.set("state", authorizationUrl.searchParams.get("state") ?? "")
yield* authorization.submit(` ${redirect.href}\n`)
return { rejected, notURL, credential: yield* authorization.callback }
}),
),
).finally(() => server.stop(true))
expect(result.rejected.message).toBe("This URL belongs to a different sign-in")
expect(result.notURL.message).toBe("Paste the full URL from the browser's address bar")
expect(result.credential.access).toBe("access")
expect(tokenRequests).toHaveLength(1)
expect(tokenRequests[0]?.get("code")).toBe("pasted")
})
test("reports a rejected token exchange on the callback page", async () => {
const server = Bun.serve({
port: 0,
@@ -45,6 +45,15 @@ export default {
"mcp.error.status": "Connection failed",
"mcp.error.actionLabel": "Details for {{name}}",
"mcp.signIn.open": "Sign in to {{name}}",
"mcp.signIn.description":
"Your browser opens so you can sign in to {{name}}. If it can't reach the machine running OpenCode, paste the URL it was redirected to below.",
"mcp.signIn.waiting": "Waiting for authorization…",
"mcp.signIn.failed": "Authorization failed: {{error}}",
"mcp.signIn.expired": "Authorization expired",
"mcp.signIn.openBrowser": "Open browser",
"mcp.signIn.paste.label": "Redirect URL",
"mcp.signIn.paste.placeholder": "http://127.0.0.1:…/callback?code=…",
"mcp.signIn.paste.required": "Paste the URL from the browser's address bar",
"mcp.error.open": "{{name}} failed, view error",
"mcp.error.title": "MCP server: {{name}}",
"mcp.error.copy": "Copy details",
@@ -3,8 +3,10 @@ import { Button } from "@opencode/ui/button"
import { Dialog, DialogBody, DialogHeader, DialogTitleGroup } from "@opencode/ui/dialog"
import { ErrorDetails } from "@opencode/ui/error-details"
import { Icon } from "@opencode/ui/icon"
import { Loader } from "@opencode/ui/loader"
import { MenuScroll } from "@opencode/ui/menu-scroll"
import { Switch } from "@opencode/ui/switch"
import { TextInput } from "@opencode/ui/text-input"
import { showToast } from "@opencode/ui/toast"
import { Tooltip } from "@opencode/ui/tooltip"
import { TruncatedText } from "@opencode/ui/truncated-text"
@@ -19,6 +21,7 @@ import {
Index,
on,
onCleanup,
onMount,
Show,
type Component,
type JSX,
@@ -1042,13 +1045,30 @@ function useMcpToggle(session: MountedSession, directory: () => string) {
if (!method || method.type !== "oauth") throw new Error(ctx.t("mcp.interactiveAuth", { name: input.name }))
const attempt = await client.integration.oauth.connect({
integrationID: current.integrationID,
methodID: method.id,
location: ref,
})
const integrationID = current.integrationID
const attempt = await client.integration.oauth.connect({ integrationID, methodID: method.id, location: ref })
system.openExternal(attempt.data.url)
if (attempt.data.paste)
ctx.dialogs.open((dialog) => (
<McpSignInDialog
session={session}
name={input.name}
integrationID={integrationID}
attemptID={attempt.data.attemptID}
url={attempt.data.url}
lifetime={attempt.data.time.expires - attempt.data.time.created}
directory={ref.directory}
onDone={async () => {
const latest = data()
latest.location.mcp.server.invalidate(ref)
latest.location.mcp.resource.invalidate(ref)
await Promise.all([latest.location.mcp.server.sync(ref), latest.location.mcp.resource.sync(ref)])
dialog.close()
}}
/>
))
}
source.location.mcp.resource.invalidate(ref)
@@ -1145,3 +1165,162 @@ function McpErrorDialog(props: { session: MountedSession; directory: string; nam
</Show>
)
}
// Waits for an MCP sign-in that also accepts the redirect URL pasted from the browser, for when the browser cannot
// reach the machine running the server (SSH, a remote desktop connection, a container). Like the app's MCPs dialog.
function McpSignInDialog(props: {
session: MountedSession
name: string
integrationID: string
attemptID: string
url: string
/** The attempt's lifetime in milliseconds, from the server's `time`; the two clocks may disagree. */
lifetime: number
directory: string
onDone: () => Promise<void>
}) {
const ctx = usePlugin()
const errorID = createUniqueId()
const attempt = () => ({
integrationID: props.integrationID,
attemptID: props.attemptID,
location: { directory: props.directory },
})
const deadline = Date.now() + props.lifetime
const [store, setStore] = createStore<{ value: string; error?: string; failed?: string; submitting: boolean }>({
value: "",
submitting: false,
})
let input: HTMLInputElement | undefined
let timer: ReturnType<typeof setTimeout> | undefined
let disposed = false
let settled = false
const fail = (message: string) => {
settled = true
setStore("failed", ctx.t("mcp.signIn.failed", { error: message }))
}
const poll = async () => {
const result = await props.session.server.client.integration.oauth
.status(attempt())
.then((response) => ({ ok: true as const, status: response.data }))
.catch((error) => ({ ok: false as const, error }))
if (disposed) return
// A failed request says nothing about the attempt, which may still be pending, so keep polling until it expires.
if (!result.ok) {
if (Date.now() >= deadline) return fail(result.error instanceof Error ? result.error.message : String(result.error))
timer = setTimeout(() => void poll(), 1_000)
return
}
if (result.status.status === "complete") {
settled = true
return props.onDone()
}
if (result.status.status === "failed") return fail(result.status.message)
if (result.status.status === "expired") return fail(ctx.t("mcp.signIn.expired"))
timer = setTimeout(() => void poll(), 1_000)
}
// A rejected paste leaves the attempt waiting; an accepted one settles through the status poll.
const submit = async (event: SubmitEvent) => {
event.preventDefault()
const code = store.value.trim()
if (!code) return setStore("error", ctx.t("mcp.signIn.paste.required"))
setStore({ error: undefined, submitting: true })
const error = await props.session.server.client.integration.oauth
.complete({ ...attempt(), code })
.then(() => undefined)
.catch((error) => (error instanceof Error ? error.message : String(error)))
setStore({ error, submitting: false })
}
onMount(() => {
input?.focus({ preventScroll: true })
void poll()
})
// Dismissing the dialog ends the attempt, so a late browser callback cannot connect the server.
onCleanup(() => {
disposed = true
clearTimeout(timer)
if (settled) return
void props.session.server.client.integration.oauth.cancel(attempt()).catch(() => undefined)
})
return (
<Dialog>
<DialogHeader>
<DialogTitleGroup title={ctx.t("mcp.signIn.open", { name: props.name })} />
</DialogHeader>
<DialogBody>
<div class="flex flex-col gap-5 px-3 pb-3 text-[13px] font-[440] leading-5 tracking-[-0.04px] text-v2-text-text-muted">
<div>{ctx.t("mcp.signIn.description", { name: props.name })}</div>
<Show
when={store.failed}
fallback={
<div class="flex items-center gap-2">
<Loader class="size-4 shrink-0 text-v2-icon-icon-muted" />
<span>{ctx.t("mcp.signIn.waiting")}</span>
</div>
}
>
{(failed) => (
<div role="alert" class="text-v2-state-fg-danger">
{failed()}
</div>
)}
</Show>
<div class="flex flex-wrap items-center gap-2">
<Button variant="neutral" icon="arrow-up-right" onClick={() => ctx.system.openExternal(props.url)}>
{ctx.t("mcp.signIn.openBrowser")}
</Button>
</div>
<Show when={!store.failed}>
<form onSubmit={submit} class="flex flex-col items-start gap-5 self-stretch">
<label class="flex w-full flex-col gap-2 font-[530] leading-4 text-v2-text-text-base">
{ctx.t("mcp.signIn.paste.label")}
<TextInput
ref={input}
class="!w-full"
name="redirect"
placeholder={ctx.t("mcp.signIn.paste.placeholder")}
value={store.value}
invalid={store.error !== undefined}
aria-describedby={store.error ? errorID : undefined}
autocomplete="off"
spellcheck={false}
onInput={(event) => setStore({ value: event.currentTarget.value, error: undefined })}
/>
</label>
<Show when={store.error}>
{(error) => (
<div id={errorID} role="alert" class="-mt-4 text-xs text-v2-state-fg-danger">
{error()}
</div>
)}
</Show>
<Button type="submit" variant="contrast" disabled={store.submitting}>
{ctx.t("common.continue")}
</Button>
</form>
</Show>
</div>
</DialogBody>
</Dialog>
)
}
+5 -1
View File
@@ -6879,7 +6879,7 @@
}
}
},
"description": "Complete a code-based OAuth attempt and store the resulting credential.",
"description": "Complete a code-based OAuth attempt and store the resulting credential. For an automatic attempt with `paste`, `code` is the redirect URL copied from the browser, and the result arrives through the attempt status.",
"summary": "Complete OAuth connection",
"requestBody": {
"content": {
@@ -19260,6 +19260,10 @@
"type": "string",
"enum": ["auto", "code"]
},
"paste": {
"type": "boolean",
"enum": [true]
},
"time": {
"type": "object",
"properties": {
+2 -1
View File
@@ -154,7 +154,8 @@ export const IntegrationGroup = HttpApiGroup.make("server.integration")
OpenApi.annotations({
identifier: "integration.oauth.complete",
summary: "Complete OAuth connection",
description: "Complete a code-based OAuth attempt and store the resulting credential.",
description:
"Complete a code-based OAuth attempt and store the resulting credential. For an automatic attempt with `paste`, `code` is the redirect URL copied from the browser, and the result arrives through the attempt status.",
}),
),
)
+2
View File
@@ -97,6 +97,8 @@ export class Attempt extends Schema.Class<Attempt>("Integration.Attempt")({
url: Schema.String,
instructions: Schema.String,
mode: Schema.Literals(["auto", "code"]),
/** An `auto` attempt also accepts the redirect URL pasted from the browser as its `code`. */
paste: Schema.optionalKey(Schema.Literal(true)),
time: AttemptTime,
}) {}
+8 -6
View File
@@ -213,15 +213,17 @@ export const IntegrationHandler = HttpApiBuilder.group(Api, "server.integration"
attemptID: error.attemptID,
message: `OAuth attempt not found: ${error.attemptID}`,
})
if (error._tag === "Integration.CodeRequired")
return new InvalidRequestError({
message: "Authorization code is required",
kind: "integration_code_required",
})
return new InvalidRequestError({
message:
error._tag === "Integration.CodeRequired"
? "Authorization code is required"
error.cause instanceof Error && error.cause.message.trim()
? error.cause.message
: "Authentication failed",
kind:
error._tag === "Integration.CodeRequired"
? "integration_code_required"
: "integration_authorization",
kind: "integration_authorization",
})
}),
)
@@ -666,6 +666,7 @@ function OAuthAuto(props: {
const toast = useToast()
const clipboard = useClipboard()
const newDesigns = useNewDesigns()
const theme = useTheme().surface("dialog")
let timer: ReturnType<typeof setTimeout> | undefined
let settled = false
@@ -686,8 +687,24 @@ function OAuthAuto(props: {
.catch(toast.error)
}
const [error, setError] = createSignal<string>()
const paste = (value: string) => {
if (!value.trim()) return open()
setError(undefined)
void client.api.integration.oauth
.complete({
integrationID: props.integration.id,
attemptID: props.attempt.attemptID,
location: locationQuery(props.location),
code: value,
})
.catch((cause) => setError(errorMessage(cause)))
}
Keymap.createLayer(() => ({
mode: "modal",
// The paste prompt owns typing and Enter.
enabled: !props.attempt.paste,
commands: [
{ bind: newDesigns() ? "return" : "o", title: "Open authorization URL", group: "Dialog", run: open },
{ bind: "c", title: "Copy authorization details", group: "Dialog", run: copy },
@@ -734,14 +751,47 @@ function OAuthAuto(props: {
})
return (
<OAuthView
title={props.title}
url={props.attempt.url}
instructions={props.attempt.instructions}
message="Waiting for authorization…"
onCopy={copy}
onOpen={open}
/>
<Show
when={props.attempt.paste}
fallback={
<OAuthView
title={props.title}
url={props.attempt.url}
instructions={props.attempt.instructions}
message="Waiting for authorization…"
onCopy={copy}
onOpen={open}
/>
}
>
<DialogPrompt
title={props.title}
subtitle={props.attempt.instructions}
placeholder="Redirect URL"
onConfirm={paste}
actions={[
{
title: "copy link",
command: "dialog.integration.copy",
commandTitle: "Copy authorization link",
onTrigger: copy,
},
]}
description={() => (
<box gap={1}>
<Show when={!newDesigns()}>
<text fg={theme.text.muted}>{props.attempt.instructions}</text>
</Show>
<Link href={props.attempt.url} fg={theme.markdown.link} />
<text fg={theme.text.muted}>
Waiting for authorization… Press enter to open the link. If the browser can't reach this machine, paste
the URL it was redirected to.
</text>
<Show when={error()}>{(value) => <text fg={theme.text.feedback.error.base}>{value()}</text>}</Show>
</box>
)}
/>
</Show>
)
}
+1
View File
@@ -268,6 +268,7 @@ export const Definitions = {
"dialog.prompt.submit": keybind("return", "Submit dialog prompt"),
"dialog.integration.rename": keybind("ctrl+r", "Rename integration account"),
"dialog.integration.delete": keybind("ctrl+d", "Delete integration account"),
"dialog.integration.copy": keybind("tab", "Copy authorization link"),
"dialog.worktree.generate": keybind("tab", "Generate worktree name"),
"dialog.move_session.new": keybind("ctrl+a", "New worktree"),
"dialog.move_session.move": keybind("ctrl+m", "Move session to worktree"),
@@ -367,6 +367,7 @@ The viewer shows recent output and stays open after exit; output redirected to a
| `dialog.prompt.submit` | `return` | Submit dialog prompt |
| `dialog.integration.rename` | `ctrl+r` | Rename integration account |
| `dialog.integration.delete` | `ctrl+d` | Delete integration account |
| `dialog.integration.copy` | `tab` | Copy authorization link |
| `dialog.worktree.generate` | `tab` | Generate worktree name |
| `dialog.move_session.new` | `ctrl+a` | New worktree |
| `dialog.move_session.move` | `ctrl+m` | Move session to worktree |
@@ -212,6 +212,16 @@ If the server needs authentication, run `/mcps`, select it, and complete authori
opencode mcp auth sentry
```
### Browser on another machine
When OpenCode runs over SSH, in a container, or on a remote server, the browser cannot reach OpenCode after you sign in and shows a page that does not load. Copy that page's URL from the address bar and paste it into the sign-in prompt:
```text
http://127.0.0.1:52817/callback?code=...&state=...
```
The prompt appears in `/mcps`, in `opencode mcp auth`, and in the desktop and web app's sign-in dialog while OpenCode waits for the browser.
### Client registration
OpenCode identifies itself to the authorization server with the first method that applies: