Skip to content

Commit 2bd9bc4

Browse files
committed
[Server] Serve an extension's methods under the modern lifecycle
The modern dispatcher takes the method-to-extension map the builder collects, so a method belonging to an extension this server does not serve is answered -32601 naming the extension instead of a bare "no handler found". It is still an unknown method - the server genuinely does not implement it - but the caller can now act on the answer.
1 parent 7e38c94 commit 2bd9bc4

3 files changed

Lines changed: 83 additions & 3 deletions

File tree

src/Server/Builder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ final class Builder
236236
/** @var list<class-string<\Mcp\Schema\JsonRpc\Request>|class-string<\Mcp\Schema\JsonRpc\Notification>> */
237237
private array $extensionMessages = [];
238238

239+
/** @var array<string, string> RPC method to the extension identifier defining it */
240+
private array $extensionMethods = [];
241+
239242
/**
240243
* @var LoaderInterface[]
241244
*/
@@ -406,6 +409,9 @@ public function enableExtension(ExtensionInterface ...$extensions): self
406409
// downstream ever sees it.
407410
foreach ($extension->getMessages() as $message) {
408411
$this->extensionMessages[] = $message;
412+
// Recorded even though the handler answers it, so a server with
413+
// the extension *off* can say so instead of "no such method".
414+
$this->extensionMethods[$message::getMethod()] = $id;
409415
}
410416

