MCP Server to Relational Database
The connection generator wires a TypeScript MCP Server to a Relational Database project, making a Prisma client available to all tools registered inside createServer.
Prerequisites
Section titled “Prerequisites”Before using this generator, ensure you have:
- A
ts#mcp-serverproject - 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 MCP server project as the source and your relational database project as the target. If the project contains multiple MCP server 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. |
| preferInstallDependencies | boolean | true | Whether to prefer installing dependencies after the generator runs. Set to false to defer installing when batching multiple generators (an install still runs if needed so subsequent generators can compute the Nx project graph); install once at the end. |
Generator Output
Section titled “Generator Output”The generator modifies two files in your MCP server’s source directory:
Directorypackages/my-service/src/my-mcp
- server.ts Prisma client fetched and available to all tools registered inside
createServer - Dockerfile RDS CA bundle installed for SSL connections to Aurora
- server.ts Prisma client fetched and available to all tools registered inside
Additionally, the <mcp-server-name>-dev target is updated to depend on the database’s dev target.
Using the Database in MCP Tools
Section titled “Using the Database in MCP Tools”The Prisma client is fetched inside createServer and available to all tools and resources registered there:
import { getPrisma as getMyDb } from ':my-scope/my-db';
export const createServer = async () => { const myDb = await getMyDb(); const server = new McpServer({ name: 'my-service', version: '1.0.0' }); // register tools/resources that use myDb return server;};Multiple Databases
Section titled “Multiple Databases”Running the generator again with a different target adds the second database alongside the first. Both are fetched inside createServer:
export const createServer = async () => { const postgresDb = await getPostgresDb(); const mysqlDb = await getMysqlDb(); const server = new McpServer({ ... }); // register tools using both clients return server;};Infrastructure
Section titled “Infrastructure”The generated MCP server construct implements IGrantable and IConnectable, so you can grant network and IAM access to the database directly on the construct.
import { SecurityGroup } from 'aws-cdk-lib/aws-ec2';import { RuntimeNetworkConfiguration } from 'aws-cdk-lib/aws-bedrockagentcore';import { MyDatabase } from ':my-scope/common-constructs';
const db = new MyDatabase(this, 'Db', { vpc, ... });
const myMcpServer = new MyMcpServer(this, 'MyMcpServer', { networkConfiguration: RuntimeNetworkConfiguration.usingVpc(this, { vpc, vpcSubnets: { subnetType: SubnetType.PRIVATE_WITH_EGRESS }, securityGroups: [ new SecurityGroup(this, 'MyMcpServerSecurityGroup', { vpc, allowAllOutbound: true }), ], }),});
db.allowDefaultPortFrom(myMcpServer);db.grantConnect(myMcpServer);allowDefaultPortFrom opens the security group rule so the MCP server runtime can reach the database port. grantConnect grants IAM rds-db:connect permission to the server’s execution role.
Run the MCP server 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:
module "my_database" { source = "../../common/terraform/src/app/dbs/my-database" vpc_id = aws_vpc.main.id database_subnet_ids = aws_subnet.database[*].id lambda_subnet_ids = aws_subnet.private[*].id}
module "my_mcp_server" { source = "../../common/terraform/src/app/mcp-servers/my-mcp-server" enable_vpc = true vpc_id = aws_vpc.main.id subnet_ids = aws_subnet.private[*].id
appconfig_application_id = module.runtime_config_appconfig.application_id appconfig_application_arn = module.runtime_config_appconfig.application_arn
additional_iam_policy_statements = [ { Effect = "Allow" Action = ["rds-db:connect"] Resource = [ "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}" ] } ]}
resource "aws_vpc_security_group_ingress_rule" "mcp_server_to_database" { description = "Allow the MCP server runtime to connect to the database" security_group_id = module.my_database.security_group_id referenced_security_group_id = module.my_mcp_server.security_group_id from_port = module.my_database.cluster_port to_port = module.my_database.cluster_port ip_protocol = "tcp"}
resource "aws_vpc_security_group_egress_rule" "mcp_server_to_database" { description = "Allow outbound traffic from the MCP server runtime to the database" security_group_id = module.my_mcp_server.security_group_id referenced_security_group_id = module.my_database.security_group_id from_port = module.my_database.cluster_port to_port = module.my_database.cluster_port ip_protocol = "tcp"}appconfig_application_id/appconfig_application_arn come from the shared runtime configuration 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:
module "runtime_config_appconfig" { source = "../../common/terraform/src/core/runtime-config/appconfig"
application_name = "my-app-runtime-config" namespaces = ["connection", "agentcore", "database"]}SSL Requirements When Connecting Without RDS Proxy
Section titled “SSL Requirements When Connecting Without RDS Proxy”The connection generator updates the Dockerfile to install the Amazon RDS CA bundle at /usr/local/share/ca-certificates/rds-bundle.crt. Set NODE_EXTRA_CA_CERTS to that path so Node.js trusts the certificate when connecting without RDS Proxy:
new MyMcpServer(this, 'MyMcpServer', { ... environmentVariables: { NODE_EXTRA_CA_CERTS: '/usr/local/share/ca-certificates/rds-bundle.crt', },});module "my_mcp_server" { ... environment_variables = { NODE_EXTRA_CA_CERTS = "/usr/local/share/ca-certificates/rds-bundle.crt" }}For more details, see the Amazon RDS SSL/TLS documentation. When using RDS Proxy, you do not need to configure NODE_EXTRA_CA_CERTS.
Local Development
Section titled “Local Development”pnpm nx <mcp-server-name>-dev <project-name>yarn nx <mcp-server-name>-dev <project-name>npx nx <mcp-server-name>-dev <project-name>bunx nx <mcp-server-name>-dev <project-name>This starts the MCP server and all connected databases. The LOCAL_DEV=true environment variable causes each Prisma client to connect to its local Docker database instead of Aurora.