Skip to content

Commit 138fea6

Browse files
committed
Remove event dispatcher
1 parent 1fa635d commit 138fea6

File tree

6 files changed

+18
-45
lines changed

6 files changed

+18
-45
lines changed

composer.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
"opis/json-schema": "^2.4",
2424
"phpdocumentor/reflection-docblock": "^5.6",
2525
"psr/container": "^2.0",
26-
"psr/event-dispatcher": "^1.0",
2726
"psr/log": "^1.0 || ^2.0 || ^3.0",
28-
"symfony/event-dispatcher": "^7.3",
2927
"symfony/finder": "^6.4 || ^7.3",
3028
"symfony/uid": "^6.4 || ^7.3"
3129
},

src/Capability/Registry.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use Mcp\Schema\ResourceTemplate;
3030
use Mcp\Schema\ServerCapabilities;
3131
use Mcp\Schema\Tool;
32-
use Psr\EventDispatcher\EventDispatcherInterface;
32+
use Mcp\Server\NotificationPublisher;
3333
use Psr\Log\LoggerInterface;
3434
use Psr\Log\NullLogger;
3535

@@ -61,8 +61,8 @@ class Registry
6161
private array $resourceTemplates = [];
6262

6363
public function __construct(
64+
private readonly NotificationPublisher $notificationPublisher,
6465
private readonly ReferenceHandler $referenceHandler = new ReferenceHandler(),
65-
private readonly ?EventDispatcherInterface $eventDispatcher = null,
6666
private readonly LoggerInterface $logger = new NullLogger(),
6767
) {
6868
}
@@ -75,12 +75,12 @@ public function getCapabilities(): ServerCapabilities
7575

7676
return new ServerCapabilities(
7777
tools: true, // [] !== $this->tools,
78-
toolsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
78+
toolsListChanged: true,
7979
resources: [] !== $this->resources || [] !== $this->resourceTemplates,
8080
resourcesSubscribe: false,
81-
resourcesListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
81+
resourcesListChanged: true,
8282
prompts: [] !== $this->prompts,
83-
promptsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
83+
promptsListChanged: true,
8484
logging: false, // true,
8585
completions: true,
8686
);
@@ -102,7 +102,7 @@ public function registerTool(Tool $tool, callable|array|string $handler, bool $i
102102

103103
$this->tools[$toolName] = new ToolReference($tool, $handler, $isManual);
104104

105-
$this->eventDispatcher?->dispatch(new ToolListChangedEvent());
105+
$this->notificationPublisher->enqueue(new ToolListChangedEvent());
106106
}
107107

