connect_database
Connect to a specific MySQL database using the MCP server to enable SQL query execution and database management through Claude.
Instructions
Connect to a specific MySQL database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes |
Implementation Reference
- src/mysqldb_mcp_server/server.py:207-216 (handler)The connect_database tool handler, decorated with @mcp.tool() for registration. It retrieves the QueryExecutor from context and executes a USE query to switch to the specified database.@mcp.tool() def connect_database(database: str, ctx: Context) -> str: """Connect to a specific MySQL database""" try: executor = _get_executor(ctx) result = executor.execute_single_query(f"USE `{database}`") return json.dumps(result, indent=2) except (ConnectionError, QueryError) as e: return str(e)
- Helper function used by connect_database to obtain the QueryExecutor instance from the MCP context.def _get_executor(ctx: Context) -> QueryExecutor: """Helper function to get QueryExecutor from context""" mysql_ctx = ctx.request_context.lifespan_context return QueryExecutor(mysql_ctx)