Connect a database

Connect a PostgreSQL or MySQL database so the agent can look up data, such as an order's status. Test the connection, then assign its tools to the agent.

Before you start

You need an agent, its id in AGENT_ID and a database user with read-only permission.

For Supabase, use the Session pooler connection details: the direct host only has IPv6.

To connect it from the platform, see Database.

  1. Connect the database

    Send the password when creating the connection: it is stored as a secret and is not returned by reads. Also define which columns the agent must not see. (Connect a database)

    import { Platic } from "platic-sdk";
    
    const platic = new Platic({ apiKey: process.env.PLATIC_API_KEY! });
    const agentId = process.env.AGENT_ID!;
    
    const db = await platic.tools.databases.create({
      name: "store",
      description: "Orders and customers of the online store.",
      connectionString: process.env.DATABASE_URL!,
      hiddenColumns: ["customers.phone"],
    });

    DATABASE_URL is the connection string, like postgres://reader:<password>@db.acme.com:5432/store. The response has the connection's id (keep it in DATABASE_ID) and, in tools, its four tools.

  2. Test it

    Test the connection to check that Platica can reach the database and see its tables. (Test a database)

    const test = await platic.tools.databases.test(db.id);
    if (!test.connected) throw new Error(`${test.error.code}: ${test.error.message}`);

    If connected is false, error says what to check. See Test a database.

  3. Give its tools to the agent

    Each tool is connected separately, with its id. It applies right away, without publishing the agent. (Connect a tool)

    for (const tool of db.tools) {
      await platic.agents.tools.connect(agentId, tool.id);
    }

Check it worked

Use List the agent's tools to check that all four database tools are assigned. Ask about a known value, such as an order's status, and check the reply.

Next steps