Skip to main content
Glama

mcp-youtube

by kirbah
database.service.ts1.87 kB
import { MongoClient, Db } from "mongodb"; const DATABASE_NAME = "youtube_niche_analysis"; // These will hold the single, shared connection promise and client instance. let connectionPromise: Promise<MongoClient> | null = null; let mongoClient: MongoClient | null = null; /** * Gets a promise that resolves to the connected MongoClient. * This function ensures the connection is only attempted once. * @returns A promise that resolves to the connected MongoClient instance. */ function getClientPromise(): Promise<MongoClient> { // If the promise doesn't exist, create it. This block runs only once. if (!connectionPromise) { const connectionString = process.env.MDB_MCP_CONNECTION_STRING; if (!connectionString) { // Use Promise.reject to handle errors in an async context return Promise.reject( new Error("MDB_MCP_CONNECTION_STRING environment variable is required") ); } const client = new MongoClient(connectionString); connectionPromise = client.connect().then((connectedClient) => { mongoClient = connectedClient; // Store the resolved client return connectedClient; }); } return connectionPromise; } /** * The new central point for accessing the database. * It lazily connects on the first call and returns the Db object. * @returns A promise that resolves to the Db instance. */ export async function getDb(): Promise<Db> { const client = await getClientPromise(); return client.db(DATABASE_NAME); } /** * Disconnects from the database if a connection was established. */ export async function disconnectFromDatabase(): Promise<void> { // We only need to close the client that was successfully connected. if (mongoClient) { await mongoClient.close(); mongoClient = null; connectionPromise = null; // Reset for potential future connections in tests, etc. } }

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/kirbah/mcp-youtube'

If you have feedback or need assistance with the MCP directory API, please join our Discord server