| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- class QueryGoodsInfoTool:
- name = 'query_goods_info'
- route_path = '/mcp/tools/queryGoodsInfo'
- def __init__(self, api_client=None):
- self.api_client = api_client
- def metadata(self):
- return {
- 'name': self.name,
- 'description': (
- '使用场景:用户明确要查已审核商品资料时使用,可按 SKU 或中文品名查询,'
- '返回与后台编码查询页相同的列表字段,包括起运国、目的国、属性、'
- '报关编码、清关编码和税率。SKU 与中文品名至少提供一个;'
- '用户没说明是 SKU 还是中文品名时必须先询问,确认前不得调用。'
- '不得根据号码格式猜测,不得跨字段或跨工具试查。'
- '同一 SKU 按起运国和目的国会返回多行。'
- '禁止使用:不查未审核商品,不导出文件,不修改商品或清关资料。'
- ),
- 'input_schema': {
- 'type': 'object',
- 'properties': {
- 'sku': {
- 'type': 'string',
- 'minLength': 1,
- 'maxLength': 2000,
- 'description': (
- 'SKU。仅当用户明确说明是 SKU 时使用。'
- '多个 SKU 用逗号或换行分隔,最多 50 个,每个最多 100 个字符。'
- ),
- },
- 'cn_name': {
- 'type': 'string',
- 'minLength': 1,
- 'maxLength': 100,
- 'description': (
- '中文品名。仅当用户明确说明是中文品名时使用,按包含关系查询。'
- ),
- },
- 'page': {
- 'type': 'integer', 'minimum': 1, 'maximum': 100,
- 'default': 1,
- },
- 'limit': {
- 'type': 'integer', 'minimum': 1, 'maximum': 100,
- 'default': 20,
- },
- },
- 'additionalProperties': False,
- },
- }
- def call(
- self,
- sku='',
- cn_name='',
- page=1,
- limit=20,
- request_id='rq_query_goods_info',
- **_ignored,
- ):
- if self.api_client is None:
- raise RuntimeError('api client is required for query_goods_info')
- payload = {
- 'page': self._bounded_integer(page, 'page'),
- 'limit': self._bounded_integer(limit, 'limit'),
- }
- sku = self._text(sku, 'sku', 2000)
- cn_name = self._text(cn_name, 'cn_name', 100)
- if sku:
- payload['sku'] = sku
- if cn_name:
- payload['cn_name'] = cn_name
- if 'sku' not in payload and 'cn_name' not in payload:
- raise ValueError('sku or cn_name is required')
- return self.api_client.call_tool(
- self.name, self.route_path, payload, request_id
- )
- @staticmethod
- def _text(value, field, max_length):
- if value in (None, ''):
- return ''
- if not isinstance(value, str):
- raise ValueError(field + ' must be a string')
- value = value.strip()
- if len(value) > max_length:
- raise ValueError(field + ' is too long')
- return value
- @staticmethod
- def _bounded_integer(value, field):
- if isinstance(value, bool) or not isinstance(value, int) or value < 1 or value > 100:
- raise ValueError(field + ' is invalid')
- return value
|