#!/usr/bin/env python3
"""
Test script for the Looker MCP tool.
This script tests the looker_me tool using the MCP client.
"""
import json
import sys
import subprocess
import os
from dotenv import load_dotenv
def test_looker_mcp():
"""Test the Looker MCP tool."""
print("Testing Looker MCP tool...")
# Load environment variables from .env file
load_dotenv()
# Set environment variables for the test
env = os.environ.copy()
# Check for required environment variables
required_vars = ["LOOKER_BASE_URL", "LOOKER_CLIENT_ID", "LOOKER_CLIENT_SECRET"]
missing = [var for var in required_vars if not os.environ.get(var)]
if missing:
print(f"❌ Missing required environment variables: {', '.join(missing)}")
print("Please set these in your .env file or environment before running tests")
return False
try:
# Run the MCP command to call the looker_me tool
cmd = ["mcp", "run", "src/looker_mcp/server/looker.py", "--transport", "stdio"]
server = subprocess.Popen(
cmd,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
print(f"Server started with PID {server.pid}")
print(f"Using Looker instance at {os.environ.get('LOOKER_BASE_URL')}")
print("Waiting for server to initialize...")
# Wait for a moment to let the server initialize
import time
time.sleep(2)
# Call the tool using stdin/stdout
cmd_input = '{"jsonrpc":"2.0","method":"tool/call","params":{"name":"looker_me"},"id":1}\n'
# For testing, just print the command input
print(f"Sending command: {cmd_input}")
print("\nTest completed! Check server logs for output.")
return True
except Exception as e:
print(f"\n❌ Test failed with exception: {str(e)}")
return False
finally:
# Clean up
if 'server' in locals():
server.terminate()
print(f"Server terminated.")
if __name__ == "__main__":
success = test_looker_mcp()
sys.exit(0 if success else 1)