Skip to content

Commit 079a4be

Browse files
committed
Adopt Schema component in server handlers
1 parent f7bf9a7 commit 079a4be

File tree

101 files changed

+1185
-2093
lines changed

Some content is hidden

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

101 files changed

+1185
-2093
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/ExamplePrompt.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,28 @@
1212
namespace App;
1313

1414
use Mcp\Capability\Prompt\MetadataInterface;
15-
use Mcp\Capability\Prompt\PromptGet;
16-
use Mcp\Capability\Prompt\PromptGetResult;
17-
use Mcp\Capability\Prompt\PromptGetResultMessages;
1815
use Mcp\Capability\Prompt\PromptGetterInterface;
16+
use Mcp\Schema\Content\PromptMessage;
17+
use Mcp\Schema\Content\TextContent;
18+
use Mcp\Schema\Enum\Role;
19+
use Mcp\Schema\Request\GetPromptRequest;
20+
use Mcp\Schema\Result\GetPromptResult;
1921

2022
/**
2123
* @author Tobias Nyholm <[email protected]>
2224
*/
2325
class ExamplePrompt implements MetadataInterface, PromptGetterInterface
2426
{
25-
public function get(PromptGet $input): PromptGetResult
27+
public function get(GetPromptRequest $request): GetPromptResult
2628
{
27-
$firstName = $input->arguments['first name'] ?? null;
29+
$firstName = $request->arguments['firstName'] ?? null;
2830

29-
return new PromptGetResult(
31+
return new GetPromptResult(
32+
[new PromptMessage(
33+
Role::User,
34+
new TextContent(\sprintf('Hello %s', $firstName ?? 'World')),
35+
)],
3036
$this->getDescription(),
31-
[new PromptGetResultMessages(
32-
'user',
33-
\sprintf('Hello %s', $firstName ?? 'World')
34-
)]
3537
);
3638
}
3739

examples/cli/src/ExampleResource.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
namespace App;
1313

1414
use Mcp\Capability\Resource\MetadataInterface;
15-
use Mcp\Capability\Resource\ResourceRead;
1615
use Mcp\Capability\Resource\ResourceReaderInterface;
17-
use Mcp\Capability\Resource\ResourceReadResult;
16+
use Mcp\Schema\Content\TextResourceContents;
17+
use Mcp\Schema\Request\ReadResourceRequest;
18+
use Mcp\Schema\Result\ReadResourceResult;
1819

1920
/**
2021
* @author Tobias Nyholm <[email protected]>
2122
*/
2223
class ExampleResource implements MetadataInterface, ResourceReaderInterface
2324
{
24-
public function read(ResourceRead $input): ResourceReadResult
25+
public function read(ReadResourceRequest $request): ReadResourceResult
2526
{
26-
return new ResourceReadResult(
27-
'Content of '.$this->getName(),
28-
$this->getUri(),
29-
);
27+
return new ReadResourceResult([
28+
new TextResourceContents($this->getUri(), null, 'Content of My Resource'),
29+
]);
3030
}
3131

3232
public function getUri(): string
@@ -36,7 +36,7 @@ public function getUri(): string
3636

3737
public function getName(): string
3838
{
39-
return 'My resource';
39+
return 'my-resource';
4040
}
4141

4242
public function getDescription(): ?string

examples/cli/src/ExampleTool.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@
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\Request\CallToolRequest;
18+
use Mcp\Schema\Result\CallToolResult;
1819

1920
/**
2021
* @author Tobias Nyholm <[email protected]>
2122
*/
2223
class ExampleTool implements MetadataInterface, ToolExecutorInterface
2324
{
24-
public function call(ToolCall $input): ToolCallResult
25+
public function call(CallToolRequest $request): CallToolResult
2526
{
26-
$format = $input->arguments['format'] ?? 'Y-m-d H:i:s';
27+
$format = $request->arguments['format'] ?? 'Y-m-d H:i:s';
2728

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

3336
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/Prompt/PromptGet.php

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

src/Capability/Prompt/PromptGetResult.php

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

src/Capability/Prompt/PromptGetResultMessages.php

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

0 commit comments

Comments
 (0)