-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_manual.py
More file actions
110 lines (84 loc) · 3.15 KB
/
test_manual.py
File metadata and controls
110 lines (84 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Manual test script to verify the NYTimes API wrapper works.
Run this before connecting to Claude Desktop to catch any API issues early.
"""
from nytimes_mcp_server.nyt_api import NYTimesBookAPI
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
def test_api_key():
"""First, verify the API key is loaded correctly."""
api_key = os.getenv("NYTIMES_API_KEY")
if api_key:
print(f" API key found (length: {len(api_key)} characters)")
print(f" First 5 chars: {api_key[:5]}...")
else:
print(" API key not found!")
return False
return True
def test_best_sellers():
"""Test getting books from a best sellers list."""
api = NYTimesBookAPI()
print("\nTest 1: Get Best Sellers List")
print("=" * 60)
try:
results = api.get_best_sellers(
list_name="combined-print-and-e-book-fiction"
)
print(f" List: {results['display_name']}")
print(f" Date: {results['bestsellers_date']}")
print(f" Number of books: {len(results['books'])}")
print(f"\nTop 3 books:")
for book in results['books'][:3]:
print(f"\n #{book['rank']} - {book['title']}")
print(f" by {book['author']}")
print(f" Weeks on list: {book['weeks_on_list']}")
api.close()
return True
except Exception as e:
print(f" Test failed: {e}")
api.close()
return False
def test_overview():
"""Test getting the overview of all lists."""
api = NYTimesBookAPI()
print("\nTest 2: Get Best Sellers Overview")
print("=" * 60)
try:
results = api.get_best_sellers_overview()
print(f" Found {results['num_lists']} lists")
print(f" Date: {results['bestsellers_date']}")
print(f"\nFirst 3 lists:")
for i, lst in enumerate(results['lists'][:3], 1):
print(f"\n {i}. {lst['display_name']}")
print(f" List name: {lst['list_name']}")
print(f" Top book: {lst['books'][0]['title'] if lst['books'] else 'N/A'}")
api.close()
return True
except Exception as e:
print(f" Test failed: {e}")
api.close()
return False
if __name__ == "__main__":
print("NYTimes Books API - Diagnostic Tests")
print("=" * 60)
# Test 1: Check API key
if not test_api_key():
print("\n Fix your API key configuration before continuing")
exit(1)
# Test 2: Best sellers list
if not test_best_sellers():
print("\n Basic API connectivity failed")
print("\nTroubleshooting:")
print("1. Verify your API key is valid at https://developer.nytimes.com/")
print("2. Check that you've enabled the Books API for your key")
print("3. Try regenerating your API key")
exit(1)
# Test 3: Overview
if not test_overview():
print("\n Overview endpoint failed")
exit(1)
print("\n" + "=" * 60)
print(" All tests passed! Your MCP server is ready to connect to Claude Desktop.")
print("=" * 60)