| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import math
- class TrialGoodsTaxTool:
- name = 'trial_goods_tax'
- route_path = '/mcp/tools/trialGoodsTax'
- def __init__(self, api_client=None):
- self.api_client = api_client
- def metadata(self):
- number = {'type': 'number', 'exclusiveMinimum': 0, 'maximum': 1000000000}
- country = {
- 'type': 'string',
- 'minLength': 2,
- 'maxLength': 3,
- 'pattern': '^[A-Za-z]{2,3}$',
- 'description': '国家代码,例如 CN、US。不得传中文国名或“国家名-代码”。',
- }
- return {
- 'name': self.name,
- 'description': (
- '使用场景:用户明确要对某一条已审核商品做关税试算时使用。'
- '必须同时提供 SKU、起运国国家代码、目的国国家代码、采购单价、SKU数量、总毛重和箱数。'
- '起运国和目的国只能填国家代码,例如 CN、US。'
- '同一 SKU 有多条时,号码类型不明确时必须先询问用户,国家不明确也必须先询问,确认前不得调用。'
- '不得根据号码格式猜测,不得跨字段或跨工具试查。'
- '返回预计关税金额,不保存试算结果。'
- '禁止使用:不查询商品列表,不修改商品或清关资料,不创建订单,不导出文件。'
- '查商品资料用 query_goods_info。'
- ),
- 'input_schema': {
- 'type': 'object',
- 'properties': {
- 'sku': {
- 'type': 'string',
- 'minLength': 1,
- 'maxLength': 100,
- 'description': 'SKU。仅当用户明确说明是 SKU 时使用。',
- },
- 'departure_country': dict(country, description='起运国。'),
- 'destination_country': dict(country, description='目的国。'),
- 'price': dict(number, description='采购单价,必须大于 0。'),
- 'qty': dict(number, description='SKU数量,必须大于 0。'),
- 'gross_weight': dict(number, description='总毛重,必须大于 0。'),
- 'box': dict(number, description='箱数,必须大于 0。'),
- },
- 'required': [
- 'sku', 'departure_country', 'destination_country',
- 'price', 'qty', 'gross_weight', 'box',
- ],
- 'additionalProperties': False,
- },
- }
- def call(
- self,
- sku='',
- departure_country='',
- destination_country='',
- price=None,
- qty=None,
- gross_weight=None,
- box=None,
- request_id='rq_trial_goods_tax',
- **_ignored,
- ):
- if self.api_client is None:
- raise RuntimeError('api client is required for trial_goods_tax')
- payload = {
- 'sku': self._text(sku, 'sku', 100),
- 'departure_country': self._country(departure_country, 'departure_country'),
- 'destination_country': self._country(destination_country, 'destination_country'),
- 'price': self._positive(price, 'price'),
- 'qty': self._positive(qty, 'qty'),
- 'gross_weight': self._positive(gross_weight, 'gross_weight'),
- 'box': self._positive(box, 'box'),
- }
- return self.api_client.call_tool(
- self.name, self.route_path, payload, request_id
- )
- @staticmethod
- def _country(value, field):
- if not isinstance(value, str):
- raise ValueError(field + ' must be a country code')
- value = value.strip().upper()
- if (
- len(value) < 2 or len(value) > 3
- or not value.isascii() or not value.isalpha()
- ):
- raise ValueError(field + ' must be a country code')
- return value
- @staticmethod
- def _text(value, field, max_length):
- if not isinstance(value, str):
- raise ValueError(field + ' must be a string')
- value = value.strip()
- if not value or len(value) > max_length:
- raise ValueError(field + ' is invalid')
- return value
- @staticmethod
- def _positive(value, field):
- if isinstance(value, bool) or not isinstance(value, (int, float)):
- raise ValueError(field + ' is invalid')
- if not math.isfinite(value) or value <= 0 or value > 1000000000:
- raise ValueError(field + ' is invalid')
- return value
|