tools.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import json
  2. from typing import Any
  3. from pydantic import Field
  4. from mantis_mcp.mcp.engine import mcp
  5. from mantis_mcp.services.rest_client import ClientREST, HTTPMethod, RestApiError
  6. def _format_result(data: Any) -> str:
  7. return json.dumps(data, indent=2, ensure_ascii=False)
  8. def _format_error(error: RestApiError) -> str:
  9. return json.dumps(
  10. {"error": True, "status_code": error.status_code, "message": error.message},
  11. indent=2,
  12. ensure_ascii=False,
  13. )
  14. def _parse_values(values: str) -> dict[str, Any]:
  15. parsed = json.loads(values)
  16. if not isinstance(parsed, dict):
  17. raise ValueError("values must be a JSON object")
  18. return parsed
  19. def _build_enum_field(field_name: str, value: int) -> dict[str, int]:
  20. return {field_name: {"id": value}}
  21. @mcp.tool()
  22. def create_issue(
  23. summary: str = Field(..., description="The summary of the issue"),
  24. description: str = Field(..., description="The description of the issue"),
  25. project_id: str = Field(..., description="The ID of the project"),
  26. category: int = Field(..., description="The ID of the category"),
  27. priority: int | None = Field(None, description="The ID of the priority"),
  28. severity: int | None = Field(None, description="The ID of the severity"),
  29. reproducibility: int | None = Field(None, description="The ID of the reproducibility"),
  30. values: str | None = Field(None, description="The values of the issue"),
  31. ) -> str:
  32. """Create a new issue in a MantisBT project."""
  33. client = ClientREST()
  34. body: dict[str, Any] = {
  35. "summary": summary,
  36. "description": description,
  37. "project": {"id": int(project_id)},
  38. "category": {"id": category},
  39. }
  40. if priority is not None:
  41. body.update(_build_enum_field("priority", priority))
  42. if severity is not None:
  43. body.update(_build_enum_field("severity", severity))
  44. if reproducibility is not None:
  45. body.update(_build_enum_field("reproducibility", reproducibility))
  46. if values:
  47. body.update(_parse_values(values))
  48. try:
  49. return _format_result(
  50. client.execute(HTTPMethod.POST, "/api/rest/issues", params=body)
  51. )
  52. except (RestApiError, ValueError) as error:
  53. if isinstance(error, RestApiError):
  54. return _format_error(error)
  55. return json.dumps({"error": True, "message": str(error)}, indent=2)
  56. @mcp.tool()
  57. def modify_issue(
  58. id: int = Field(..., description="The ID of the issue"),
  59. summary: str | None = Field(None, description="The summary of the issue"),
  60. description: str | None = Field(None, description="The description of the issue"),
  61. category: int | None = Field(None, description="The ID of the category"),
  62. priority: int | None = Field(None, description="The ID of the priority"),
  63. severity: int | None = Field(None, description="The ID of the severity"),
  64. reproducibility: int | None = Field(None, description="The ID of the reproducibility"),
  65. values: str | None = Field(None, description="The values of the issue"),
  66. status: str | None = Field(None, description="The ID of the status"),
  67. handler: str | None = Field(None, description="The ID of the handler"),
  68. ) -> str:
  69. """Modify an existing issue in a MantisBT project."""
  70. client = ClientREST()
  71. body: dict[str, Any] = {}
  72. if summary is not None:
  73. body["summary"] = summary
  74. if description is not None:
  75. body["description"] = description
  76. if category is not None:
  77. body["category"] = {"id": category}
  78. if priority is not None:
  79. body.update(_build_enum_field("priority", priority))
  80. if severity is not None:
  81. body.update(_build_enum_field("severity", severity))
  82. if reproducibility is not None:
  83. body.update(_build_enum_field("reproducibility", reproducibility))
  84. if status is not None:
  85. body["status"] = {"name": status}
  86. if handler is not None:
  87. body["handler"] = {"name": handler}
  88. if values:
  89. body.update(_parse_values(values))
  90. if not body:
  91. return json.dumps(
  92. {"error": True, "message": "At least one field must be provided to modify"},
  93. indent=2,
  94. )
  95. try:
  96. return _format_result(
  97. client.execute(HTTPMethod.PATCH, f"/api/rest/issues/{id}", params=body)
  98. )
  99. except (RestApiError, ValueError) as error:
  100. if isinstance(error, RestApiError):
  101. return _format_error(error)
  102. return json.dumps({"error": True, "message": str(error)}, indent=2)
  103. @mcp.tool()
  104. def add_issue_note(
  105. id: int = Field(..., description="The ID of the issue") ,
  106. note: str = Field(..., description="The note to add"),
  107. view_state: str | None = Field(None, description="The ID of the view state"),
  108. files: list[dict[str, str]] | None = Field(None, description="The files to add"),
  109. ) -> str:
  110. """Add a comment to an existing MantisBT issue."""
  111. client = ClientREST()
  112. body: dict[str, Any] = {"text": note}
  113. if view_state is not None:
  114. body["view_state"] = {"name": view_state}
  115. if files:
  116. body["files"] = files
  117. try:
  118. return _format_result(
  119. client.execute(HTTPMethod.POST, f"/api/rest/issues/{id}/notes", params=body)
  120. )
  121. except RestApiError as error:
  122. return _format_error(error)