tools.aws.helpers module

Generic boto3 invoke helper for tools/aws handlers.

async tools.aws.helpers.aws_method(ctx, service, method, *, region=None, request_json=None)[source]

Invoke an arbitrary boto3 client(service).method(**kwargs) call.

Generic escape hatch that lets any AWS tool reach an arbitrary boto3 operation: it resolves the caller’s credentials, decodes the optional request_json payload into keyword arguments, runs the blocking boto3 call off the event loop, and serialises the result. This is why every per-service JSON tool (ec2, s3, lambda, iam, etc.) can be defined as a thin wrapper rather than hand-coding each API method.

Resolves a boto3 Session via tools.aws.base._get_session() (which reads the user’s stored AWS key from Redis or falls back to AWS_* env vars), parses request_json with jsonutil.loads(), and executes the nested _run closure inside asyncio.to_thread() so the network call never stalls the loop. Results are serialised with tools.aws.base._dumps() and failures wrapped by tools.aws.base._err(); missing credentials surface tools.manage_api_keys.missing_api_key_error(). Any AWS-side mutation depends entirely on which method is requested – it has no Redis, event-bus, KG, or LLM interaction of its own.

Called directly by the per-service sts handlers in tools.aws.sts and by the _handler closure that wrap_aws_method() returns for every other AWS service module; also exercised directly in tests/test_aws_tools.py.

Parameters:
  • ctx – The tool ToolContext (or None) used to resolve AWS credentials.

  • service (str) – The boto3 service name to build a client for (e.g. ec2).

  • method (str) – The client method to invoke on that service (e.g. describe_instances).

  • region (str | None) – Optional AWS region name; the session default is used when None.

  • request_json (str | None) – Optional JSON string decoded into the keyword arguments passed to the boto3 method.

Returns:

The JSON-serialized boto3 response on success, or a {"error": ...} JSON string when credentials are missing, the JSON is invalid, or the API call raises.

Return type:

str

tools.aws.helpers.wrap_aws_method(service, method)[source]

Build an async tool handler bound to one boto3 service and method.

Factory that captures a service/method pair and returns an async _handler closure suitable for use as the handler of an AWS tool definition. This is what lets each tools/aws/* module declare dozens of JSON-driven operations declaratively (a list of (name, method, description) tuples) instead of writing a coroutine per API call; the returned closure simply forwards to aws_method() with the captured names.

Pure closure construction with no side effects of its own – it touches no Redis, event bus, KG, or LLM; all credential resolution, JSON parsing, the threaded boto3 call, and result/error serialisation happen later inside aws_method() when the returned handler runs.

Called by the per-service tool builders across the tools/aws/* modules (ec2, s3, lambda, iam, rds, ecs, route53, cloudwatch, sqs, sns, ecr, elbv2, dynamodb, logs, secretsmanager) to populate their tool lists – e.g. wrap_aws_method("ec2", meth) inside tools.aws.ec2._ec2_json_tools().

Parameters:
  • service (str) – The boto3 service name the produced handler will target (e.g. ec2).

  • method (str) – The client method the produced handler will invoke (e.g. run_instances).

Returns:

An async _handler(request_json=None, region=None, ctx=None) -> str coroutine function that delegates to aws_method().