app.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. import argparse
  2. import json
  3. import sys
  4. import uuid
  5. from config import GatewayConfig
  6. from constants import DEVICE_INVALID_MESSAGE
  7. from mcp_protocol import McpProtocolHandler
  8. from public_gateway import PublicGatewayApp
  9. from public_server import serve_public
  10. from services.api_client import ApiClient
  11. from services.auth_client import AuthClient
  12. from services.gateway_session_store import GatewaySessionStore
  13. from services.scoped_api_client import ScopedApiClient
  14. from services.token_store import FileTokenStore, RedisSocketClient, RedisTokenStore
  15. from tools.list_order_filter_options import ListOrderFilterOptionsTool
  16. from tools.list_outbound_filter_options import ListOutboundFilterOptionsTool
  17. from tools.export_pending_outbound_orders import ExportPendingOutboundOrdersTool
  18. from tools.export_out_of_province_port_data import (
  19. ExportOutOfProvincePortDataTool,
  20. )
  21. from tools.list_pending_outbound_export_filter_options import (
  22. ListPendingOutboundExportFilterOptionsTool,
  23. )
  24. from tools.query_order import QueryOrderTool
  25. from tools.query_customs_declaration_files import (
  26. QueryCustomsDeclarationFilesTool,
  27. )
  28. from tools.query_order_exact import QueryOrderExactTool
  29. from tools.query_order_detail import QueryOrderDetailTool
  30. from tools.query_outbound_detail import QueryOutboundDetailTool
  31. from tools.query_outbound_list import QueryOutboundListTool
  32. from tools.query_track import QueryTrackTool
  33. def parse_int_list(value):
  34. if not value:
  35. return []
  36. return [int(item.strip()) for item in value.split(',') if item.strip()]
  37. def parse_string_list(value):
  38. result = []
  39. for item in (value or '').split(','):
  40. item = item.strip()
  41. if item and item not in result:
  42. result.append(item)
  43. return result
  44. class GatewayApp:
  45. def __init__(self, auth_client=None, api_client=None, token_store=None):
  46. self.auth_client = auth_client
  47. self.api_client = api_client
  48. self.token_store = token_store
  49. self._tools = {
  50. 'query_order': QueryOrderTool(api_client=api_client),
  51. 'query_track': QueryTrackTool(api_client=api_client),
  52. 'query_order_exact': QueryOrderExactTool(api_client=api_client),
  53. 'query_order_detail': QueryOrderDetailTool(api_client=api_client),
  54. 'query_customs_declaration_files':
  55. QueryCustomsDeclarationFilesTool(api_client=api_client),
  56. 'query_outbound_list': QueryOutboundListTool(api_client=api_client),
  57. 'query_outbound_detail': QueryOutboundDetailTool(api_client=api_client),
  58. 'list_outbound_filter_options': ListOutboundFilterOptionsTool(
  59. api_client=api_client
  60. ),
  61. 'list_order_filter_options': ListOrderFilterOptionsTool(
  62. api_client=api_client
  63. ),
  64. 'export_pending_outbound_orders': ExportPendingOutboundOrdersTool(
  65. api_client=api_client
  66. ),
  67. 'export_out_of_province_port_data':
  68. ExportOutOfProvincePortDataTool(api_client=api_client),
  69. 'list_pending_outbound_export_filter_options':
  70. ListPendingOutboundExportFilterOptionsTool(api_client=api_client),
  71. }
  72. @classmethod
  73. def from_config(cls, config, redis_client=None):
  74. if config.token_store_type == 'redis':
  75. redis = redis_client or RedisSocketClient(
  76. host=config.redis_host,
  77. port=config.redis_port,
  78. db=config.redis_db,
  79. password=config.redis_password,
  80. timeout=config.timeout_seconds,
  81. )
  82. token_store = RedisTokenStore(
  83. redis,
  84. prefix=config.redis_prefix,
  85. session_key=config.session_key,
  86. refresh_skew_seconds=config.refresh_skew_seconds,
  87. )
  88. elif config.token_store_type == 'file':
  89. token_store = FileTokenStore(
  90. config.token_store_path,
  91. refresh_skew_seconds=config.refresh_skew_seconds,
  92. )
  93. else:
  94. raise ValueError('unsupported token store type: {0}'.format(config.token_store_type))
  95. auth_client = AuthClient(
  96. base_url=config.auth_base_url,
  97. client_type=config.client_type,
  98. token_store=token_store,
  99. timeout=config.timeout_seconds,
  100. session_key=config.session_key,
  101. )
  102. api_client = ApiClient(
  103. base_url=config.tools_base_url,
  104. token_store=token_store,
  105. timeout=config.timeout_seconds,
  106. )
  107. return cls(auth_client=auth_client, api_client=api_client, token_store=token_store)
  108. def registered_tool_names(self):
  109. return tuple(self._tools.keys())
  110. def _enabled_tool_names(self, response):
  111. if not isinstance(response, dict):
  112. raise RuntimeError('invalid enabled tool response')
  113. if response.get('code') != 'MCP_0000':
  114. raise RuntimeError(response.get('msg') or 'list enabled tools failed')
  115. data = response.get('data')
  116. codes = data.get('tool_codes') if isinstance(data, dict) else None
  117. if not isinstance(codes, list):
  118. raise RuntimeError('invalid enabled tool response')
  119. return {
  120. code.strip().lower()
  121. for code in codes
  122. if isinstance(code, str) and code.strip()
  123. }
  124. def _load_enabled_tool_names(self, request_id=''):
  125. if self.api_client is None or not hasattr(self.api_client, 'list_enabled_tools'):
  126. raise RuntimeError('enabled tool client unavailable')
  127. return self._enabled_tool_names(
  128. self.api_client.list_enabled_tools(request_id=request_id)
  129. )
  130. def list_tools(self, request_id=''):
  131. request_id = self.build_request_id(request_id)
  132. enabled = self._load_enabled_tool_names(request_id)
  133. return [
  134. tool.metadata()
  135. for name, tool in self._tools.items()
  136. if name in enabled
  137. ]
  138. def build_request_id(self, request_id=''):
  139. request_id = str(request_id or '').strip()
  140. if request_id:
  141. return request_id
  142. return 'rq_{0}'.format(uuid.uuid4().hex[:16])
  143. def ensure_session(self):
  144. if self.token_store is None:
  145. return
  146. session = self.token_store.get()
  147. if not session or not session.get('token'):
  148. raise RuntimeError(DEVICE_INVALID_MESSAGE)
  149. if self.token_store.is_expiring():
  150. if self.auth_client is None:
  151. raise RuntimeError('mcp token expiring but auth client missing')
  152. self.auth_client.refresh(session['token'])
  153. def call_tool(self, name, arguments=None, request_id=''):
  154. if name not in self._tools:
  155. raise KeyError('tool not registered: {0}'.format(name))
  156. tool = self._tools[name]
  157. if getattr(tool, 'requires_session', True):
  158. self.ensure_session()
  159. request_id = self.build_request_id(request_id)
  160. if name not in self._load_enabled_tool_names(request_id):
  161. raise RuntimeError('tool disabled: {0}'.format(name))
  162. arguments = arguments or {}
  163. return tool.call(request_id=request_id, **arguments)
  164. def create_protocol_handler(self):
  165. return McpProtocolHandler(self)
  166. def run_cli(self, argv=None, stdin=None, stdout=None):
  167. stdin = stdin or sys.stdin
  168. stdout = stdout or sys.stdout
  169. parser = argparse.ArgumentParser(prog='mcp-gateway')
  170. subparsers = parser.add_subparsers(dest='command', required=True)
  171. subparsers.add_parser('list-tools')
  172. subparsers.add_parser('serve-stdio')
  173. public_parser = subparsers.add_parser('serve-public')
  174. public_parser.add_argument('--host', default='0.0.0.0')
  175. public_parser.add_argument('--port', type=int, default=8765)
  176. call_parser = subparsers.add_parser('call')
  177. call_parser.add_argument('--tool', required=True)
  178. call_parser.add_argument('--keyword', default='')
  179. call_parser.add_argument('--order-id', type=int, default=0)
  180. call_parser.add_argument('--order-number', default='')
  181. call_parser.add_argument('--section', default='全部')
  182. call_parser.add_argument('--order-numbers', default='')
  183. call_parser.add_argument('--tracking-number', default='')
  184. call_parser.add_argument('--tracking-numbers', default='')
  185. call_parser.add_argument('--reference-number', default='')
  186. call_parser.add_argument('--reference-numbers', default='')
  187. call_parser.add_argument('--outbound-number', default='')
  188. call_parser.add_argument('--outbound-numbers', default='')
  189. call_parser.add_argument('--bl-numbers', default='')
  190. call_parser.add_argument('--container-code', default='')
  191. call_parser.add_argument('--container-codes', default='')
  192. call_parser.add_argument('--so-number', default='')
  193. call_parser.add_argument('--so-numbers', default='')
  194. call_parser.add_argument('--shipment-id', default='')
  195. call_parser.add_argument('--receiver-country', default='')
  196. call_parser.add_argument('--product-ids', default='')
  197. call_parser.add_argument('--customer-ids', default='')
  198. call_parser.add_argument('--sales-id', type=int, default=0)
  199. call_parser.add_argument('--warehouse-ids', default='')
  200. call_parser.add_argument('--warehouse-id', type=int, default=0)
  201. call_parser.add_argument('--department-id', type=int, default=0)
  202. call_parser.add_argument('--outbound-status', type=int, default=0)
  203. call_parser.add_argument('--shipping-method', type=int, default=0)
  204. call_parser.add_argument('--is-direct-send', type=int, default=None)
  205. call_parser.add_argument('--trailer-types', default='')
  206. call_parser.add_argument('--declaration-types', default='')
  207. call_parser.add_argument('--clearance-types', default='')
  208. call_parser.add_argument('--closing-time-start', default='')
  209. call_parser.add_argument('--closing-time-end', default='')
  210. call_parser.add_argument('--est-loading-time-start', default='')
  211. call_parser.add_argument('--est-loading-time-end', default='')
  212. call_parser.add_argument('--create-date-start', default='')
  213. call_parser.add_argument('--create-date-end', default='')
  214. call_parser.add_argument('--loading-time-start', default='')
  215. call_parser.add_argument('--loading-time-end', default='')
  216. call_parser.add_argument('--inbound-date-start', default='')
  217. call_parser.add_argument('--inbound-date-end', default='')
  218. call_parser.add_argument('--outbound-date-start', default='')
  219. call_parser.add_argument('--outbound-date-end', default='')
  220. call_parser.add_argument('--filter-type', default='')
  221. call_parser.add_argument('--page', type=int, default=1)
  222. call_parser.add_argument('--limit', type=int, default=20)
  223. call_parser.add_argument('--request-id', default='')
  224. args = parser.parse_args(argv or [])
  225. if args.command == 'list-tools':
  226. payload = self.list_tools()
  227. elif args.command == 'serve-stdio':
  228. return self.create_protocol_handler().run_stdio(stdin=stdin, stdout=stdout)
  229. elif args.command == 'serve-public':
  230. config = GatewayConfig.from_env()
  231. redis = RedisSocketClient(
  232. host=config.redis_host,
  233. port=config.redis_port,
  234. db=config.redis_db,
  235. password=config.redis_password,
  236. timeout=config.timeout_seconds,
  237. )
  238. session_store = GatewaySessionStore(
  239. redis,
  240. prefix=config.redis_prefix,
  241. ttl_seconds=config.gateway_session_ttl_seconds,
  242. )
  243. public_app = PublicGatewayApp(
  244. session_store=session_store,
  245. api_client=ScopedApiClient(config.tools_base_url, timeout=config.timeout_seconds),
  246. )
  247. return serve_public(
  248. public_app,
  249. host=args.host,
  250. port=args.port,
  251. enable_rate_limit=config.rate_limit_enabled,
  252. rate_limit_max_requests=config.rate_limit_max_requests,
  253. rate_limit_window_seconds=config.rate_limit_window_seconds,
  254. max_in_flight_per_tool=config.max_in_flight_per_tool,
  255. )
  256. elif args.command == 'call':
  257. tool_args = {
  258. 'page': args.page,
  259. 'limit': args.limit,
  260. }
  261. if args.tool == 'query_order':
  262. if not args.keyword:
  263. raise ValueError('--keyword is required for query_order')
  264. tool_args['keyword'] = args.keyword
  265. elif args.tool == 'query_track':
  266. if args.order_id > 0:
  267. tool_args['order_id'] = args.order_id
  268. if args.order_number:
  269. tool_args['order_number'] = args.order_number
  270. if args.tracking_number:
  271. tool_args['tracking_number'] = args.tracking_number
  272. if args.order_id <= 0 and not args.order_number and not args.tracking_number:
  273. raise ValueError('--order-id, --order-number or --tracking-number is required for query_track')
  274. elif args.tool == 'query_order_exact':
  275. exact_strings = {
  276. 'order_number': args.order_number,
  277. 'reference_number': args.reference_number,
  278. 'tracking_number': args.tracking_number,
  279. 'outbound_number': args.outbound_number,
  280. 'container_code': args.container_code,
  281. 'so_number': args.so_number,
  282. 'shipment_id': args.shipment_id,
  283. 'receiver_country': args.receiver_country,
  284. 'inbound_date_start': args.inbound_date_start,
  285. 'inbound_date_end': args.inbound_date_end,
  286. 'outbound_date_start': args.outbound_date_start,
  287. 'outbound_date_end': args.outbound_date_end,
  288. }
  289. for field, value in exact_strings.items():
  290. if value:
  291. tool_args[field] = value
  292. exact_number_lists = {
  293. 'order_numbers': args.order_numbers,
  294. 'reference_numbers': args.reference_numbers,
  295. 'tracking_numbers': args.tracking_numbers,
  296. 'outbound_numbers': args.outbound_numbers,
  297. 'container_codes': args.container_codes,
  298. 'so_numbers': args.so_numbers,
  299. }
  300. for field, value in exact_number_lists.items():
  301. if value:
  302. tool_args[field] = parse_string_list(value)
  303. exact_lists = {
  304. 'product_ids': args.product_ids,
  305. 'customer_ids': args.customer_ids,
  306. 'warehouse_ids': args.warehouse_ids,
  307. }
  308. for field, value in exact_lists.items():
  309. if value:
  310. tool_args[field] = parse_int_list(value)
  311. if args.sales_id > 0:
  312. tool_args['sales_id'] = args.sales_id
  313. if args.department_id > 0:
  314. tool_args['department_id'] = args.department_id
  315. elif args.tool == 'query_order_detail':
  316. if not args.order_number:
  317. raise ValueError('--order-number is required for query_order_detail')
  318. tool_args['order_number'] = args.order_number
  319. tool_args['section'] = args.section
  320. elif args.tool == 'query_customs_declaration_files':
  321. if args.outbound_numbers:
  322. tool_args['outbound_numbers'] = parse_string_list(
  323. args.outbound_numbers
  324. )
  325. if args.order_numbers:
  326. tool_args['order_numbers'] = parse_string_list(
  327. args.order_numbers
  328. )
  329. elif args.tool == 'query_outbound_list':
  330. outbound_number_lists = {
  331. 'outbound_numbers': args.outbound_numbers,
  332. 'order_numbers': args.order_numbers,
  333. 'container_codes': args.container_codes,
  334. 'so_numbers': args.so_numbers,
  335. 'bl_numbers': args.bl_numbers,
  336. }
  337. for field, value in outbound_number_lists.items():
  338. if value:
  339. tool_args[field] = parse_string_list(value)
  340. outbound_mode_lists = {
  341. 'trailer_types': args.trailer_types,
  342. 'declaration_types': args.declaration_types,
  343. 'clearance_types': args.clearance_types,
  344. }
  345. for field, value in outbound_mode_lists.items():
  346. if value:
  347. tool_args[field] = parse_int_list(value)
  348. outbound_dates = {
  349. 'closing_time_start': args.closing_time_start,
  350. 'closing_time_end': args.closing_time_end,
  351. 'est_loading_time_start': args.est_loading_time_start,
  352. 'est_loading_time_end': args.est_loading_time_end,
  353. 'create_date_start': args.create_date_start,
  354. 'create_date_end': args.create_date_end,
  355. 'loading_time_start': args.loading_time_start,
  356. 'loading_time_end': args.loading_time_end,
  357. }
  358. for field, value in outbound_dates.items():
  359. if value:
  360. tool_args[field] = value
  361. if args.outbound_status > 0:
  362. tool_args['outbound_status'] = args.outbound_status
  363. if args.shipping_method > 0:
  364. tool_args['shipping_method'] = args.shipping_method
  365. if args.warehouse_id == -1 or args.warehouse_id > 0:
  366. tool_args['warehouse_id'] = args.warehouse_id
  367. if args.is_direct_send is not None:
  368. tool_args['is_direct_send'] = args.is_direct_send
  369. elif args.tool == 'query_outbound_detail':
  370. if not args.outbound_number:
  371. raise ValueError(
  372. '--outbound-number is required for '
  373. 'query_outbound_detail'
  374. )
  375. tool_args['outbound_number'] = args.outbound_number
  376. elif args.tool in (
  377. 'list_order_filter_options', 'list_outbound_filter_options'
  378. ):
  379. if not args.filter_type:
  380. raise ValueError(
  381. '--filter-type is required for '
  382. + args.tool
  383. )
  384. tool_args['filter_type'] = args.filter_type
  385. tool_args['keyword'] = args.keyword
  386. else:
  387. if args.keyword:
  388. tool_args['keyword'] = args.keyword
  389. payload = self.call_tool(
  390. args.tool,
  391. tool_args,
  392. request_id=args.request_id,
  393. )
  394. else:
  395. raise RuntimeError('unsupported command')
  396. stdout.write(json.dumps(payload, ensure_ascii=False))
  397. return 0
  398. def main(argv=None):
  399. config = GatewayConfig.from_env()
  400. app = GatewayApp.from_config(config)
  401. return app.run_cli(argv=argv)
  402. if __name__ == '__main__':
  403. raise SystemExit(main(sys.argv[1:]))