Lab
Build a Production-Ready MCP Server
45 min
Advanced3 Free Attempts
Instructions
Objective
Build a Python class that wraps an MCP server with production-ready features including health checks, metrics collection, and structured logging.
Requirements
Create a class ProductionMCPServer that:
-
Tracks metrics with
record_tool_call(tool_name: str, success: bool, duration_ms: float):- Stores counts and latencies per tool
- Returns nothing
-
Provides metrics summary with
get_metrics() -> dict:- Returns:
{"tools": {tool_name: {"calls": int, "successes": int, "errors": int, "avg_latency_ms": float}}}
- Returns:
-
Logs events with
log(level: str, message: str, extra: dict = None):- Stores logs with timestamp, level, message, and extra data
- Returns nothing
-
Gets logs with
get_logs() -> list:- Returns list of log entries
-
Checks health with
health_check() -> dict:- Returns:
{"status": "healthy"|"unhealthy", "checks": {"metrics": bool, "logs": bool}} - Status is healthy if all checks pass
- Returns:
Example Usage
server = ProductionMCPServer()
# Record some tool calls
server.record_tool_call("search", success=True, duration_ms=150.0)
server.record_tool_call("search", success=True, duration_ms=200.0)
server.record_tool_call("search", success=False, duration_ms=50.0)
# Get metrics
metrics = server.get_metrics()
# {
# "tools": {
# "search": {
# "calls": 3,
# "successes": 2,
# "errors": 1,
# "avg_latency_ms": 133.33
# }
# }
# }
# Log events
server.log("info", "Server started", {"port": 8000})
server.log("error", "Database connection failed")
# Get logs
logs = server.get_logs()
# [{"timestamp": "...", "level": "info", "message": "Server started", "extra": {"port": 8000}}, ...]
# Health check
health = server.health_check()
# {"status": "healthy", "checks": {"metrics": True, "logs": True}}
Hints
- Use
datetime.utcnow().isoformat()for timestamps - Average latency = sum of all latencies / number of calls
- Health check should verify metrics and logs are accessible
Grading Rubric
record_tool_call correctly tracks calls, successes, errors, and latency25 points
get_metrics returns correct summary with average latency calculation25 points
log and get_logs correctly store and retrieve structured log entries25 points
health_check returns correct status based on system checks25 points
Your Solution
This lab requires Python
🐍Python(required)
3 free attempts remaining