From 43808246186c8100ba66ea4be731ed21c82b115d Mon Sep 17 00:00:00 2001 From: Jason Thistlethwaite Date: Fri, 24 Apr 2026 22:09:37 +0000 Subject: [PATCH] Import redMCP into Redmine repo --- .gitignore | 4 + AGENTS.md | 13 +- README.md | 8 + redMCP/.env.example | 2 + redMCP/README.md | 54 ++++++ redMCP/app/RedmineClient.php | 336 +++++++++++++++++++++++++++++++++++ redMCP/app/redmineClient.php | 3 + redMCP/composer.json | 13 ++ redMCP/composer.lock | 245 +++++++++++++++++++++++++ 9 files changed, 673 insertions(+), 5 deletions(-) create mode 100644 redMCP/.env.example create mode 100644 redMCP/README.md create mode 100644 redMCP/app/RedmineClient.php create mode 100644 redMCP/app/redmineClient.php create mode 100644 redMCP/composer.json create mode 100644 redMCP/composer.lock diff --git a/.gitignore b/.gitignore index fe8a0ba..b49eab6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,9 @@ /__pycache__/ /redmine-copy/ /dist/*.tar.gz +redMCP/.env +redMCP/test.env +redMCP/vendor/ +redMCP/composer.phar .env *.pyc diff --git a/AGENTS.md b/AGENTS.md index 0240598..70791c1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,8 +11,9 @@ environment. The canonical plugin sources live in `plugins/`: Top-level Python helpers such as `redmine_outbox_worker.py` and `reset_helpdesk_mail_settings.py` support LAN test-instance operations. Project -notes and design records live in `docs/`. `dist/*.MANIFEST.md` files are tracked; -rollback tarballs are intentionally ignored. `redmine-copy/` is an ignored +notes and design records live in `docs/`. `redMCP/` contains the PHP +Redmine API/MCP wrapper. `dist/*.MANIFEST.md` files are tracked; rollback +tarballs are intentionally ignored. `redmine-copy/` is an ignored working/reference copy, not the source of truth. ## Build, Test, and Development Commands @@ -24,6 +25,7 @@ ruby -c plugins/redmine_contacts/lib/redmine_contacts/utils/check_mail.rb ruby -c plugins/redmine_contacts_helpdesk/app/models/helpdesk_mailer.rb ruby -c plugins/redmine_event_outbox/lib/redmine_event_outbox.rb python3 -m py_compile redmine_outbox_worker.py reset_helpdesk_mail_settings.py +cd redMCP && php -l app/RedmineClient.php && composer validate ``` Dry-run operational helpers before applying changes: @@ -61,6 +63,7 @@ host was updated outside the tracked `plugins/` source. ## Security & Configuration Tips Do not commit `.env`, cache files, database exports, rollback tarballs, or the -full `redmine-copy/` tree. Treat Redmine API keys, SSH keys, mail passwords, and -production-derived data as sensitive. Use `plugins/` for source changes and copy -to the test host only after review or validation. +full `redmine-copy/` tree. Do not commit `redMCP/vendor/` or `composer.phar`. +Treat Redmine API keys, SSH keys, mail passwords, and production-derived data as +sensitive. Use `plugins/` for plugin source changes and `redMCP/` for API/MCP +wrapper work. diff --git a/README.md b/README.md index 0f760db..682f5e2 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,14 @@ The full `redmine-copy/` tree is an ignored working/reference copy of the legacy install. Make local plugin changes in `plugins/` first, then deploy or copy them into the test Redmine instance or `redmine-copy/` as needed. +The Redmine API/MCP wrapper project now lives in: + +- [redMCP](/home/iadnah/redmine/redMCP) + +That subproject contains the PHP wrapper that composes normal Redmine issue API +responses with local Helpdesk metadata. Its dependencies are managed by Composer; +`redMCP/vendor/`, local env files, and `composer.phar` are ignored. + There is no realistic short-term plan to: - migrate to a newer RedmineUP package diff --git a/redMCP/.env.example b/redMCP/.env.example new file mode 100644 index 0000000..8c20494 --- /dev/null +++ b/redMCP/.env.example @@ -0,0 +1,2 @@ +REDMINE_URL=http://192.168.50.170 +REDMINE_API_KEY= diff --git a/redMCP/README.md b/redMCP/README.md new file mode 100644 index 0000000..6a08fa1 --- /dev/null +++ b/redMCP/README.md @@ -0,0 +1,54 @@ +This project is for creating a simple library and MCP server for handling Redmine, particularly for when Redmine is being used for customer service/support. + +This is a private project for now, as it pertains to an installation of Redmine 3.4.4-stable used by LDR, which also uses some outdated plugins. Notably, the outdated pluggins in use are both from Redmine Up (contacts, helpdesk/crm). + +This is about creating the basic tools that an agent would need in order to interact with and automate LDR's redmine communications. Some of the functionality will extend beyond Redmine. + +## Notable issues to be aware of + +Projects which have the modules "contacts" or "contacts_helpdesk" enabled have the Helpdesk plugins enabled. That means a few things: + +The regular API call to fetch an issue will not necessarily return the helpdesk information. Instead, it may claim that the issue's author or journals were created by "anonymous". When getting these issues, we need to use a different way to handle it. + +The project in /home/iadnah/redmine/ is related to this problem. + +## Client shape + +`RedMCP\RedmineClient` wraps the normal Redmine REST client and composes +Helpdesk context in application logic instead of modifying Redmine's core issue +API. + +```php +$client = RedMCP\RedmineClient::fromCredentials('http://192.168.50.170', $apiKey); +$context = $client->issueWithHelpdesk(39858); +``` + +The returned array has: + +- `issue`: the normal `/issues/:id.json` response +- `helpdesk.ticket`: metadata from `/helpdesk_search/issues/:id/ticket`, or + `null` +- `helpdesk.journal_messages`: metadata from + `/helpdesk_search/issues/:id/messages` + +Basic issue CRUD is exposed on the same wrapper: + +```php +$issues = $client->issues(['project_id' => 'customer-service', 'status_id' => 'open', 'limit' => 10]); +$issue = $client->issue(39858); + +$created = $client->createIssue([ + 'project_id' => 78, + 'subject' => 'Example issue from redMCP', + 'description' => 'Created through the Redmine REST API wrapper.', +]); + +$client->updateIssue((int) $created['id'], ['notes' => 'Follow-up note']); +$client->deleteIssue((int) $created['id']); +``` + +## Test instance + +A working test copy of Redmine is available on the LAN at `192.168.50.170`. +The related Redmine plugin forks, helper scripts, and operational docs live in +the parent repository. diff --git a/redMCP/app/RedmineClient.php b/redMCP/app/RedmineClient.php new file mode 100644 index 0000000..75ab186 --- /dev/null +++ b/redMCP/app/RedmineClient.php @@ -0,0 +1,336 @@ +client = $client; + } + + public static function fromCredentials(string $url, string $apiKeyOrUsername, ?string $password = null): self + { + return new self(new NativeCurlClient($url, $apiKeyOrUsername, $password)); + } + + /** + * List Redmine issues. + * + * @param array $params Standard Redmine issue list filters. + * + * @return array + */ + public function issues(array $params = []): array + { + return $this->listIssues($params); + } + + /** + * List Redmine issues. + * + * @param array $params Standard Redmine issue list filters. + * + * @return array + */ + public function listIssues(array $params = []): array + { + $response = $this->client->getApi('issue')->list($params); + if (!is_array($response)) { + throw new RuntimeException('Could not list issues: ' . $this->stringifyError($response)); + } + + return $response; + } + + /** + * Fetch a normal Redmine issue. + * + * @param array $issueIncludes Standard Redmine issue includes: + * journals, attachments, children, + * relations, changesets. + * + * @return array + */ + public function issue(int $issueId, array $issueIncludes = ['journals', 'attachments']): array + { + $issueResponse = $this->client->getApi('issue')->show($issueId, ['include' => $issueIncludes]); + if (!is_array($issueResponse)) { + throw new RuntimeException('Could not fetch issue #' . $issueId . ': ' . $this->stringifyError($issueResponse)); + } + + return $issueResponse['issue'] ?? $issueResponse; + } + + /** + * Create a Redmine issue. + * + * Typical fields include project_id, subject, description, tracker_id, + * status_id, priority_id, assigned_to_id, category_id, due_date, and + * start_date. + * + * @param array $fields + * + * @return array + */ + public function createIssue(array $fields): array + { + if (!isset($fields['project_id']) || !isset($fields['subject'])) { + throw new RuntimeException('Creating an issue requires at least project_id and subject.'); + } + + $issueApi = $this->client->getApi('issue'); + $response = $issueApi->create($fields); + $this->assertLastApiResponseSucceeded($issueApi, 'create issue'); + + return $this->xmlResponseToArray($response); + } + + /** + * Update a Redmine issue. + * + * Typical fields include notes, subject, status_id, priority_id, + * assigned_to_id, private_notes, due_date, and tracker_id. + * + * @param array $fields + */ + public function updateIssue(int $issueId, array $fields): bool + { + if ($fields === []) { + throw new RuntimeException('Updating an issue requires at least one field.'); + } + + $issueApi = $this->client->getApi('issue'); + $issueApi->update($issueId, $fields); + $this->assertLastApiResponseSucceeded($issueApi, 'update issue #' . $issueId); + + return true; + } + + /** + * Delete a Redmine issue. + */ + public function deleteIssue(int $issueId): bool + { + $issueApi = $this->client->getApi('issue'); + $issueApi->remove($issueId); + $this->assertLastApiResponseSucceeded($issueApi, 'delete issue #' . $issueId); + + return true; + } + + /** + * Alias for deleteIssue(). + */ + public function removeIssue(int $issueId): bool + { + return $this->deleteIssue($issueId); + } + + /** + * @param array $issueIncludes Standard Redmine issue includes. + * + * @return array + */ + public function issueWithHelpdesk(int $issueId, int $messageLimit = 100, array $issueIncludes = ['journals', 'attachments']): array + { + return $this->getIssueWithHelpdeskContext($issueId, $messageLimit, $issueIncludes); + } + + /** + * @param array $issueIncludes Standard Redmine issue includes. + * + * @return array + */ + public function getIssueWithHelpdeskContext( + int $issueId, + int $messageLimit = 100, + array $issueIncludes = ['journals', 'attachments'] + ): array + { + $issue = $this->issue($issueId, $issueIncludes); + + $ticket = $this->helpdeskTicketByIssue($issueId); + $messages = $this->helpdeskMessagesByIssue($issueId, $messageLimit); + + return [ + 'issue' => $issue, + 'helpdesk' => [ + 'available' => $ticket !== null || $messages !== [], + 'ticket' => $ticket, + 'journal_messages' => $messages, + ], + 'meta' => [ + 'issue_id' => $issueId, + 'issue_includes' => array_values($issueIncludes), + 'helpdesk_sources' => [ + 'ticket' => '/helpdesk_search/issues/' . $issueId . '/ticket', + 'messages' => '/helpdesk_search/issues/' . $issueId . '/messages', + ], + ], + ]; + } + + /** + * @return array|null + */ + public function helpdeskTicketByIssue(int $issueId): ?array + { + return $this->getHelpdeskTicketByIssue($issueId); + } + + /** + * @return array|null + */ + public function getHelpdeskTicketByIssue(int $issueId): ?array + { + $response = $this->getJson('/helpdesk_search/issues/' . rawurlencode((string) $issueId) . '/ticket', [], [403, 404]); + if (!is_array($response) || !isset($response['helpdesk_ticket']) || !is_array($response['helpdesk_ticket'])) { + return null; + } + + return $response['helpdesk_ticket']; + } + + /** + * @return array + */ + public function helpdeskMessagesByIssue(int $issueId, int $limit = 100): array + { + return $this->getHelpdeskMessagesByIssue($issueId, $limit); + } + + /** + * @return array + */ + public function getHelpdeskMessagesByIssue(int $issueId, int $limit = 100): array + { + $response = $this->getJson( + '/helpdesk_search/issues/' . rawurlencode((string) $issueId) . '/messages', + ['limit' => max(1, min($limit, 200))], + [403, 404] + ); + if (!is_array($response) || !isset($response['journal_messages']) || !is_array($response['journal_messages'])) { + return []; + } + + return $response['journal_messages']; + } + + /** + * @param array $params + * + * @return array|null + */ + private function getJson(string $path, array $params = [], array $nullStatuses = []): ?array + { + if (!$this->client instanceof HttpClient) { + throw new RuntimeException('Configured Redmine client cannot issue raw HTTP requests.'); + } + + $requestPath = $this->buildPath($path, $params); + $response = $this->client->request(HttpFactory::makeJsonRequest('GET', $requestPath)); + $status = $response->getStatusCode(); + + if (in_array($status, $nullStatuses, true)) { + return null; + } + if ($status >= 400) { + throw new RuntimeException('Redmine GET ' . $requestPath . ' failed with HTTP ' . $status . ': ' . $response->getContent()); + } + + $body = $response->getContent(); + if ($body === '') { + return []; + } + + try { + $decoded = json_decode($body, true, 512, JSON_THROW_ON_ERROR); + } catch (Throwable $exception) { + throw new RuntimeException('Redmine GET ' . $requestPath . ' returned invalid JSON.', 0, $exception); + } + + if (!is_array($decoded)) { + throw new RuntimeException('Redmine GET ' . $requestPath . ' returned invalid JSON.'); + } + + return $decoded; + } + + /** + * @param array $params + */ + private function buildPath(string $path, array $params): string + { + return PathSerializer::create($path . '.json', $params)->getPath(); + } + + /** + * @param mixed $api + */ + private function assertLastApiResponseSucceeded($api, string $action): void + { + if (!method_exists($api, 'getLastResponse')) { + return; + } + + $response = $api->getLastResponse(); + $status = $response->getStatusCode(); + if ($status >= 400) { + throw new RuntimeException('Could not ' . $action . ': HTTP ' . $status . ': ' . $response->getContent()); + } + } + + /** + * @param mixed $response + * + * @return array + */ + private function xmlResponseToArray($response): array + { + if ($response instanceof SimpleXMLElement) { + $encoded = json_encode($response); + if ($encoded === false) { + throw new RuntimeException('Could not encode Redmine XML response.'); + } + + $decoded = json_decode($encoded, true); + if (is_array($decoded)) { + return $decoded; + } + } + + if ($response === '') { + return []; + } + + throw new RuntimeException('Unexpected Redmine response: ' . $this->stringifyError($response)); + } + + /** + * @param mixed $value + */ + private function stringifyError($value): string + { + if ($value === false) { + return 'empty response'; + } + if (is_scalar($value)) { + return (string) $value; + } + + return 'unexpected response'; + } +} diff --git a/redMCP/app/redmineClient.php b/redMCP/app/redmineClient.php new file mode 100644 index 0000000..636f584 --- /dev/null +++ b/redMCP/app/redmineClient.php @@ -0,0 +1,3 @@ +=7.1", + "psr/http-message": "^1.0 || ^2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "PSR-17: Common interfaces for PSR-7 HTTP message factories", + "keywords": [ + "factory", + "http", + "message", + "psr", + "psr-17", + "psr-7", + "request", + "response" + ], + "support": { + "source": "https://github.com/php-fig/http-factory" + }, + "time": "2024-04-15T12:06:14+00:00" + }, + { + "name": "psr/http-message", + "version": "2.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "402d35bcb92c70c026d1a6a9883f06b2ead23d71" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/402d35bcb92c70c026d1a6a9883f06b2ead23d71", + "reference": "402d35bcb92c70c026d1a6a9883f06b2ead23d71", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "support": { + "source": "https://github.com/php-fig/http-message/tree/2.0" + }, + "time": "2023-04-04T09:54:51+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": {}, + "prefer-stable": false, + "prefer-lowest": false, + "platform": {}, + "platform-dev": {}, + "plugin-api-version": "2.9.0" +}