Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 21 additions & 39 deletions src/Capability/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,36 +161,10 @@ public function registerPrompt(

public function clear(): void
{
$clearCount = 0;

foreach ($this->tools as $name => $tool) {
if (!$tool->isManual) {
unset($this->tools[$name]);
++$clearCount;
}
}
foreach ($this->resources as $uri => $resource) {
if (!$resource->isManual) {
unset($this->resources[$uri]);
++$clearCount;
}
}
foreach ($this->prompts as $name => $prompt) {
if (!$prompt->isManual) {
unset($this->prompts[$name]);
++$clearCount;
}
}
foreach ($this->resourceTemplates as $uriTemplate => $template) {
if (!$template->isManual) {
unset($this->resourceTemplates[$uriTemplate]);
++$clearCount;
}
}

if ($clearCount > 0) {
$this->logger->debug(\sprintf('Removed %d discovered elements from internal registry.', $clearCount));
}
$this->tools = array_filter($this->tools, static fn ($t) => !$t->isDiscovered);
$this->resources = array_filter($this->resources, static fn ($r) => !$r->isDiscovered);
$this->prompts = array_filter($this->prompts, static fn ($p) => !$p->isDiscovered);
$this->resourceTemplates = array_filter($this->resourceTemplates, static fn ($t) => !$t->isDiscovered);
}

public function hasTools(): bool
Expand Down Expand Up @@ -344,10 +318,10 @@ public function getPrompt(string $name): PromptReference
public function getDiscoveryState(): DiscoveryState
{
return new DiscoveryState(
tools: array_filter($this->tools, static fn ($tool) => !$tool->isManual),
resources: array_filter($this->resources, static fn ($resource) => !$resource->isManual),
prompts: array_filter($this->prompts, static fn ($prompt) => !$prompt->isManual),
resourceTemplates: array_filter($this->resourceTemplates, static fn ($template) => !$template->isManual),
tools: array_filter($this->tools, static fn ($t) => $t->isDiscovered),
resources: array_filter($this->resources, static fn ($r) => $r->isDiscovered),
prompts: array_filter($this->prompts, static fn ($p) => $p->isDiscovered),
resourceTemplates: array_filter($this->resourceTemplates, static fn ($t) => $t->isDiscovered),
);
}

Expand All @@ -360,21 +334,29 @@ public function setDiscoveryState(DiscoveryState $state): void
// Clear existing discovered elements
$this->clear();

// Import new discovered elements
// Import new discovered elements — skip any that conflict with manual or dynamic registrations
foreach ($state->getTools() as $name => $tool) {
$this->tools[$name] = $tool;
if (!isset($this->tools[$name])) {
$this->tools[$name] = new ToolReference($tool->tool, $tool->handler, isDiscovered: true);
}
}

foreach ($state->getResources() as $uri => $resource) {
$this->resources[$uri] = $resource;
if (!isset($this->resources[$uri])) {
$this->resources[$uri] = new ResourceReference($resource->resource, $resource->handler, isDiscovered: true);
}
}

foreach ($state->getPrompts() as $name => $prompt) {
$this->prompts[$name] = $prompt;
if (!isset($this->prompts[$name])) {
$this->prompts[$name] = new PromptReference($prompt->prompt, $prompt->handler, completionProviders: $prompt->completionProviders, isDiscovered: true);
}
}

foreach ($state->getResourceTemplates() as $uriTemplate => $template) {
$this->resourceTemplates[$uriTemplate] = $template;
if (!isset($this->resourceTemplates[$uriTemplate])) {
$this->resourceTemplates[$uriTemplate] = new ResourceTemplateReference($template->resourceTemplate, $template->handler, completionProviders: $template->completionProviders, isDiscovered: true);
}
}

// Dispatch events for the imported elements
Expand Down
1 change: 1 addition & 0 deletions src/Capability/Registry/ElementReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ElementReference
public function __construct(
public readonly \Closure|array|string $handler,
public readonly bool $isManual = false,
public readonly bool $isDiscovered = false,
) {
}
}
3 changes: 2 additions & 1 deletion src/Capability/Registry/PromptReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function __construct(
\Closure|array|string $handler,
bool $isManual = false,
public readonly array $completionProviders = [],
bool $isDiscovered = false,
) {
parent::__construct($handler, $isManual);
parent::__construct($handler, $isManual, $isDiscovered);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Capability/Registry/ResourceReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function __construct(
public readonly Resource $resource,
callable|array|string $handler,
bool $isManual = false,
bool $isDiscovered = false,
) {
parent::__construct($handler, $isManual);
parent::__construct($handler, $isManual, $isDiscovered);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Capability/Registry/ResourceTemplateReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public function __construct(
callable|array|string $handler,
bool $isManual = false,
public readonly array $completionProviders = [],
bool $isDiscovered = false,
) {
parent::__construct($handler, $isManual);
parent::__construct($handler, $isManual, $isDiscovered);

$this->compileTemplate();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Capability/Registry/ToolReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function __construct(
public readonly Tool $tool,
callable|array|string $handler,
bool $isManual = false,
bool $isDiscovered = false,
) {
parent::__construct($handler, $isManual);
parent::__construct($handler, $isManual, $isDiscovered);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"resources": [
{
"name": "priority_config_discovered",
"name": "priority_config_manual",
"uri": "config://priority",
"description": "A resource discovered via attributes.\n\nThis will be overridden by a manual registration with the same URI."
"description": "Manually registered resource that overrides a discovered one."
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"uri": "config://priority",
"mimeType": "text/plain",
"text": "Discovered Priority Config: Low"
"text": "Manual Priority Config: HIGH (overrides discovered)"
}
]
}
Loading
Loading