Skip to content

Environment Variables

Environment variables are defined within the server. These can be used to store configuration or credentials that you want to decouple from your code base. To avoid boilerplate code and to enable secure connections we reccommend using Connectors when available.

You can access the defined variables as in any other node application. The following sample code utilizes the dotenv to access the environment variables.

import { z } from "zod";
import dotenv from 'dotenv';
import { defineTool, postgres, sql } from "@mcpexpress/core";
export const recentOrders = defineTool({
// Other declarations
...
// "shop_database" is the connector's name on the Connectors page; `shop` is yours.
connectors: { shop: postgres("shop_database") },
// Tool function with connectors provided during runtime
execute: async ({ customerId }, { connectors }) => {
dotenv.config();
const host = 'some.host';
const port = Number(process.env.PORT) || 3000;
...
return { orders: rows };
},
});