Skip to content

Commit 385cda9

Browse files
committed
Adopt Schema component in server handlers
1 parent f7bf9a7 commit 385cda9

File tree

78 files changed

+1080
-1788
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1080
-1788
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"phpunit/phpunit": "^10.5",
2929
"symfony/console": "^6.4 || ^7.0",
3030
"psr/cache": "^3.0",
31-
"php-cs-fixer/shim": "^3.84"
31+
"php-cs-fixer/shim": "^3.84",
32+
"nyholm/nsa": "^1.3"
3233
},
3334
"suggest": {
3435
"symfony/console": "To use SymfonyConsoleTransport for STDIO",

examples/cli/index.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323

2424
// Configure the JsonRpcHandler and build the functionality
2525
$jsonRpcHandler = new Mcp\Server\JsonRpcHandler(
26-
new Mcp\Message\Factory(),
27-
App\Builder::buildRequestHandlers(),
28-
App\Builder::buildNotificationHandlers(),
26+
Mcp\Message\Factory::make(),
27+
App\Builder::buildMethodHandlers(),
2928
$logger
3029
);
3130

examples/cli/src/Builder.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,19 @@
1414
use Mcp\Capability\PromptChain;
1515
use Mcp\Capability\ResourceChain;
1616
use Mcp\Capability\ToolChain;
17-
use Mcp\Server\NotificationHandler\InitializedHandler;
18-
use Mcp\Server\NotificationHandlerInterface;
19-
use Mcp\Server\RequestHandler\InitializeHandler;
20-
use Mcp\Server\RequestHandler\PingHandler;
21-
use Mcp\Server\RequestHandler\PromptGetHandler;
22-
use Mcp\Server\RequestHandler\PromptListHandler;
23-
use Mcp\Server\RequestHandler\ResourceListHandler;
24-
use Mcp\Server\RequestHandler\ResourceReadHandler;
25-
use Mcp\Server\RequestHandler\ToolCallHandler;
26-
use Mcp\Server\RequestHandler\ToolListHandler;
27-
use Mcp\Server\RequestHandlerInterface;
17+
use Mcp\Server\MethodHandlerInterface;
18+
use Mcp\Server\NotificationHandler;
19+
use Mcp\Server\RequestHandler;
2820

2921
/**
3022
* @author Tobias Nyholm <[email protected]>
3123
*/
3224
class Builder
3325
{
3426
/**
35-
* @return list<RequestHandlerInterface>
27+
* @return list<MethodHandlerInterface>
3628
*/
37-
public static function buildRequestHandlers(): array
29+
public static function buildMethodHandlers(): array
3830
{
3931
$promptManager = new PromptChain([
4032
new ExamplePrompt(),
@@ -49,24 +41,15 @@ public static function buildRequestHandlers(): array
4941
]);
5042

5143
return [
52-
new InitializeHandler(),
53-
new PingHandler(),
54-
new PromptListHandler($promptManager),
55-
new PromptGetHandler($promptManager),
56-
new ResourceListHandler($resourceManager),
57-
new ResourceReadHandler($resourceManager),
58-
new ToolCallHandler($toolManager),
59-
new ToolListHandler($toolManager),
60-
];
61-
}
62-
63-
/**
64-
* @return list<NotificationHandlerInterface>
65-
*/
66-
public static function buildNotificationHandlers(): array
67-
{
68-
return [
69-
new InitializedHandler(),
44+
new NotificationHandler\InitializedHandler(),
45+
new RequestHandler\InitializeHandler(),
46+
new RequestHandler\PingHandler(),
47+
new RequestHandler\PromptListHandler($promptManager),
48+
new RequestHandler\PromptGetHandler($promptManager),
49+
new RequestHandler\ResourceListHandler($resourceManager),
50+
new RequestHandler\ResourceReadHandler($resourceManager),
51+
new RequestHandler\ToolCallHandler($toolManager),
52+
new RequestHandler\ToolListHandler($toolManager),
7053
];
7154
}
7255
}

examples/cli/src/ExampleTool.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@
1212
namespace App;
1313

1414
use Mcp\Capability\Tool\MetadataInterface;
15-
use Mcp\Capability\Tool\ToolCall;
16-
use Mcp\Capability\Tool\ToolCallResult;
1715
use Mcp\Capability\Tool\ToolExecutorInterface;
16+
use Mcp\Schema\Content\TextContent;
17+
use Mcp\Schema\Result\CallToolResult;
1818

1919
/**
2020
* @author Tobias Nyholm <[email protected]>
2121
*/
2222
class ExampleTool implements MetadataInterface, ToolExecutorInterface
2323
{
24-
public function call(ToolCall $input): ToolCallResult
24+
public function call(string $name, array $arguments): CallToolResult
2525
{
26-
$format = $input->arguments['format'] ?? 'Y-m-d H:i:s';
26+
$format = $arguments['format'] ?? 'Y-m-d H:i:s';
2727

28-
return new ToolCallResult(
29-
(new \DateTime('now', new \DateTimeZone('UTC')))->format($format)
30-
);
28+
return new CallToolResult([
29+
new TextContent(
30+
(new \DateTime('now', new \DateTimeZone('UTC')))->format($format),
31+
),
32+
]);
3133
}
3234

3335
public function getName(): string

phpstan.dist.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ parameters:
77
excludePaths:
88
- examples/cli/vendor/* (?)
99
treatPhpDocTypesAsCertain: false
10+
ignoreErrors:
11+
-
12+
message: "#^Method .*::test.*\\(\\) has no return type specified\\.$#"

src/Capability/Tool/ToolCall.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Capability/Tool/ToolCallResult.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Capability/Tool/ToolExecutorInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313

1414
use Mcp\Exception\ToolExecutionException;
1515
use Mcp\Exception\ToolNotFoundException;
16+
use Mcp\Schema\Result\CallToolResult;
1617

1718
/**
1819
* @author Tobias Nyholm <[email protected]>
1920
*/
2021
interface ToolExecutorInterface
2122
{
2223
/**
24+
* @param array<string, mixed> $arguments
25+
*
2326
* @throws ToolExecutionException if the tool execution fails
2427
* @throws ToolNotFoundException if the tool is not found
2528
*/
26-
public function call(ToolCall $input): ToolCallResult;
29+
public function call(string $name, array $arguments): CallToolResult;
2730
}

src/Capability/ToolChain.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
use Mcp\Capability\Tool\CollectionInterface;
1515
use Mcp\Capability\Tool\IdentifierInterface;
1616
use Mcp\Capability\Tool\MetadataInterface;
17-
use Mcp\Capability\Tool\ToolCall;
18-
use Mcp\Capability\Tool\ToolCallResult;
1917
use Mcp\Capability\Tool\ToolExecutorInterface;
2018
use Mcp\Exception\InvalidCursorException;
2119
use Mcp\Exception\ToolExecutionException;
2220
use Mcp\Exception\ToolNotFoundException;
21+
use Mcp\Schema\Result\CallToolResult;
2322

2423
/**
2524
* A collection of tools. All tools need to implement IdentifierInterface.
@@ -60,18 +59,18 @@ public function getMetadata(int $count, ?string $lastIdentifier = null): iterabl
6059
}
6160
}
6261

63-
public function call(ToolCall $input): ToolCallResult
62+
public function call(string $name, array $arguments): CallToolResult
6463
{
6564
foreach ($this->items as $item) {
66-
if ($item instanceof ToolExecutorInterface && $input->name === $item->getName()) {
65+
if ($item instanceof ToolExecutorInterface && $name === $item->getName()) {
6766
try {
68-
return $item->call($input);
67+
return $item->call($name, $arguments);
6968
} catch (\Throwable $e) {
70-
throw new ToolExecutionException($input, $e);
69+
throw new ToolExecutionException($name, $e);
7170
}
7271
}
7372
}
7473

75-
throw new ToolNotFoundException($input);
74+
throw new ToolNotFoundException($name);
7675
}
7776
}

src/Exception/LogicException.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the official PHP MCP SDK.
5+
*
6+
* A collaboration between Symfony and the PHP Foundation.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mcp\Exception;
13+
14+
/**
15+
* @author Christopher Hertel <[email protected]>
16+
*/
17+
final class LogicException extends \LogicException implements ExceptionInterface
18+
{
19+
}

0 commit comments

Comments
 (0)