| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import json
- from typing import Any
- from pydantic import Field
- from mantis_mcp.mcp.engine import mcp
- from mantis_mcp.services.rest_client import ClientREST, HTTPMethod, RestApiError
- def _format_result(data: Any) -> str:
- return json.dumps(data, indent=2, ensure_ascii=False)
- def _format_error(error: RestApiError) -> str:
- return json.dumps(
- {"error": True, "status_code": error.status_code, "message": error.message},
- indent=2,
- ensure_ascii=False,
- )
- def _parse_values(values: str) -> dict[str, Any]:
- parsed = json.loads(values)
- if not isinstance(parsed, dict):
- raise ValueError("values must be a JSON object")
- return parsed
- def _build_enum_field(field_name: str, value: int) -> dict[str, dict]:
- return {field_name: {"id": value}}
- @mcp.tool()
- def create_issue(
- summary: str = Field(..., description="The summary of the issue"),
- description: str = Field(..., description="The description of the issue"),
- project_id: str = Field(..., description="The ID of the project"),
- category: int = Field(..., description="The ID of the category"),
- priority: int | None = Field(None, description="The ID of the priority"),
- severity: int | None = Field(None, description="The ID of the severity"),
- reproducibility: int | None = Field(None, description="The ID of the reproducibility"),
- values: str | None = Field(None, description="The values of the issue"),
- ) -> str:
- """Create a new issue in a MantisBT project."""
- client = ClientREST()
- body: dict[str, Any] = {
- "summary": summary,
- "description": description,
- "project": {"id": int(project_id)},
- "category": {"id": category},
- }
- if priority is not None:
- body.update(_build_enum_field("priority", priority))
- if severity is not None:
- body.update(_build_enum_field("severity", severity))
- if reproducibility is not None:
- body.update(_build_enum_field("reproducibility", reproducibility))
- if values:
- body.update(_parse_values(values))
- try:
- return _format_result(
- client.execute(HTTPMethod.POST, "/api/rest/issues", params=body)
- )
- except (RestApiError, ValueError) as error:
- if isinstance(error, RestApiError):
- return _format_error(error)
- return json.dumps({"error": True, "message": str(error)}, indent=2)
- @mcp.tool()
- def modify_issue(
- id: int = Field(..., description="The ID of the issue"),
- summary: str | None = Field(None, description="The summary of the issue"),
- description: str | None = Field(None, description="The description of the issue"),
- category: int | None = Field(None, description="The ID of the category"),
- priority: int | None = Field(None, description="The ID of the priority"),
- severity: int | None = Field(None, description="The ID of the severity"),
- reproducibility: int | None = Field(None, description="The ID of the reproducibility"),
- values: str | None = Field(None, description="The values of the issue"),
- status: str | None = Field(None, description="The ID of the status"),
- handler: str | None = Field(None, description="The ID of the handler"),
- ) -> str:
- """Modify an existing issue in a MantisBT project."""
- client = ClientREST()
- body: dict[str, Any] = {}
- if summary is not None:
- body["summary"] = summary
- if description is not None:
- body["description"] = description
- if category is not None:
- body["category"] = {"id": category}
- if priority is not None:
- body.update(_build_enum_field("priority", priority))
- if severity is not None:
- body.update(_build_enum_field("severity", severity))
- if reproducibility is not None:
- body.update(_build_enum_field("reproducibility", reproducibility))
- if status is not None:
- body["status"] = {"name": status}
- if handler is not None:
- body["handler"] = {"name": handler}
- if values:
- body.update(_parse_values(values))
- if not body:
- return json.dumps(
- {"error": True, "message": "At least one field must be provided to modify"},
- indent=2,
- )
- try:
- return _format_result(
- client.execute(HTTPMethod.PATCH, f"/api/rest/issues/{id}", params=body)
- )
- except (RestApiError, ValueError) as error:
- if isinstance(error, RestApiError):
- return _format_error(error)
- return json.dumps({"error": True, "message": str(error)}, indent=2)
- @mcp.tool()
- def add_issue_note(
- id: int = Field(..., description="The ID of the issue") ,
- note: str = Field(..., description="The note to add"),
- view_state: str | None = Field(None, description="The ID of the view state"),
- files: list[dict[str, str]] | None = Field(None, description="The files to add"),
- ) -> str:
- """Add a comment to an existing MantisBT issue."""
- client = ClientREST()
- body: dict[str, Any] = {"text": note}
- if view_state is not None:
- body["view_state"] = {"name": view_state}
- if files:
- body["files"] = files
- try:
- return _format_result(
- client.execute(HTTPMethod.POST, f"/api/rest/issues/{id}/notes", params=body)
- )
- except RestApiError as error:
- return _format_error(error)
- @mcp.tool()
- def add_issue_relationship(
- id: int = Field(..., description="The ID of the issue") ,
- related_id: int = Field(..., description="The ID of the issue related to this issue"),
- relationship: str = Field(..., description="Type of the relationship")
- ) -> str:
- """Add a relation between two existing issues."""
- client = ClientREST()
- body:dict[str, Any] = {
- "issue" : {"id" : related_id},
- "type" : {"name" : relationship}
- }
- try:
- return _format_result(
- client.execute(HTTPMethod.POST, f"/api/rest/issues/{id}/relationships", params=body)
- )
- except RestApiError as error:
- return _format_error(error)
|