run402-mcp
Version:
MCP server for Run402 — full-stack backend infrastructure for AI agents: Postgres, auth, storage, serverless functions and atomic deploys. Paid with x402/MPP. Includes $0.03 image generation.
203 lines • 8.78 kB
TypeScript
/**
* The dev session: a real project user's sign-in for a local dev server
* (`run402 dev --login <email>`), owned here once.
*
* `run402 dev` signs the user in through the project's own emailed code and
* stores what the token endpoint answered under the config base directory
* (never the project directory), one file per project and email, mode 0600.
* The dev server (`@run402/astro` under `astro dev`) is handed the file's
* path and keeps it fresh itself, because it can outlive the `run402 dev`
* that started it and an access token lives an hour. Both sides call this
* module, so the file's format and the refresh have one implementation.
*
* Refresh tokens are single-use. Two dev servers of one project share one
* file, so a refresh runs under a lock file and re-reads the session first:
* whoever comes second finds it fresh and spends nothing.
*
* What the dev server forwards to the data plane is never the user's own
* access token: a project admin's carries the `project_admin` role, which a
* deployed site never reads the database as. It is the token the gateway
* mints for a deployed function (`r.auth.actorToken`), asked for each minute.
*
* Nothing here is minted with a service key, and nothing reaches a browser.
*/
import type { ActorTokenResult, AuthSessionResult, EmailCodeVerifyOptions, MagicLinkOptions, MagicLinkRequestResult, MagicLinkVerifyResult } from "../namespaces/auth.js";
import type { ProjectBranchCarries, ProjectBranchCreateOptions, ProjectBranchCreateResult, ProjectBranchesListResult } from "../namespaces/branches.types.js";
/** The variable `run402 dev --login` sets for the dev server: the session file's path, never a token. */
export declare const DEV_SESSION_FILE_ENV = "RUN402_DEV_SESSION_FILE";
/** The variable naming this module's URL, so the dev server runs the very code that wrote the file. */
export declare const DEV_SESSION_MODULE_ENV = "RUN402_DEV_SESSION_MODULE";
/** The one reusable branch `run402 dev --branch` runs against. */
export declare const DEV_BRANCH_NAME = "dev";
/** The stored sign-in. Field names are this module's contract with itself across versions. */
export interface DevSignIn {
version: 1;
project_id: string;
/** Set on a branch's sign-in: the project it is a branch of, so signing out of the parent finds it. */
parent_project_id?: string;
api_base: string;
/** Where local code writes: the live project, or a branch of it. */
target: "live" | "branch";
user: {
id: string;
email: string;
email_verified: boolean;
};
access_token: string;
refresh_token: string;
/** ISO-8601. */
access_expires_at: string;
/** ISO-8601. */
signed_in_at: string;
}
/** The auth calls a dev session makes; `r.auth` of any SDK client that holds the project's keys. */
export interface DevSessionAuth {
requestMagicLink(projectId: string, opts: MagicLinkOptions): Promise<MagicLinkRequestResult>;
verifyEmailCode(projectId: string, opts: EmailCodeVerifyOptions): Promise<MagicLinkVerifyResult>;
refreshSession(projectId: string, refreshToken: string): Promise<AuthSessionResult>;
endSession(projectId: string, refreshToken: string): Promise<void>;
actorToken(projectId: string, accessToken: string): Promise<ActorTokenResult>;
}
/** Where a project's dev sign-ins are kept. */
export declare function devSessionsDir(projectId: string): string;
/** The file of one project user's dev sign-in. */
export declare function devSessionPath(projectId: string, email: string): string;
/** Parse a stored sign-in; `null` for anything this module did not write. */
export declare function parseDevSignIn(raw: string): DevSignIn | null;
/** Write the session beside itself and rename over it, so a reader never sees half a file. */
export declare function writeDevSignIn(path: string, signIn: DevSignIn): void;
/** The command that completes or repeats a sign-in, for hints and next actions. */
export declare function devLoginCommand(opts: {
email: string;
target: "live" | "branch";
branchProjectId?: string;
code?: string;
}): string;
export interface DevSignInOptions {
/** `r.auth` of a client holding the keys of `projectId`. */
auth: DevSessionAuth;
projectId: string;
/** When `projectId` is a branch: the project it is a branch of. */
parentProjectId?: string;
apiBase: string;
email: string;
target: "live" | "branch";
branchProjectId?: string;
/** The emailed code, when completing a sign-in started without a terminal. */
code?: string;
/** Asks the terminal for the code; absent when there is no terminal. */
prompt?: (question: string) => Promise<string>;
now?: number;
}
export type DevSignInResult = {
status: "signed_in";
path: string;
user: DevSignIn["user"];
target: DevSignIn["target"];
reused: boolean;
} | {
status: "code_sent";
email: string;
command: string;
};
/**
* Sign in, or reuse a stored sign-in. `signed_in`: the dev server can start
* with `path`. `code_sent`: there was no terminal; run `command` with the
* emailed code. Never returns a token.
*/
export declare function devSignIn(opts: DevSignInOptions): Promise<DevSignInResult>;
export interface DevSignOutEntry {
project_id: string;
email: string;
target: "live" | "branch";
/** False when the gateway could not be told; the local file is deleted either way. */
revoked: boolean;
}
/**
* End the stored sign-ins of a project and of its branches (one email, or
* all): the refresh token stops working at the gateway and the file is
* deleted. `authFor(projectId)` gives `r.auth` of a client holding that
* project's keys.
*/
export declare function devSignOut(opts: {
authFor: (projectId: string) => Pick<DevSessionAuth, "endSession">;
projectId: string;
email?: string;
}): Promise<DevSignOutEntry[]>;
export interface DevBranch {
branch_project_id: string;
parent_project_id: string;
name: string;
/** ISO-8601. */
expires_at: string;
email_mode: string;
/** False when an active branch of that name was reused. */
created: boolean;
/** What the branch has of its parent, when the gateway says. */
carries?: ProjectBranchCarries;
keys: {
anon_key: string;
service_key: string;
};
}
/**
* The branch `run402 dev --branch` runs against. With no id it is the
* project's `dev` branch, found or created by the gateway in one call
* (`reuse: true`), which also renews it near expiry and answers its keys.
* With an id it is that active branch, with the keys this machine holds.
*/
export declare function ensureDevBranch(opts: {
branches: {
create(projectId: string, opts?: ProjectBranchCreateOptions): Promise<ProjectBranchCreateResult & {
reused?: boolean;
}>;
list(projectId: string): Promise<ProjectBranchesListResult>;
};
/** The keys this machine holds for a project, for a branch named by id. */
keysFor?: (projectId: string) => Promise<{
anon_key?: string;
service_key?: string;
} | null | undefined>;
parentProjectId: string;
branchProjectId?: string;
/** Give the `dev` branch the project's secrets it lacks. Not for a branch named by id. */
copySecrets?: boolean;
}): Promise<DevBranch>;
/** What one dev request runs as. */
export interface DevSessionNow {
signIn: DevSignIn;
/** The gateway's data-plane token for this user: what `db()` forwards. */
actorToken: string;
}
export interface DevSessionSource {
/**
* The session a request runs as. `null`: there is no sign-in left (signed
* out, or its refresh token is over). `{ unavailable }`: the sign-in
* stands, but the gateway gave no data-plane token for it.
*/
current(): Promise<DevSessionNow | {
unavailable: string;
} | null>;
}
/**
* The session file as a source that keeps itself fresh, for the dev server.
* `auth` is `r.auth` of a client holding the project's keys; `now` and `wait`
* are passed in so the refresh and its lock can be tested without a clock.
*/
export declare function createDevSessionSource(opts: {
path: string;
auth: Pick<DevSessionAuth, "refreshSession" | "actorToken">;
now?: () => number;
wait?: (ms: number) => Promise<void>;
}): DevSessionSource;
/**
* The dev server's door: the session file as a self-refreshing source, with a
* client built from the file's own API base and the project's anon key (the
* only credential a user's own auth routes take).
*/
export declare function openDevSession(opts: {
path: string;
anonKey: string;
serviceKey?: string;
}): DevSessionSource;
//# sourceMappingURL=dev-session.d.ts.map