File size: 5,562 Bytes
aca8ab4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
"""
Debug script to test MCP arXiv client with enhanced error handling.
This script helps diagnose issues with MCP server connections and downloads.
"""
import os
import sys
import logging
from pathlib import Path

# Setup detailed logging
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Add project root to path
sys.path.insert(0, str(Path(__file__).parent))

from utils.mcp_arxiv_client import MCPArxivClient
from utils.schemas import Paper
from datetime import datetime


def test_client_initialization():
    """Test client initialization and storage setup."""
    print("\n" + "="*80)
    print("TEST 1: Client Initialization")
    print("="*80)

    try:
        client = MCPArxivClient(storage_path="./data/mcp_papers")
        print(f"βœ“ Client initialized successfully")
        print(f"  Storage path: {client.storage_path}")
        print(f"  Storage exists: {client.storage_path.exists()}")
        return client
    except Exception as e:
        print(f"βœ— Client initialization failed: {str(e)}")
        import traceback
        traceback.print_exc()
        return None


def test_search_papers(client):
    """Test paper search functionality."""
    print("\n" + "="*80)
    print("TEST 2: Search Papers")
    print("="*80)

    if not client:
        print("⊘ Skipped - no client available")
        return []

    try:
        papers = client.search_papers("machine learning", max_results=2)
        print(f"βœ“ Search completed successfully")
        print(f"  Found {len(papers)} papers")
        for i, paper in enumerate(papers, 1):
            print(f"  {i}. {paper.title[:60]}...")
            print(f"     arXiv ID: {paper.arxiv_id}")
        return papers
    except Exception as e:
        print(f"βœ— Search failed: {str(e)}")
        import traceback
        traceback.print_exc()
        return []


def test_download_paper(client, papers):
    """Test paper download functionality."""
    print("\n" + "="*80)
    print("TEST 3: Download Paper")
    print("="*80)

    if not client or not papers:
        print("⊘ Skipped - no client or papers available")
        return

    paper = papers[0]
    print(f"Attempting to download: {paper.title[:60]}...")
    print(f"arXiv ID: {paper.arxiv_id}")
    print(f"Expected path: {client.storage_path / f'{paper.arxiv_id}.pdf'}")

    try:
        pdf_path = client.download_paper(paper)

        if pdf_path:
            print(f"βœ“ Download completed successfully")
            print(f"  File path: {pdf_path}")
            print(f"  File exists: {pdf_path.exists()}")
            if pdf_path.exists():
                print(f"  File size: {pdf_path.stat().st_size / 1024:.1f} KB")
        else:
            print(f"βœ— Download returned None (check logs above for details)")
            print(f"  This could indicate:")
            print(f"    - MCP server error")
            print(f"    - Storage path mismatch")
            print(f"    - Network/API issue")

    except Exception as e:
        print(f"βœ— Download failed with exception: {str(e)}")
        import traceback
        traceback.print_exc()


def test_storage_contents(client):
    """Check storage directory contents."""
    print("\n" + "="*80)
    print("TEST 4: Storage Directory Contents")
    print("="*80)

    if not client:
        print("⊘ Skipped - no client available")
        return

    try:
        pdf_files = list(client.storage_path.glob("*.pdf"))
        print(f"Storage path: {client.storage_path}")
        print(f"Total PDF files: {len(pdf_files)}")

        if pdf_files:
            print("\nFiles in storage:")
            for i, pdf_file in enumerate(pdf_files[:10], 1):
                size_kb = pdf_file.stat().st_size / 1024
                print(f"  {i}. {pdf_file.name} ({size_kb:.1f} KB)")

            if len(pdf_files) > 10:
                print(f"  ... and {len(pdf_files) - 10} more files")
        else:
            print("  (no PDF files found)")

    except Exception as e:
        print(f"βœ— Storage check failed: {str(e)}")
        import traceback
        traceback.print_exc()


def main():
    """Run all diagnostic tests."""
    print("\n")
    print("β•”" + "="*78 + "β•—")
    print("β•‘" + " "*20 + "MCP arXiv Client Diagnostic Tool" + " "*26 + "β•‘")
    print("β•š" + "="*78 + "╝")

    # Check environment
    print("\nEnvironment Configuration:")
    print(f"  USE_MCP_ARXIV: {os.getenv('USE_MCP_ARXIV', 'not set')}")
    print(f"  MCP_ARXIV_STORAGE_PATH: {os.getenv('MCP_ARXIV_STORAGE_PATH', 'not set')}")

    # Run tests
    client = test_client_initialization()
    test_storage_contents(client)
    papers = test_search_papers(client)
    test_download_paper(client, papers)

    # Final summary
    print("\n" + "="*80)
    print("DIAGNOSTIC SUMMARY")
    print("="*80)
    print("Review the logs above to identify any issues.")
    print("\nCommon issues and solutions:")
    print("  1. 'Cannot mix str and non-str arguments' error:")
    print("     β†’ Now handled with robust type checking in _call_tool")
    print("  2. 'File not found after download':")
    print("     β†’ Check MCP server storage path configuration")
    print("     β†’ Review 'MCP response' logs to see what server returned")
    print("  3. 'Connection failed':")
    print("     β†’ Ensure MCP server is running and accessible")
    print("     β†’ Check server command in logs")
    print("\n")


if __name__ == "__main__":
    main()