# Connections, remotes and identity

CLI, SDK and MCP share private client configuration. A **connection** names a
service endpoint and its private credential reference. A **remote** remembers a
route to one stable ledger ID. Neither operation enrolls a ledger, grants access,
synchronizes records or transfers authority.

## Ordinary local-to-shared use

Start locally, then run `op-sys ledgers publish --endpoint <service-url>` when
ready for collaboration. It composes authentication, organization selection,
synchronization and protected authority transfer, remembers `origin`, and retains
the local ledger. Future publishing reuses the saved destination. An authorized
participant can run `op-sys ledgers clone "<ledger-url>"`; the URL supplies the
service, ledger and organization, never credentials. Both flows keep local work
selected and expose structured setup/progress when input is needed.

Connections and remotes are advanced controls, not mandatory setup chores.
Use the explicit commands below to choose between accounts, edit destinations or
work directly against a service without a local replica.

## Connect to a service

`auth login --endpoint <endpoint>` creates and selects a named connection with its
own private credential store. Subsequent setup/login calls reuse that selection.
A published managed endpoint can supply the default when available. Setup never
carries a local ledger selection into a newly selected service or uploads records.
To use several services, create and select their connections explicitly. Changing
the endpoint of an already selected connection requires `connections update` and
fresh authentication; passing a different URL to login does not redirect credentials.
Account operations, including billing and deletion, use the selected connection.

The same explicit steps apply to managed and customer-hosted endpoints:

```sh
op-sys connections create --name work --endpoint https://records.example.com
op-sys connections select --name work
op-sys auth login
op-sys workspaces list
op-sys selection update --workspace <workspace-id>
op-sys selection get
```

Browser sign-in uses the existing service authentication flow. `selection update --workspace` verifies
workspace access and records the verified identity. Ledger grants are managed separately. Manual association remains available for explicit credential
configuration and identity inspection.
For machine credentials, use the `auth login --credential-env NAME --credential-scope <scope>` or a private
credential file against the selected connection. Login verifies and associates
that identity automatically. Tokens and keys never appear in inspection results.
Each new connection has an isolated private credential store; `credential_ref` is
an opaque local reference, not a token or a path to supply yourself.

Use `--connection work` before a CLI command for an invocation-specific override.
Names are exact. When a user and project both define `work`, use `user:work` or
`project:work`. Explicit settings win over project defaults, then user defaults.
An explicitly supplied state root is isolated; an SDK caller can opt into another
user context with `user_config_root`. Automatically discovered project contexts
also consult the configured user context.

```python
from op_sys import OpSys

client = OpSys(connection="work", ledger="ledger_...")
client.connections.list()
client.selection.get()
client.identity.get()
```

MCP exposes the same structured capabilities, such as `op_sys_connections_create`,
`op_sys_remotes_select`, `op_sys_identity_associate` and
`op_sys_selection_update`. Discover their input/output schemas with
`op-sys tools list`. No MCP-specific credential setup is required.

## Choose a ledger and its remote

```sh
op-sys remotes create --ledger-id <ledger-id> --name origin \
  --connection work --remote-ledger-id <same-ledger-id>
op-sys remotes select --ledger-id <ledger-id> --name origin
op-sys remotes list --ledger-id <ledger-id>
```

A remote retains the **same** ledger identity at both ends. Adding it does not
create a second ledger. Unknown local names fail; an explicit hosted ledger ID is
resolved and authorized by the service when used. Conflicting connection/remote
selection fails instead of silently switching destinations. Local names are
resolved to stable IDs before being pinned.

`selection update --connection local --ledger <id-or-local-name>` explicitly
chooses local use. `selection update` without options reloads current defaults.
Reconfiguration updates this client and saves defaults for future clients. Existing
SDK/MCP clients retain their initialized endpoint, identity and selected ledger
until explicitly reconfigured. Changing defaults elsewhere does not redirect them.
Each explicit record-operation ledger argument overrides the selected ledger.

Changing or removing a remote leaves accepted records, provenance and authority
untouched. Connections and remotes are configured explicitly; old singleton settings,
credential files and database mappings are neither imported nor rewritten.

## Identity and credential changes

Local participant identity is persisted privately without an account. First identity
inspection provisions local state if needed; it performs no network request. The
local identity and hosted identity remain distinct. An association never rewrites
historical actor labels or operation identities.

A session may renew for the same verified subject. A changed account or tenant
requires explicit client reconfiguration. Associated machine credentials may rotate
only to the same verified service principal. Unassociated machine keys
remain bound to their captured credential fingerprint until identity association.
A changed endpoint gets a fresh credential reference and requires authentication;
saved credentials are never forwarded to the edited endpoint. Existing clients
retain their old endpoint. To revoke a session, use `auth logout` before removing
its connection; removing a configuration name does not revoke server credentials.

Configuration generations retain private permissions, locking, revision conflict
checks and recovery from incomplete writes. Client errors expose a stable core
code, operation, retry classification and recovery guidance. SDK errors retain
`OpSysError` and provide these fields through `core_error`.

## Direct HTTP integrations

An application does not need the CLI, SDK, MCP or a local configuration file. Supply
its chosen service URL and authentication explicitly. For example, the existing
hosted principal endpoint accepts an authenticated request:

```sh
curl --fail-with-body "$VEYLAR_ENDPOINT/v1/identity?tenant_id=$TENANT_ID" \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

For machine keys,
use a credential with the operation's required service scope. A browser access
token also needs the authorized organization context. Handle refresh, retries and
errors in the calling application, or use the supplied clients for convenience.
The hosted service never reads or manages another device's named connections.

See [how ledgers work](/docs/raw/manuals/ledger-core.md) for create/append/read and independent-participant
permissions through the selected connection.
