| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import json
- from typing import Any
- from mantis_mcp.mcp.engine import mcp
- from mantis_mcp.mcp.filters import filter_response
- from mantis_mcp.services.rest_client import ClientREST, HTTPMethod, RestApiError
- def _read_resource(
- *,
- endpoint: str,
- params: dict[str, Any] | None = None,
- whitelist: list[str] | None = None,
- blacklist: list[str] | None = None,
- ) -> str:
- client = ClientREST()
- try:
- data = client.execute(HTTPMethod.GET, endpoint, params=params)
- filtered = filter_response(data, whitelist=whitelist, blacklist=blacklist)
- return json.dumps(filtered, indent=2, ensure_ascii=False)
- except RestApiError as error:
- return json.dumps(
- {"error": True, "status_code": error.status_code, "message": error.message},
- indent=2,
- ensure_ascii=False,
- )
- @mcp.resource("mantisbt://issues")
- def get_issues() -> str:
- """Retrieve all issues from MantisBT."""
- return _read_resource(endpoint="/api/rest/issues?page_size=0")
- @mcp.resource("mantisbt://issues/assigned")
- def get_assigned_issues() -> str:
- """Retrieve issues assigned to the current user."""
- return _read_resource(
- endpoint="/api/rest/issues",
- params={"filter_id": "assigned"},
- )
- @mcp.resource("mantisbt://issues/{issue_id}")
- def get_issue(issue_id: str) -> str:
- """Retrieve a single issue by ID."""
- return _read_resource(
- endpoint=f"/api/rest/issues/{issue_id}",
- blacklist=["history", "notes"],
- )
- @mcp.resource("mantisbt://issues/{issue_id}/notes")
- def get_issue_notes(issue_id: str) -> str:
- """Retrieve notes for an issue."""
- return _read_resource(
- endpoint=f"/api/rest/issues/{issue_id}",
- params={"select": "notes"},
- whitelist=["notes"],
- )
- @mcp.resource("mantisbt://issues/{issue_id}/history")
- def get_issue_history(issue_id: str) -> str:
- """Retrieve history for an issue."""
- return _read_resource(
- endpoint=f"/api/rest/issues/{issue_id}",
- params={"select": "history"},
- whitelist=["history"],
- )
- @mcp.resource("mantisbt://projects")
- def get_projects() -> str:
- """Retrieve all projects accessible by the current user."""
- return _read_resource(endpoint="/api/rest/projects")
- @mcp.resource("mantisbt://projects/{project_id}")
- def get_project(project_id: str) -> str:
- """Retrieve project details by ID."""
- return _read_resource(endpoint=f"/api/rest/projects/{project_id}")
- @mcp.resource("mantisbt://projects/{project_id}/issues")
- def get_project_issues(project_id: str) -> str:
- """Retrieve project details by ID."""
- return _read_resource(endpoint=f"/api/rest/issues?project_id={project_id}&page_size=0")
- @mcp.resource("mantisbt://projects/{project_id}/handlers")
- def get_project_handlers(project_id: str) -> str:
- """Retrieve users who can be assigned issues in a project."""
- return _read_resource(endpoint=f"/api/rest/projects/{project_id}/handlers")
|