11import asyncio
22import logging
33import os
4+ import sys
45from mysql .connector import connect , Error
56from mcp .server import Server
67from mcp .types import Resource , Tool , TextContent
@@ -131,12 +132,16 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
131132 result .extend ([table [0 ] for table in tables ])
132133 return [TextContent (type = "text" , text = "\n " .join (result ))]
133134
134- # Regular SELECT queries
135- elif query . strip (). upper (). startswith ( "SELECT" ) :
135+ # Handle all other queries that return result sets (SELECT, SHOW, DESCRIBE etc.)
136+ elif cursor . description is not None :
136137 columns = [desc [0 ] for desc in cursor .description ]
137- rows = cursor .fetchall ()
138- result = ["," .join (map (str , row )) for row in rows ]
139- return [TextContent (type = "text" , text = "\n " .join (["," .join (columns )] + result ))]
138+ try :
139+ rows = cursor .fetchall ()
140+ result = ["," .join (map (str , row )) for row in rows ]
141+ return [TextContent (type = "text" , text = "\n " .join (["," .join (columns )] + result ))]
142+ except Error as e :
143+ logger .warning (f"Error fetching results: { str (e )} " )
144+ return [TextContent (type = "text" , text = f"Query executed but error fetching results: { str (e )} " )]
140145
141146 # Non-SELECT queries
142147 else :
@@ -151,8 +156,15 @@ async def main():
151156 """Main entry point to run the MCP server."""
152157 from mcp .server .stdio import stdio_server
153158
154- logger .info ("Starting MySQL MCP server..." )
159+ # Add additional debug output
160+ print ("Starting MySQL MCP server with config:" , file = sys .stderr )
155161 config = get_db_config ()
162+ print (f"Host: { config ['host' ]} " , file = sys .stderr )
163+ print (f"Port: { config ['port' ]} " , file = sys .stderr )
164+ print (f"User: { config ['user' ]} " , file = sys .stderr )
165+ print (f"Database: { config ['database' ]} " , file = sys .stderr )
166+
167+ logger .info ("Starting MySQL MCP server..." )
156168 logger .info (f"Database config: { config ['host' ]} /{ config ['database' ]} as { config ['user' ]} " )
157169
158170 async with stdio_server () as (read_stream , write_stream ):
0 commit comments