|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Test HTTP MCP server with SSE streaming for agentic tools. |
| 4 | +
|
| 5 | +REQUIREMENTS: |
| 6 | + - Binary built with: cargo build --release --features "ai-enhanced,autoagents-experimental,faiss,ollama,server-http" |
| 7 | + - Start server: ./target/release/codegraph-official serve --transport http --port 3000 |
| 8 | +
|
| 9 | +Usage: |
| 10 | + python3 test_http_mcp_client.py |
| 11 | +""" |
| 12 | + |
| 13 | +import json |
| 14 | +import requests |
| 15 | +import sseclient |
| 16 | +from typing import Optional |
| 17 | + |
| 18 | +MCP_URL = "http://127.0.0.1:3000/mcp" |
| 19 | +SSE_URL = "http://127.0.0.1:3000/sse" |
| 20 | + |
| 21 | +def send_mcp_request(request: dict, session_id: Optional[str] = None): |
| 22 | + """Send MCP request and handle SSE response stream.""" |
| 23 | + headers = {"Content-Type": "application/json"} |
| 24 | + if session_id: |
| 25 | + headers["Mcp-Session-Id"] = session_id |
| 26 | + |
| 27 | + response = requests.post(MCP_URL, json=request, headers=headers, stream=True) |
| 28 | + response.raise_for_status() |
| 29 | + |
| 30 | + # Extract session ID from response |
| 31 | + new_session_id = response.headers.get("Mcp-Session-Id") |
| 32 | + |
| 33 | + # Parse SSE stream |
| 34 | + client = sseclient.SSEClient(response) |
| 35 | + results = [] |
| 36 | + for event in client.events(): |
| 37 | + if event.data: |
| 38 | + results.append(json.loads(event.data)) |
| 39 | + |
| 40 | + return results, new_session_id |
| 41 | + |
| 42 | +def test_initialize(): |
| 43 | + """Test MCP initialize handshake.""" |
| 44 | + print("Testing initialize...") |
| 45 | + |
| 46 | + request = { |
| 47 | + "jsonrpc": "2.0", |
| 48 | + "id": 1, |
| 49 | + "method": "initialize", |
| 50 | + "params": { |
| 51 | + "protocolVersion": "2025-06-18", |
| 52 | + "capabilities": {}, |
| 53 | + "clientInfo": { |
| 54 | + "name": "test-http-client", |
| 55 | + "version": "1.0.0" |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + results, session_id = send_mcp_request(request) |
| 61 | + assert session_id, "No session ID returned" |
| 62 | + assert results, "No response received" |
| 63 | + |
| 64 | + print(f"✓ Initialize successful (session: {session_id})") |
| 65 | + return session_id |
| 66 | + |
| 67 | +def test_list_tools(session_id: str): |
| 68 | + """Test listing available tools.""" |
| 69 | + print("Testing list tools...") |
| 70 | + |
| 71 | + request = { |
| 72 | + "jsonrpc": "2.0", |
| 73 | + "id": 2, |
| 74 | + "method": "tools/list", |
| 75 | + "params": {} |
| 76 | + } |
| 77 | + |
| 78 | + results, _ = send_mcp_request(request, session_id) |
| 79 | + assert results, "No tools returned" |
| 80 | + |
| 81 | + tools = results[0].get("result", {}).get("tools", []) |
| 82 | + print(f"✓ Found {len(tools)} tools") |
| 83 | + |
| 84 | + # Verify agentic tools present |
| 85 | + agentic_tools = [t for t in tools if t["name"].startswith("agentic_")] |
| 86 | + print(f"✓ Found {len(agentic_tools)} agentic tools") |
| 87 | + |
| 88 | + return tools |
| 89 | + |
| 90 | +def test_vector_search(session_id: str): |
| 91 | + """Test vector search tool.""" |
| 92 | + print("Testing vector search...") |
| 93 | + |
| 94 | + request = { |
| 95 | + "jsonrpc": "2.0", |
| 96 | + "id": 3, |
| 97 | + "method": "tools/call", |
| 98 | + "params": { |
| 99 | + "name": "vector_search", |
| 100 | + "arguments": { |
| 101 | + "query": "graph database", |
| 102 | + "limit": 3 |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + results, _ = send_mcp_request(request, session_id) |
| 108 | + assert results, "No search results" |
| 109 | + |
| 110 | + print(f"✓ Vector search returned {len(results)} events") |
| 111 | + |
| 112 | +def main(): |
| 113 | + """Run all HTTP MCP tests.""" |
| 114 | + print("=" * 72) |
| 115 | + print("CodeGraph HTTP MCP Server Test") |
| 116 | + print("=" * 72) |
| 117 | + |
| 118 | + try: |
| 119 | + # Test initialize handshake |
| 120 | + session_id = test_initialize() |
| 121 | + |
| 122 | + # Test list tools |
| 123 | + test_list_tools(session_id) |
| 124 | + |
| 125 | + # Test vector search |
| 126 | + test_vector_search(session_id) |
| 127 | + |
| 128 | + print("\n" + "=" * 72) |
| 129 | + print("All tests passed! ✓") |
| 130 | + print("=" * 72) |
| 131 | + |
| 132 | + except Exception as e: |
| 133 | + print(f"\n✗ Test failed: {e}") |
| 134 | + raise |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + main() |
0 commit comments