Skip to content

Commit 41486bf

Browse files
[Server] Reorganize server handlers and transport structure for better organization (#86)
* refactor: reorganize server handlers and transport structure * feat: move the test files to corresponding directories * fix: linter issues * fix: correct PHPDoc formatting and streamline handler imports * refactor: update import statement for ServerBuilder to Builder
1 parent 3dcd2f7 commit 41486bf

29 files changed

+65
-67
lines changed

src/Capability/Registry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
use Mcp\Event\ResourceTemplateListChangedEvent;
2424
use Mcp\Event\ToolListChangedEvent;
2525
use Mcp\Exception\InvalidCursorException;
26+
use Mcp\Schema\Page;
2627
use Mcp\Schema\Prompt;
2728
use Mcp\Schema\Resource;
2829
use Mcp\Schema\ResourceTemplate;
2930
use Mcp\Schema\ServerCapabilities;
3031
use Mcp\Schema\Tool;
31-
use Mcp\Server\RequestHandler\Reference\Page;
3232
use Psr\EventDispatcher\EventDispatcherInterface;
3333
use Psr\Log\LoggerInterface;
3434
use Psr\Log\NullLogger;

src/Capability/Registry/ReferenceProviderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Mcp\Capability\Registry;
1313

14-
use Mcp\Server\RequestHandler\Reference\Page;
14+
use Mcp\Schema\Page;
1515

1616
/**
1717
* Interface for providing access to registered MCP elements.

src/Server/RequestHandler/Reference/Page.php renamed to src/Schema/Page.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mcp\Server\RequestHandler\Reference;
12+
namespace Mcp\Schema;
1313

1414
/**
15-
* @extends \ArrayObject<int|string, mixed>
15+
* @phpstan-type PageItem Tool|Prompt|ResourceTemplate|Resource
16+
*
17+
* @extends \ArrayObject<int|string, PageItem>
1618
*/
1719
final class Page extends \ArrayObject
1820
{
1921
/**
20-
* @param array<int|string, mixed> $references Items can be Tool, Prompt, ResourceTemplate, or Resource
22+
* @param array<int|string, PageItem> $references Items can be Tool, Prompt, ResourceTemplate, or Resource
2123
*/
2224
public function __construct(
2325
public readonly array $references,

src/Server.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace Mcp;
1313

14-
use Mcp\JsonRpc\Handler;
1514
use Mcp\Server\Builder;
16-
use Mcp\Server\TransportInterface;
15+
use Mcp\Server\Handler\JsonRpcHandler;
16+
use Mcp\Server\Transport\TransportInterface;
1717
use Psr\Log\LoggerInterface;
1818
use Psr\Log\NullLogger;
1919
use Symfony\Component\Uid\Uuid;
@@ -25,7 +25,7 @@
2525
final class Server
2626
{
2727
public function __construct(
28-
private readonly Handler $jsonRpcHandler,
28+
private readonly JsonRpcHandler $jsonRpcHandler,
2929
private readonly LoggerInterface $logger = new NullLogger(),
3030
) {
3131
}

src/Server/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use Mcp\Capability\Tool\ToolCaller;
3030
use Mcp\Capability\Tool\ToolCallerInterface;
3131
use Mcp\Exception\ConfigurationException;
32-
use Mcp\JsonRpc\Handler;
3332
use Mcp\Schema\Annotations;
3433
use Mcp\Schema\Implementation;
3534
use Mcp\Schema\Prompt;
@@ -39,6 +38,7 @@
3938
use Mcp\Schema\Tool;
4039
use Mcp\Schema\ToolAnnotations;
4140
use Mcp\Server;
41+
use Mcp\Server\Handler\JsonRpcHandler;
4242
use Mcp\Server\Session\InMemorySessionStore;
4343
use Mcp\Server\Session\SessionFactory;
4444
use Mcp\Server\Session\SessionFactoryInterface;
@@ -334,7 +334,7 @@ public function build(): Server
334334
$sessionStore = $this->sessionStore ?? new InMemorySessionStore($sessionTtl);
335335

336336
return new Server(
337-
jsonRpcHandler: Handler::make(
337+
jsonRpcHandler: JsonRpcHandler::make(
338338
registry: $registry,
339339
referenceProvider: $registry,
340340
implementation: $this->serverInfo,

src/JsonRpc/Handler.php renamed to src/Server/Handler/JsonRpcHandler.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mcp\JsonRpc;
12+
namespace Mcp\Server\Handler;
1313

1414
use Mcp\Capability\Prompt\PromptGetterInterface;
1515
use Mcp\Capability\Registry\ReferenceProviderInterface;
@@ -20,15 +20,14 @@
2020
use Mcp\Exception\HandlerNotFoundException;
2121
use Mcp\Exception\InvalidInputMessageException;
2222
use Mcp\Exception\NotFoundExceptionInterface;
23+
use Mcp\JsonRpc\MessageFactory;
2324
use Mcp\Schema\Implementation;
2425
use Mcp\Schema\JsonRpc\Error;
2526
use Mcp\Schema\JsonRpc\HasMethodInterface;
2627
use Mcp\Schema\JsonRpc\Request;
2728
use Mcp\Schema\JsonRpc\Response;
2829
use Mcp\Schema\Request\InitializeRequest;
29-
use Mcp\Server\MethodHandlerInterface;
30-
use Mcp\Server\NotificationHandler;
31-
use Mcp\Server\RequestHandler;
30+
use Mcp\Server\Handler;
3231
use Mcp\Server\Session\SessionFactoryInterface;
3332
use Mcp\Server\Session\SessionInterface;
3433
use Mcp\Server\Session\SessionStoreInterface;
@@ -41,7 +40,7 @@
4140
*
4241
* @author Christopher Hertel <[email protected]>
4342
*/
44-
class Handler
43+
class JsonRpcHandler
4544
{
4645
/**
4746
* @var array<int, MethodHandlerInterface>
@@ -80,15 +79,15 @@ public static function make(
8079
sessionFactory: $sessionFactory,
8180
sessionStore: $sessionStore,
8281
methodHandlers: [
83-
new NotificationHandler\InitializedHandler(),
84-
new RequestHandler\InitializeHandler($registry->getCapabilities(), $implementation),
85-
new RequestHandler\PingHandler(),
86-
new RequestHandler\ListPromptsHandler($referenceProvider, $paginationLimit),
87-
new RequestHandler\GetPromptHandler($promptGetter),
88-
new RequestHandler\ListResourcesHandler($referenceProvider, $paginationLimit),
89-
new RequestHandler\ReadResourceHandler($resourceReader),
90-
new RequestHandler\CallToolHandler($toolCaller, $logger),
91-
new RequestHandler\ListToolsHandler($referenceProvider, $paginationLimit),
82+
new Notification\InitializedHandler(),
83+
new Handler\Request\InitializeHandler($registry->getCapabilities(), $implementation),
84+
new Handler\Request\PingHandler(),
85+
new Handler\Request\ListPromptsHandler($referenceProvider, $paginationLimit),
86+
new Handler\Request\GetPromptHandler($promptGetter),
87+
new Handler\Request\ListResourcesHandler($referenceProvider, $paginationLimit),
88+
new Handler\Request\ReadResourceHandler($resourceReader),
89+
new Handler\Request\CallToolHandler($toolCaller, $logger),
90+
new Handler\Request\ListToolsHandler($referenceProvider, $paginationLimit),
9291
],
9392
logger: $logger,
9493
);

src/Server/MethodHandlerInterface.php renamed to src/Server/Handler/MethodHandlerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mcp\Server;
12+
namespace Mcp\Server\Handler;
1313

1414
use Mcp\Exception\ExceptionInterface;
1515
use Mcp\Schema\JsonRpc\Error;

src/Server/NotificationHandler/InitializedHandler.php renamed to src/Server/Handler/Notification/InitializedHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mcp\Server\NotificationHandler;
12+
namespace Mcp\Server\Handler\Notification;
1313

1414
use Mcp\Schema\JsonRpc\Error;
1515
use Mcp\Schema\JsonRpc\HasMethodInterface;
1616
use Mcp\Schema\JsonRpc\Response;
1717
use Mcp\Schema\Notification\InitializedNotification;
18-
use Mcp\Server\MethodHandlerInterface;
18+
use Mcp\Server\Handler\MethodHandlerInterface;
1919
use Mcp\Server\Session\SessionInterface;
2020

2121
/**

src/Server/RequestHandler/CallToolHandler.php renamed to src/Server/Handler/Request/CallToolHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mcp\Server\RequestHandler;
12+
namespace Mcp\Server\Handler\Request;
1313

1414
use Mcp\Capability\Tool\ToolCallerInterface;
1515
use Mcp\Exception\ExceptionInterface;
1616
use Mcp\Schema\JsonRpc\Error;
1717
use Mcp\Schema\JsonRpc\HasMethodInterface;
1818
use Mcp\Schema\JsonRpc\Response;
1919
use Mcp\Schema\Request\CallToolRequest;
20-
use Mcp\Server\MethodHandlerInterface;
20+
use Mcp\Server\Handler\MethodHandlerInterface;
2121
use Mcp\Server\Session\SessionInterface;
2222
use Psr\Log\LoggerInterface;
2323
use Psr\Log\NullLogger;

src/Server/RequestHandler/GetPromptHandler.php renamed to src/Server/Handler/Request/GetPromptHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mcp\Server\RequestHandler;
12+
namespace Mcp\Server\Handler\Request;
1313

1414
use Mcp\Capability\Prompt\PromptGetterInterface;
1515
use Mcp\Exception\ExceptionInterface;
1616
use Mcp\Schema\JsonRpc\Error;
1717
use Mcp\Schema\JsonRpc\HasMethodInterface;
1818
use Mcp\Schema\JsonRpc\Response;
1919
use Mcp\Schema\Request\GetPromptRequest;
20-
use Mcp\Server\MethodHandlerInterface;
20+
use Mcp\Server\Handler\MethodHandlerInterface;
2121
use Mcp\Server\Session\SessionInterface;
2222

2323
/**

0 commit comments

Comments
 (0)