Skip to content

Commit c0404cb

Browse files
author
yanbei.pyb
committed
Merge branch yinuo/mcp into master
Title: to #65849001 executeScript to #65849001 executeScript Link: https://code.alibaba-inc.com/idb/alibabacloud-dms-mcp-server/codereview/21454259
2 parents c09ec41 + 6aa97e9 commit c0404cb

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

src/alibabacloud_dms_mcp_server/server.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async def getInstance(host: str, port: int, sid: Optional[str] = None) -> Dict[s
6767
instance = data.get('Instance', {})
6868
return instance
6969
except Exception as error:
70-
logging(error)
70+
logging.error(error)
7171
raise error
7272

7373

@@ -123,7 +123,7 @@ async def searchDatabase(searchKey: str, pageNumber: int = 1, pageSize: int = 20
123123
db_list = search_db_list.get('SearchDatabase', [])
124124
return db_list
125125
except Exception as error:
126-
logging(error)
126+
logging.error(error)
127127
raise error
128128

129129

@@ -175,7 +175,7 @@ async def getDatabase(host: str, port: int, schemaName: str, sid: Optional[str]
175175
database = data.get('Database', {})
176176
return database
177177
except Exception as error:
178-
logging(error)
178+
logging.error(error)
179179
raise error
180180

181181

@@ -194,7 +194,6 @@ async def getDatabase(host: str, port: int, schemaName: str, sid: Optional[str]
194194
- TableGuid: Unique table identifier
195195
""")
196196
async def searchTable(searchKey: str) -> List[Dict[str, Any]]:
197-
198197
if not isinstance(searchKey, str) or not searchKey.strip():
199198
logging.error("Invalid searchKey parameter: %s", searchKey)
200199
raise ValueError("searchKey must be a non-empty string")
@@ -237,7 +236,6 @@ async def searchTable(searchKey: str) -> List[Dict[str, Any]]:
237236
238237
""")
239238
async def listTables(databaseId: int, searchName: str, pageNumber: int = 1, pageSize: int = 200) -> Dict[str, Any]:
240-
241239
client = create_client()
242240
list_table_request = dms_enterprise_20181101_models.ListTablesRequest(
243241
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]:
293291
raise error
294292

295293

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+
296338
def main():
297339
mcp.run(transport=os.getenv('SERVER_TRANSPORT', 'stdio'))
298340

0 commit comments

Comments
 (0)