public_server.py 9.1 KB

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