Open History to see the last commit
1/**
2 * A one-use pairing code for linking, minted on the environment being linked to with the
3 * user's own session there, so a link card can be answered without fetching a code by hand.
4 * It carries exactly what an agent sign-in with the chosen access needs.
5 */
6import { type AuthMcpClientAccess, mcpClientScopes } from "@t3tools/contracts";
7import * as Schema from "effect/Schema";
8import * as Effect from "effect/Effect";
9import type * as HttpClient from "effect/http/HttpClient";
10import * as Option from "effect/Option";
11import type { Atom } from "effect/reactivity";
12import * as SubscriptionRef from "effect/SubscriptionRef";
13
14import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
15import type { PreparedConnection } from "../connection/model.ts";
16import type { EnvironmentRegistry } from "../connection/registry.ts";
17import * as EnvironmentSupervisor from "../connection/supervisor.ts";
18import * as ManagedRelay from "../relay/managedRelay.ts";
19import { executeAuthenticatedEnvironmentHttpRequest } from "./environmentHttpAuth.ts";
20import { createEnvironmentCommand } from "./runtime.ts";
21
22const MINT_TIMEOUT_MS = 8_000;
23const PEER_PAIRING_CODE_LABEL = "T3 Code link";
24
25/** Minting failed; the user can still paste a code from the other environment. */
26export class PeerPairingCodeError extends Schema.TaggedError<PeerPairingCodeError>()(
27 "PeerPairingCodeError",
28 {
29 reason: Schema.Literals(["not_allowed", "unavailable"]),
30 cause: Schema.Defect(),
31 },
32) {
33 override get message(): string {
34 return this.reason === "not_allowed"
35 ? "Your session there cannot create pairing codes. Paste a code from there instead."
36 : "Could not get a pairing code from there. Paste a code from there instead.";
37 }
38}
39
40const mintPeerPairingCode = Effect.fn("clientRuntime.state.mintPeerPairingCode")(function* (
41 prepared: PreparedConnection,
42 access: AuthMcpClientAccess,
43): Effect.fn.Return<string, PeerPairingCodeError, HttpClient.HttpClient> {
44 const signer = yield* Effect.serviceOption(ManagedRelay.ManagedRelayDpopSigner);
45 const remoteAuthorization = yield* Effect.serviceOption(
46 RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization,
47 );
48 const minted = yield* executeAuthenticatedEnvironmentHttpRequest({
49 prepared,
50 signer,
51 remoteAuthorization,
52 group: "auth",
53 method: "POST",
54 url: (urls) => urls.pairingCredential(),
55 timeoutMs: MINT_TIMEOUT_MS,
56 request: ({ client, headers }) =>
57 client.pairingCredential({
58 headers,
59 payload: { label: PEER_PAIRING_CODE_LABEL, scopes: mcpClientScopes(access) },
60 }),
61 }).pipe(
62 Effect.mapError(
63 (cause) =>
64 new PeerPairingCodeError({
65 reason:
66 cause._tag === "EnvironmentScopeRequiredError" ||
67 cause._tag === "EnvironmentOperationForbiddenError"
68 ? "not_allowed"
69 : "unavailable",
70 cause,
71 }),
72 ),
73 );
74 return minted.credential;
75});
76
77/**
78 * Mints a code on the environment it runs in. Each run mints its own: a code
79 * is one-use, so two overlapping answers must never share one.
80 */
81export function createPeerPairingCodeCommand<R, E>(
82 runtime: Atom.AtomRuntime<EnvironmentRegistry | HttpClient.HttpClient | R, E>,
83) {
84 return createEnvironmentCommand(runtime, {
85 label: "environment-data:peer-pairing-code:mint",
86 execute: Effect.fn(function* (access: AuthMcpClientAccess) {
87 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
88 const prepared = yield* SubscriptionRef.get(supervisor.prepared);
89 if (Option.isNone(prepared)) {
90 return yield* new PeerPairingCodeError({
91 reason: "unavailable",
92 cause: "The environment is not connected.",
93 });
94 }
95 return yield* mintPeerPairingCode(prepared.value, access);
96 }),
97 });
98}