Skip to content

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.

Before using this generator, ensure you have:

  1. A ts#mcp-server project
  2. A ts#rdb project
  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - connection
  5. Fill in the required parameters
    • Click Generate

    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.

    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.

    The generator modifies server.ts 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

    Additionally, the <mcp-server-name>-serve-local target is updated to depend on the database’s serve-local target.

    The Prisma client is fetched inside createServer and available to all tools and resources registered there:

    packages/my-service/src/my-mcp/server.ts
    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;
    };

    Running the generator again with a different target adds the second database alongside the first. Both are fetched inside createServer:

    packages/my-service/src/my-mcp/server.ts
    export const createServer = async () => {
    const postgresDb = await getPostgresDb();
    const mysqlDb = await getMysqlDb();
    const server = new McpServer({ ... });
    // register tools using both clients
    return server;
    };

    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 { MyDatabase } from ':my-scope/common-constructs';
    const db = new MyDatabase(this, 'Db', { vpc, ... });
    const myMcpServer = new MyMcpServer(this, 'MyMcpServer', { vpc, ... });
    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.

    Terminal window
    pnpm nx <mcp-server-name>-serve-local <project-name>

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