Skip to main content
Glama

MCP Learning Project

by BerdTan
fixed_test_client.pyโ€ข5.22 kB
#!/usr/bin/env python3 """ Fixed test client for the MCP Testing Harness """ import asyncio import json import logging import sys from pathlib import Path # Set up logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) async def test_mcp_client(): """Test connecting to our MCP server.""" print("๐Ÿงช Testing MCP Client Connection") print("=" * 35) try: # Connect to the server print("๐Ÿ“ก Connecting to localhost:8000...") reader, writer = await asyncio.open_connection("localhost", 8000) print("โœ… Connected successfully!") # Send initialization request init_request = { "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": { "name": "MCP Test Client", "version": "1.0.0" } } } print("๐Ÿ“ค Sending initialization request...") writer.write(json.dumps(init_request).encode()) await writer.drain() # Read response data = await reader.read(1024) response = json.loads(data.decode()) print("๐Ÿ“ฅ Received initialization response:") print(json.dumps(response, indent=2)) if "result" in response: print("โœ… Initialization successful!") # Wait a moment before sending next request await asyncio.sleep(0.1) # Test tools listing tools_request = { "jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {} } print("\n๐Ÿ“ค Sending tools/list request...") writer.write(json.dumps(tools_request).encode()) await writer.drain() # Read response with timeout try: data = await asyncio.wait_for(reader.read(1024), timeout=5.0) if data: response = json.loads(data.decode()) print("๐Ÿ“ฅ Received tools response:") print(json.dumps(response, indent=2)) if "result" in response and "tools" in response["result"]: tools = response["result"]["tools"] print(f"โœ… Found {len(tools)} tools:") for tool in tools: print(f" - {tool.get('name', 'unknown')}: {tool.get('description', 'No description')}") else: print("โš ๏ธ No tools found or unexpected response format") else: print("โš ๏ธ No data received from tools/list request") except asyncio.TimeoutError: print("โš ๏ธ Timeout waiting for tools/list response") except json.JSONDecodeError as e: print(f"โš ๏ธ Invalid JSON response: {e}") # Test a built-in tool await asyncio.sleep(0.1) list_servers_request = { "jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": { "name": "list_servers", "arguments": {} } } print("\n๐Ÿ“ค Testing list_servers tool...") writer.write(json.dumps(list_servers_request).encode()) await writer.drain() # Read response with timeout try: data = await asyncio.wait_for(reader.read(1024), timeout=5.0) if data: response = json.loads(data.decode()) print("๐Ÿ“ฅ Received list_servers response:") print(json.dumps(response, indent=2)) else: print("โš ๏ธ No data received from list_servers request") except asyncio.TimeoutError: print("โš ๏ธ Timeout waiting for list_servers response") except json.JSONDecodeError as e: print(f"โš ๏ธ Invalid JSON response: {e}") else: print("โŒ Initialization failed!") print(f"Error: {response.get('error', 'Unknown error')}") # Close connection writer.close() await writer.wait_closed() print("\nโœ… Test completed!") except ConnectionRefusedError: print("โŒ Connection refused! Make sure the server is running on localhost:8000") print("๐Ÿ’ก Start the server with: py -3 start_server.py") except Exception as e: print(f"โŒ Error during test: {e}") import traceback traceback.print_exc() if __name__ == "__main__": asyncio.run(test_mcp_client())

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/BerdTan/mcpharness'

If you have feedback or need assistance with the MCP directory API, please join our Discord server