TypeScript Strands Agent to Relational Database
The connection generator wires a TypeScript Strands Agent to a Relational Database project, making a Prisma client available inside your agent factory.
Prerequisites
Section titled “Prerequisites”Before using this generator, ensure you have:
- A
ts#strands-agentproject - A
ts#rdbproject
Run the Generator
Section titled “Run the Generator”- Install the Nx Console VSCode Plugin if you haven't already
- Open the Nx Console in VSCode
- Click
Generate (UI)in the "Common Nx Commands" section - Search for
@aws/nx-plugin - connection - Fill in the required parameters
- Click
Generate
pnpm nx g @aws/nx-plugin:connectionyarn nx g @aws/nx-plugin:connectionnpx nx g @aws/nx-plugin:connectionbunx nx g @aws/nx-plugin:connectionYou can also perform a dry-run to see what files would be changed
pnpm nx g @aws/nx-plugin:connection --dry-runyarn nx g @aws/nx-plugin:connection --dry-runnpx nx g @aws/nx-plugin:connection --dry-runbunx nx g @aws/nx-plugin:connection --dry-runSelect your Strands Agent project as the source and your relational database project as the target. If the project contains multiple agent components, specify sourceComponent to disambiguate.
Options
Section titled “Options”| Parameter | Type | Default | Description |
|---|---|---|---|
| sourceProject Required | string | - | The source project |
| targetProject Required | string | - | The target project to connect to |
| sourceComponent | string | - | The source component to connect from (component name, path relative to source project root, or generator id). Use '.' to explicitly select the project as the source. |
| targetComponent | string | - | The target component to connect to (component name, path relative to target project root, or generator id). Use '.' to explicitly select the project as the target. |
Generator Output
Section titled “Generator Output”The generator modifies a single file:
Directorypackages/my-service/src/my-agent
- agent.ts Prisma client fetched inside
getAgentand available to tools
- agent.ts Prisma client fetched inside
Additionally, the agent’s <agent-name>-serve-local target is updated to depend on the database’s serve-local target.
How It Works
Section titled “How It Works”The Prisma client is instantiated inside getAgent(). Since the ts#strands-agent generator configures a single Agent per session, the client is also reused for the lifetime of the session.
Agent Definition
Section titled “Agent Definition”getAgent is updated to import and call the Prisma getter at the top of its body:
import { getPrisma as getMyDb } from ':my-scope/my-db';
export const getAgent = async () => { const myDb = await getMyDb(); // ... return new Agent({ /* use myDb in tools */ });};Multiple Databases
Section titled “Multiple Databases”Running the generator again with a different target adds the second database alongside the first:
import { getPrisma as getMyDb } from ':my-scope/my-db';import { getPrisma as getOtherDb } from ':my-scope/other-db';
export const getAgent = async () => { const myDb = await getMyDb(); const otherDb = await getOtherDb(); // ... return new Agent({ /* use both clients in tools */ });};Infrastructure
Section titled “Infrastructure”The generated agent construct implements IGrantable and IConnectable, so you can grant network and IAM access to the database directly on the construct.
import { MyDatabase } from ':my-scope/common-constructs';
const db = new MyDatabase(this, 'Db', { vpc, ... });const myAgent = new MyAgent(this, 'MyAgent', { vpc, ... });
db.allowDefaultPortFrom(myAgent);db.grantConnect(myAgent);allowDefaultPortFrom opens the security group rule so the agent runtime can reach the database port. grantConnect grants IAM rds-db:connect permission to the agent’s execution role.
Pass the database module outputs into your agent module so it can reach the database and read its runtime configuration:
module "my_database" { source = "../../common/terraform/src/app/dbs/my-database" vpc_id = module.vpc.vpc_id database_subnet_ids = module.vpc.private_isolated_subnet_ids}
module "my_agent" { source = "../../common/terraform/src/app/agents/my-agent"
appconfig_application_id = module.my_database.appconfig_application_id database_cluster_resource_id = module.my_database.cluster_resource_id database_runtime_user = module.my_database.database_runtime_user database_security_group_id = module.my_database.security_group_id database_port = module.my_database.cluster_port}Ensure the agent’s execution role has rds-db:connect permission and that its security group can reach the database security group on the database port.
Local Development
Section titled “Local Development”pnpm nx <agent-name>-serve-local <project-name>yarn nx <agent-name>-serve-local <project-name>npx nx <agent-name>-serve-local <project-name>bunx nx <agent-name>-serve-local <project-name>This starts the agent and all connected databases. The SERVE_LOCAL=true environment variable causes each Prisma client to connect to its local Docker database instead of Aurora.