|
10 | 10 | import anthropic |
11 | 11 | from mcp import ClientSession |
12 | 12 | from mcp.client.sse import sse_client |
| 13 | +from tabulate import tabulate |
13 | 14 |
|
14 | 15 | # This function is no longer needed as we're using the connect tool |
15 | 16 | # Kept for reference but not used |
@@ -160,7 +161,7 @@ async def generate_sql_with_anthropic(user_query, schema_text, anthropic_api_key |
160 | 161 | # Use response template prefilling to force Claude to produce JSON |
161 | 162 | # This works by adding an assistant message that starts with the JSON structure |
162 | 163 | response = client.messages.create( |
163 | | - model="claude-3-5-sonnet-20240620", |
| 164 | + model="claude-3-7-sonnet-20250219", |
164 | 165 | max_tokens=1024, |
165 | 166 | system=system_prompt, |
166 | 167 | messages=[ |
@@ -343,32 +344,62 @@ async def main(): |
343 | 344 |
|
344 | 345 | # Extract and format results |
345 | 346 | if hasattr(result, 'content') and result.content: |
346 | | - content = result.content[0] |
347 | | - if hasattr(content, 'text'): |
348 | | - query_results = json.loads(content.text) |
349 | | - print("\nQuery Results:") |
350 | | - print("==============") |
351 | | - if query_results: |
352 | | - # Pretty print the results |
353 | | - if isinstance(query_results, list) and len(query_results) > 0: |
354 | | - # Print column headers |
355 | | - headers = query_results[0].keys() |
356 | | - header_row = ' | '.join(str(h) for h in headers) |
357 | | - separator = '-' * len(header_row) |
358 | | - print(header_row) |
359 | | - print(separator) |
360 | | - |
361 | | - # Print data rows |
362 | | - for row in query_results: |
363 | | - print(' | '.join(str(row.get(h, '')) for h in headers)) |
364 | | - |
365 | | - print(f"\nTotal rows: {len(query_results)}") |
366 | | - else: |
367 | | - print(json.dumps(query_results, indent=2)) |
| 347 | + print("\nQuery Results:") |
| 348 | + print("==============") |
| 349 | + |
| 350 | + # Handle the different possible structures for results |
| 351 | + if isinstance(result.content, list): |
| 352 | + # Extract multiple text items from content array |
| 353 | + query_results = [] |
| 354 | + for item in result.content: |
| 355 | + if hasattr(item, 'text') and item.text: |
| 356 | + try: |
| 357 | + # Parse each text item as JSON |
| 358 | + row = json.loads(item.text) |
| 359 | + query_results.append(row) |
| 360 | + except json.JSONDecodeError: |
| 361 | + print(f"Warning: Could not parse result: {item.text}") |
| 362 | + else: |
| 363 | + # Legacy format handling |
| 364 | + content = result.content[0] |
| 365 | + if hasattr(content, 'text'): |
| 366 | + try: |
| 367 | + query_results = json.loads(content.text) |
| 368 | + except json.JSONDecodeError: |
| 369 | + print(f"Error: Could not parse content: {content.text}") |
| 370 | + query_results = [] |
| 371 | + else: |
| 372 | + query_results = [] |
| 373 | + |
| 374 | + if query_results: |
| 375 | + # Pretty print the results |
| 376 | + if isinstance(query_results, list) and len(query_results) > 0: |
| 377 | + # Use tabulate to format the table |
| 378 | + table = tabulate( |
| 379 | + query_results, |
| 380 | + headers="keys", |
| 381 | + tablefmt="pretty", # Options: "plain", "simple", "github", "grid", "fancy_grid", "pipe", "orgtbl", "jira", etc. |
| 382 | + numalign="right", |
| 383 | + stralign="left" |
| 384 | + ) |
| 385 | + print(table) |
| 386 | + print(f"\nTotal rows: {len(query_results)}") |
| 387 | + elif isinstance(query_results, dict): |
| 388 | + # Handle single dictionary case |
| 389 | + table = tabulate( |
| 390 | + [query_results], |
| 391 | + headers="keys", |
| 392 | + tablefmt="pretty", |
| 393 | + numalign="right", |
| 394 | + stralign="left" |
| 395 | + ) |
| 396 | + print(table) |
| 397 | + print("\nTotal rows: 1") |
368 | 398 | else: |
369 | | - print("Query executed successfully but returned no results.") |
| 399 | + print(json.dumps(query_results, indent=2)) |
370 | 400 | else: |
371 | | - print("Query executed but returned an unexpected format.") |
| 401 | + print("Query executed successfully but returned no results.") |
| 402 | + |
372 | 403 | else: |
373 | 404 | print("Query executed but returned no content.") |
374 | 405 | except Exception as e: |
|
0 commit comments