|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Artemeon\M2G\Service\Mcp; |
| 6 | + |
| 7 | +use Artemeon\M2G\Helper\IssueIdParser; |
| 8 | +use Artemeon\M2G\Service\MantisConnector; |
| 9 | +use HelgeSverre\Toon\Toon; |
| 10 | +use InvalidArgumentException; |
| 11 | +use Mcp\Schema\Content\TextContent; |
| 12 | +use Mcp\Schema\Result\CallToolResult; |
| 13 | +use Mcp\Schema\Tool; |
| 14 | +use Mcp\Schema\ToolAnnotations; |
| 15 | +use Mcp\Server\ClientGateway; |
| 16 | + |
| 17 | +class AssignIssueToMeTool implements McpTool |
| 18 | +{ |
| 19 | + public const NAME = 'mantis-assign-issue-to-me'; |
| 20 | + |
| 21 | + public function __construct(private readonly MantisConnector $mantisConnector) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + public function getDefinition(): Tool |
| 26 | + { |
| 27 | + return new Tool( |
| 28 | + name: self::NAME, |
| 29 | + title: 'Assign Mantis Issue To Me', |
| 30 | + inputSchema: [ |
| 31 | + 'type' => 'object', |
| 32 | + 'properties' => [ |
| 33 | + 'id' => [ |
| 34 | + 'type' => 'integer', |
| 35 | + 'description' => 'The numeric Mantis issue ID.', |
| 36 | + 'minimum' => 1, |
| 37 | + ], |
| 38 | + 'url' => [ |
| 39 | + 'type' => 'string', |
| 40 | + 'description' => 'A Mantis issue URL (e.g. https://mantis.example.com/view.php?id=12345). The numeric id is extracted from the ?id= query parameter.', |
| 41 | + ], |
| 42 | + ], |
| 43 | + 'required' => [], |
| 44 | + ], |
| 45 | + description: 'Assign a Mantis ticket (by ID or URL) to the current user (the owner of the configured API token).', |
| 46 | + annotations: new ToolAnnotations( |
| 47 | + readOnlyHint: false, |
| 48 | + destructiveHint: false, |
| 49 | + idempotentHint: true, |
| 50 | + openWorldHint: true, |
| 51 | + ), |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function execute(array $arguments, ClientGateway $gateway): CallToolResult |
| 56 | + { |
| 57 | + /** @var int|string|null $id */ |
| 58 | + $id = $arguments['id'] ?? null; |
| 59 | + /** @var string|null $url */ |
| 60 | + $url = $arguments['url'] ?? null; |
| 61 | + |
| 62 | + try { |
| 63 | + $issueId = IssueIdParser::parse($id, $url); |
| 64 | + } catch (InvalidArgumentException $e) { |
| 65 | + return CallToolResult::error([new TextContent($e->getMessage())]); |
| 66 | + } |
| 67 | + |
| 68 | + $user = $this->mantisConnector->fetchCurrentUser(); |
| 69 | + if ($user === null) { |
| 70 | + return CallToolResult::error([ |
| 71 | + new TextContent('Unable to determine the current Mantis user.'), |
| 72 | + ]); |
| 73 | + } |
| 74 | + |
| 75 | + if (!$this->mantisConnector->assignIssue($issueId, $user->id)) { |
| 76 | + return CallToolResult::error([ |
| 77 | + new TextContent(sprintf('Failed to assign Mantis issue %d to %s.', $issueId, $user->realName ?? $user->name)), |
| 78 | + ]); |
| 79 | + } |
| 80 | + |
| 81 | + $payload = [ |
| 82 | + 'issue_id' => $issueId, |
| 83 | + 'assigned_to' => $user->realName ?? $user->name, |
| 84 | + ]; |
| 85 | + |
| 86 | + return CallToolResult::success([new TextContent(Toon::encode($payload))]); |
| 87 | + } |
| 88 | +} |
0 commit comments