#!/usr/bin/env python3 """ Test script for MCP server. """ import requests import json MCP_URL = "http://localhost:8000/mcp" def test_tools_list(): """Test tools/list endpoint.""" print("Testing tools/list...") request = { "jsonrpc": "2.0", "method": "tools/list", "id": 1 } response = requests.post(MCP_URL, json=request) response.raise_for_status() result = response.json() print(f"Response: {json.dumps(result, indent=2)}") if "result" in result and "tools" in result["result"]: tools = result["result"]["tools"] print(f"\n✓ Found {len(tools)} tools:") for tool in tools: print(f" - {tool['name']}: {tool['description']}") return True else: print("✗ Unexpected response format") return False def test_echo_tool(): """Test echo tool.""" print("\nTesting echo tool...") request = { "jsonrpc": "2.0", "method": "tools/call", "params": { "name": "echo", "arguments": { "text": "Hello, MCP!" } }, "id": 2 } response = requests.post(MCP_URL, json=request) response.raise_for_status() result = response.json() print(f"Response: {json.dumps(result, indent=2)}") if "result" in result: print("✓ Echo tool works!") return True else: print("✗ Echo tool failed") return False def test_weather_tool(): """Test weather tool.""" print("\nTesting weather tool...") request = { "jsonrpc": "2.0", "method": "tools/call", "params": { "name": "weather", "arguments": { "location": "San Francisco, CA" } }, "id": 3 } response = requests.post(MCP_URL, json=request) response.raise_for_status() result = response.json() print(f"Response: {json.dumps(result, indent=2)}") if "result" in result: print("✓ Weather tool works!") return True else: print("✗ Weather tool failed") return False def test_health(): """Test health endpoint.""" print("\nTesting health endpoint...") response = requests.get("http://localhost:8000/health") response.raise_for_status() result = response.json() print(f"Health: {json.dumps(result, indent=2)}") return True if __name__ == "__main__": print("=" * 50) print("MCP Server Test Suite") print("=" * 50) try: # Test health first test_health() # Test tools/list if not test_tools_list(): print("\n✗ tools/list test failed") exit(1) # Test echo tool if not test_echo_tool(): print("\n✗ Echo tool test failed") exit(1) # Test weather tool if not test_weather_tool(): print("\n✗ Weather tool test failed") exit(1) print("\n" + "=" * 50) print("✓ All tests passed!") print("=" * 50) except requests.exceptions.ConnectionError: print("\n✗ Cannot connect to MCP server") print("Make sure the server is running:") print(" cd home-voice-agent/mcp-server") print(" python server/mcp_server.py") exit(1) except Exception as e: print(f"\n✗ Test failed: {e}") exit(1)