Skip to content

credentials

Class info

Classes

Name Children Inherits
StoreError
pyt3client.credentials
The credentials file cannot be read, or no environment matches.
    T3Credentials
    pyt3client.models.auth
    What a successful sign-in yields; persist it to reconnect without pairing again.
      T3NativeCredentials
      pyt3client.models.auth
      A sign-in for T3 Code's native surface: its WebSocket RPC and REST API.
        _StoreFile
        pyt3client.credentials

          🛈 DocStrings

          Saved sign-ins, one per environment alias (written by pyt3 login).

          The file is $XDG_CONFIG_HOME/pyt3client/credentials.json (falling back to ~/.config/pyt3client/credentials.json), readable by the owner only. Next to the MCP sign-ins (environments) it keeps native sign-ins (native, written by pyt3 login-native) under the same aliases. It holds bearer tokens: anyone who can read it can act on those environments at the approved access level until the token expires (30 days) or is revoked in T3 Code under Settings -> Sharing & access.

          StoreError

          Bases: Exception

          The credentials file cannot be read, or no environment matches.

          Source code in src/pyt3client/credentials.py
          32
          33
          class StoreError(Exception):
              """The credentials file cannot be read, or no environment matches."""
          

          config_dir

          config_dir() -> Path
          

          $XDG_CONFIG_HOME/pyt3client, or ~/.config/pyt3client.

          Source code in src/pyt3client/credentials.py
          36
          37
          38
          39
          def config_dir() -> Path:
              """`$XDG_CONFIG_HOME/pyt3client`, or `~/.config/pyt3client`."""
              base = os.environ.get("XDG_CONFIG_HOME") or str(Path.home() / ".config")
              return Path(base) / "pyt3client"
          

          default_alias

          default_alias(label: str, base_url: str) -> str
          

          An alias from the environment's label, else its host: lower case, no spaces.

          Source code in src/pyt3client/credentials.py
          46
          47
          48
          49
          def default_alias(label: str, base_url: str) -> str:
              """An alias from the environment's label, else its host: lower case, no spaces."""
              alias = re.sub(r"[^a-z0-9._-]+", "-", label.strip().lower()).strip("-")
              return alias or (urlsplit(base_url).hostname or "t3")
          

          load

          load() -> dict[str, T3Credentials]
          

          All saved MCP sign-ins by alias; empty when nothing is saved yet.

          Source code in src/pyt3client/credentials.py
          70
          71
          72
          73
          74
          75
          76
          77
          78
          79
          80
          def load() -> dict[str, T3Credentials]:
              """All saved MCP sign-ins by alias; empty when nothing is saved yet."""
              stored = _read()
              try:
                  return {
                      alias: T3Credentials.model_validate(entry)
                      for alias, entry in stored.environments.items()
                  }
              except ValueError as error:
                  msg = f"{credentials_path()} holds an invalid sign-in: {error}"
                  raise StoreError(msg) from error
          

          load_native

          load_native() -> dict[str, T3NativeCredentials]
          

          All saved native sign-ins by alias.

          Source code in src/pyt3client/credentials.py
          83
          84
          85
          86
          87
          88
          89
          90
          91
          92
          93
          def load_native() -> dict[str, T3NativeCredentials]:
              """All saved native sign-ins by alias."""
              stored = _read()
              try:
                  return {
                      alias: T3NativeCredentials.model_validate(entry)
                      for alias, entry in stored.native.items()
                  }
              except ValueError as error:
                  msg = f"{credentials_path()} holds an invalid native sign-in: {error}"
                  raise StoreError(msg) from error
          

          save

          save(environments: Mapping[str, T3Credentials]) -> Path
          

          Replace the saved MCP sign-ins (native ones stay); the file has mode 0600.

          Source code in src/pyt3client/credentials.py
          109
          110
          111
          112
          113
          114
          def save(environments: Mapping[str, T3Credentials]) -> Path:
              """Replace the saved MCP sign-ins (native ones stay); the file has mode 0600."""
              stored = _read()
              return _write(
                  _StoreFile(version=STORE_VERSION, environments=_entries(environments), native=stored.native)
              )
          

          save_native

          save_native(native: Mapping[str, T3NativeCredentials]) -> Path
          

          Replace the saved native sign-ins (MCP ones stay).

          Source code in src/pyt3client/credentials.py
          117
          118
          119
          120
          121
          122
          def save_native(native: Mapping[str, T3NativeCredentials]) -> Path:
              """Replace the saved native sign-ins (MCP ones stay)."""
              stored = _read()
              return _write(
                  _StoreFile(version=STORE_VERSION, environments=stored.environments, native=_entries(native))
              )
          

          select

          select(environments: Mapping[str, T3Credentials], alias: str | None) -> tuple[str, T3Credentials]
          

          The environment named alias, or the only saved one when alias is None.

          Raises:

          Type Description
          StoreError

          Nothing is saved, the alias is unknown, or several are saved and none was chosen.

          Source code in src/pyt3client/credentials.py
          138
          139
          140
          141
          142
          143
          144
          145
          146
          147
          148
          149
          150
          151
          152
          153
          154
          155
          156
          157
          158
          159
          160
          161
          def select(
              environments: Mapping[str, T3Credentials], alias: str | None
          ) -> tuple[str, T3Credentials]:
              """The environment named `alias`, or the only saved one when `alias` is None.
          
              Raises:
                  StoreError: Nothing is saved, the alias is unknown, or several are saved
                      and none was chosen.
              """
              if not environments:
                  msg = "No environment saved. Run `pyt3 login <url> <pairing-code>` first."
                  raise StoreError(msg)
              if alias is not None:
                  if alias in environments:
                      return alias, environments[alias]
                  msg = f"No saved environment {alias!r}. Saved: {', '.join(sorted(environments))}"
                  raise StoreError(msg)
              if len(environments) == 1:
                  return next(iter(environments.items()))
              msg = (
                  "Several environments are saved; choose one with --env or PYT3_ENV: "
                  f"{', '.join(sorted(environments))}"
              )
              raise StoreError(msg)