test_server.py•4.65 kB
"""
Simple test script to verify MCP server is working.
Run this while the server is running in another terminal.
"""
import requests
import json
def test_health_check():
"""Test basic health check endpoint."""
print("\n=== Test 1: Health Check ===")
response = requests.get("http://localhost:8000/")
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
assert response.status_code == 200
assert response.json()["status"] == "running"
print("✓ PASSED")
def test_mcp_initialize():
"""Test MCP initialize protocol."""
print("\n=== Test 2: MCP Initialize ===")
payload = {
"jsonrpc": "2.0",
"method": "initialize",
"params": {},
"id": 1
}
response = requests.post(
"http://localhost:8000/mcp/message",
json=payload
)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
assert response.status_code == 200
result = response.json()
assert result["jsonrpc"] == "2.0"
assert "result" in result
assert result["result"]["protocolVersion"] == "2024-11-05"
print("✓ PASSED")
def test_tools_list():
"""Test listing available tools."""
print("\n=== Test 3: List Tools ===")
payload = {
"jsonrpc": "2.0",
"method": "tools/list",
"params": {},
"id": 2
}
response = requests.post(
"http://localhost:8000/mcp/message",
json=payload
)
print(f"Status Code: {response.status_code}")
result = response.json()
tools = result["result"]["tools"]
print(f"Found {len(tools)} tools:")
for tool in tools:
print(f" - {tool['name']}: {tool['description'][:50]}...")
assert len(tools) == 3
tool_names = [t["name"] for t in tools]
assert "github_get_repo" in tool_names
assert "github_search_code" in tool_names
assert "github_get_file" in tool_names
print("✓ PASSED")
def test_tool_call():
"""Test calling a tool (placeholder response)."""
print("\n=== Test 4: Call Tool (github_get_repo) ===")
payload = {
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "github_get_repo",
"arguments": {
"owner": "anthropics",
"repo": "mcp"
}
},
"id": 3
}
response = requests.post(
"http://localhost:8000/mcp/message",
json=payload
)
print(f"Status Code: {response.status_code}")
result = response.json()
content = result["result"]["content"][0]["text"]
print(f"Response preview: {content[:100]}...")
assert "anthropics/mcp" in content
assert "PLACEHOLDER" in content
print("✓ PASSED (Returns placeholder as expected)")
def test_invalid_method():
"""Test error handling for invalid method."""
print("\n=== Test 5: Invalid Method (Error Handling) ===")
payload = {
"jsonrpc": "2.0",
"method": "invalid_method",
"params": {},
"id": 4
}
response = requests.post(
"http://localhost:8000/mcp/message",
json=payload
)
print(f"Status Code: {response.status_code}")
result = response.json()
print(f"Response: {json.dumps(result, indent=2)}")
assert "error" in result
assert result["error"]["code"] == -32603
print("✓ PASSED (Error handled correctly)")
def main():
"""Run all tests."""
print("=" * 60)
print("MCP Server Test Suite")
print("=" * 60)
print("\nMake sure the server is running at http://localhost:8000")
print("Run: python src/server/main.py")
print()
input("Press Enter when server is ready...")
try:
# Run tests
test_health_check()
test_mcp_initialize()
test_tools_list()
test_tool_call()
test_invalid_method()
print("\n" + "=" * 60)
print("ALL TESTS PASSED!")
print("=" * 60)
print("\nYour MCP server is working correctly!")
print("Next steps:")
print(" 1. Implement GitHub API integration")
print(" 2. Add vector database")
print(" 3. Implement RAG retrieval pipeline")
except requests.exceptions.ConnectionError:
print("\n❌ ERROR: Could not connect to server!")
print("Make sure the server is running:")
print(" python src/server/main.py")
except AssertionError as e:
print(f"\n❌ TEST FAILED: {e}")
except Exception as e:
print(f"\n❌ UNEXPECTED ERROR: {e}")
if __name__ == "__main__":
main()