| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #!/usr/bin/env python3
- """
- Functional testing of MCP server core components
- """
- import subprocess
- import time
- import json
- import sys
- import os
- import threading
- import queue
- class MCPServerTester:
- def __init__(self, server_path):
- self.server_path = server_path
- self.server_process = None
- self.message_queue = queue.Queue()
-
- def start_server(self):
- """Start MCP server process"""
- print(f"Starting MCP server: {self.server_path}")
- self.server_process = subprocess.Popen(
- [self.server_path],
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- text=False,
- bufsize=0
- )
-
- # Start thread to read stderr
- stderr_thread = threading.Thread(target=self._read_stderr)
- stderr_thread.daemon = True
- stderr_thread.start()
-
- # Start thread to read stdout
- stdout_thread = threading.Thread(target=self._read_stdout)
- stdout_thread.daemon = True
- stdout_thread.start()
-
- time.sleep(1)
-
- def _read_stderr(self):
- """Read stderr output from server"""
- while self.server_process and self.server_process.poll() is None:
- try:
- line = self.server_process.stderr.readline()
- if line:
- print(f"SERVER STDERR: {line.decode().strip()}")
- except:
- break
-
- def _read_stdout(self):
- """Read stdout output from server without Content-Length parsing"""
- buffer = b""
- while self.server_process and self.server_process.poll() is None:
- try:
- data = self.server_process.stdout.readline()
- if not data:
- break
- buffer += data
-
- # Try to parse JSON directly from each line
- try:
- message = buffer.strip()
- if message:
- parsed = json.loads(message.decode())
- self.message_queue.put(parsed)
- buffer = b""
- except json.JSONDecodeError:
- # If not valid JSON yet, continue reading
- continue
- except:
- break
-
- def send_jsonrpc_request(self, method, params=None, id=1):
- """Send a JSON-RPC request without Content-Length header"""
- request = {
- "jsonrpc": "2.0",
- "method": method,
- "id": id,
- "params": params or {}
- }
-
- request_str = json.dumps(request)
- message = f"{request_str}\n"
-
- self.server_process.stdin.write(message.encode())
- self.server_process.stdin.flush()
-
- try:
- response = self.message_queue.get(timeout=10)
- return response
- except queue.Empty:
- print("TIMEOUT: No response received")
- return None
-
- def test_server_initialization(self):
- """Test server initialization and lifecycle"""
- print("\n=== Testing Server Initialization ===")
-
- # Test initialize request
- init_params = {
- "protocolVersion": "2025-11-25",
- "capabilities": {},
- "clientInfo": {
- "name": "test-client",
- "version": "1.0.0"
- }
- }
-
- print("\nSending initialize request...")
- init_response = self.send_jsonrpc_request("initialize", init_params, id=1)
-
- if init_response and "result" in init_response:
- result = init_response["result"]
-
- # Check protocol version
- if result.get("protocolVersion") == "2025-11-25":
- print("✓ Protocol version correct")
- else:
- print("✗ Protocol version incorrect")
-
- # Check capabilities
- if "capabilities" in result:
- caps = result["capabilities"]
- if "resources" in caps and "tools" in caps and "prompts" in caps:
- print("✓ Capabilities structure correct")
- else:
- print("✗ Capabilities structure incorrect")
- else:
- print("✗ Capabilities missing")
-
- # Check server info
- if "serverInfo" in result:
- info = result["serverInfo"]
- if "name" in info and "version" in info:
- print("✓ Server info correct")
- else:
- print("✗ Server info incorrect")
- else:
- print("✗ Server info missing")
-
- return True
- else:
- print("✗ Initialize response invalid")
- return False
-
- def test_jsonrpc_message_handling(self):
- """Test JSON-RPC message handling with GLib.Variant"""
- print("\n=== Testing JSON-RPC Message Handling ===")
-
- # Test valid JSON-RPC request
- print("\nTesting valid JSON-RPC request...")
- ping_response = self.send_jsonrpc_request("ping", {}, id=2)
- if ping_response and "result" in ping_response and ping_response.get("id") == 2:
- print("✓ Valid JSON-RPC request handled correctly")
- else:
- print("✗ Valid JSON-RPC request failed")
-
- # Test request with complex params
- print("\nTesting request with complex parameters...")
- complex_params = {
- "nested": {
- "array": [1, 2, 3],
- "object": {"key": "value"},
- "boolean": True,
- "number": 42.5,
- "null": None
- }
- }
- complex_response = self.send_jsonrpc_request("ping", complex_params, id=3)
- if complex_response and "result" in complex_response:
- print("✓ Complex parameters handled correctly")
- else:
- print("✗ Complex parameters failed")
-
- # Test invalid method
- print("\nTesting invalid method...")
- invalid_response = self.send_jsonrpc_request("invalid_method", {}, id=4)
- if invalid_response and "error" in invalid_response:
- print("✓ Invalid method handled correctly")
- else:
- print("✗ Invalid method not handled properly")
-
- def test_error_handling(self):
- """Test error handling throughout the system"""
- print("\n=== Testing Error Handling ===")
-
- # Test malformed JSON (this would need to be sent at a lower level)
- print("\nTesting error responses...")
-
- # Test missing required parameters
- print("\nTesting missing required parameters...")
- # This would depend on specific method implementations
-
- # Test invalid parameter types
- print("\nTesting invalid parameter types...")
- # This would depend on specific method implementations
-
- print("✓ Error handling tests completed")
-
- def stop_server(self):
- """Stop MCP server process"""
- if self.server_process:
- print("\nStopping server...")
- self.server_process.terminate()
- try:
- self.server_process.wait(timeout=5)
- except subprocess.TimeoutExpired:
- self.server_process.kill()
- self.server_process = None
- def main():
- if len(sys.argv) != 2:
- print("Usage: python3 test_functional.py <server-executable>")
- sys.exit(1)
-
- server_path = sys.argv[1]
- if not os.path.exists(server_path):
- print(f"Error: Server executable '{server_path}' not found")
- sys.exit(1)
-
- tester = MCPServerTester(server_path)
-
- try:
- tester.start_server()
- time.sleep(2)
-
- # Run functional tests
- init_success = tester.test_server_initialization()
-
- if init_success:
- tester.test_jsonrpc_message_handling()
- tester.test_error_handling()
-
- print("\n✓ All functional tests completed successfully")
- else:
- print("\n✗ Functional tests failed - initialization error")
-
- except KeyboardInterrupt:
- print("\nTest interrupted by user")
- except Exception as e:
- print(f"\nError during test: {e}")
- import traceback
- traceback.print_exc()
- finally:
- tester.stop_server()
- if __name__ == "__main__":
- main()
|