1
1
import asyncio
2
2
import logging
3
3
import os
4
+ import sys
4
5
from mysql .connector import connect , Error
5
6
from mcp .server import Server
6
7
from mcp .types import Resource , Tool , TextContent
@@ -131,12 +132,16 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
131
132
result .extend ([table [0 ] for table in tables ])
132
133
return [TextContent (type = "text" , text = "\n " .join (result ))]
133
134
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 :
136
137
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 )} " )]
140
145
141
146
# Non-SELECT queries
142
147
else :
@@ -151,8 +156,15 @@ async def main():
151
156
"""Main entry point to run the MCP server."""
152
157
from mcp .server .stdio import stdio_server
153
158
154
- logger .info ("Starting MySQL MCP server..." )
159
+ # Add additional debug output
160
+ print ("Starting MySQL MCP server with config:" , file = sys .stderr )
155
161
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..." )
156
168
logger .info (f"Database config: { config ['host' ]} /{ config ['database' ]} as { config ['user' ]} " )
157
169
158
170
async with stdio_server () as (read_stream , write_stream ):
0 commit comments