Hosted payment connector
Pay Connect is the hosted service that links a person, an MCP host, or a terminal to a payment wallet. It joins Privy sign-in, wallet authorization, tenant policy, hosted MCP access, funding, and CLI setup so the payment runtime can act for the correct wallet without giving the caller direct custody of its credentials. It is the hosted companion to the Rust payment runtime.
Service role
Pay Connect is the trust boundary between browser users, Privy, MCP hosts, hosted web pages, and the local pay CLI. It keeps the identity-to-wallet link and admits only requests that can use that link. The service keeps pending flows and tenant records in memory, so a restart ends active flows and removes its hosted tenant state.
System boundaries
The service coordinates several separate systems rather than replacing them:
- The pages app presents the
/connectand funding screens. It sends its API requests to Pay Connect. - Privy authenticates users and holds their embedded Solana wallets. Pay Connect uses server-side operator credentials to prepare those wallets for payment signing.
- The hosted MCP endpoint exposes the same payment capability as the local MCP interface, but with a tenant selected from the request token. See MCP payment tools.
- The
payCLI uses a browser round trip to link a local account to a hosted wallet. See payment CLI.
This boundary keeps browser identity and hosted wallet policy separate from local files, keychains, and the payment calls handled by the runtime.
References: rust/…/lib.rs• L1-13 rust/…/lib.rs• L42-67 rust/…/lib.rs• L292-350
Tenant identity
A tenant is the service record that binds an opaque subject to one remote wallet, its provider credentials, and a spending policy. Pay Connect creates this record after identity approval, then passes it into the payment runtime for each hosted call.
Privy wallet identity
Privy supplies the browser identity for hosted users. Pay Connect verifies the Privy access token for the configured app, uses the stable Privy user identity as the tenant subject, and finds the user's first embedded Solana wallet. If the user has no such wallet, the service asks Privy to create one for that user.
The tenant uses the Privy wallet address and provider identity that Pay Connect needs for later signing. The service does not make the user's Privy secret a browser credential.
References: rust/…/privy.rs• L1-23 rust/…/privy.rs• L296-321 rust/…/privy.rs• L361-443 rust/…/oauth.rs• L996-1071
Wallet authorization
Wallet creation adds Pay's configured signer to the Privy wallet and can attach a Privy policy. An existing wallet must already permit that signer. If it does not, approval stops and the user must add the signer before the wallet can act for payment tools.
After authorization, the tenant also receives the hosted spend policy. The payment runtime applies both the provider's wallet rules and Pay Connect's policy before it signs a payment. See remote wallet providers for the runtime-side provider contract.
References: rust/…/privy.rs• L379-415 rust/…/privy.rs• L417-441 rust/…/oauth.rs• L1052-1068 rust/…/tenants.rs• L26-34 rust/…/tenants.rs• L544-569
Tenant reuse
Pay Connect derives a stable opaque subject from the provider identity. A signed browser cookie points to that subject, so one user can return from another browser flow and keep the same tenant wallet. The cookie is only a shortcut; the tenant registry remains the source of the wallet and policy binding.
Guests can receive a wallet later without changing the host connection. A one-time link ticket binds the guest subject to a signed-in Privy wallet after the browser confirms the attachment. The tenant record is read-only from the payment tool view, so tools cannot rewrite the hosted account.
References: rust/…/tenants.rs• L48-56 rust/…/tenants.rs• L223-256 rust/…/tenants.rs• L258-325 rust/…/tenants.rs• L404-438 rust/…/oauth.rs• L1291-1389
Hosted MCP access
Pay Connect serves payment tools at /mcp through streamable HTTP. An MCP host can use OAuth when it can open a browser, or a configured static bearer token when it can only send a fixed header. In both cases, authentication produces a tenant identity before the payment runtime handles the tool call.
OAuth consent
The OAuth server supports one scope, mcp, for the /mcp resource. A host discovers the service from the protected-resource response, registers as a public client, and starts an authorization-code flow with PKCE. Pay Connect validates the client, redirect URI, resource, and PKCE request before it sends the browser to the consent page.
The consent page can approve with a remembered browser wallet, a Privy wallet, or a wallet-less guest subject. A new or empty Privy wallet can pause approval for funding and continue the same pending request after the user finishes or skips that step. Access tokens identify the tenant but do not carry wallet credentials.
This diagram shows how a host request becomes an authenticated tenant session.
sequenceDiagram
participant Host as MCP host
participant Connect as Pay Connect
participant Browser
participant Privy
participant Runtime as Payment runtime
Host->>Connect: Request /mcp without a valid bearer
Connect-->>Host: 401 with resource metadata
Host->>Connect: Register and start OAuth with PKCE
Connect->>Browser: Redirect to consent page
Browser->>Privy: Sign in when needed
Privy-->>Browser: Privy access token
Browser->>Connect: Approve wallet or guest subject
Connect-->>Host: One-time code at registered callback
Host->>Connect: Exchange code and verifier
Connect-->>Host: Access and refresh tokens
Host->>Connect: Call /mcp with access token
Connect->>Runtime: Attach tenant wallet and policy
Runtime-->>Host: Payment tool result
References: rust/…/oauth.rs• L30-40 rust/…/oauth.rs• L547-581 rust/…/oauth.rs• L672-758 rust/…/oauth.rs• L889-984 rust/…/oauth.rs• L1164-1265
MCP request scope
The MCP gateway accepts a live OAuth access token or a matching static token. It rejects requests without a valid bearer and checks the request host against an allowlist to reduce DNS-rebinding risk. The accepted tenant is attached to the request, and the hosted context turns it into one mainnet remote account with its spend policy.
Known hosts have profiles for redirect and client-name checks. HTTPS redirects are allowed, loopback HTTP is allowed for local clients, and unknown custom schemes are rejected. This keeps host-specific callback rules in the OAuth boundary instead of in the payment tools.
References: rust/…/mcp.rs• L46-58 rust/…/mcp.rs• L152-214 rust/…/mcp.rs• L216-277 rust/…/hosts.rs• L86-187 rust/…/hosts.rs• L197-232 rust/…/tenants.rs• L544-570
CLI onboarding
CLI onboarding links a terminal to a Pay Connect wallet through a browser. The CLI owns a loopback callback and the PKCE verifier, while Pay Connect owns browser consent and the one-time exchange. The local account is written only after the CLI receives a ready wallet and verifies its address through the selected remote provider.
Browser handshake
The CLI first checks that Pay Connect is reachable. It then binds an ephemeral listener on 127.0.0.1, creates a state value and PKCE pair, and opens the /v1/cli link in the browser. Pay Connect validates the callback and challenge, stores a short-lived link, and sends the browser to the shared /connect page. The callback accepts a code only when its state matches the attempt.
The CLI sends the code and verifier to /v1/cli/complete. Pay Connect consumes the one-time grant and returns a tenant-scoped Pay Connect token. The token gives the CLI access to the hosted wallet without exposing Privy operator credentials or browser wallet secrets.
This diagram shows the terminal, browser, Pay Connect, and local account boundary.
sequenceDiagram
participant CLI as pay CLI
participant Browser
participant Connect as Pay Connect
participant Privy
participant Local as Local account store
CLI->>Connect: Check /health
CLI->>CLI: Bind loopback callback and create PKCE pair
CLI->>Browser: Open /v1/cli with callback, state, and challenge
Browser->>Connect: Open shared consent page
Browser->>Privy: Sign in and authorize wallet when needed
Privy-->>Connect: Wallet identity and signing status
Connect-->>Browser: Redirect with one-time code
Browser->>CLI: Return code to loopback callback
CLI->>Connect: Exchange code with verifier
Connect-->>CLI: Ready wallet identity and scoped token
CLI->>CLI: Verify wallet address through provider
CLI->>Local: Store credentials and remote account
References: rust/…/connect_onboard.rs• L35-105 rust/…/connect_onboard.rs• L296-328 rust/…/connect_onboard.rs• L330-360 rust/…/cli.rs• L213-240 rust/…/cli.rs• L529-553
Wallet handoff
Only a ready exchange result can create a local remote account. The result identifies the payconnect provider, the mainnet wallet, its address, and an API token scoped to that tenant. The CLI checks those fields and the provider's credential requirements, then asks the provider to resolve the wallet address before it saves credentials and the account.
A pending, incomplete, unsupported, or mismatched result leaves the local account unchanged. This handoff lets the hosted service provision the identity while the CLI keeps the local account contract consistent with the runtime's remote provider support.
References: rust/…/connect_onboard.rs• L89-208 rust/…/connect_onboard.rs• L239-270 rust/…/cli.rs• L518-553
Funding path
Funding gives a tenant a browser path to add payment funds before a paying call. Pay Connect owns the redirect context and the identity of the wallet to fund, while the hosted pages and funding service own the card flow and its payment APIs.
Hosted funding
The /fund route redirects to the pages app's funding screen. The CLI can add the wallet address, account name, callback, payment method, and state to the /cli URL. After the hosted funding flow finishes, Pay Connect checks that the wallet has funds before it returns a payment ID and optional transaction signature to the CLI. The CLI checks the callback state before it accepts that result.
When a newly signed-in Privy wallet has no known stablecoin balance, the OAuth or CLI link stays pending and the consent page sends the user to funding first. A balance lookup that cannot confirm an empty wallet does not add this detour, so a balance service outage does not block approval. A direct CLI top-up does not need Privy sign-in because the wallet address is already supplied by the terminal.
This diagram shows the two funding entry points and the shared settlement check.
flowchart TD
A[New Privy wallet] --> B{Balance known to be empty?}
B -- No --> C[Approve wallet or CLI link]
B -- Yes --> D[Open hosted funding page]
E[CLI top-up with wallet address] --> D
D --> F[Payment settles]
F --> G{Wallet has funds?}
G -- No --> H[Keep request pending]
G -- Yes --> I[Return payment result]
I --> C
References: rust/…/lib.rs• L69-125 rust/…/lib.rs• L292-300 rust/…/cli.rs• L242-292 rust/…/cli.rs• L353-471 rust/…/connect_onboard.rs• L651-786
Operating model
Pay Connect runs as a separately reachable service with the pages app as its browser front end. Its public URL is used for OAuth issuer and resource metadata, while the pages URL is used for consent and funding navigation. MCP, Privy, and local development behavior are optional service capabilities.
The main service routes are:
| Route family | Purpose |
|---|---|
/v1/cli and /v1/cli/complete | Start browser linking and exchange the one-time CLI code for a tenant-scoped token. |
/v1/cli with DELETE | Revoke a previously issued CLI token. |
/oauth/* and /.well-known/* | Discover and run hosted OAuth authorization. |
/mcp | Serve payment tools to an authenticated MCP host. |
/connect, /authorize, /fund, and /cli | Redirect browser navigation to the pages app or start a direct CLI top-up. |
/v1/wallets/* | Let a scoped CLI token reach its hosted wallet. |
The service uses bounded in-memory registries for tenants, OAuth records, CLI links, pending requests, and token hashes. This is suitable for a bounded service process but means those flows do not survive a restart. CLI links expire after minutes, one-time grants expire after a short exchange window, and issued CLI tokens have a longer limited lifetime. The runtime remote wallet page explains how the downstream runtime uses the resulting wallet identity.
References: rust/…/lib.rs• L40-67 rust/…/lib.rs• L292-395 rust/…/cli.rs• L30-33 rust/…/cli.rs• L65-197
Change checklist
When these files or sections are changed, remember to consider these:
- A Privy change must keep token verification, user identity, wallet lookup or creation, signer authorization, policy choice, and the
pay_corePrivy credential fields aligned. - A tenant identity change must preserve the mapping from OAuth subject or static-token fingerprint to wallet, account, and spend policy. It must also preserve signed browser-cookie reuse and guest linking.
- An OAuth change must keep resource metadata, issuer metadata, host redirect rules, PKCE checks, token records, and MCP bearer validation in agreement.
- A CLI flow change must keep the loopback callback state, browser URL parameters, one-time exchange, ready-result schema, tenant-scoped token, and remote-provider address check aligned.
- A CLI token change must keep token issuance, wallet API authentication, expiry, eviction, and revocation behavior consistent.
- A pages URL or public URL change must preserve consent redirects, OAuth discovery URLs, cookie security, funding redirects, and MCP allowed hosts.
- A funding change must keep the wallet address and callback state attached to the hosted page, then return a settled payment result in the shape expected by the CLI and consent flow.
- A balance-probe change must preserve the different failure policies: an unknown balance must not force a funding detour, but an unknown settlement status must not complete a top-up.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Pay Connect restarts | Pending browser flows, OAuth tokens, CLI links, token hashes, and in-memory tenant records disappear. | Start the flow again; persistent hosted state is a separate service concern. |
| Privy token has a wrong signature, issuer, audience, or expiry | The browser cannot approve a tenant. | Verify the token against the configured app and signing keys before using its subject. |
| Existing Privy wallet lacks Pay's signer | Approval stops before payment access is granted. | Add the configured signer and approve again. |
| Two requests provision the same user at once | A duplicate wallet or inconsistent tenant binding could result. | Claim provisioning per stable subject before contacting Privy. |
| MCP request lacks a valid bearer or uses an unapproved host | The host receives an authorization or transport error. | Complete OAuth or configure a static token, and keep the public host in the allowlist. |
| OAuth callback state or PKCE verifier does not match | The code or token exchange fails. | Use the per-attempt loopback state and verifier; both the CLI and service validate them. |
| CLI health check cannot reach the selected Pay Connect URL | The CLI stops before it opens a browser or waits for a callback. | Start the local service when using local mode, or check PAY_CONNECT_URL; use PAY_CONNECT_LOCAL=1 for the default local URL. |
| CLI receives a non-ready or incomplete result | No local credentials or account are saved. | Require status: ready, validate provider fields and credentials, and resolve the wallet address before saving. |
| CLI exchanges a code with the wrong verifier or reuses a code | Pay Connect rejects the grant. | Keep the per-attempt verifier private and consume the one-time code only once. |
| A CLI token is revoked or expired | Hosted wallet API calls become unauthorized. | Obtain a new onboarding result and store its new tenant-scoped token. |
| Funding callback has missing or wrong state | The CLI rejects the payment result and cannot continue the wait. | Keep the callback on loopback and bind it to the funding attempt's state. |
| Funding reports payment before the wallet has funds | The top-up request could complete before settlement. | Check the wallet balance before consuming the pending top-up. |
| Balance lookup fails while checking for an empty wallet | The user does not get an unnecessary funding detour. | Treat an unknown balance as funded and allow approval to continue. |
Key files
| File | Why to read it |
|---|---|
| rust/…/lib.rs | Defines Pay Connect state, service boundaries, page redirects, wallet probing, and route groups. |
| rust/…/cli.rs | Defines CLI link and direct top-up lifecycles, one-time grants, tenant-scoped tokens, wallet access, and revocation. |
| rust/…/main.rs | Shows how the service URL, pages app, MCP, and Privy capabilities are started. |
| rust/…/oauth.rs | Explains OAuth discovery, consent, PKCE, token exchange, Privy approval, funding pause, and guest linking. |
| rust/…/privy.rs | Defines Privy identity verification, wallet selection or creation, signer checks, and hosted credentials. |
| rust/…/tenants.rs | Connects subjects to remote wallets, browser cookies, spend policy, and payment-tool scope. |
| rust/…/mcp.rs | Defines hosted MCP bearer authentication, tenant admission, and host checks. |
| rust/…/hosts.rs | Describes known MCP host callback profiles and redirect safety rules. |
| rust/…/connect_onboard.rs | Shows the health check, browser handshake, loopback callback, funding return, provider verification, token handoff, and local account registration. |
| docs/rust-payment-runtime.md | Provides the parent view of the runtime that executes calls after Pay Connect selects a tenant wallet. |
| docs/runtime-remote-wallet-providers.md | Explains the remote provider contract that hosted and local wallet identities must satisfy. |
| docs/payment-cli.md | Explains how the CLI exposes onboarding and uses the resulting local account. |
| docs/mcp-payment-tools.md | Explains the payment tools that hosted MCP access exposes after tenant authentication. |