public_server.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import hashlib
  2. import json
  3. import logging
  4. import threading
  5. import time
  6. import uuid
  7. from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
  8. from constants import DEVICE_INVALID_MESSAGE
  9. from mcp_protocol import McpProtocolHandler
  10. from services.request_context import RequestContextParser
  11. from utils.rate_limiter import SimpleRateLimiter
  12. logger = logging.getLogger(__name__)
  13. def extract_client_ip(headers, client_address):
  14. if client_address and len(client_address) > 0:
  15. return str(client_address[0])
  16. return ''
  17. class PublicMcpHttpHandler:
  18. def __init__(self, gateway_app, context_parser=None, rate_limiter=None):
  19. self.gateway_app = gateway_app
  20. self.context_parser = context_parser or RequestContextParser()
  21. self.rate_limiter = rate_limiter
  22. # Cache registered tool names at startup for rate-key validation;
  23. # unknown names fall back to the bare IP bucket, preventing bucket explosion
  24. self._known_tools = frozenset(gateway_app.registered_tool_names())
  25. @staticmethod
  26. def _build_trace_request_id(headers):
  27. return 'rq_http_{0}'.format(uuid.uuid4().hex[:16])
  28. @staticmethod
  29. def _client_request_id(headers):
  30. incoming = ''
  31. for key, value in (headers or {}).items():
  32. if str(key).lower() == 'x-request-id':
  33. incoming = str(value or '').strip()
  34. break
  35. if not incoming:
  36. return ''
  37. return hashlib.sha256(incoming.encode('utf-8')).hexdigest()[:16]
  38. def _check_rate_limit(
  39. self,
  40. rate_key,
  41. method,
  42. trace_request_id,
  43. jsonrpc_id=None,
  44. tool_name='',
  45. *,
  46. log_identity=None
  47. ):
  48. """Returns an error response if rate limit exceeded, else None.
  49. rate_key — the bucket key used by the limiter (session-based for tools/call,
  50. IP-based for tools/list)
  51. log_identity — optional string shown in warning logs (e.g. client_ip)
  52. """
  53. if self.rate_limiter and rate_key and not self.rate_limiter.is_allowed(rate_key):
  54. logger.warning(
  55. "MCP public rate limit exceeded",
  56. extra={
  57. 'request_id': trace_request_id,
  58. 'jsonrpc_id': jsonrpc_id,
  59. 'protocol_method': method,
  60. 'tool_code': tool_name,
  61. 'client_identity': log_identity or rate_key,
  62. 'protocol_code': -32029,
  63. 'diagnostic_reason': 'RATE_LIMIT_EXCEEDED',
  64. },
  65. )
  66. return McpProtocolHandler._error_response(
  67. jsonrpc_id,
  68. -32029,
  69. 'Rate limit exceeded. Please try again later.',
  70. trace_request_id,
  71. )
  72. return None
  73. def handle_json_rpc(
  74. self,
  75. headers,
  76. message,
  77. client_ip='',
  78. trace_request_id='',
  79. ):
  80. request_id = message.get('id') if isinstance(message, dict) else None
  81. trace_request_id = str(trace_request_id or '').strip() \
  82. or self._build_trace_request_id(headers)
  83. method = str((message or {}).get('method') or '').strip()
  84. message_params = (message or {}).get('params') or {}
  85. tool_name = str(message_params.get('name') or '').strip() \
  86. if isinstance(message_params, dict) else ''
  87. try:
  88. if method == 'initialize':
  89. # initialize is a stateless handshake that only returns server metadata;
  90. # rate-limiting it would block clients from connecting at all, so we skip it.
  91. return McpProtocolHandler._success_response(request_id, {
  92. 'protocolVersion': McpProtocolHandler.protocol_version,
  93. 'capabilities': {'tools': {'listChanged': False}},
  94. 'serverInfo': {
  95. 'name': McpProtocolHandler.server_name,
  96. 'version': McpProtocolHandler.server_version,
  97. },
  98. })
  99. if method == 'tools/list':
  100. # Keep list traffic in a dedicated IP-based bucket so it does not
  101. # compete with the per-session tools/call quota.
  102. blocked = self._check_rate_limit(
  103. 'list:{0}'.format(client_ip),
  104. method,
  105. trace_request_id,
  106. request_id,
  107. log_identity=client_ip,
  108. )
  109. if blocked:
  110. return blocked
  111. context = self.context_parser.parse(headers or {})
  112. if not context.has_session():
  113. logger.warning(
  114. 'MCP public device session unavailable',
  115. extra={
  116. 'request_id': trace_request_id,
  117. 'jsonrpc_id': request_id,
  118. 'protocol_method': method,
  119. 'tool_code': '',
  120. 'protocol_code': -32001,
  121. 'diagnostic_reason': 'GATEWAY_SESSION_NOT_FOUND',
  122. },
  123. )
  124. return McpProtocolHandler._error_response(
  125. request_id,
  126. -32001,
  127. DEVICE_INVALID_MESSAGE,
  128. trace_request_id,
  129. )
  130. tools = []
  131. for tool in self.gateway_app.list_tools(
  132. context.gateway_session_id,
  133. request_id=trace_request_id,
  134. ):
  135. normalized = dict(tool)
  136. if 'input_schema' in normalized:
  137. normalized['inputSchema'] = normalized.pop('input_schema')
  138. tools.append(normalized)
  139. return McpProtocolHandler._success_response(request_id, {'tools': tools})
  140. if method == 'tools/call':
  141. if not isinstance(message_params, dict):
  142. raise ValueError('tool parameters must be an object')
  143. # Parse context first — tools/call always requires a valid session
  144. context = self.context_parser.parse(headers or {})
  145. if not context.has_session():
  146. logger.warning(
  147. 'MCP public device session unavailable',
  148. extra={
  149. 'request_id': trace_request_id,
  150. 'jsonrpc_id': request_id,
  151. 'protocol_method': method,
  152. 'tool_code': tool_name,
  153. 'protocol_code': -32001,
  154. 'diagnostic_reason': 'GATEWAY_SESSION_NOT_FOUND',
  155. },
  156. )
  157. return McpProtocolHandler._error_response(
  158. request_id,
  159. -32001,
  160. DEVICE_INVALID_MESSAGE,
  161. trace_request_id,
  162. )
  163. # Session-based rate limiting: each employee gets an independent quota per tool.
  164. # Unknown tool names fall back to the bare session bucket to prevent key explosion.
  165. session_id = context.gateway_session_id
  166. rate_key = '{0}:{1}'.format(session_id, tool_name) if tool_name in self._known_tools else session_id
  167. blocked = self._check_rate_limit(
  168. rate_key,
  169. method,
  170. trace_request_id,
  171. request_id,
  172. tool_name,
  173. log_identity=client_ip,
  174. )
  175. if blocked:
  176. return blocked
  177. params = message_params
  178. result = self.gateway_app.call_tool(
  179. session_id,
  180. params.get('name'),
  181. params.get('arguments') or {},
  182. request_id=trace_request_id,
  183. client_ip=client_ip,
  184. )
  185. return McpProtocolHandler._tool_call_response(
  186. request_id,
  187. tool_name,
  188. result,
  189. )
  190. return McpProtocolHandler._error_response(
  191. request_id,
  192. -32601,
  193. 'Method not found: {0}'.format(method),
  194. trace_request_id,
  195. )
  196. except Exception as exc:
  197. if method == 'tools/call':
  198. diagnostic_reason = (
  199. 'PARAM_VALIDATION_FAILED'
  200. if isinstance(exc, ValueError)
  201. else 'UNEXPECTED_EXCEPTION'
  202. )
  203. logger.error(
  204. 'MCP public tool request failed',
  205. extra={
  206. 'request_id': trace_request_id,
  207. 'jsonrpc_id': request_id,
  208. 'protocol_method': method,
  209. 'tool_code': tool_name,
  210. 'response_code': 'MCP_9001',
  211. 'diagnostic_reason': diagnostic_reason,
  212. 'exception_class': exc.__class__.__name__,
  213. },
  214. )
  215. return McpProtocolHandler._tool_exception_response(
  216. request_id,
  217. tool_name,
  218. exc,
  219. trace_request_id,
  220. )
  221. logger.error(
  222. "MCP public request failed",
  223. extra={
  224. 'request_id': trace_request_id,
  225. 'jsonrpc_id': request_id,
  226. 'protocol_method': method,
  227. 'tool_code': tool_name,
  228. 'protocol_code': -32000,
  229. 'diagnostic_reason': 'UNEXPECTED_EXCEPTION',
  230. 'exception_class': exc.__class__.__name__,
  231. },
  232. )
  233. return McpProtocolHandler._error_response(
  234. request_id,
  235. -32000,
  236. 'Gateway request failed. Please try again later.',
  237. trace_request_id,
  238. )
  239. def create_http_handler(gateway_app, rate_limiter=None):
  240. rpc_handler = PublicMcpHttpHandler(gateway_app, rate_limiter=rate_limiter)
  241. class Handler(BaseHTTPRequestHandler):
  242. def log_message(self, format, *args):
  243. # Override to use Python logging instead of stderr
  244. logger.info(f"{self.address_string()} - {format % args}")
  245. def do_GET(self):
  246. if self.path == '/health':
  247. self._write_json({'ok': True})
  248. return
  249. self.send_response(404)
  250. self.end_headers()
  251. def do_POST(self):
  252. request_headers = dict(self.headers.items())
  253. client_ip = extract_client_ip(request_headers, self.client_address)
  254. trace_request_id = rpc_handler._build_trace_request_id(request_headers)
  255. client_request_id_hash = rpc_handler._client_request_id(
  256. request_headers
  257. )
  258. length = int(self.headers.get('Content-Length') or '0')
  259. if self.path != '/mcp':
  260. logger.warning(f"[HTTP] 404: ip={client_ip}, path={self.path}")
  261. if length > 0:
  262. self.rfile.read(length)
  263. self.send_response(404)
  264. self.end_headers()
  265. return
  266. body = self.rfile.read(length).decode('utf-8-sig')
  267. try:
  268. message = json.loads(body)
  269. except json.JSONDecodeError as exc:
  270. logger.warning(
  271. 'MCP public invalid JSON',
  272. extra={
  273. 'request_id': trace_request_id,
  274. 'jsonrpc_id': None,
  275. 'protocol_method': '',
  276. 'tool_code': '',
  277. 'protocol_code': -32700,
  278. 'diagnostic_reason': 'PARAM_VALIDATION_FAILED',
  279. 'exception_class': exc.__class__.__name__,
  280. 'client_identity': client_ip,
  281. },
  282. )
  283. self._write_json(McpProtocolHandler._error_response(
  284. None,
  285. -32700,
  286. 'Parse error',
  287. trace_request_id,
  288. ))
  289. return
  290. method = message.get('method', '')
  291. request_id = message.get('id') if message.get('id') is not None else ''
  292. logger.info(
  293. 'MCP public request',
  294. extra={
  295. 'request_id': trace_request_id,
  296. 'jsonrpc_id': request_id,
  297. 'protocol_method': method,
  298. 'client_identity': client_ip,
  299. 'client_request_id_hash': client_request_id_hash,
  300. },
  301. )
  302. response = rpc_handler.handle_json_rpc(
  303. request_headers,
  304. message,
  305. client_ip,
  306. trace_request_id=trace_request_id,
  307. )
  308. self._write_json(response)
  309. def _write_json(self, payload):
  310. raw = json.dumps(payload, ensure_ascii=False).encode('utf-8')
  311. self.send_response(200)
  312. self.send_header('Content-Type', 'application/json; charset=utf-8')
  313. self.send_header('Content-Length', str(len(raw)))
  314. self.end_headers()
  315. self.wfile.write(raw)
  316. return Handler
  317. def serve_public(gateway_app, host='0.0.0.0', port=8765, enable_rate_limit=True,
  318. rate_limit_max_requests=60, rate_limit_window_seconds=60):
  319. # Configure logging
  320. logging.basicConfig(
  321. level=logging.INFO,
  322. format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  323. datefmt='%Y-%m-%d %H:%M:%S'
  324. )
  325. # Configure rate limiting
  326. rate_limiter = None
  327. if enable_rate_limit:
  328. rate_limiter = SimpleRateLimiter(
  329. max_requests=rate_limit_max_requests,
  330. window_seconds=rate_limit_window_seconds,
  331. )
  332. logger.info(f"Rate limiting enabled: {rate_limit_max_requests} requests/{rate_limit_window_seconds}s per session (tools/call) / per IP (tools/list)")
  333. logger.info(f"Starting public MCP Gateway on {host}:{port}")
  334. server = ThreadingHTTPServer((host, int(port)), create_http_handler(gateway_app, rate_limiter))
  335. # Schedule periodic cleanup to prevent unbounded memory growth in the rate limiter
  336. if rate_limiter is not None:
  337. def _cleanup_loop():
  338. while True:
  339. time.sleep(300)
  340. # Use the actual window as max_age to avoid deleting entries still within the window
  341. rate_limiter.cleanup(max_age_seconds=rate_limit_window_seconds)
  342. t = threading.Thread(target=_cleanup_loop, daemon=True)
  343. t.start()
  344. try:
  345. server.serve_forever()
  346. except KeyboardInterrupt:
  347. logger.info("Shutting down public MCP Gateway")
  348. server.shutdown()