Skip to content

Commit 62935d2

Browse files
committed
Client Update (Claude 3.7 API)
1 parent 3fbb98c commit 62935d2

File tree

3 files changed

+68
-25
lines changed

3 files changed

+68
-25
lines changed

client/claude_cli.py

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import anthropic
1111
from mcp import ClientSession
1212
from mcp.client.sse import sse_client
13+
from tabulate import tabulate
1314

1415
# This function is no longer needed as we're using the connect tool
1516
# Kept for reference but not used
@@ -160,7 +161,7 @@ async def generate_sql_with_anthropic(user_query, schema_text, anthropic_api_key
160161
# Use response template prefilling to force Claude to produce JSON
161162
# This works by adding an assistant message that starts with the JSON structure
162163
response = client.messages.create(
163-
model="claude-3-5-sonnet-20240620",
164+
model="claude-3-7-sonnet-20250219",
164165
max_tokens=1024,
165166
system=system_prompt,
166167
messages=[
@@ -343,32 +344,62 @@ async def main():
343344

344345
# Extract and format results
345346
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")
368398
else:
369-
print("Query executed successfully but returned no results.")
399+
print(json.dumps(query_results, indent=2))
370400
else:
371-
print("Query executed but returned an unexpected format.")
401+
print("Query executed successfully but returned no results.")
402+
372403
else:
373404
print("Query executed but returned no content.")
374405
except Exception as e:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ dependencies = [
99
"asyncpg>=0.30.0",
1010
"mcp[cli]>=1.5.0",
1111
"pydantic-ai>=0.0.46",
12+
"tabulate>=0.9.0",
1213
]

uv.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)