@aws/nx-plugin-mcp 1.0.0-rc.4 → 1.0.0-rc.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/aws-nx-mcp.js +2240 -1030
- package/docs/get_started/building-with-ai.mdx +116 -0
- package/docs/get_started/concepts.mdx +52 -0
- package/docs/get_started/existing-project.mdx +176 -0
- package/docs/get_started/quick-start.mdx +266 -0
- package/docs/get_started/tutorials/contribute-generator.mdx +405 -0
- package/docs/get_started/tutorials/dungeon-game/1.mdx +1205 -0
- package/docs/get_started/tutorials/dungeon-game/2.mdx +237 -0
- package/docs/get_started/tutorials/dungeon-game/3.mdx +76 -0
- package/docs/get_started/tutorials/dungeon-game/4.mdx +162 -0
- package/docs/get_started/tutorials/dungeon-game/overview.mdx +144 -0
- package/docs/get_started/tutorials/dungeon-game/wrap-up.mdx +41 -0
- package/docs/get_started/tutorials/existing-project.mdx +4 -0
- package/docs/guides/agentcore-gateway.mdx +376 -0
- package/docs/guides/connection/agentcore-gateway-gateway.mdx +154 -0
- package/docs/guides/connection/agentcore-gateway-mcp.mdx +134 -0
- package/docs/guides/connection/py-agent-a2a.mdx +47 -15
- package/docs/guides/connection/py-agent-dynamodb.mdx +116 -0
- package/docs/guides/connection/py-agent-gateway.mdx +176 -0
- package/docs/guides/connection/py-agent-mcp.mdx +42 -13
- package/docs/guides/connection/py-agent-rdb.mdx +178 -0
- package/docs/guides/connection/py-fast-api-dynamodb.mdx +56 -0
- package/docs/guides/connection/py-fast-api-rdb.mdx +184 -0
- package/docs/guides/connection/py-mcp-server-dynamodb.mdx +116 -0
- package/docs/guides/connection/py-mcp-server-rdb.mdx +187 -0
- package/docs/guides/connection/react-agui.mdx +4 -4
- package/docs/guides/connection/react-fastapi.mdx +38 -2
- package/docs/guides/connection/react-py-agent.mdx +7 -13
- package/docs/guides/connection/react-smithy.mdx +3 -3
- package/docs/guides/connection/react-trpc.mdx +1 -1
- package/docs/guides/connection/react-ts-agent.mdx +8 -8
- package/docs/guides/connection/smithy-dynamodb.mdx +4 -4
- package/docs/guides/connection/smithy-rdb.mdx +5 -5
- package/docs/guides/connection/trpc-dynamodb.mdx +4 -4
- package/docs/guides/connection/trpc-rdb.mdx +5 -5
- package/docs/guides/connection/ts-agent-a2a.mdx +12 -9
- package/docs/guides/connection/ts-agent-dynamodb.mdx +3 -3
- package/docs/guides/connection/ts-agent-gateway.mdx +141 -0
- package/docs/guides/connection/ts-agent-mcp.mdx +11 -8
- package/docs/guides/connection/ts-agent-rdb.mdx +66 -21
- package/docs/guides/connection/ts-mcp-server-dynamodb.mdx +3 -3
- package/docs/guides/connection/ts-mcp-server-rdb.mdx +66 -16
- package/docs/guides/connection.mdx +104 -5
- package/docs/guides/docker-bundling.mdx +68 -8
- package/docs/guides/fastapi.mdx +244 -4
- package/docs/guides/license.mdx +264 -109
- package/docs/guides/local-development.mdx +87 -0
- package/docs/guides/nx-generator.mdx +7 -2
- package/docs/guides/py-agent.mdx +257 -49
- package/docs/guides/py-dynamodb.mdx +476 -0
- package/docs/guides/py-mcp-server.mdx +61 -2
- package/docs/guides/py-rdb.mdx +254 -0
- package/docs/guides/react-website-auth.mdx +58 -1
- package/docs/guides/react-website.mdx +130 -19
- package/docs/guides/security.mdx +75 -0
- package/docs/guides/terraform-project.mdx +1 -1
- package/docs/guides/trpc.mdx +45 -9
- package/docs/guides/ts-agent.mdx +149 -9
- package/docs/guides/ts-dynamodb.mdx +62 -239
- package/docs/guides/ts-mcp-server.mdx +66 -3
- package/docs/guides/ts-nx-plugin.mdx +1 -1
- package/docs/guides/ts-rdb.mdx +117 -470
- package/docs/guides/ts-smithy-api.mdx +183 -4
- package/docs/guides/typescript-infrastructure.mdx +9 -1
- package/docs/guides/typescript-project.mdx +5 -10
- package/docs/guides/workspace.mdx +2 -2
- package/docs/snippets/agent/architecture.mdx +1 -1
- package/docs/snippets/agent/bedrock-deployment.mdx +4 -0
- package/docs/snippets/agent/runtime-arn.mdx +21 -0
- package/docs/snippets/agent/securing-your-agent.mdx +39 -0
- package/docs/snippets/api/access-logging.mdx +33 -0
- package/docs/snippets/api/type-safe-api-integrations.mdx +31 -0
- package/docs/snippets/api/waf-configuration.mdx +1 -1
- package/docs/snippets/connection/dynamodb-local-development.mdx +2 -2
- package/docs/snippets/connection/py-dynamodb-local-development.mdx +7 -0
- package/docs/snippets/connection/py-lambda-rdb-ssl-requirements.mdx +7 -0
- package/docs/snippets/connection/rdb-api-infrastructure.mdx +50 -18
- package/docs/snippets/dynamodb/deploying-table.mdx +166 -0
- package/docs/snippets/dynamodb/gsi-config.mdx +38 -0
- package/docs/snippets/dynamodb/infrastructure.mdx +33 -0
- package/docs/snippets/dynamodb/local-dev-start.mdx +13 -0
- package/docs/snippets/dynamodb/local-dev-windows.mdx +15 -0
- package/docs/snippets/mcp/architecture.mdx +1 -1
- package/docs/snippets/mcp/bedrock-deployment.mdx +4 -0
- package/docs/snippets/mcp/config.mdx +3 -2
- package/docs/snippets/rdb/architecture.mdx +38 -0
- package/docs/snippets/rdb/cluster-instances.mdx +31 -0
- package/docs/snippets/rdb/deletion-protection.mdx +34 -0
- package/docs/snippets/rdb/deploying.mdx +187 -0
- package/docs/snippets/rdb/encryption-key-rotation.mdx +30 -0
- package/docs/snippets/rdb/engine-version.mdx +63 -0
- package/docs/snippets/rdb/infrastructure.mdx +35 -0
- package/docs/snippets/rdb/logging.mdx +32 -0
- package/docs/snippets/rdb/rds-proxy.mdx +50 -0
- package/docs/snippets/rdb/removal-policy.mdx +57 -0
- package/docs/snippets/rdb/serverless-capacity.mdx +32 -0
- package/docs/snippets/required-prerequisites.mdx +1 -1
- package/docs/snippets/trivy-image-scan.mdx +27 -0
- package/generators.json +101 -2
- package/package.json +1 -1
- package/src/agentcore-gateway/gateway-connection/schema.json +31 -0
- package/src/agentcore-gateway/mcp-connection/schema.json +31 -0
- package/src/agentcore-gateway/schema.json +70 -0
- package/src/connection/schema.json +5 -0
- package/src/infra/app/schema.json +5 -0
- package/src/init/schema.json +35 -0
- package/src/license/schema.json +11 -0
- package/src/preset/schema.json +11 -5
- package/src/py/agent/a2a-connection/schema.json +5 -0
- package/src/py/agent/gateway-connection/schema.json +31 -0
- package/src/py/agent/mcp-connection/schema.json +5 -0
- package/src/py/agent/react-connection/schema.json +5 -0
- package/src/py/agent/schema.json +6 -1
- package/src/py/api/schema.json +5 -0
- package/src/py/dynamodb/agent-connection/schema.json +27 -0
- package/src/py/dynamodb/fast-api-connection/schema.json +23 -0
- package/src/py/dynamodb/mcp-server-connection/schema.json +27 -0
- package/src/py/dynamodb/schema.json +75 -0
- package/src/py/fast-api/react/schema.json +5 -0
- package/src/py/fast-api/schema.json +5 -0
- package/src/py/lambda-function/schema.json +5 -0
- package/src/py/mcp-server/schema.json +5 -0
- package/src/py/project/schema.json +5 -0
- package/src/py/rdb/agent-connection/schema.json +27 -0
- package/src/py/rdb/fast-api-connection/schema.json +23 -0
- package/src/py/rdb/mcp-server-connection/schema.json +27 -0
- package/src/py/rdb/schema.json +77 -0
- package/src/smithy/project/schema.json +5 -0
- package/src/smithy/react-connection/schema.json +5 -0
- package/src/smithy/ts/api/schema.json +5 -0
- package/src/terraform/project/schema.json +5 -0
- package/src/trpc/backend/schema.json +5 -0
- package/src/trpc/react/schema.json +5 -0
- package/src/ts/agent/a2a-connection/schema.json +5 -0
- package/src/ts/agent/gateway-connection/schema.json +31 -0
- package/src/ts/agent/mcp-connection/schema.json +5 -0
- package/src/ts/agent/react-connection/schema.json +5 -0
- package/src/ts/agent/schema.json +5 -0
- package/src/ts/api/schema.json +5 -0
- package/src/ts/astro-docs/schema.json +3 -3
- package/src/ts/docs/schema.json +3 -3
- package/src/ts/dynamodb/agent-connection/schema.json +5 -0
- package/src/ts/dynamodb/mcp-server-connection/schema.json +5 -0
- package/src/ts/dynamodb/schema.json +25 -2
- package/src/ts/dynamodb/smithy-connection/schema.json +5 -0
- package/src/ts/dynamodb/trpc-connection/schema.json +5 -0
- package/src/ts/lambda-function/schema.json +5 -0
- package/src/ts/lib/schema.json +5 -0
- package/src/ts/mcp-server/schema.json +5 -0
- package/src/ts/nx-generator/schema.json +5 -0
- package/src/ts/nx-plugin/schema.json +5 -0
- package/src/ts/rdb/agent-connection/schema.json +5 -0
- package/src/ts/rdb/mcp-server-connection/schema.json +5 -0
- package/src/ts/rdb/schema.json +6 -1
- package/src/ts/rdb/smithy-connection/schema.json +5 -0
- package/src/ts/rdb/trpc-connection/schema.json +5 -0
- package/src/ts/react-website/app/schema.json +11 -6
- package/src/ts/react-website/cognito-auth/schema.json +5 -0
- package/src/ts/react-website/runtime-config/schema.json +5 -0
- package/src/ts/website/app/schema.json +11 -6
- package/src/ts/website/auth/schema.json +5 -0
- /package/docs/snippets/connection/{lambda-rdb-ssl-requirements.mdx → ts-lambda-rdb-ssl-requirements.mdx} +0 -0
- /package/docs/snippets/connection/{mcp-server-rdb-ssl-requirements.mdx → ts-mcp-server-rdb-ssl-requirements.mdx} +0 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Python Agent to Relational Database
|
|
3
|
+
description: Connect a Python Agent to a Relational Database
|
|
4
|
+
when:
|
|
5
|
+
sourceType: py#agent
|
|
6
|
+
targetType: py#rdb
|
|
7
|
+
---
|
|
8
|
+
import { FileTree } from '@astrojs/starlight/components';
|
|
9
|
+
import Link from '@components/link.astro';
|
|
10
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
11
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
12
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
13
|
+
import Infrastructure from '@components/infrastructure.astro';
|
|
14
|
+
|
|
15
|
+
The `connection` generator wires a <Link path="guides/py-agent">Python Agent</Link> to a <Link path="guides/py-rdb">Python Relational Database</Link> project, making the database session available inside your agent tools.
|
|
16
|
+
|
|
17
|
+
## Prerequisites
|
|
18
|
+
|
|
19
|
+
Before using this generator, ensure you have:
|
|
20
|
+
|
|
21
|
+
1. A <Link path="guides/py-agent">`py#agent`</Link> project
|
|
22
|
+
2. A <Link path="guides/py-rdb">`py#rdb`</Link> project
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Run the Generator
|
|
27
|
+
|
|
28
|
+
<RunGenerator generator="connection" />
|
|
29
|
+
|
|
30
|
+
Select your Agent project as the source and your relational database project as the target. If the project contains multiple agent components, specify `sourceComponent` to disambiguate.
|
|
31
|
+
|
|
32
|
+
### Options
|
|
33
|
+
|
|
34
|
+
<GeneratorParameters generator="connection" />
|
|
35
|
+
|
|
36
|
+
## Generator Output
|
|
37
|
+
|
|
38
|
+
<FileTree>
|
|
39
|
+
|
|
40
|
+
- packages/my\_service
|
|
41
|
+
- project.json Adds a dependency from `my_agent-dev` on the database's `dev` target
|
|
42
|
+
- pyproject.toml Adds the database package as a workspace dependency
|
|
43
|
+
- my\_service
|
|
44
|
+
- my\_agent
|
|
45
|
+
- Dockerfile Adds the RDS CA bundle used for direct Aurora connections
|
|
46
|
+
|
|
47
|
+
</FileTree>
|
|
48
|
+
|
|
49
|
+
## Using the Database in Agent Tools
|
|
50
|
+
|
|
51
|
+
Import `session_context` from your database package and use it inside your agent tools:
|
|
52
|
+
|
|
53
|
+
```python title="packages/my_service/my_service/my_agent/agent.py"
|
|
54
|
+
from sqlmodel import select
|
|
55
|
+
from my_scope.my_db import session_context
|
|
56
|
+
from my_scope.my_db.models.example import ExampleModel
|
|
57
|
+
from strands import tool
|
|
58
|
+
|
|
59
|
+
@tool
|
|
60
|
+
async def list_examples() -> list:
|
|
61
|
+
"""List all example records."""
|
|
62
|
+
async with session_context() as session:
|
|
63
|
+
return [item.model_dump() for item in (await session.execute(select(ExampleModel))).all()]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Infrastructure
|
|
67
|
+
|
|
68
|
+
The generated agent construct implements `IGrantable` and `IConnectable`, so you can grant network and IAM access to the database directly on the construct.
|
|
69
|
+
|
|
70
|
+
<Infrastructure>
|
|
71
|
+
<Fragment slot="cdk">
|
|
72
|
+
|
|
73
|
+
```ts title="packages/infra/src/stacks/application-stack.ts"
|
|
74
|
+
import { SecurityGroup } from 'aws-cdk-lib/aws-ec2';
|
|
75
|
+
import { RuntimeNetworkConfiguration } from 'aws-cdk-lib/aws-bedrockagentcore';
|
|
76
|
+
import { MyDatabase } from ':my-scope/common-constructs';
|
|
77
|
+
|
|
78
|
+
const db = new MyDatabase(this, 'Db', { vpc, ... });
|
|
79
|
+
|
|
80
|
+
const myAgent = new MyAgent(this, 'MyAgent', {
|
|
81
|
+
networkConfiguration: RuntimeNetworkConfiguration.usingVpc(this, {
|
|
82
|
+
vpc,
|
|
83
|
+
vpcSubnets: { subnetType: SubnetType.PRIVATE_WITH_EGRESS },
|
|
84
|
+
securityGroups: [
|
|
85
|
+
new SecurityGroup(this, 'MyAgentSecurityGroup', { vpc, allowAllOutbound: true }),
|
|
86
|
+
],
|
|
87
|
+
}),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
db.allowDefaultPortFrom(myAgent);
|
|
91
|
+
db.grantConnect(myAgent);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
`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.
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
</Fragment>
|
|
98
|
+
<Fragment slot="terraform">
|
|
99
|
+
|
|
100
|
+
Run the agent inside the same VPC as the database, grant it `rds-db:connect` via `additional_iam_policy_statements`, and open the network path with a pair of security group rules. The `aws_vpc.main` and `aws_subnet` resources are defined in the database deployment guide:
|
|
101
|
+
|
|
102
|
+
```hcl title="packages/infra/src/main.tf"
|
|
103
|
+
module "my_database" {
|
|
104
|
+
source = "../../common/terraform/src/app/dbs/my-database"
|
|
105
|
+
vpc_id = aws_vpc.main.id
|
|
106
|
+
database_subnet_ids = aws_subnet.database[*].id
|
|
107
|
+
lambda_subnet_ids = aws_subnet.private[*].id
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
module "my_agent" {
|
|
111
|
+
source = "../../common/terraform/src/app/agents/my-agent"
|
|
112
|
+
enable_vpc = true
|
|
113
|
+
vpc_id = aws_vpc.main.id
|
|
114
|
+
subnet_ids = aws_subnet.private[*].id
|
|
115
|
+
|
|
116
|
+
appconfig_application_id = module.runtime_config_appconfig.application_id
|
|
117
|
+
appconfig_application_arn = module.runtime_config_appconfig.application_arn
|
|
118
|
+
|
|
119
|
+
additional_iam_policy_statements = [
|
|
120
|
+
{
|
|
121
|
+
Effect = "Allow"
|
|
122
|
+
Action = ["rds-db:connect"]
|
|
123
|
+
Resource = [
|
|
124
|
+
"arn:aws:rds-db:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:dbuser:${module.my_database.connect_resource_id}/${module.my_database.database_runtime_user}"
|
|
125
|
+
]
|
|
126
|
+
}
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
resource "aws_vpc_security_group_ingress_rule" "agent_to_database" {
|
|
131
|
+
description = "Allow the agent runtime to connect to the database"
|
|
132
|
+
security_group_id = module.my_database.security_group_id
|
|
133
|
+
referenced_security_group_id = module.my_agent.security_group_id
|
|
134
|
+
from_port = module.my_database.cluster_port
|
|
135
|
+
to_port = module.my_database.cluster_port
|
|
136
|
+
ip_protocol = "tcp"
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
resource "aws_vpc_security_group_egress_rule" "agent_to_database" {
|
|
140
|
+
description = "Allow outbound traffic from the agent runtime to the database"
|
|
141
|
+
security_group_id = module.my_agent.security_group_id
|
|
142
|
+
referenced_security_group_id = module.my_database.security_group_id
|
|
143
|
+
from_port = module.my_database.cluster_port
|
|
144
|
+
to_port = module.my_database.cluster_port
|
|
145
|
+
ip_protocol = "tcp"
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
`appconfig_application_id`/`appconfig_application_arn` come from the shared <Link path="guides/runtime-config">runtime configuration</Link> AppConfig application declared once in your root module, not from the database module. Include the `database` namespace when instantiating it so the database module's runtime configuration entry is deployed:
|
|
150
|
+
|
|
151
|
+
```hcl title="packages/infra/src/main.tf"
|
|
152
|
+
module "runtime_config_appconfig" {
|
|
153
|
+
source = "../../common/terraform/src/core/runtime-config/appconfig"
|
|
154
|
+
|
|
155
|
+
application_name = "my-app-runtime-config"
|
|
156
|
+
namespaces = ["connection", "agentcore", "database"]
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
</Fragment>
|
|
161
|
+
</Infrastructure>
|
|
162
|
+
|
|
163
|
+
### SSL Requirements When Connecting Without RDS Proxy
|
|
164
|
+
|
|
165
|
+
When the agent connects directly to the Aurora cluster (without RDS Proxy), the connection generator updates the generated agent Dockerfile to install the Amazon RDS CA bundle into the system trust store:
|
|
166
|
+
|
|
167
|
+
```dockerfile
|
|
168
|
+
ADD https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /usr/local/share/ca-certificates/rds-global-bundle.crt
|
|
169
|
+
RUN update-ca-certificates
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
When using RDS Proxy, you do not need to configure the RDS CA bundle in the agent runtime.
|
|
173
|
+
|
|
174
|
+
## Local Development
|
|
175
|
+
|
|
176
|
+
<NxCommands commands={["<agent-name>-dev <project-name>"]} />
|
|
177
|
+
|
|
178
|
+
This starts the agent and all connected databases. The `LOCAL_DEV=true` environment variable causes the database client to connect to its local Docker database instead of Aurora.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: FastAPI to DynamoDB
|
|
3
|
+
description: Connect a FastAPI to a Python DynamoDB project
|
|
4
|
+
when:
|
|
5
|
+
sourceType: py#fast-api
|
|
6
|
+
targetType: py#dynamodb
|
|
7
|
+
---
|
|
8
|
+
import Link from '@components/link.astro';
|
|
9
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
10
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
11
|
+
import Snippet from '@components/snippet.astro';
|
|
12
|
+
|
|
13
|
+
The `connection` generator wires a <Link path="guides/fastapi">FastAPI</Link> to a <Link path="guides/py-dynamodb">Python DynamoDB</Link> project, configuring local development so both start together automatically.
|
|
14
|
+
|
|
15
|
+
## Prerequisites
|
|
16
|
+
|
|
17
|
+
Before using this generator, ensure you have:
|
|
18
|
+
|
|
19
|
+
1. A <Link path="guides/fastapi">`py#fast-api`</Link> project
|
|
20
|
+
2. A <Link path="guides/py-dynamodb">`py#dynamodb`</Link> project
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Run the Generator
|
|
25
|
+
|
|
26
|
+
<RunGenerator generator="connection" />
|
|
27
|
+
|
|
28
|
+
Select your FastAPI project as the source and your DynamoDB project as the target.
|
|
29
|
+
|
|
30
|
+
### Options
|
|
31
|
+
|
|
32
|
+
<GeneratorParameters generator="connection" />
|
|
33
|
+
|
|
34
|
+
## Generator Output
|
|
35
|
+
|
|
36
|
+
The generator updates the FastAPI's `project.json` to add a dependency from its `dev` target to the DynamoDB project's `dev` target, and adds the DynamoDB package as a workspace dependency. No source files are modified.
|
|
37
|
+
|
|
38
|
+
## Using DynamoDB in Route Handlers
|
|
39
|
+
|
|
40
|
+
Import entity classes from the DynamoDB package and use them inside your route handlers:
|
|
41
|
+
|
|
42
|
+
```python title="packages/my_api/my_api/api.py"
|
|
43
|
+
from my_scope.my_table.entities.example import ExampleModel
|
|
44
|
+
|
|
45
|
+
@app.get("/examples")
|
|
46
|
+
def list_examples():
|
|
47
|
+
return list(ExampleModel.scan())
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Infrastructure
|
|
51
|
+
|
|
52
|
+
<Snippet name="connection/lambda-dynamodb-access" />
|
|
53
|
+
|
|
54
|
+
## Local Development
|
|
55
|
+
|
|
56
|
+
<Snippet name="connection/py-dynamodb-local-development" />
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: FastAPI to Relational Database
|
|
3
|
+
description: Connect a FastAPI to a Python Relational Database
|
|
4
|
+
when:
|
|
5
|
+
sourceType: py#fast-api
|
|
6
|
+
targetType: py#rdb
|
|
7
|
+
---
|
|
8
|
+
import { FileTree } from '@astrojs/starlight/components';
|
|
9
|
+
import Link from '@components/link.astro';
|
|
10
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
11
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
12
|
+
import NxCommands from '@components/nx-commands.astro';
|
|
13
|
+
import Infrastructure from '@components/infrastructure.astro';
|
|
14
|
+
|
|
15
|
+
The `connection` generator wires a <Link path="guides/fastapi">FastAPI</Link> to a <Link path="guides/py-rdb">Python Relational Database</Link> project, injecting a typed SQLModel session into your route handlers via a FastAPI dependency.
|
|
16
|
+
|
|
17
|
+
## Prerequisites
|
|
18
|
+
|
|
19
|
+
Before using this generator, ensure you have:
|
|
20
|
+
|
|
21
|
+
1. A <Link path="guides/fastapi">`py#fast-api`</Link> project
|
|
22
|
+
2. A <Link path="guides/py-rdb">`py#rdb`</Link> project
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Run the Generator
|
|
27
|
+
|
|
28
|
+
<RunGenerator generator="connection" />
|
|
29
|
+
|
|
30
|
+
Select your FastAPI project as the source and your relational database project as the target.
|
|
31
|
+
|
|
32
|
+
### Options
|
|
33
|
+
|
|
34
|
+
<GeneratorParameters generator="connection" />
|
|
35
|
+
|
|
36
|
+
## Generator Output
|
|
37
|
+
|
|
38
|
+
The generator modifies your FastAPI project:
|
|
39
|
+
|
|
40
|
+
<FileTree>
|
|
41
|
+
|
|
42
|
+
- packages/my_api
|
|
43
|
+
- project.json Adds a dependency from `dev` on the database's `dev` target
|
|
44
|
+
- pyproject.toml Adds the database package as a workspace dependency
|
|
45
|
+
- my_api
|
|
46
|
+
- dependencies
|
|
47
|
+
- my_db.py FastAPI `MyDbSession` dependency for the database session
|
|
48
|
+
|
|
49
|
+
</FileTree>
|
|
50
|
+
|
|
51
|
+
## Using the Database in Route Handlers
|
|
52
|
+
|
|
53
|
+
This generator configures an injectable [FastAPI Dependency](https://fastapi.tiangolo.com/tutorial/dependencies/) which you can use in your route handlers:
|
|
54
|
+
|
|
55
|
+
```python title="packages/my_api/my_api/api.py" {2,6,10}
|
|
56
|
+
from sqlmodel import select
|
|
57
|
+
from my_api.dependencies.my_db import MyDbSession
|
|
58
|
+
from my_scope.my_db.models.example import ExampleModel
|
|
59
|
+
|
|
60
|
+
@app.get("/examples")
|
|
61
|
+
async def list_examples(my_db: MyDbSession):
|
|
62
|
+
return (await my_db.execute(select(ExampleModel))).all()
|
|
63
|
+
|
|
64
|
+
@app.post("/examples")
|
|
65
|
+
async def create_example(name: str, my_db: MyDbSession):
|
|
66
|
+
item = ExampleModel(name=name)
|
|
67
|
+
my_db.add(item)
|
|
68
|
+
await my_db.commit()
|
|
69
|
+
await my_db.refresh(item)
|
|
70
|
+
return item
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
FastAPI automatically opens a new session per request and closes it when the handler returns.
|
|
74
|
+
|
|
75
|
+
## Infrastructure
|
|
76
|
+
|
|
77
|
+
To allow the FastAPI Lambda function to connect to the database at runtime, it must be deployed into the same VPC as the database and granted network and IAM access.
|
|
78
|
+
|
|
79
|
+
<Infrastructure>
|
|
80
|
+
<Fragment slot="cdk">
|
|
81
|
+
|
|
82
|
+
In your application stack, deploy the API into the same VPC as the database, then call `allowDefaultPortFrom` and `grantConnect` to open the network path and grant IAM `rds-db:connect` permission to the Lambda handler:
|
|
83
|
+
|
|
84
|
+
```ts title="packages/infra/src/stacks/application-stack.ts"
|
|
85
|
+
import { MyDatabase } from ':my-scope/common-constructs';
|
|
86
|
+
|
|
87
|
+
const db = new MyDatabase(this, 'Db', { vpc, ... });
|
|
88
|
+
|
|
89
|
+
const api = new MyApi(this, 'Api', {
|
|
90
|
+
integrations: MyApi.defaultIntegrations(this)
|
|
91
|
+
.withDefaultOptions({
|
|
92
|
+
vpc,
|
|
93
|
+
vpcSubnets: { subnetType: SubnetType.PRIVATE_WITH_EGRESS },
|
|
94
|
+
})
|
|
95
|
+
.build(),
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
Object.entries(api.integrations).forEach(([operation, integration]) => {
|
|
99
|
+
db.allowDefaultPortFrom(integration.handler, `Allow ${operation} to connect to the database`);
|
|
100
|
+
db.grantConnect(integration.handler);
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Deploy the API Lambda functions into a **private subnet with egress**, not a private isolated subnet. At runtime, `session_context()` retrieves database configuration from AWS AppConfig, which is a public AWS service endpoint that requires outbound internet access.
|
|
105
|
+
|
|
106
|
+
:::note
|
|
107
|
+
This example grants every handler in your API access, but if only some handlers need access it's better to configure individually.
|
|
108
|
+
:::
|
|
109
|
+
|
|
110
|
+
</Fragment>
|
|
111
|
+
<Fragment slot="terraform">
|
|
112
|
+
|
|
113
|
+
Deploy the API into the same VPC as the database, grant it `rds-db:connect` via `additional_iam_policy_statements`, and open the network path with a pair of security group rules:
|
|
114
|
+
|
|
115
|
+
The example below references the VPC resources from the <Link path="guides/py-rdb">Python Relational Database</Link> deployment guide: `aws_subnet.database` subnets have no internet route, while `aws_subnet.private` subnets have NAT egress.
|
|
116
|
+
|
|
117
|
+
```hcl title="packages/infra/src/main.tf"
|
|
118
|
+
module "my_database" {
|
|
119
|
+
source = "../../common/terraform/src/app/dbs/my-database"
|
|
120
|
+
vpc_id = aws_vpc.main.id
|
|
121
|
+
database_subnet_ids = aws_subnet.database[*].id
|
|
122
|
+
lambda_subnet_ids = aws_subnet.private[*].id
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module "api" {
|
|
126
|
+
source = "../../common/terraform/src/app/apis/my-api"
|
|
127
|
+
enable_vpc = true
|
|
128
|
+
vpc_id = aws_vpc.main.id
|
|
129
|
+
subnet_ids = aws_subnet.private[*].id
|
|
130
|
+
|
|
131
|
+
appconfig_application_id = module.runtime_config_appconfig.application_id
|
|
132
|
+
appconfig_application_arn = module.runtime_config_appconfig.application_arn
|
|
133
|
+
|
|
134
|
+
additional_iam_policy_statements = [
|
|
135
|
+
{
|
|
136
|
+
Effect = "Allow"
|
|
137
|
+
Action = ["rds-db:connect"]
|
|
138
|
+
Resource = [
|
|
139
|
+
"arn:aws:rds-db:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:dbuser:${module.my_database.connect_resource_id}/${module.my_database.database_runtime_user}"
|
|
140
|
+
]
|
|
141
|
+
}
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
resource "aws_vpc_security_group_ingress_rule" "api_to_database" {
|
|
146
|
+
description = "Allow the API Lambda functions to connect to the database"
|
|
147
|
+
security_group_id = module.my_database.security_group_id
|
|
148
|
+
referenced_security_group_id = module.api.security_group_id
|
|
149
|
+
from_port = module.my_database.cluster_port
|
|
150
|
+
to_port = module.my_database.cluster_port
|
|
151
|
+
ip_protocol = "tcp"
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
resource "aws_vpc_security_group_egress_rule" "api_to_database" {
|
|
155
|
+
description = "Allow outbound traffic from the API Lambda functions to the database"
|
|
156
|
+
security_group_id = module.api.security_group_id
|
|
157
|
+
referenced_security_group_id = module.my_database.security_group_id
|
|
158
|
+
from_port = module.my_database.cluster_port
|
|
159
|
+
to_port = module.my_database.cluster_port
|
|
160
|
+
ip_protocol = "tcp"
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Deploy the API Lambda functions into **private subnets with egress**, not private isolated subnets. `appconfig_application_id`/`appconfig_application_arn` come from the shared <Link path="guides/runtime-config">runtime configuration</Link> AppConfig application declared once in your root module, not from the database module — passing them sets `RUNTIME_CONFIG_APP_ID` on the Lambda functions and grants them read access to the application.
|
|
165
|
+
|
|
166
|
+
The AppConfig application must expose the `database` namespace so the database module's runtime configuration entry is deployed. Include it in the `namespaces` when instantiating the `runtime-config/appconfig` module:
|
|
167
|
+
|
|
168
|
+
```hcl title="packages/infra/src/main.tf"
|
|
169
|
+
module "runtime_config_appconfig" {
|
|
170
|
+
source = "../../common/terraform/src/core/runtime-config/appconfig"
|
|
171
|
+
|
|
172
|
+
application_name = "my-app-runtime-config"
|
|
173
|
+
namespaces = ["connection", "agentcore", "database"]
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
</Fragment>
|
|
178
|
+
</Infrastructure>
|
|
179
|
+
|
|
180
|
+
## Local Development
|
|
181
|
+
|
|
182
|
+
<NxCommands commands={["dev <project-name>"]} />
|
|
183
|
+
|
|
184
|
+
This starts the FastAPI and all connected databases. The `LOCAL_DEV=true` environment variable causes the database client to connect to its local Docker database instead of Aurora.
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Python MCP Server to DynamoDB
|
|
3
|
+
description: Connect a Python MCP Server to a Python DynamoDB project
|
|
4
|
+
when:
|
|
5
|
+
sourceType: py#mcp-server
|
|
6
|
+
targetType: py#dynamodb
|
|
7
|
+
---
|
|
8
|
+
import Link from '@components/link.astro';
|
|
9
|
+
import RunGenerator from '@components/run-generator.astro';
|
|
10
|
+
import GeneratorParameters from '@components/generator-parameters.astro';
|
|
11
|
+
import Infrastructure from '@components/infrastructure.astro';
|
|
12
|
+
import Snippet from '@components/snippet.astro';
|
|
13
|
+
|
|
14
|
+
The `connection` generator wires a <Link path="guides/py-mcp-server">Python MCP Server</Link> to a <Link path="guides/py-dynamodb">Python DynamoDB</Link> project, configuring local development so both start together automatically.
|
|
15
|
+
|
|
16
|
+
## Prerequisites
|
|
17
|
+
|
|
18
|
+
Before using this generator, ensure you have:
|
|
19
|
+
|
|
20
|
+
1. A <Link path="guides/py-mcp-server">`py#mcp-server`</Link> project
|
|
21
|
+
2. A <Link path="guides/py-dynamodb">`py#dynamodb`</Link> project
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Run the Generator
|
|
26
|
+
|
|
27
|
+
<RunGenerator generator="connection" />
|
|
28
|
+
|
|
29
|
+
Select your MCP server project as the source and your DynamoDB project as the target. If the project contains multiple MCP server components, specify `sourceComponent` to disambiguate.
|
|
30
|
+
|
|
31
|
+
### Options
|
|
32
|
+
|
|
33
|
+
<GeneratorParameters generator="connection" />
|
|
34
|
+
|
|
35
|
+
## Generator Output
|
|
36
|
+
|
|
37
|
+
The generator updates the MCP server's `<mcp-server-name>-dev` target in `project.json` to depend on the DynamoDB project's `dev` target, and adds the DynamoDB package as a workspace dependency. No source files are modified.
|
|
38
|
+
|
|
39
|
+
## Using DynamoDB in Tools
|
|
40
|
+
|
|
41
|
+
Import entity classes from the DynamoDB package and use them inside your MCP server tools:
|
|
42
|
+
|
|
43
|
+
```python title="packages/my_project/my_project/my_mcp_server/server.py"
|
|
44
|
+
from my_scope.my_table.entities.example import ExampleModel
|
|
45
|
+
|
|
46
|
+
@mcp.tool()
|
|
47
|
+
def list_examples() -> str:
|
|
48
|
+
"""List all example items."""
|
|
49
|
+
items = list(ExampleModel.scan())
|
|
50
|
+
return str([item.attribute_values for item in items])
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Infrastructure
|
|
54
|
+
|
|
55
|
+
To allow the MCP server's Lambda function to access the DynamoDB table, grant the necessary permissions in your infrastructure.
|
|
56
|
+
|
|
57
|
+
<Infrastructure>
|
|
58
|
+
<Fragment slot="cdk">
|
|
59
|
+
|
|
60
|
+
```ts title="packages/infra/src/stacks/application-stack.ts"
|
|
61
|
+
import { MyTable } from ':my-scope/common-constructs';
|
|
62
|
+
|
|
63
|
+
const table = new MyTable(this, 'Table');
|
|
64
|
+
const myMcpServer = new MyMcpServer(this, 'MyMcpServer');
|
|
65
|
+
|
|
66
|
+
table.grantReadWriteData(myMcpServer);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`grantReadWriteData` grants both the DynamoDB and KMS permissions to the MCP server's execution role.
|
|
70
|
+
</Fragment>
|
|
71
|
+
<Fragment slot="terraform">
|
|
72
|
+
|
|
73
|
+
```hcl title="packages/infra/src/main.tf"
|
|
74
|
+
module "my_table" {
|
|
75
|
+
source = "../../common/terraform/src/app/dynamodb/my-table"
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
resource "aws_iam_role_policy" "dynamodb_access" {
|
|
79
|
+
role = module.my_mcp_server.lambda_role_name
|
|
80
|
+
|
|
81
|
+
policy = jsonencode({
|
|
82
|
+
Version = "2012-10-17"
|
|
83
|
+
Statement = [
|
|
84
|
+
{
|
|
85
|
+
Effect = "Allow"
|
|
86
|
+
Action = [
|
|
87
|
+
"dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:UpdateItem",
|
|
88
|
+
"dynamodb:DeleteItem", "dynamodb:Query", "dynamodb:Scan",
|
|
89
|
+
"dynamodb:BatchGetItem", "dynamodb:BatchWriteItem",
|
|
90
|
+
]
|
|
91
|
+
Resource = [
|
|
92
|
+
module.my_table.table_arn,
|
|
93
|
+
"${module.my_table.table_arn}/index/*",
|
|
94
|
+
]
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
Effect = "Allow"
|
|
98
|
+
Action = [
|
|
99
|
+
"kms:Encrypt",
|
|
100
|
+
"kms:Decrypt",
|
|
101
|
+
"kms:ReEncrypt*",
|
|
102
|
+
"kms:GenerateDataKey*",
|
|
103
|
+
"kms:DescribeKey"
|
|
104
|
+
]
|
|
105
|
+
Resource = [module.my_table.kms_key_arn]
|
|
106
|
+
},
|
|
107
|
+
]
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
</Fragment>
|
|
112
|
+
</Infrastructure>
|
|
113
|
+
|
|
114
|
+
## Local Development
|
|
115
|
+
|
|
116
|
+
<Snippet name="connection/py-dynamodb-local-development" />
|