bind_auth_code.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. class BindAuthCodeTool:
  2. name = 'bind_auth_code'
  3. requires_session = False
  4. def __init__(self, auth_client=None):
  5. self.auth_client = auth_client
  6. def metadata(self):
  7. return {
  8. 'name': self.name,
  9. 'description': 'Bind a one-time Workbuddy auth code and create the local MCP session.',
  10. 'input_schema': {
  11. 'type': 'object',
  12. 'properties': {
  13. 'auth_code': {
  14. 'type': 'string',
  15. 'description': 'One-time auth code copied from the FMS backend.',
  16. },
  17. },
  18. 'required': ['auth_code'],
  19. },
  20. }
  21. def call(self, auth_code, request_id=''):
  22. if self.auth_client is None:
  23. raise RuntimeError('auth client is required for bind_auth_code')
  24. auth_code = str(auth_code or '').strip()
  25. if not auth_code:
  26. raise ValueError('auth_code is required')
  27. response = self.auth_client.exchange(auth_code)
  28. if (response.get('code') or '') != 'MCP_0000':
  29. raise RuntimeError(response.get('msg') or 'auth_code bind failed')
  30. data = response.get('data') or {}
  31. return {
  32. 'code': 'MCP_0000',
  33. 'msg': 'success',
  34. 'data': {
  35. 'status': 'bound',
  36. 'summary': '绑定成功,可以继续使用 query_order 查询订单。',
  37. 'expire_time': data.get('expire_time'),
  38. 'tips': [
  39. '后续工具调用会自动使用本机已保存的 MCP 会话。',
  40. '如果授权失效,请回后台重新获取 Workbuddy 授权码。',
  41. ],
  42. },
  43. 'meta': {
  44. 'request_id': request_id,
  45. },
  46. }