Skip to content

Python MCP Server to Relational Database

The connection generator wires a Python MCP Server to a Python Relational Database project, making the database session available to all tools registered in the server.

Before using this generator, ensure you have:

  1. A py#mcp-server project
  2. A py#rdb project
Terminal window
pnpm nx g @aws/nx-plugin:connection
You can also perform a dry-run to see what files would be changed
Terminal window
pnpm nx g @aws/nx-plugin:connection --dry-run

Select 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.

ParameterTypeDefaultDescription
sourceProject Requiredstring-The source project
targetProject Requiredstring-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 booleantrueWhether 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.
  • Directorypackages/my_service
    • project.json Adds a dependency from my_mcp_server-dev on the database’s dev target
    • pyproject.toml Adds the database package as a workspace dependency
    • Directorymy_service
      • Directorymy_mcp_server
        • Dockerfile Adds the RDS CA bundle used for direct Aurora connections

Import session_context from your database package and use it inside your MCP server tools:

packages/my_service/my_service/my_mcp_server/server.py
from sqlmodel import select
from my_scope.my_db import session_context
from my_scope.my_db.models.example import ExampleModel
@mcp.tool()
async def list_examples() -> str:
"""List all example records."""
async with session_context() as session:
items = (await session.execute(select(ExampleModel))).all()
return str([item.model_dump() for item in items])

Running the generator again with a different target adds the second database alongside the first. Both session contexts are available to all tools:

packages/my_service/my_service/my_mcp_server/server.py
from my_scope.my_db import session_context as my_db_session_context
from my_scope.other_db import session_context as other_db_session_context

The generated MCP server construct implements IGrantable and IConnectable, so you can grant network and IAM access to the database directly on the construct.

packages/infra/src/stacks/application-stack.ts
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.

SSL Requirements When Connecting Without RDS Proxy

Section titled “SSL Requirements When Connecting Without RDS Proxy”

When the MCP server connects directly to the Aurora cluster (without RDS Proxy), the connection generator updates the generated MCP server Dockerfile to install the Amazon RDS CA bundle into the system trust store:

ADD https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /usr/local/share/ca-certificates/rds-global-bundle.crt
RUN update-ca-certificates

When using RDS Proxy, you do not need to configure the RDS CA bundle in the MCP server runtime.

Terminal window
pnpm nx <mcp-server-name>-dev <project-name>

This starts the MCP server and all connected databases. The LOCAL_DEV=true environment variable causes the database client to connect to its local Docker database instead of Aurora.