public_server.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import json
  2. import logging
  3. import threading
  4. import time
  5. import uuid
  6. from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
  7. from constants import DEVICE_INVALID_MESSAGE
  8. from mcp_protocol import McpProtocolHandler
  9. from services.request_context import RequestContextParser
  10. from utils.rate_limiter import SimpleRateLimiter
  11. logger = logging.getLogger(__name__)
  12. def extract_client_ip(headers, client_address):
  13. if client_address and len(client_address) > 0:
  14. return str(client_address[0])
  15. return ''
  16. class PublicMcpHttpHandler:
  17. def __init__(self, gateway_app, context_parser=None, rate_limiter=None):
  18. self.gateway_app = gateway_app
  19. self.context_parser = context_parser or RequestContextParser()
  20. self.rate_limiter = rate_limiter
  21. # Cache registered tool names at startup for rate-key validation;
  22. # unknown names fall back to the bare IP bucket, preventing bucket explosion
  23. self._known_tools = frozenset(t.get('name', '') for t in gateway_app.list_tools())
  24. def _check_rate_limit(self, client_ip, rate_key, method, request_id=None):
  25. """Returns an error response if rate limit exceeded, else None."""
  26. if self.rate_limiter and client_ip and not self.rate_limiter.is_allowed(rate_key):
  27. logger.warning(f"[RATE_LIMIT] rate limit exceeded: ip={client_ip}, method={method}")
  28. return McpProtocolHandler._error_response(request_id, -32000, 'Rate limit exceeded. Please try again later.')
  29. return None
  30. def handle_json_rpc(self, headers, message, client_ip=''):
  31. request_id = message.get('id') if isinstance(message, dict) else None
  32. method = str((message or {}).get('method') or '').strip()
  33. try:
  34. if method == 'initialize':
  35. # initialize is a stateless handshake that only returns server metadata;
  36. # rate-limiting it would block clients from connecting at all, so we skip it.
  37. return McpProtocolHandler._success_response(request_id, {
  38. 'protocolVersion': McpProtocolHandler.protocol_version,
  39. 'capabilities': {'tools': {'listChanged': False}},
  40. 'serverInfo': {
  41. 'name': McpProtocolHandler.server_name,
  42. 'version': McpProtocolHandler.server_version,
  43. },
  44. })
  45. if method == 'tools/list':
  46. # Use a dedicated bucket for list so it doesn't compete with tools/call quota
  47. blocked = self._check_rate_limit(client_ip, 'list:{0}'.format(client_ip), method, request_id)
  48. if blocked:
  49. return blocked
  50. tools = []
  51. for tool in self.gateway_app.list_tools():
  52. normalized = dict(tool)
  53. if 'input_schema' in normalized:
  54. normalized['inputSchema'] = normalized.pop('input_schema')
  55. tools.append(normalized)
  56. return McpProtocolHandler._success_response(request_id, {'tools': tools})
  57. if method == 'tools/call':
  58. # Per-tool independent quota: only registered tool names get their own bucket;
  59. # unrecognized names fall into the shared IP bucket to prevent bucket explosion
  60. tool_name = ((message.get('params') or {}).get('name') or '').strip()
  61. rate_key = '{0}:{1}'.format(client_ip, tool_name) if tool_name in self._known_tools else client_ip
  62. blocked = self._check_rate_limit(client_ip, rate_key, method, request_id)
  63. if blocked:
  64. return blocked
  65. context = self.context_parser.parse(headers or {})
  66. if not context.has_session():
  67. raise RuntimeError(DEVICE_INVALID_MESSAGE)
  68. params = message.get('params') or {}
  69. result = self.gateway_app.call_tool(
  70. context.gateway_session_id,
  71. params.get('name'),
  72. params.get('arguments') or {},
  73. request_id='rq_http_{0}_{1}'.format(request_id, uuid.uuid4().hex[:16]),
  74. client_ip=client_ip,
  75. )
  76. structured_content = result.get('data') or {}
  77. return McpProtocolHandler._success_response(request_id, {
  78. 'content': [{'type': 'text', 'text': McpProtocolHandler._render_text(structured_content)}],
  79. 'structuredContent': structured_content,
  80. 'isError': False,
  81. })
  82. return McpProtocolHandler._error_response(request_id, -32601, 'Method not found: {0}'.format(method))
  83. except Exception as exc:
  84. if method == 'tools/call':
  85. return McpProtocolHandler._success_response(request_id, {
  86. 'content': [{'type': 'text', 'text': str(exc)}],
  87. 'isError': True,
  88. })
  89. return McpProtocolHandler._error_response(request_id, -32000, str(exc))
  90. def create_http_handler(gateway_app, rate_limiter=None):
  91. rpc_handler = PublicMcpHttpHandler(gateway_app, rate_limiter=rate_limiter)
  92. class Handler(BaseHTTPRequestHandler):
  93. def log_message(self, format, *args):
  94. # Override to use Python logging instead of stderr
  95. logger.info(f"{self.address_string()} - {format % args}")
  96. def do_GET(self):
  97. if self.path == '/health':
  98. self._write_json({'ok': True})
  99. return
  100. self.send_response(404)
  101. self.end_headers()
  102. def do_POST(self):
  103. client_ip = extract_client_ip(dict(self.headers.items()), self.client_address)
  104. if self.path != '/mcp':
  105. logger.warning(f"[HTTP] 404: ip={client_ip}, path={self.path}")
  106. self.send_response(404)
  107. self.end_headers()
  108. return
  109. length = int(self.headers.get('Content-Length') or '0')
  110. body = self.rfile.read(length).decode('utf-8-sig')
  111. try:
  112. message = json.loads(body)
  113. except json.JSONDecodeError as exc:
  114. logger.warning(f"[HTTP] invalid JSON: ip={client_ip}, error={exc}")
  115. self._write_json(McpProtocolHandler._error_response(None, -32700, 'Parse error'))
  116. return
  117. method = message.get('method', '')
  118. request_id = message.get('id') if message.get('id') is not None else ''
  119. logger.info(f"[HTTP] request: ip={client_ip}, method={method}, id={request_id}")
  120. response = rpc_handler.handle_json_rpc(dict(self.headers.items()), message, client_ip)
  121. self._write_json(response)
  122. def _write_json(self, payload):
  123. raw = json.dumps(payload, ensure_ascii=False).encode('utf-8')
  124. self.send_response(200)
  125. self.send_header('Content-Type', 'application/json; charset=utf-8')
  126. self.send_header('Content-Length', str(len(raw)))
  127. self.end_headers()
  128. self.wfile.write(raw)
  129. return Handler
  130. def serve_public(gateway_app, host='0.0.0.0', port=8765, enable_rate_limit=True,
  131. rate_limit_max_requests=60, rate_limit_window_seconds=60):
  132. # Configure logging
  133. logging.basicConfig(
  134. level=logging.INFO,
  135. format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  136. datefmt='%Y-%m-%d %H:%M:%S'
  137. )
  138. # Configure rate limiting
  139. rate_limiter = None
  140. if enable_rate_limit:
  141. rate_limiter = SimpleRateLimiter(
  142. max_requests=rate_limit_max_requests,
  143. window_seconds=rate_limit_window_seconds,
  144. )
  145. logger.info(f"Rate limiting enabled: {rate_limit_max_requests} requests/{rate_limit_window_seconds}s per IP")
  146. logger.info(f"Starting public MCP Gateway on {host}:{port}")
  147. server = ThreadingHTTPServer((host, int(port)), create_http_handler(gateway_app, rate_limiter))
  148. # Schedule periodic cleanup to prevent unbounded memory growth in the rate limiter
  149. if rate_limiter is not None:
  150. def _cleanup_loop():
  151. while True:
  152. time.sleep(300)
  153. # Use the actual window as max_age to avoid deleting entries still within the window
  154. rate_limiter.cleanup(max_age_seconds=rate_limit_window_seconds)
  155. t = threading.Thread(target=_cleanup_loop, daemon=True)
  156. t.start()
  157. try:
  158. server.serve_forever()
  159. except KeyboardInterrupt:
  160. logger.info("Shutting down public MCP Gateway")
  161. server.shutdown()