#!/usr/bin/env python3
"""
Debug MCP tool binding differences between working and failing tools
"""

from workflows.clean_swe_workflow import CleanSWEWorkflow
from pathlib import Path

def test_tool_invocation():
    """Test both working and failing MCP tools"""
    workflow = CleanSWEWorkflow(Path.cwd(), Path.cwd() / 'output', False)
    
    # Find tools to test
    deepwiki_tool = None
    semgrep_tool = None
    
    for tool in workflow.mcp_tools:
        if tool.name == 'ask_question':
            deepwiki_tool = tool
        elif tool.name == 'security_check':
            semgrep_tool = tool
    
    print("=== MCP TOOL BINDING DEBUG ===")
    
    # Test DeepWiki tool (working)
    if deepwiki_tool:
        print(f"\n✅ Testing WORKING tool: {deepwiki_tool.name}")
        test_args = {'repoName': 'test/repo', 'question': 'test question'}
        
        try:
            # Try direct invocation
            print(f"Direct invocation test...")
            result = deepwiki_tool.invoke(test_args)
            print(f"✅ DeepWiki tool invoked successfully")
            print(f"Result type: {type(result)}")
        except Exception as e:
            print(f"❌ DeepWiki error: {e}")
    
    # Test Semgrep tool (failing)  
    if semgrep_tool:
        print(f"\n❌ Testing FAILING tool: {semgrep_tool.name}")
        
        # Read a small test file
        try:
            with open('vulnerable_test.py', 'r') as f:
                test_content = f.read()[:500]  # Small sample
        except FileNotFoundError:
            test_content = "print('test')\neval(input())"  # Create test content
        
        test_args = {
            'code_files': [{'filename': 'test.py', 'content': test_content}]
        }
        
        try:
            print(f"Direct invocation test...")
            result = semgrep_tool.invoke(test_args)
            print(f"✅ Semgrep tool invoked successfully")
            print(f"Result type: {type(result)}")
            if 'vulnerabilit' in str(result).lower():
                print(f"🚨 Security issues detected")
        except Exception as e:
            print(f"❌ Semgrep error: {e}")
            import traceback
            traceback.print_exc()

if __name__ == "__main__":
    test_tool_invocation()