|
| 1 | +<?php |
| 2 | + |
| 3 | +// SPDX-License-Identifier: EUPL-1.2 |
| 4 | + |
| 5 | +declare(strict_types=1); |
| 6 | + |
| 7 | +namespace Core\Mcp\Dependencies; |
| 8 | + |
| 9 | +/** |
| 10 | + * Represents a single tool dependency. |
| 11 | + * |
| 12 | + * Defines what must be satisfied before a tool can execute. |
| 13 | + */ |
| 14 | +class ToolDependency |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Create a new tool dependency. |
| 18 | + * |
| 19 | + * @param DependencyType $type The type of dependency |
| 20 | + * @param string $key The identifier (tool name, state key, context key, etc.) |
| 21 | + * @param string|null $description Human-readable description for error messages |
| 22 | + * @param bool $optional If true, this is a soft dependency (warning, not error) |
| 23 | + * @param array $metadata Additional metadata for custom validation |
| 24 | + */ |
| 25 | + public function __construct( |
| 26 | + public readonly DependencyType $type, |
| 27 | + public readonly string $key, |
| 28 | + public readonly ?string $description = null, |
| 29 | + public readonly bool $optional = false, |
| 30 | + public readonly array $metadata = [], |
| 31 | + ) {} |
| 32 | + |
| 33 | + /** |
| 34 | + * Create a tool_called dependency. |
| 35 | + */ |
| 36 | + public static function toolCalled(string $toolName, ?string $description = null): self |
| 37 | + { |
| 38 | + return new self( |
| 39 | + type: DependencyType::TOOL_CALLED, |
| 40 | + key: $toolName, |
| 41 | + description: $description ?? "Tool '{$toolName}' must be called first", |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Create a session_state dependency. |
| 47 | + */ |
| 48 | + public static function sessionState(string $stateKey, ?string $description = null): self |
| 49 | + { |
| 50 | + return new self( |
| 51 | + type: DependencyType::SESSION_STATE, |
| 52 | + key: $stateKey, |
| 53 | + description: $description ?? "Session state '{$stateKey}' is required", |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Create a context_exists dependency. |
| 59 | + */ |
| 60 | + public static function contextExists(string $contextKey, ?string $description = null): self |
| 61 | + { |
| 62 | + return new self( |
| 63 | + type: DependencyType::CONTEXT_EXISTS, |
| 64 | + key: $contextKey, |
| 65 | + description: $description ?? "Context '{$contextKey}' is required", |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Create an entity_exists dependency. |
| 71 | + */ |
| 72 | + public static function entityExists(string $entityType, ?string $description = null, array $metadata = []): self |
| 73 | + { |
| 74 | + return new self( |
| 75 | + type: DependencyType::ENTITY_EXISTS, |
| 76 | + key: $entityType, |
| 77 | + description: $description ?? "Entity '{$entityType}' must exist", |
| 78 | + metadata: $metadata, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Create a custom dependency with callback metadata. |
| 84 | + */ |
| 85 | + public static function custom(string $name, ?string $description = null, array $metadata = []): self |
| 86 | + { |
| 87 | + return new self( |
| 88 | + type: DependencyType::CUSTOM, |
| 89 | + key: $name, |
| 90 | + description: $description, |
| 91 | + metadata: $metadata, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Mark this dependency as optional (soft dependency). |
| 97 | + */ |
| 98 | + public function asOptional(): self |
| 99 | + { |
| 100 | + return new self( |
| 101 | + type: $this->type, |
| 102 | + key: $this->key, |
| 103 | + description: $this->description, |
| 104 | + optional: true, |
| 105 | + metadata: $this->metadata, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Convert to array representation. |
| 111 | + */ |
| 112 | + public function toArray(): array |
| 113 | + { |
| 114 | + return [ |
| 115 | + 'type' => $this->type->value, |
| 116 | + 'key' => $this->key, |
| 117 | + 'description' => $this->description, |
| 118 | + 'optional' => $this->optional, |
| 119 | + 'metadata' => $this->metadata, |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Create from array representation. |
| 125 | + */ |
| 126 | + public static function fromArray(array $data): self |
| 127 | + { |
| 128 | + return new self( |
| 129 | + type: DependencyType::from($data['type']), |
| 130 | + key: $data['key'], |
| 131 | + description: $data['description'] ?? null, |
| 132 | + optional: $data['optional'] ?? false, |
| 133 | + metadata: $data['metadata'] ?? [], |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
0 commit comments