naqsha.tools¶
Decorator-Driven API and tool execution
naqsha.tools ¶
Tool contracts and starter tool implementations.
FunctionTool ¶
Small adapter for simple Python callables.
Source code in src/naqsha/tools/base.py
Tool ¶
Bases: Protocol
Source code in src/naqsha/tools/base.py
ToolObservation
dataclass
¶
Source code in src/naqsha/tools/base.py
to_dict ¶
from_trace_payload
classmethod
¶
Rebuild a persisted QAOA observation dict into a ToolObservation.
Source code in src/naqsha/tools/base.py
ToolSpec
dataclass
¶
Source code in src/naqsha/tools/base.py
AgentContext
dataclass
¶
Runtime context injected into tools that request it.
This is the stable public API surface for tool authors. Tools can access
runtime state by including a ctx: AgentContext parameter in their signature.
The Core Runtime will automatically inject this context at execution time.
Fields
shared_memory: Memory namespace accessible by all agents in a team (V2). For V2 Dynamic Memory Engine, this is a MemoryScope. For V1 compatibility, this may be a MemoryPort. private_memory: Memory namespace isolated to this specific agent (V2). For V2 Dynamic Memory Engine, this is a MemoryScope. For V1 compatibility, this may be a MemoryPort. span: Current trace span for attribution (V2). workspace_path: Absolute path to the Team Workspace root. agent_id: Unique identifier for the current agent. run_id: Unique identifier for the current run.
Source code in src/naqsha/tools/context.py
RiskTier ¶
Bases: StrEnum
Tool risk classification for Tool Policy enforcement.
Source code in src/naqsha/tools/decorator.py
ToolDefinitionError ¶
agent ¶
ToolExecutor ¶
Executes decorated tools with automatic AgentContext injection.
The executor inspects signatures, injects AgentContext when requested via
type hints, handles sync and async tool functions, and wraps exceptions as
failed ToolObservation values so the runtime can enforce circuit breaker
policy without crashing the agent loop.
Source code in src/naqsha/tools/executor.py
execute ¶
Execute a tool function with automatic context injection.
For async tools, returns a coroutine that must be awaited.
Source code in src/naqsha/tools/executor.py
ToolRegistry ¶
Registry for tools decorated with @agent.tool.
The registry: - Holds references to decorated tool functions - Supports lookup by name - Exports schema lists for Model Adapters - Validates that registered functions are properly decorated
Source code in src/naqsha/tools/registry.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
register ¶
Register a tool function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
A function decorated with @agent.tool |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the function is not decorated or name conflicts |
Source code in src/naqsha/tools/registry.py
get ¶
Look up a tool by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Tool name |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Any] | None
|
The tool function, or None if not found |
has ¶
names ¶
export_schemas ¶
Export tool schemas for Model Adapters.
Returns a list of schema dictionaries suitable for inclusion in model API requests. Each schema includes name, description, and parameters.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of tool schemas |
Source code in src/naqsha/tools/registry.py
get_risk_tier ¶
Get the risk tier for a tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Tool name |
required |
Returns:
| Type | Description |
|---|---|
RiskTier | None
|
The tool's risk tier, or None if not found |
Source code in src/naqsha/tools/registry.py
is_read_only ¶
Check if a tool is read-only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Tool name |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the tool is read-only, False otherwise |
Source code in src/naqsha/tools/registry.py
validate_arguments ¶
Validate a conservative subset of JSON Schema used by starter tools.
Source code in src/naqsha/tools/base.py
decorated_to_function_tool ¶
decorated_to_function_tool(
func: Callable[..., Any],
get_context: Callable[[], AgentContext],
) -> FunctionTool
Wrap a decorated tool function as a FunctionTool with runtime context injection.
Source code in src/naqsha/tools/decorated_adapter.py
tool ¶
tool(
*,
risk_tier: RiskTier = RiskTier.READ_ONLY,
description: str | None = None,
) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Decorator for defining tools using the Decorator-Driven API.
Usage
@agent.tool(risk_tier=RiskTier.WRITE, description="Write a file") def write_file(path: str, content: str, ctx: AgentContext) -> str: '''Write content to a file.''' # Implementation return "File written"
The decorator:
1. Generates a JSON Schema from the function's type hints and docstring
2. Stores the schema on the function as __tool_schema__
3. Marks the function as a tool with __is_naqsha_tool__ = True
4. Stores the risk tier as __tool_risk_tier__
5. Stores whether the tool is read-only as __tool_read_only__
Parameters with type hint AgentContext are automatically injected at runtime
and omitted from the public schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
risk_tier
|
RiskTier
|
Tool risk classification (READ_ONLY, WRITE, HIGH) |
READ_ONLY
|
description
|
str | None
|
Optional description (defaults to first line of docstring) |
None
|
Raises:
| Type | Description |
|---|---|
ToolDefinitionError
|
If the function signature is malformed |