use_database
Switch to a different database within MySQL to manage tables, query data, and perform operations in the selected database context.
Instructions
Switch to a different database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name |
Implementation Reference
- src/index.ts:501-517 (handler)The handler function for the 'use_database' tool. It switches the current database context by executing a 'USE' SQL statement on the MySQL connection pool.async ({ database }) => { const p = await getPool(); await p.query(`USE \`${database}\``); const output = { success: true, database }; return { content: [ { type: "text" as const, text: `Switched to database ${database}`, }, ], structuredContent: output, }; }
- src/index.ts:498-500 (schema)Zod schema defining the input parameters for the 'use_database' tool, requiring a 'database' string.{ database: z.string().describe("Database name"), },
- src/index.ts:495-518 (registration)Registration of the 'use_database' tool using McpServer.tool(), including name, description, input schema, and handler function.server.tool( "use_database", "Switch to a different database", { database: z.string().describe("Database name"), }, async ({ database }) => { const p = await getPool(); await p.query(`USE \`${database}\``); const output = { success: true, database }; return { content: [ { type: "text" as const, text: `Switched to database ${database}`, }, ], structuredContent: output, }; } );
- src/index.ts:76-81 (helper)Helper function to initialize and return the MySQL connection pool, used by the 'use_database' handler.async function getPool(): Promise<Pool> { if (!pool) { pool = mysql.createPool(getConnectionConfig()); } return pool; }