Skip to content

Commit 47b0e3c

Browse files
marcreichelclaude
andcommitted
feat: Add MCP tool to assign a Mantis issue to the current user
Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent fa3c770 commit 47b0e3c

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

src/Service/MantisConnector.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,31 @@ final public function patchUpstreamField(MantisIssue $issue): bool
278278
}
279279
}
280280

281+
/**
282+
* @throws JsonException
283+
*/
284+
final public function assignIssue(int $issueId, int $handlerId): bool
285+
{
286+
$body = json_encode([
287+
'handler' => [
288+
'id' => $handlerId,
289+
],
290+
], JSON_THROW_ON_ERROR);
291+
292+
try {
293+
$this->client->patch(
294+
'issues/' . $issueId,
295+
[
296+
'body' => $body,
297+
],
298+
);
299+
300+
return true;
301+
} catch (Exception | GuzzleException) {
302+
return false;
303+
}
304+
}
305+
281306
/**
282307
* @param array{
283308
* id: int,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

src/Service/McpServer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Artemeon\M2G\Service;
66

7+
use Artemeon\M2G\Service\Mcp\AssignIssueToMeTool;
78
use Artemeon\M2G\Service\Mcp\AttachmentTool;
89
use Artemeon\M2G\Service\Mcp\CurrentUserResource;
910
use Artemeon\M2G\Service\Mcp\IssueAttachmentsTool;
@@ -57,6 +58,7 @@ private function tools(): array
5758
new UnassignedIssuesTool($this->mantisConnector),
5859
new SyncToGithubTool($this->issueSyncService),
5960
new AttachmentTool($this->mantisConnector),
61+
new AssignIssueToMeTool($this->mantisConnector),
6062
];
6163
}
6264

0 commit comments

Comments
 (0)