@@ -67,7 +67,7 @@ async def getInstance(host: str, port: int, sid: Optional[str] = None) -> Dict[s
67
67
instance = data .get ('Instance' , {})
68
68
return instance
69
69
except Exception as error :
70
- logging (error )
70
+ logging . error (error )
71
71
raise error
72
72
73
73
@@ -123,7 +123,7 @@ async def searchDatabase(searchKey: str, pageNumber: int = 1, pageSize: int = 20
123
123
db_list = search_db_list .get ('SearchDatabase' , [])
124
124
return db_list
125
125
except Exception as error :
126
- logging (error )
126
+ logging . error (error )
127
127
raise error
128
128
129
129
@@ -175,7 +175,7 @@ async def getDatabase(host: str, port: int, schemaName: str, sid: Optional[str]
175
175
database = data .get ('Database' , {})
176
176
return database
177
177
except Exception as error :
178
- logging (error )
178
+ logging . error (error )
179
179
raise error
180
180
181
181
@@ -194,7 +194,6 @@ async def getDatabase(host: str, port: int, schemaName: str, sid: Optional[str]
194
194
- TableGuid: Unique table identifier
195
195
""" )
196
196
async def searchTable (searchKey : str ) -> List [Dict [str , Any ]]:
197
-
198
197
if not isinstance (searchKey , str ) or not searchKey .strip ():
199
198
logging .error ("Invalid searchKey parameter: %s" , searchKey )
200
199
raise ValueError ("searchKey must be a non-empty string" )
@@ -237,7 +236,6 @@ async def searchTable(searchKey: str) -> List[Dict[str, Any]]:
237
236
238
237
""" )
239
238
async def listTables (databaseId : int , searchName : str , pageNumber : int = 1 , pageSize : int = 200 ) -> Dict [str , Any ]:
240
-
241
239
client = create_client ()
242
240
list_table_request = dms_enterprise_20181101_models .ListTablesRequest (
243
241
search_name = searchName , database_id = databaseId , page_number = pageNumber , page_size = pageSize , return_guid = True )
@@ -293,6 +291,50 @@ async def getMetaTableDetailInfo(tableGuid: str) -> Dict[str, Any]:
293
291
raise error
294
292
295
293
294
+ @mcp .tool (name = "executeScript" ,
295
+ description = """
296
+ Execute SQL script against a database in DMS and return structured results.
297
+ Parameters:
298
+ databaseId (int): Required DMS databaseId. Obtained via getDatabase tool.
299
+ script (str): SQL script to execute.
300
+ Returns:
301
+ Dict[str, Any] containing:
302
+ - RequestId (str): Unique request identifier
303
+ - Results (List[Dict]): List of result sets from executed script:
304
+ Each result set contains:
305
+ - ColumnNames (List[str]): Ordered list of column names
306
+ - RowCount (int): Number of rows returned
307
+ - Rows (List[Dict[str, str]]): List of rows with column name -> value mapping
308
+ - Success (bool): Whether this result set was successfully retrieved
309
+ - Success (bool): Overall operation success status
310
+
311
+ """ )
312
+ async def executeScript (databaseId : int , script : str , logic : bool = False ) -> Dict [str , Any ]:
313
+ if not isinstance (databaseId , int ) or databaseId <= 0 :
314
+ error_msg = f"Invalid databaseId parameter: { databaseId !r} "
315
+ logging .error (error_msg )
316
+ raise ValueError ("databaseId must be a positive integer" )
317
+
318
+ if not isinstance (script , str ) or not script .strip ():
319
+ error_msg = "Script parameter must be a non-empty string"
320
+ logging .error (error_msg )
321
+ raise ValueError (error_msg )
322
+
323
+ client = create_client ()
324
+ execute_script_request = dms_enterprise_20181101_models .ExecuteScriptRequest (
325
+ db_id = databaseId , script = script , logic = logic )
326
+ try :
327
+ response = client .execute_script (execute_script_request )
328
+ if response is None or not hasattr (response , 'body' ) or response .body is None :
329
+ logging .warning ("Empty or invalid response received from DMS service" )
330
+ return []
331
+ data = response .body .to_map ()
332
+ return data
333
+ except Exception as error :
334
+ logging .error (error )
335
+ raise error
336
+
337
+
296
338
def main ():
297
339
mcp .run (transport = os .getenv ('SERVER_TRANSPORT' , 'stdio' ))
298
340
0 commit comments