app.py 23 KB

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