4
4
from north_mcp_python_sdk import NorthMCPServer
5
5
from north_mcp_python_sdk .auth import get_authenticated_user , get_authenticated_user_optional
6
6
7
- mcp = NorthMCPServer ("Demo with Custom Routes" , port = 5222 )
7
+ # MCP server with operational endpoints for container orchestration
8
+ mcp = NorthMCPServer ("MCP Server with K8s Endpoints" , port = 5222 )
8
9
9
10
10
11
@mcp .tool ()
@@ -21,45 +22,71 @@ def add(a: int, b: int) -> int:
21
22
22
23
@mcp .custom_route ("/health" , methods = ["GET" ])
23
24
async def health_check (request : Request ) -> PlainTextResponse :
24
- """Health check endpoint - no authentication required """
25
+ """Kubernetes liveness probe endpoint - automatically bypasses authentication """
25
26
return PlainTextResponse ("OK" )
26
27
27
28
28
- @mcp .custom_route ("/status" , methods = ["GET" ])
29
- async def status_check (request : Request ) -> JSONResponse :
30
- """Status endpoint - no authentication required"""
29
+ @mcp .custom_route ("/ready" , methods = ["GET" ])
30
+ async def readiness_check (request : Request ) -> JSONResponse :
31
+ """Kubernetes readiness probe - checks if server is ready to accept traffic"""
32
+ # In a real implementation, you might check database connections,
33
+ # external service availability, etc.
31
34
return JSONResponse ({
32
- "status" : "running" ,
33
- "server" : "NorthMCP Demo" ,
34
- "authenticated" : False
35
+ "status" : "ready" ,
36
+ "server" : "MCP Server with K8s Endpoints" ,
37
+ "checks" : {
38
+ "mcp_protocol" : "ok" ,
39
+ "tools_loaded" : "ok"
40
+ }
35
41
})
36
42
37
43
38
- @mcp .custom_route ("/user-info" , methods = ["GET" ])
39
- async def user_info (request : Request ) -> JSONResponse :
40
- """User info endpoint that shows auth is optional for custom routes"""
44
+ @mcp .custom_route ("/metrics" , methods = ["GET" ])
45
+ async def metrics_endpoint (request : Request ) -> PlainTextResponse :
46
+ """Prometheus metrics endpoint for monitoring"""
47
+ # In a real implementation, you would return actual Prometheus metrics
48
+ metrics = """# HELP mcp_requests_total Total number of MCP requests
49
+ # TYPE mcp_requests_total counter
50
+ mcp_requests_total 42
51
+
52
+ # HELP mcp_tools_total Number of available MCP tools
53
+ # TYPE mcp_tools_total gauge
54
+ mcp_tools_total 1
55
+ """
56
+ return PlainTextResponse (metrics , media_type = "text/plain" )
57
+
58
+
59
+ @mcp .custom_route ("/status" , methods = ["GET" ])
60
+ async def status_check (request : Request ) -> JSONResponse :
61
+ """General status endpoint for monitoring dashboards"""
41
62
user = get_authenticated_user_optional ()
42
63
64
+ # This endpoint works without auth but can show auth info if provided
65
+ status_data = {
66
+ "status" : "running" ,
67
+ "server" : "MCP Server with K8s Endpoints" ,
68
+ "version" : "1.0.0" ,
69
+ "uptime_seconds" : 3600 , # In real implementation, track actual uptime
70
+ "authenticated_request" : user is not None
71
+ }
72
+
43
73
if user :
44
- return JSONResponse ({
45
- "authenticated" : True ,
46
- "email" : user .email ,
47
- "connectors" : list (user .connector_access_tokens .keys ())
48
- })
49
- else :
50
- return JSONResponse ({
51
- "authenticated" : False ,
52
- "message" : "This custom route works without authentication!"
53
- })
74
+ status_data ["user_email" ] = user .email
75
+ status_data ["available_connectors" ] = list (user .connector_access_tokens .keys ())
76
+
77
+ return JSONResponse (status_data )
54
78
55
79
56
80
if __name__ == "__main__" :
57
- print ("Starting server with custom routes..." )
58
- print ("Try these endpoints:" )
59
- print (" GET /health - Simple health check" )
60
- print (" GET /status - Status information" )
61
- print (" GET /user-info - Shows authentication is optional" )
62
- print (" POST /mcp - MCP protocol endpoint (requires auth)" )
63
- print (" GET /sse - Server-sent events endpoint (requires auth)" )
81
+ print ("Starting MCP server with Kubernetes operational endpoints..." )
82
+ print ("\n Operational endpoints (no authentication required):" )
83
+ print (" GET /health - Liveness probe for Kubernetes" )
84
+ print (" GET /ready - Readiness probe for Kubernetes" )
85
+ print (" GET /metrics - Prometheus metrics for monitoring" )
86
+ print (" GET /status - General status for dashboards" )
87
+ print ("\n MCP protocol endpoints (authentication required):" )
88
+ print (" POST /mcp - JSON-RPC MCP communication" )
89
+ print (" GET /sse - Server-sent events for streaming" )
90
+ print ("\n Perfect for deployment in Kubernetes with proper health checks!" )
64
91
65
92
mcp .run (transport = "streamable-http" )
0 commit comments