您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

mcp_server_service.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from api.db.db_models import DB, MCPServer
  17. from api.db.services.common_service import CommonService
  18. class MCPServerService(CommonService):
  19. """Service class for managing MCP server related database operations.
  20. This class extends CommonService to provide specialized functionality for MCP server management,
  21. including MCP server creation, updates, and deletions.
  22. Attributes:
  23. model: The MCPServer model class for database operations.
  24. """
  25. model = MCPServer
  26. @classmethod
  27. @DB.connection_context()
  28. def get_servers(cls, tenant_id: str, id_list: list[str] | None = None):
  29. """Retrieve all MCP servers associated with a tenant.
  30. This method fetches all MCP servers for a given tenant, ordered by creation time.
  31. It only includes fields for list display.
  32. Args:
  33. tenant_id (str): The unique identifier of the tenant.
  34. id_list (list[str]): Get servers by ID list. Will ignore this condition if None.
  35. Returns:
  36. list[dict]: List of MCP server dictionaries containing MCP server details.
  37. Returns None if no MCP servers are found.
  38. """
  39. fields = [
  40. cls.model.id, cls.model.name, cls.model.server_type, cls.model.url, cls.model.description,
  41. cls.model.variables, cls.model.update_date
  42. ]
  43. servers = cls.model.select(*fields).order_by(cls.model.create_time.desc()).where(cls.model.tenant_id == tenant_id)
  44. if id_list is not None:
  45. servers = servers.where(cls.model.id.in_(id_list))
  46. servers = list(servers.dicts())
  47. if not servers:
  48. return None
  49. return servers