411417
foreach ($extension->getRequestHandlers() as $handler) {
@@ -836,6 +842,7 @@ public function buildStateless(array $supportedVersions = [ProtocolVersion::V202
836842
: null,
837843
cachePolicy: $this->cachePolicy,
838844
notificationBus: $this->notificationBus,
845+
extensionMethods: $this->extensionMethods,
839846
);
840847
}
841848

src/Server/Stateless/StatelessProtocol.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ final class StatelessProtocol
8989
/**
9090
* @param iterable<RequestHandlerInterface<ResultInterface>> $requestHandlers
9191
* @param list<ProtocolVersion> $supportedVersions
92+
* @param array<string, string> $extensionMethods RPC method to the extension identifier defining it
9293
*/
9394
public function __construct(
9495
private readonly iterable $requestHandlers,
@@ -102,6 +103,7 @@ public function __construct(
102103
private readonly ?RequestStateCodec $requestStateCodec = null,
103104
?CachePolicy $cachePolicy = null,
104105
private readonly ?NotificationBusInterface $notificationBus = null,
106+
private readonly array $extensionMethods = [],
105107
) {
106108
$this->codec = $codec ?? new Rev2026Codec($configuration->serverInfo, $cachePolicy);
107109

@@ -396,7 +398,7 @@ private function dispatch(string $method, array $decoded, RequestMeta $meta, str
396398
} catch (\Throwable $e) {
397399
$this->logger->warning('Rejected an unparseable modern-era request.', ['method' => $method, 'exception' => $e]);
398400

399-
return StatelessResult::error(Error::forMethodNotFound(\sprintf('Method "%s" is not supported.', $method), $id), 404);
401+
return StatelessResult::error($this->unknownMethod($method, $id), 404);
400402
}
401403

402404
$request = $messages[0] ?? null;
@@ -410,7 +412,7 @@ private function dispatch(string $method, array $decoded, RequestMeta $meta, str
410412

411413
return StatelessResult::error(
412414
$unknownMethod
413-
? Error::forMethodNotFound($request->getMessage(), $id)
415+
? $this->unknownMethod($method, $id)
414416
: Error::forInvalidRequest($request->getMessage(), $id),
415417
$unknownMethod ? 404 : 400,
416418
);
@@ -510,7 +512,28 @@ private function dispatch(string $method, array $decoded, RequestMeta $meta, str
510512
return $this->encode($method, $id, $result->result, null === $input);
511513
}
512514

513-
return StatelessResult::error(Error::forMethodNotFound(\sprintf('No handler found for method "%s".', $method), $id), 404);
515+
return StatelessResult::error($this->unknownMethod($method, $id), 404);
516+
}
517+
518+
/**
519+
* A method with no handler, said as precisely as the server can.
520+
*
521+
* An extension's method is still `-32601` when the extension is off — the
522+
* server genuinely does not implement it — but naming the extension turns
523+
* an opaque refusal into something the caller can act on.
524+
*/
525+
private function unknownMethod(string $method, string|int $id): Error
526+
{
527+
$extension = $this->extensionMethods[$method] ?? null;
528+
529+
if (null !== $extension) {
530+
return Error::forMethodNotFound(
531+
\sprintf('Method "%s" belongs to the "%s" extension, which this server does not serve.', $method, $extension),
532+
$id,
533+
);
534+
}
535+
536+
return Error::forMethodNotFound(\sprintf('No handler found for method "%s".', $method), $id);
514537
}
515538

516539
/**

tests/Unit/Server/Stateless/StatelessProtocolTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Mcp\Server\Stateless\StatelessResult;
3535
use Mcp\Server\Subscription\InMemoryNotificationBus;
3636
use Mcp\Server\Wire\CachePolicy;
37+
use Mcp\Tests\Unit\Server\Extension\ThingExtension;
3738
use PHPUnit\Framework\Attributes\DataProvider;
3839
use PHPUnit\Framework\Attributes\TestDox;
3940
use PHPUnit\Framework\TestCase;
@@ -791,6 +792,55 @@ public function testAcknowledgmentReflectsWhatTheServerCanDo(): void
791792
$this->assertSame(['toolsListChanged' => true], (array) $first['params']['notifications']);
792793
}
793794

795+
#[TestDox('an extension method is served by the extension that claims it')]
796+
public function testExtensionMethodIsServed(): void
797+
{
798+
$protocol = Server::builder()
799+
->setServerInfo('test-server', '1.0.0')
800+
->enableExtension(new ThingExtension())
801+
->buildStateless([ProtocolVersion::V2026_07_28]);
802+
803+
$answer = self::callWithHeaders($protocol, 'com.example/things.list', [], [
804+
'MCP-Protocol-Version' => ProtocolVersion::V2026_07_28->value,
805+
'Mcp-Method' => 'com.example/things.list',
806+
]);
807+
808+
$this->assertSame(200, $answer['status']);
809+
$this->assertSame(['a', 'b'], $answer['body']['result']['things']);
810+
}
811+
812+
#[TestDox('the extension is advertised under capabilities.extensions')]
813+
public function testExtensionIsAdvertised(): void
814+
{
815+
$protocol = Server::builder()
816+
->setServerInfo('test-server', '1.0.0')
817+
->enableExtension(new ThingExtension())
818+
->buildStateless([ProtocolVersion::V2026_07_28]);
819+
820+
$answer = self::call($protocol, 'server/discover');
821+
822+
$this->assertSame(['flavour' => 'vanilla'], (array) $answer['body']['result']['capabilities']['extensions']['com.example/things']);
823+
}
824+
825+
#[TestDox('a method of an extension this server does not serve says so by name')]
826+
public function testDisabledExtensionMethodNamesItsExtension(): void
827+
{
828+
$protocol = Server::builder()
829+
->setServerInfo('test-server', '1.0.0')
830+
->buildStateless([ProtocolVersion::V2026_07_28]);
831+
832+
$answer = self::callWithHeaders($protocol, 'com.example/things.list', [], [
833+
'MCP-Protocol-Version' => ProtocolVersion::V2026_07_28->value,
834+
'Mcp-Method' => 'com.example/things.list',
835+
]);
836+
837+
$this->assertSame(404, $answer['status']);
838+
$this->assertSame(Error::METHOD_NOT_FOUND, $answer['body']['error']['code']);
839+
// Without the extension enabled there is nothing to name it by.
840+
$this->assertStringContainsString('com.example/things.list', $answer['body']['error']['message']);
841+
$this->assertStringNotContainsString('extension', $answer['body']['error']['message']);
842+
}
843+
794844
#[TestDox('a notification is acknowledged with no body, never answered')]
795845
public function testNotificationIsAcknowledged(): void
796846
{

0 commit comments

Comments
 (0)