108108
/**
@@ -121,7 +121,7 @@ public function registerResource(Resource $resource, callable|array|string $hand
121121

122122
$this->resources[$uri] = new ResourceReference($resource, $handler, $isManual);
123123

124-
$this->eventDispatcher?->dispatch(new ResourceListChangedEvent());
124+
$this->notificationPublisher->enqueue(new ResourceListChangedEvent());
125125
}
126126

127127
/**
@@ -145,7 +145,7 @@ public function registerResourceTemplate(
145145

146146
$this->resourceTemplates[$uriTemplate] = new ResourceTemplateReference($template, $handler, $isManual, $completionProviders);
147147

148-
$this->eventDispatcher?->dispatch(new ResourceTemplateListChangedEvent());
148+
$this->notificationPublisher->enqueue(new ResourceTemplateListChangedEvent());
149149
}
150150

151151
/**
@@ -169,7 +169,7 @@ public function registerPrompt(
169169

170170
$this->prompts[$promptName] = new PromptReference($prompt, $handler, $isManual, $completionProviders);
171171

172-
$this->eventDispatcher?->dispatch(new PromptListChangedEvent());
172+
$this->notificationPublisher->enqueue(new PromptListChangedEvent());
173173
}
174174

175175
/**

src/Server/NotificationPublisher.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Mcp\Schema\Notification\PromptListChangedNotification;
2222
use Mcp\Schema\Notification\ResourceListChangedNotification;
2323
use Mcp\Schema\Notification\ToolListChangedNotification;
24-
use Symfony\Component\EventDispatcher\EventDispatcher;
2524
use Symfony\Contracts\EventDispatcher\Event;
2625

2726
/**
@@ -46,16 +45,12 @@ public function __construct(
4645
) {
4746
}
4847

49-
public static function make(EventDispatcher $eventDispatcher): self
48+
public static function make(): self
5049
{
51-
$instance = new self(MessageFactory::make());
52-
53-
$eventDispatcher->addListener(Event::class, [$instance, 'onEvent']);
54-
55-
return $instance;
50+
return new self(MessageFactory::make());
5651
}
5752

58-
public function onEvent(Event $event): void
53+
public function enqueue(Event $event): void
5954
{
6055
$eventClass = $event::class;
6156
if (!isset(self::EVENTS_TO_NOTIFICATIONS[$eventClass])) {

src/Server/ServerBuilder.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@
3333
use Mcp\Schema\ToolAnnotations;
3434
use Mcp\Server;
3535
use Psr\Container\ContainerInterface;
36-
use Psr\EventDispatcher\EventDispatcherInterface;
3736
use Psr\Log\LoggerInterface;
3837
use Psr\Log\NullLogger;
3938
use Psr\SimpleCache\CacheInterface;
40-
use Symfony\Component\EventDispatcher\EventDispatcher;
41-
use Symfony\Contracts\EventDispatcher\Event;
4239

4340
/**
4441
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
@@ -51,8 +48,6 @@ final class ServerBuilder
5148

5249
private ?CacheInterface $cache = null;
5350

54-
private ?EventDispatcherInterface $eventDispatcher = null;
55-
5651
private ?ContainerInterface $container = null;
5752

5853
private ?int $paginationLimit = 50;
@@ -144,13 +139,6 @@ public function withLogger(LoggerInterface $logger): self
144139
return $this;
145140
}
146141

147-
public function withEventDispatcher(EventDispatcherInterface $eventDispatcher): self
148-
{
149-
$this->eventDispatcher = $eventDispatcher;
150-
151-
return $this;
152-
}
153-
154142
/**
155143
* Provides a PSR-11 DI container, primarily for resolving user-defined handler classes.
156144
* Defaults to a basic internal container.
@@ -219,19 +207,10 @@ public function withPrompt(callable|array|string $handler, ?string $name = null,
219207
*/
220208
public function build(): Server
221209
{
222-
$internalDispatcher = new EventDispatcher();
223-
224-
if ($this->eventDispatcher instanceof EventDispatcherInterface) {
225-
$internalDispatcher->addListener(
226-
Event::class,
227-
fn ($event) => $this->eventDispatcher?->dispatch($event)
228-
);
229-
}
230-
231210
$logger = $this->logger ?? new NullLogger();
232-
211+
$notificationPublisher = NotificationPublisher::make();
233212
$container = $this->container ?? new Container();
234-
$registry = new Registry(new ReferenceHandler($container), $internalDispatcher, $logger);
213+
$registry = new Registry($notificationPublisher, new ReferenceHandler($container), $logger);
235214

236215
$this->registerManualElements($registry, $logger);
237216

@@ -242,7 +221,7 @@ public function build(): Server
242221

243222
return new Server(
244223
Handler::make($registry, $this->serverInfo, $logger),
245-
NotificationPublisher::make($internalDispatcher),
224+
$notificationPublisher,
246225
$logger,
247226
);
248227
}

tests/Capability/Discovery/DiscoveryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Mcp\Capability\Registry\ResourceReference;
2020
use Mcp\Capability\Registry\ResourceTemplateReference;
2121
use Mcp\Capability\Registry\ToolReference;
22+
use Mcp\Server\NotificationPublisher;
2223
use Mcp\Tests\Capability\Attribute\CompletionProviderFixture;
2324
use Mcp\Tests\Capability\Discovery\Fixtures\DiscoverableToolHandler;
2425
use Mcp\Tests\Capability\Discovery\Fixtures\InvocablePromptFixture;
@@ -34,7 +35,7 @@ class DiscoveryTest extends TestCase
3435

3536
protected function setUp(): void
3637
{
37-
$this->registry = new Registry();
38+
$this->registry = new Registry(NotificationPublisher::make());
3839
$this->discoverer = new Discoverer($this->registry);
3940
}
4041

tests/Server/NotificationPublisherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testOnEventCreatesNotification(): void
3131
foreach (NotificationPublisher::EVENTS_TO_NOTIFICATIONS as $eventClass => $notificationClass) {
3232
/** @var Event $event */
3333
$event = new $eventClass();
34-
$notificationPublisher->onEvent($event);
34+
$notificationPublisher->enqueue($event);
3535
$expectedNotifications[] = $notificationClass;
3636
}
3737

0 commit comments

Comments
 (0)