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¶
Execution context for HAP tools and resources. |
|
Interface for authentication providers. |
|
Interface for resource providers. |
|
Log levels for context messages. |
|
Progress reporting state. |
|
Simple auth provider for testing. |
|
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:
resource_provider (Optional[IResourceProvider])
auth_provider (Optional[IAuthProvider])
log_handler (Optional[Callable[[str, LogLevel, dict], None]])
progress_handler (Optional[Callable[[int, int, str, ProgressState], None]])
- create_child_context(**overrides)¶
Create a child context with optional overrides.
- Return type:
- end_nested_progress()¶
End the current nested progress operation.
- 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.
- 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
- start_nested_progress(total, description='')¶
Start a nested progress operation.
- async update_nested_progress(current, message='')¶
Update the current nested progress.
- class hap.context.IAuthProvider¶
Bases:
Protocol
Interface for authentication providers.
- class hap.context.IResourceProvider¶
Bases:
Protocol
Interface for resource providers.
- class hap.context.LogLevel¶
-
Log levels for context messages.
Initialize self. See help(type(self)) for accurate signature.
- class hap.context.SimpleAuthProvider(user=None, scopes=None)¶
Simple auth provider for testing.