forked from yusufkaraaslan/Skill_Seekers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server_fastmcp_http.py
More file actions
156 lines (118 loc) · 4.74 KB
/
test_server_fastmcp_http.py
File metadata and controls
156 lines (118 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
"""
Tests for FastMCP server HTTP transport support.
"""
import sys
import pytest
# Skip all tests if mcp package is not installed
pytest.importorskip("mcp.server")
from starlette.testclient import TestClient
from skill_seekers.mcp.server_fastmcp import mcp
class TestFastMCPHTTP:
"""Test FastMCP HTTP transport functionality."""
def test_health_check_endpoint(self):
"""Test that health check endpoint returns correct response."""
# Skip if mcp is None (graceful degradation for testing)
if mcp is None:
pytest.skip("FastMCP not available (graceful degradation)")
# Get the SSE app
app = mcp.sse_app()
# Add health check endpoint
from starlette.responses import JSONResponse
from starlette.routing import Route
async def health_check(_request):
return JSONResponse(
{
"status": "healthy",
"server": "skill-seeker-mcp",
"version": "2.1.1",
"transport": "http",
"endpoints": {
"health": "/health",
"sse": "/sse",
"messages": "/messages/",
},
}
)
app.routes.insert(0, Route("/health", health_check, methods=["GET"]))
# Test with TestClient
with TestClient(app) as client:
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "healthy"
assert data["server"] == "skill-seeker-mcp"
assert data["transport"] == "http"
assert "endpoints" in data
assert data["endpoints"]["health"] == "/health"
assert data["endpoints"]["sse"] == "/sse"
def test_sse_endpoint_exists(self):
"""Test that SSE endpoint is available."""
# Skip if mcp is None (graceful degradation for testing)
if mcp is None:
pytest.skip("FastMCP not available (graceful degradation)")
app = mcp.sse_app()
with TestClient(app):
# SSE endpoint should exist (even if we can't fully test it without MCP client)
# Just verify the route is registered
routes = [route.path for route in app.routes if hasattr(route, "path")]
# The SSE app has routes registered by FastMCP
assert len(routes) > 0
def test_cors_middleware(self):
"""Test that CORS middleware can be added."""
# Skip if mcp is None (graceful degradation for testing)
if mcp is None:
pytest.skip("FastMCP not available (graceful degradation)")
app = mcp.sse_app()
from starlette.middleware.cors import CORSMiddleware
# Should be able to add CORS middleware without error
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Verify middleware was added
assert len(app.user_middleware) > 0
class TestArgumentParsing:
"""Test command-line argument parsing."""
def test_parse_args_default(self):
"""Test default argument parsing (stdio mode)."""
from skill_seekers.mcp.server_fastmcp import parse_args
# Save original argv
original_argv = sys.argv
try:
# Test default (no arguments)
sys.argv = ["server_fastmcp.py"]
args = parse_args()
assert args.http is False # Default is stdio
assert args.port == 8000
assert args.host == "127.0.0.1"
assert args.log_level == "INFO"
finally:
sys.argv = original_argv
def test_parse_args_http_mode(self):
"""Test HTTP mode argument parsing."""
from skill_seekers.mcp.server_fastmcp import parse_args
original_argv = sys.argv
try:
sys.argv = ["server_fastmcp.py", "--http", "--port", "8080", "--host", "0.0.0.0"]
args = parse_args()
assert args.http is True
assert args.port == 8080
assert args.host == "0.0.0.0"
finally:
sys.argv = original_argv
def test_parse_args_log_level(self):
"""Test log level argument parsing."""
from skill_seekers.mcp.server_fastmcp import parse_args
original_argv = sys.argv
try:
sys.argv = ["server_fastmcp.py", "--log-level", "DEBUG"]
args = parse_args()
assert args.log_level == "DEBUG"
finally:
sys.argv = original_argv
if __name__ == "__main__":
pytest.main([__file__, "-v"])