@@ -253,9 +253,12 @@ async def get_database(
253253) -> DatabaseDetail :
254254 client = create_client ()
255255 req = dms_enterprise_20181101_models .GetDatabaseRequest (host = host , port = port , schema_name = schema_name )
256- if sid : req .sid = sid
256+
257+ if sid :
258+ req .sid = sid
257259 if mcp .state .real_login_uid :
258260 req .real_login_user_uid = mcp .state .real_login_uid
261+
259262 try :
260263 resp = client .get_database (req )
261264 db_data = resp .body .to_map ().get ('Database' , {}) if resp and resp .body else {}
@@ -351,6 +354,67 @@ async def execute_script(
351354 return "当前实例尚未开启安全托管功能。您可以通过DMS控制台免费开启「安全托管模式」。请注意,该操作需要管理员或DBA身份权限。"
352355
353356
357+ async def create_data_change_order (
358+ database_id : str = Field (description = "DMS databaseId" ),
359+ script : str = Field (description = "SQL script to execute" ),
360+ logic : bool = Field (default = False , description = "Whether to use logical execution mode" )
361+ ) -> Dict [str , Any ]:
362+
363+ client = create_client ()
364+ req = dms_enterprise_20181101_models .CreateDataCorrectOrderRequest ()
365+ req .comment = "Data correct order submitted by MCP"
366+
367+ param = dms_enterprise_20181101_models .CreateDataCorrectOrderRequestParam ()
368+ param .estimate_affect_rows = 1
369+ param .sql_type = "TEXT"
370+ param .exec_sql = script
371+ param .classify = "MCP"
372+
373+ db_list = dms_enterprise_20181101_models .CreateDataCorrectOrderRequestParamDbItemList ()
374+ db_list .db_id = database_id
375+ db_list .logic = logic
376+
377+ db_items = [db_list ]
378+ param .db_item_list = db_items
379+
380+ req .param = param
381+ try :
382+ resp = client .create_data_correct_order (req )
383+ return resp .body
384+ except Exception as e :
385+ logger .error (f"Error in create_data_change_order: { e } " )
386+ raise
387+
388+
389+ async def get_order_base_info (
390+ order_id : str = Field (description = "DMS order ID" )
391+ ) -> Dict [str , Any ]:
392+
393+ client = create_client ()
394+ req = dms_enterprise_20181101_models .GetOrderBaseInfoRequest ()
395+ req .order_id = order_id
396+ try :
397+ resp = client .get_order_base_info (req )
398+ return resp .body
399+ except Exception as e :
400+ logger .error (f"Error in get_order_base_info: { e } " )
401+ raise
402+
403+
404+ async def submit_order_approval (
405+ order_id : str = Field (description = "DMS order ID" )
406+ ) -> Dict [str , Any ]:
407+
408+ client = create_client ()
409+ req = dms_enterprise_20181101_models .SubmitOrderApprovalRequest ()
410+ req .order_id = order_id
411+ try :
412+ resp = client .submit_order_approval (req )
413+ return resp .body
414+ except Exception as e :
415+ logger .error (f"Error in submit_order_approval: { e } " )
416+ raise
417+
354418async def nl2sql (
355419 database_id : str = Field (description = "DMS databaseId" ),
356420 question : str = Field (description = "Natural language question" ),
@@ -412,12 +476,28 @@ async def list_tables_configured(
412476 annotations = {"title" : "Execute SQL (Pre-configured DB)" , "readOnlyHint" : False ,
413477 "destructiveHint" : True })
414478 async def execute_script_configured (
415- script : str = Field (description = "SQL script to execute" ),
416- logic : bool = Field (description = "Whether to use logical execution mode" , default = False )
479+ script : str = Field (description = "SQL script to execute" )
480+ ) -> str :
481+ result_obj = await execute_script (database_id = self .default_database_id , script = script , logic = False )
482+ return str (result_obj )
483+
484+ @self .mcp .tool (name = "createDataChangeOrder" ,
485+ description = "Execute SQL changes through a data change order, and a corresponding order ID will be returned. "
486+ "Prefer using the executeScript tool for SQL execution; "
487+ "only use this tool when explicitly instructed to perform the operation via a order." ,
488+ annotations = {"title" : "在DMS中创建数据变更工单" , "readOnlyHint" : False , "destructiveHint" : True })
489+ async def create_data_change_order_configured (
490+ script : str = Field (description = "SQL script to execute" )
417491 ) -> str :
418- result_obj = await execute_script (database_id = self .default_database_id , script = script , logic = logic )
492+ result_obj = await create_data_change_order (database_id = self .default_database_id , script = script , logic = False )
419493 return str (result_obj )
420494
495+ self .mcp .tool (name = "getOrderInfo" , description = "Retrieve order information from DMS using the order ID." ,
496+ annotations = {"title" : "获取DMS工单详情" , "readOnlyHint" : True })(get_order_base_info )
497+
498+ self .mcp .tool (name = "submitOrderApproval" , description = "Submit the order for approval in DMS using the order ID." ,
499+ annotations = {"title" : "提交工单审批" , "readOnlyHint" : False })(submit_order_approval )
500+
421501 @self .mcp .tool (name = "askDatabase" ,
422502 description = "Ask a question in natural language to the pre-configured database and get results directly." ,
423503 annotations = {"title" : "Ask Pre-configured Database" , "readOnlyHint" : True })
@@ -494,6 +574,29 @@ async def execute_script_full_wrapper(
494574 result_obj = await execute_script (database_id = database_id , script = script , logic = logic )
495575 return str (result_obj )
496576
577+ @self .mcp .tool (name = "createDataChangeOrder" ,
578+ description = "Execute SQL changes through a data change order, and a corresponding order ID will be returned. "
579+ "Prefer using the executeScript tool for SQL execution; only use this tool when explicitly instructed to perform the operation via a order."
580+ "If you don't know the databaseId, first use getDatabase or searchDatabase to retrieve it."
581+ "(1)If you have the exact host, port, and database name, use getDatabase."
582+ "(2)If you only know the database name, use searchDatabase."
583+ "(3)If you don't know any information, ask the user to provide the necessary details."
584+ "Note: searchDatabase may return multiple databases. In this case, let the user choose which one to use." ,
585+ annotations = {"title" : "在DMS中创建数据变更工单" , "readOnlyHint" : False , "destructiveHint" : True })
586+ async def create_data_change_order_wrapper (
587+ database_id : str = Field (description = "Required DMS databaseId. Obtained via getDatabase tool" ),
588+ script : str = Field (description = "SQL script to execute" ),
589+ logic : bool = Field (description = "Whether to use logical execution mode" , default = False )
590+ ) -> str : # Return string representation
591+ result_obj = await create_data_change_order (database_id = database_id , script = script , logic = logic )
592+ return str (result_obj )
593+
594+ self .mcp .tool (name = "getOrderInfo" , description = "Retrieve order information from DMS using the order ID." ,
595+ annotations = {"title" : "获取DMS工单详情" , "readOnlyHint" : True })(get_order_base_info )
596+
597+ self .mcp .tool (name = "submitOrderApproval" , description = "Submit the order for approval in DMS using the order ID." ,
598+ annotations = {"title" : "提交工单审批" , "readOnlyHint" : False })(submit_order_approval )
599+
497600 self .mcp .tool (name = "generateSql" , description = "Generate SELECT-type SQL queries from natural language input." ,
498601 annotations = {"title" : "自然语言转SQL (DMS)" , "readOnlyHint" : True })(nl2sql )
499602
@@ -648,7 +751,6 @@ class AppState: pass
648751mcp = FastMCP (
649752 "DatabaseManagementAssistant" ,
650753 lifespan = lifespan ,
651- on_duplicate_tools = "replace" ,
652754 instructions = "Database Management Assistant (DMS) is a toolkit designed to assist users in managing and "
653755 "interacting with databases."
654756)
0 commit comments