yera.tools.creds.context

Credential context for tool invocations.

When a @tool-decorated function runs, its credentials are resolved and bound to a ContextVar before the function body executes. The tool body reads them back via tool_creds.

Symbols

def clear_tool_creds_context — Clear the current tool credential snapshot (tests and explicit teardown).
def reset_tool_creds_context — Restore the previously bound tool credential snapshot.
def set_tool_creds_context — Store the credential snapshot for the currently executing tool.
def tool_creds — Return the active tool's credentials.

clear_tool_creds_context

clear_tool_creds_context() → None

Clear the current tool credential snapshot (tests and explicit teardown).

This sets the context to None without using a prior token. Prefer :func:reset_tool_creds_context when unwinding a paired :func:set_tool_creds_context.

reset_tool_creds_context

reset_tool_creds_context(
    token: Token,
) → None

Restore the previously bound tool credential snapshot.

Parameters

token
type: Token

Context token returned by set_tool_creds_context.

set_tool_creds_context

set_tool_creds_context(
    creds: ToolCreds,
) → Token

Store the credential snapshot for the currently executing tool.

Parameters

creds
type: ToolCreds

Credential snapshot to expose to the current tool invocation.

Returns

type: Token

A ContextVar token for use with :func:reset_tool_creds_context so nested tool invocations can restore the previous snapshot.

tool_creds

tool_creds() → ToolCreds

Return the active tool's credentials.

Call from inside a @tool-decorated function to read the credentials the tool declared with creds=.... The returned ToolCreds is a read-only snapshot of the configured credential groups, flattened to dotted keys.

Examples

Read a credential declared by the currently executing tool:

python
import yera as yr

@yr.tool(creds="weather")
def weather_headers() -> dict[str, str]:
    api_key = yr.tool_creds().require("weather.api_key")
    return {"Authorization": f"Bearer {api_key}"}

Returns

type: ToolCreds

The active tool's credential snapshot.

Raises

RuntimeError

When called outside an active tool invocation.