Skip to content

Commit 3019972

Browse files
committed
Let the caller handle the construction of the notification
1 parent f1e9cbc commit 3019972

File tree

7 files changed

+15
-31
lines changed

7 files changed

+15
-31
lines changed

examples/09-standalone-cli/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$logger
2828
);
2929

30-
$notificationPublisher = Mcp\Server\NotificationPublisher::make();
30+
$notificationPublisher = new Mcp\Server\NotificationPublisher();
3131

3232
// Set up the server
3333
$sever = new Mcp\Server($jsonRpcHandler, $notificationPublisher, $logger);

src/Capability/Registry.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function registerTool(Tool $tool, callable|array|string $handler, bool $i
101101

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

104-
$this->notificationPublisher->enqueue(ToolListChangedNotification::class);
104+
$this->notificationPublisher->enqueue(new ToolListChangedNotification());
105105
}
106106

107107
/**
@@ -120,7 +120,7 @@ public function registerResource(Resource $resource, callable|array|string $hand
120120

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

123-
$this->notificationPublisher->enqueue(ResourceListChangedNotification::class);
123+
$this->notificationPublisher->enqueue(new ResourceListChangedNotification());
124124
}
125125

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

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

172-
$this->notificationPublisher->enqueue(PromptListChangedNotification::class);
172+
$this->notificationPublisher->enqueue(new PromptListChangedNotification());
173173
}
174174

175175
/**

src/Server/NotificationPublisher.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Mcp\Server;
1515

16-
use Mcp\JsonRpc\MessageFactory;
1716
use Mcp\Schema\JsonRpc\Notification;
1817

1918
/**
@@ -24,23 +23,8 @@ class NotificationPublisher
2423
/** @var list<Notification> */
2524
private array $queue = [];
2625

27-
public function __construct(
28-
private readonly MessageFactory $factory,
29-
) {
30-
}
31-
32-
public static function make(): self
33-
{
34-
return new self(MessageFactory::make());
35-
}
36-
37-
/**
38-
* @param class-string<Notification> $notificationType
39-
*/
40-
public function enqueue(string $notificationType): void
26+
public function enqueue(Notification $notification): void
4127
{
42-
$notification = $this->factory->createByType($notificationType, []);
43-
4428
$this->queue[] = $notification;
4529
}
4630

src/Server/ServerBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function withPrompt(callable|array|string $handler, ?string $name = null,
208208
public function build(): Server
209209
{
210210
$logger = $this->logger ?? new NullLogger();
211-
$notificationPublisher = NotificationPublisher::make();
211+
$notificationPublisher = new NotificationPublisher();
212212
$container = $this->container ?? new Container();
213213
$registry = new Registry($notificationPublisher, new ReferenceHandler($container), $logger);
214214

tests/Capability/Discovery/DiscoveryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class DiscoveryTest extends TestCase
3535

3636
protected function setUp(): void
3737
{
38-
$this->registry = new Registry(NotificationPublisher::make());
38+
$this->registry = new Registry(new NotificationPublisher());
3939
$this->discoverer = new Discoverer($this->registry);
4040
}
4141

tests/Capability/RegistryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testToolRegistration(): void
4747
$notificationPublisher = $this->createMock(NotificationPublisher::class);
4848
$notificationPublisher->expects($this->once())
4949
->method('enqueue')
50-
->with(ToolListChangedNotification::class);
50+
->with(new ToolListChangedNotification());
5151

5252
$registry = new Registry($notificationPublisher);
5353
$registry->registerTool($tool, fn () => null);
@@ -67,7 +67,7 @@ public function testResourceRegistration(): void
6767
$notificationPublisher = $this->createMock(NotificationPublisher::class);
6868
$notificationPublisher->expects($this->once())
6969
->method('enqueue')
70-
->with(ResourceListChangedNotification::class);
70+
->with(new ResourceListChangedNotification());
7171

7272
$registry = new Registry($notificationPublisher);
7373
$registry->registerResource($resource, fn () => null);
@@ -86,7 +86,7 @@ public function testPromptRegistration(): void
8686
$notificationPublisher = $this->createMock(NotificationPublisher::class);
8787
$notificationPublisher->expects($this->once())
8888
->method('enqueue')
89-
->with(PromptListChangedNotification::class);
89+
->with(new PromptListChangedNotification());
9090

9191
$registry = new Registry($notificationPublisher);
9292
$registry->registerPrompt($prompt, fn () => null);

tests/Server/NotificationPublisherTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class NotificationPublisherTest extends TestCase
2828
public function testEnqueue(): void
2929
{
3030
$expectedNotifications = [
31-
ToolListChangedNotification::class,
32-
ResourceListChangedNotification::class,
33-
PromptListChangedNotification::class,
31+
new ToolListChangedNotification(),
32+
new ResourceListChangedNotification(),
33+
new PromptListChangedNotification(),
3434
];
35-
$notificationPublisher = new NotificationPublisher(MessageFactory::make());
35+
$notificationPublisher = new NotificationPublisher();
3636

3737
foreach ($expectedNotifications as $notificationType) {
3838
$notificationPublisher->enqueue($notificationType);
@@ -43,7 +43,7 @@ public function testEnqueue(): void
4343
$this->assertCount(\count($expectedNotifications), $flushedNotifications);
4444

4545
foreach ($flushedNotifications as $index => $notification) {
46-
$this->assertInstanceOf($expectedNotifications[$index], $notification);
46+
$this->assertSame($expectedNotifications[$index], $notification);
4747
}
4848

4949
$this->assertEmpty(iterator_to_array($notificationPublisher->flush()));

0 commit comments

Comments
 (0)