in polardb-mysql-mcp-server/server.py [0:0]
def execute_sql(arguments: str) -> str:
config = get_db_config()
query = arguments.get("query")
if not query:
raise ValueError("Query is required")
operation_type = get_sql_operation_type(query)
logger.info(f"SQL operation type: {operation_type}")
global enable_write,enable_update,enable_insert,enable_ddl
if operation_type == 'INSERT' and not enable_insert:
logger.info(f"INSERT operation is not enabled,please check POLARDB_MYSQL_ENABLE_INSERT")
return [TextContent(type="text", text=f"INSERT operation is not enabled in current tool")]
elif operation_type == 'UPDATE' and not enable_update:
logger.info(f"UPDATE operation is not enabled,please check POLARDB_MYSQL_ENABLE_UPDATE")
return [TextContent(type="text", text=f"UPDATE operation is not enabled in current tool")]
elif operation_type == 'DELETE' and not enable_write:
logger.info(f"DELETE operation is not enabled,please check POLARDB_MYSQL_ENABLE_WRITE")
return [TextContent(type="text", text=f"DELETE operation is not enabled in current tool")]
elif operation_type == 'DDL' and not enable_ddl:
logger.info(f"DDL operation is not enabled,please check POLARDB_MYSQL_ENABLE_DDL")
return [TextContent(type="text", text=f"DDL operation is not enabled in current tool")]
else:
logger.info(f"will Executing SQL: {query}")
try:
with connect(**config) as conn:
with conn.cursor() as cursor:
cursor.execute(query)
if cursor.description is not None:
columns = [desc[0] for desc in cursor.description]
rows = cursor.fetchall()
result = [",".join(map(str, row)) for row in rows]
return [TextContent(type="text", text="\n".join([",".join(columns)] + result))]
else:
conn.commit()
return [TextContent(type="text", text=f"Query executed successfully. Rows affected: {cursor.rowcount}")]
except Error as e:
logger.error(f"Error executing SQL '{query}': {e}")
return [TextContent(type="text", text=f"Error executing query: {str(e)}")]