diff --git a/apps/mobile/src/features/threads/McpSignInSheet.tsx b/apps/mobile/src/features/threads/McpSignInSheet.tsx new file mode 100644 index 000000000000..8053fe4a32b6 --- /dev/null +++ b/apps/mobile/src/features/threads/McpSignInSheet.tsx @@ -0,0 +1,167 @@ +import type { EnvironmentId, ProviderThreadId, ThreadId } from "@t3tools/contracts"; +import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; +import { + type AtomCommandResult, + isAtomCommandInterrupted, + squashAtomCommandFailure, +} from "@t3tools/client-runtime/state/runtime"; +import * as Haptics from "expo-haptics"; +import { msg, T, useGT, useMessages } from "gt-react-native"; +import { useState } from "react"; +import { ScrollView } from "react-native"; +import { useSafeAreaInsets } from "react-native-safe-area-context"; + +import { AppText as Text, AppTextInput as TextInput } from "../../components/ui/AppText"; +import { Button } from "../../components/ui/Button"; +import { Sheet } from "../../components/ui/Sheet"; +import { tryOpenExternalUrl } from "../../lib/openExternalUrl"; +import { threadEnvironment } from "../../state/threads"; +import { useAtomCommand } from "../../state/use-atom-command"; + +export interface McpSignInTarget { + readonly threadId: ThreadId; + readonly providerThreadId: ProviderThreadId; + readonly server: string; +} + +/** + * Signs an agent's MCP server in from the notice that says it needs one. The + * agent keeps the credential; the user approves in a browser, then pastes back + * the address it lands on, which is often a localhost page on another machine. + */ +export function McpSignInSheet(props: { + readonly environmentId: EnvironmentId; + readonly target: McpSignInTarget; + readonly onClose: () => void; +}) { + const gt = useGT(); + const m = useMessages(); + const insets = useSafeAreaInsets(); + const { server } = props.target; + const start = useAtomCommand(threadEnvironment.mcpSignInStart, { + label: "start MCP sign-in", + reportFailure: false, + reportDefect: false, + }); + const complete = useAtomCommand(threadEnvironment.mcpSignInComplete, { + label: "finish MCP sign-in", + reportFailure: false, + reportDefect: false, + }); + const [url, setUrl] = useState(null); + const [callbackUrl, setCallbackUrl] = useState(""); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(null); + + const failed = (result: AtomCommandResult & { readonly _tag: "Failure" }) => { + if (isAtomCommandInterrupted(result)) return; + setError( + errorDisplayMessage(squashAtomCommandFailure(result), m) ?? + m(msg("The sign-in couldn't continue. Try again.")), + ); + }; + // The command marks the server signed in for the thread, which every notice about it shows. + const signedIn = () => { + void Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success); + props.onClose(); + }; + const openSignInPage = async () => { + setBusy(true); + setError(null); + let page = url; + if (page === null) { + const result = await start({ environmentId: props.environmentId, input: props.target }); + if (result._tag === "Failure") { + setBusy(false); + failed(result); + return; + } + if (result.value.authorizationUrl === null) { + setBusy(false); + signedIn(); + return; + } + page = result.value.authorizationUrl; + setUrl(page); + } + setBusy(false); + if (!(await tryOpenExternalUrl(page, "provider-auth"))) { + setError(gt("Could not open the sign-in page.")); + } + }; + const finish = async () => { + const pasted = callbackUrl.trim(); + if (pasted.length === 0) return; + setBusy(true); + setError(null); + const result = await complete({ + environmentId: props.environmentId, + input: { ...props.target, callbackUrl: pasted }, + }); + setBusy(false); + if (result._tag === "Success") signedIn(); + else failed(result); + }; + + return ( + + + + + Approve access in your browser. It then opens a page that may not load: copy that page's + address and paste it here. + + +