hap.context

Context system for HAP, providing execution context for tools and resources.

Similar to MCP’s context, this allows tools to: - Log messages at different levels - Report progress - Access resources - Get authentication info

Classes

HAPContext

Execution context for HAP tools and resources.

IAuthProvider

Interface for authentication providers.

IResourceProvider

Interface for resource providers.

LogLevel

Log levels for context messages.

ProgressState

Progress reporting state.

SimpleAuthProvider

Simple auth provider for testing.

SimpleResourceProvider

Simple in-memory resource provider for testing.

Module Contents

class hap.context.HAPContext(request_id, resource_provider=None, auth_provider=None, log_handler=None, progress_handler=None)

Execution context for HAP tools and resources.

Provides utilities for logging, progress reporting, resource access, and authentication info during execution.

Parameters:
create_child_context(**overrides)

Create a child context with optional overrides.

Return type:

HAPContext

async debug(message, **kwargs)

Log debug message.

Parameters:

message (str)

end_nested_progress()

End the current nested progress operation.

async error(message, **kwargs)

Log error message.

Parameters:

message (str)

get_auth_scopes()

Get the current user’s scopes.

Return type:

List[str]

get_auth_user()

Get the current authenticated user.

Return type:

Optional[str]

get_data(key, default=None)

Retrieve data from the context.

Parameters:
  • key (str)

  • default (Any)

Return type:

Any

has_scope(scope)

Check if the current user has a specific scope.

Parameters:

scope (str)

Return type:

bool

async info(message, **kwargs)

Log info message.

Parameters:

message (str)

async read_resource(uri)

Read a resource by URI.

Parameters:

uri (str) – Resource URI (e.g., “agent://my-agent/state”)

Returns:

Resource content

Raises:
  • ResourceNotFoundError – If resource doesn’t exist

  • PermissionError – If access is denied

Return type:

Any

async report_progress(current, total, message='', state=None)

Report progress for the current operation.

Parameters:
  • current (int) – Current progress value

  • total (int) – Total expected value

  • message (str) – Optional progress message

  • state (Optional[ProgressState]) – Optional progress state

set_data(key, value)

Store data in the context.

Parameters:
  • key (str)

  • value (Any)

start_nested_progress(total, description='')

Start a nested progress operation.

Parameters:
  • total (int)

  • description (str)

update_data(data)

Update context data with a dictionary.

Parameters:

data (dict)

async update_nested_progress(current, message='')

Update the current nested progress.

Parameters:
async warning(message, **kwargs)

Log warning message.

Parameters:

message (str)

async with_progress(operation, total, description='Processing')

Execute an operation with automatic progress tracking.

Parameters:
  • operation (Callable) – Async callable that accepts (current, context) args

  • total (int) – Total number of items

  • description (str) – Progress description

class hap.context.IAuthProvider

Bases: Protocol

Interface for authentication providers.

get_current_user()

Get the current authenticated user.

Return type:

Optional[str]

get_user_scopes()

Get the current user’s scopes.

Return type:

List[str]

class hap.context.IResourceProvider

Bases: Protocol

Interface for resource providers.

async read_resource(uri)

Read a resource by URI.

Parameters:

uri (str)

Return type:

Any

class hap.context.LogLevel

Bases: str, enum.Enum

Log levels for context messages.

Initialize self. See help(type(self)) for accurate signature.

class hap.context.ProgressState(*args, **kwds)

Bases: enum.Enum

Progress reporting state.

class hap.context.SimpleAuthProvider(user=None, scopes=None)

Simple auth provider for testing.

Parameters:
  • user (Optional[str])

  • scopes (Optional[List[str]])

get_current_user()

Get current user.

Return type:

Optional[str]

get_user_scopes()

Get user scopes.

Return type:

List[str]

class hap.context.SimpleResourceProvider

Simple in-memory resource provider for testing.

add_resource(uri, content)

Add a resource.

Parameters:
  • uri (str)

  • content (Any)

async read_resource(uri)

Read a resource.

Parameters:

uri (str)

Return type:

Any