query_remote_address_list.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. class QueryRemoteAddressListTool:
  2. name = 'query_remote_address_list'
  3. route_path = '/mcp/tools/queryRemoteAddressList'
  4. backend = 'base'
  5. def __init__(self, api_client=None):
  6. self.api_client = api_client
  7. def metadata(self):
  8. return {
  9. 'name': self.name,
  10. 'description': (
  11. '使用场景:用户要搜索或查看地址库列表时调用。可按地址库编号、'
  12. '名称、启用状态、国家、州/省、城市、邮编筛选,返回当前页。编号格式为'
  13. 'PY加数字,例如PY12。不传筛选条件时返回当前公司的地址库列表。'
  14. '禁止使用:修改地址库、导入地址、删除明细、把内部ID当成编号展示。'
  15. '参数只用于工具内部调用;最终回答只能展示中文业务名称和地址库编号。'
  16. ),
  17. 'input_schema': {
  18. 'type': 'object',
  19. 'properties': {
  20. 'address_no': {
  21. 'type': 'string',
  22. 'minLength': 1,
  23. 'maxLength': 20,
  24. 'description': '地址库编号,格式为PY加数字,例如PY12。',
  25. },
  26. 'remote_name': {
  27. 'type': 'string',
  28. 'minLength': 1,
  29. 'maxLength': 100,
  30. 'description': '地址库名称,按名称包含匹配。',
  31. },
  32. 'status': {
  33. 'type': 'integer',
  34. 'enum': [0, 1],
  35. 'description': '状态。1启用,0禁用。',
  36. },
  37. 'country': {
  38. 'type': 'string',
  39. 'minLength': 1,
  40. 'maxLength': 10,
  41. 'description': '国家代码,精确匹配地址明细。',
  42. },
  43. 'province': {
  44. 'type': 'string',
  45. 'minLength': 1,
  46. 'maxLength': 50,
  47. 'description': '州/省,精确匹配地址明细。',
  48. },
  49. 'city': {
  50. 'type': 'string',
  51. 'minLength': 1,
  52. 'maxLength': 50,
  53. 'description': '城市,精确匹配地址明细。',
  54. },
  55. 'postcode': {
  56. 'type': 'string',
  57. 'minLength': 1,
  58. 'maxLength': 20,
  59. 'description': '邮编,落在地址明细的开始邮编和结束邮编之间。',
  60. },
  61. 'page': {
  62. 'type': 'integer', 'minimum': 1, 'maximum': 100, 'default': 1,
  63. },
  64. 'limit': {
  65. 'type': 'integer', 'minimum': 1, 'maximum': 100, 'default': 20,
  66. },
  67. },
  68. 'additionalProperties': False,
  69. },
  70. }
  71. def call(
  72. self,
  73. address_no=None,
  74. remote_name=None,
  75. status=None,
  76. country=None,
  77. province=None,
  78. city=None,
  79. postcode=None,
  80. page=1,
  81. limit=20,
  82. request_id='rq_query_remote_address_list',
  83. ):
  84. if self.api_client is None:
  85. raise RuntimeError(
  86. 'api client is required for query_remote_address_list'
  87. )
  88. payload = {
  89. 'page': self._page(page),
  90. 'limit': self._limit(limit),
  91. }
  92. text_fields = {
  93. 'address_no': address_no,
  94. 'remote_name': remote_name,
  95. 'country': country,
  96. 'province': province,
  97. 'city': city,
  98. 'postcode': postcode,
  99. }
  100. limits = {
  101. 'address_no': 20,
  102. 'remote_name': 100,
  103. 'country': 10,
  104. 'province': 50,
  105. 'city': 50,
  106. 'postcode': 20,
  107. }
  108. for key, value in text_fields.items():
  109. if value is None:
  110. continue
  111. if not isinstance(value, str):
  112. raise ValueError(key + ' is invalid')
  113. value = value.strip()
  114. if len(value) > limits[key]:
  115. raise ValueError(key + ' is invalid')
  116. if value:
  117. payload[key] = value
  118. if status is not None:
  119. if isinstance(status, bool) or status not in (0, 1):
  120. raise ValueError('status is invalid')
  121. payload['status'] = status
  122. return self.api_client.call_tool(
  123. self.name, self.route_path, payload, request_id,
  124. )
  125. @staticmethod
  126. def _page(value):
  127. if isinstance(value, bool) or not isinstance(value, int) or value < 1 or value > 100:
  128. raise ValueError('page is invalid')
  129. return value
  130. @staticmethod
  131. def _limit(value):
  132. if isinstance(value, bool) or not isinstance(value, int) or value < 1 or value > 100:
  133. raise ValueError('limit is invalid')
  134. return value