feat: guide tool and server instructions
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Guides;
|
||||
|
||||
/**
|
||||
* Reads guide markdown files from one directory. Topic names are restricted to
|
||||
* lowercase slugs, so no path outside the directory can ever be read.
|
||||
*/
|
||||
final class GuideRepository
|
||||
{
|
||||
private const TOPIC_PATTERN = '/^[a-z0-9]+(?:-[a-z0-9]+)*$/';
|
||||
|
||||
public function __construct(private string $directory)
|
||||
{
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
public function topics(): array
|
||||
{
|
||||
$files = glob($this->directory . '/*.md');
|
||||
if ($files === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$topics = [];
|
||||
foreach ($files as $file) {
|
||||
$topic = basename($file, '.md');
|
||||
if (preg_match(self::TOPIC_PATTERN, $topic) === 1) {
|
||||
$topics[] = $topic;
|
||||
}
|
||||
}
|
||||
sort($topics);
|
||||
|
||||
return $topics;
|
||||
}
|
||||
|
||||
public function get(string $topic): ?string
|
||||
{
|
||||
if (preg_match(self::TOPIC_PATTERN, $topic) !== 1 || ! in_array($topic, $this->topics(), true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$path = $this->directory . '/' . $topic . '.md';
|
||||
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
|
||||
$content = file_get_contents($path);
|
||||
|
||||
return $content === false ? null : $content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Guides;
|
||||
|
||||
use LIT\MCP\Integrations\Integration;
|
||||
use LIT\MCP\Integrations\IntegrationRegistry;
|
||||
|
||||
/**
|
||||
* Builds the short server instructions every MCP client loads on connect.
|
||||
* Keep this text short: it occupies context in every conversation.
|
||||
*/
|
||||
final class InstructionsBuilder
|
||||
{
|
||||
public function __construct(private IntegrationRegistry $registry)
|
||||
{
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
$active = implode(', ', array_map(
|
||||
static fn (Integration $integration): string => $integration->id(),
|
||||
$this->registry->available()
|
||||
));
|
||||
|
||||
return implode("\n", [
|
||||
'LIT MCP pflegt diese WordPress-Website (Inhalte, Medien, Divi 5, Pods).',
|
||||
'Aktive Integrationen: ' . $active . '.',
|
||||
'Regeln:',
|
||||
'- Vor dem ersten Arbeitsschritt lit-guide (ohne Topic) aufrufen; Details je Thema über lit-guide '
|
||||
. 'mit topic.',
|
||||
'- Änderungen erzeugen native Divi-Module; Variablen und Presets vor festen Werten; Code-Modul nur, '
|
||||
. 'wenn kein natives Modul passt.',
|
||||
'- Den Status (Entwurf/veröffentlicht) nie ändern, außer der Nutzer verlangt es ausdrücklich.',
|
||||
'- Schreib-Tools verlangen den aktuellen rev-Wert aus dem letzten Lesevorgang.',
|
||||
'- Fehler enthalten einen hint – ihm folgen, statt blind zu wiederholen.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Integrations\Core;
|
||||
|
||||
use LIT\MCP\Abilities\Ability;
|
||||
use LIT\MCP\Abilities\Errors;
|
||||
use LIT\MCP\Guides\GuideRepository;
|
||||
use WP_Error;
|
||||
|
||||
final class GuideAbility implements Ability
|
||||
{
|
||||
private const DEFAULT_TOPIC = 'overview';
|
||||
|
||||
public function __construct(private GuideRepository $guides)
|
||||
{
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return 'lit/guide';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Anleitung';
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'Liefert Anleitungen zur Bedienung dieses MCP-Servers. Ohne topic: Übersicht und Liste aller Themen.';
|
||||
}
|
||||
|
||||
public function inputSchema(): array
|
||||
{
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'topic' => [
|
||||
'type' => 'string',
|
||||
'description' => 'Thema, z. B. "overview" oder "divi-shortform".',
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
];
|
||||
}
|
||||
|
||||
public function outputSchema(): ?array
|
||||
{
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'topic' => ['type' => 'string'],
|
||||
'topics' => ['type' => 'array', 'items' => ['type' => 'string']],
|
||||
'content' => ['type' => 'string'],
|
||||
],
|
||||
'required' => ['topic', 'topics', 'content'],
|
||||
];
|
||||
}
|
||||
|
||||
public function annotations(): array
|
||||
{
|
||||
return ['readonly' => true, 'destructive' => false, 'idempotent' => true];
|
||||
}
|
||||
|
||||
public function isDirect(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function permission(array $input): bool|WP_Error
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function execute(array $input): mixed
|
||||
{
|
||||
$topic = isset($input['topic']) && is_string($input['topic']) && $input['topic'] !== ''
|
||||
? $input['topic']
|
||||
: self::DEFAULT_TOPIC;
|
||||
$topics = $this->guides->topics();
|
||||
$content = $this->guides->get($topic);
|
||||
|
||||
if ($content === null) {
|
||||
return Errors::notFound(sprintf(
|
||||
'Unbekanntes Thema. Verfügbar: %s',
|
||||
implode(', ', $topics)
|
||||
));
|
||||
}
|
||||
|
||||
return ['topic' => $topic, 'topics' => $topics, 'content' => $content];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user