get_database_connections
Retrieve active database connection details from pg_stat_activity to monitor and manage real-time interactions with your self-hosted Supabase instance.
Instructions
Retrieves information about active database connections from pg_stat_activity.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The execute handler function that queries pg_stat_activity for active database connections using executeSqlWithFallback and processes the result with handleSqlResponse.execute: async (input: GetDbConnectionsInput, context: ToolContext) => { const client = context.selfhostedClient; // Query pg_stat_activity // Note: Access to pg_stat_activity might require superuser or specific grants. const getConnectionsSql = ` SELECT pid, datname, usename, application_name, client_addr::text, -- Cast inet to text backend_start::text, -- Cast timestamp to text state, query FROM pg_stat_activity WHERE backend_type = 'client backend' -- Exclude background workers, etc. -- Optionally filter out self? -- AND pid != pg_backend_pid() ORDER BY backend_start `; const result = await executeSqlWithFallback(client, getConnectionsSql, true); return handleSqlResponse(result, GetDbConnectionsOutputSchema); },
- Zod output schema defining the structure of database connection details returned by the tool.const GetDbConnectionsOutputSchema = z.array(z.object({ datname: z.string().nullable().describe('Database name'), usename: z.string().nullable().describe('User name'), application_name: z.string().nullable().describe('Application name (e.g., PostgREST, psql)'), client_addr: z.string().nullable().describe('Client IP address'), backend_start: z.string().nullable().describe('Time when the backend process started'), state: z.string().nullable().describe('Current connection state (e.g., active, idle)'), query: z.string().nullable().describe('Last or current query being executed'), pid: z.number().describe('Process ID of the backend'), }));
- Input schema (empty object), MCP JSON schema, and tool definition including schema assignments.const GetDbConnectionsInputSchema = z.object({}); type GetDbConnectionsInput = z.infer<typeof GetDbConnectionsInputSchema>; // Static JSON Schema for MCP capabilities const mcpInputSchema = { type: 'object', properties: {}, required: [], }; // The tool definition export const getDatabaseConnectionsTool = { name: 'get_database_connections', description: 'Retrieves information about active database connections from pg_stat_activity.', inputSchema: GetDbConnectionsInputSchema, mcpInputSchema: mcpInputSchema, outputSchema: GetDbConnectionsOutputSchema,
- src/index.ts:105-105 (registration)Registration of the getDatabaseConnectionsTool in the availableTools object for the MCP server.[getDatabaseConnectionsTool.name]: getDatabaseConnectionsTool as AppTool,
- src/index.ts:16-16 (registration)Import of the getDatabaseConnectionsTool for use in the MCP server.import { getDatabaseConnectionsTool } from './tools/get_database_connections.js';