app.py 24 KB

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