Skip to Content
px402Devnet
SDK

Hosted client

Call the MCP connector from TypeScript, with no wallet key.

import { hostedLogin, newIdempotencyKey, openHostedClient, DEFAULT_MCP_URL } from "@px402-tech/sdk";
import { homedir } from "node:os";
import { join } from "node:path";

const home = join(homedir(), ".px402");

await hostedLogin({ home, mcpUrl: DEFAULT_MCP_URL, print: console.log }); // once: prints the sign-in URL

const client = openHostedClient({ home });
const agents = await client.call("wallet_list", {});

const idempotencyKey = newIdempotencyKey(); // store it before paying, and reuse it to retry
const result = await client.call("pay", {
  agent: "<agent id or label>",
  url: "https://devnet.x402.px402.tech/paid/demo",
  idempotencyKey,
});

call(tool, args) calls one MCP tool and returns its structured result. It refreshes the sign-in when needed.

FunctionNotes
hostedLogin({ home, mcpUrl, scopes?, clientId?, print, openUrl? })OAuth with PKCE and a 127.0.0.1 redirect. Pass scopes: ["wallet:read"] for read only
openHostedClient({ home })Throws SDK_HOSTED_SIGNED_OUT if there is no sign-in
readHostedSession(home)The stored sign-in, or null. It contains tokens: do not log it
hostedLogout({ home }){ signedIn, revoked }. Deletes the local file either way

A refusal from a tool throws HostedToolError, with toolCode (for example WALLET_CAP_EXCEEDED) and retryable.