Skip to content

Commit 93c6cb0

Browse files
committed
Fix & clean up examples
1 parent 32b8b2e commit 93c6cb0

File tree

22 files changed

+136
-397
lines changed

22 files changed

+136
-397
lines changed

README.md

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,16 @@ Until the first major release, this SDK is considered
2222

2323
Features
2424
- [x] Bring back PHP-MCP examples
25-
- [ ] Glue handler, registry and reference handlers
26-
- [ ] Revive `ServerBuilder`
27-
- [ ] Revive transports
28-
- [ ] Streamable Transport https://github.com/modelcontextprotocol/php-sdk/issues/7
29-
- [ ] Http/SSE-based Transport https://github.com/modelcontextprotocol/php-sdk/issues/8
25+
- [x] Glue handler, registry and reference handlers
26+
- [x] Revive `ServerBuilder`
27+
- [x] Revive transports
28+
- [x] Streamable Transport https://github.com/modelcontextprotocol/php-sdk/issues/7
29+
- [ ] ~~Http/SSE-based Transport https://github.com/modelcontextprotocol/php-sdk/issues/8~~
3030
- [ ] Support pagination
3131
- [ ] Support Schema validation
3232
- [ ] Support multiple versions of the MCP specification https://github.com/modelcontextprotocol/php-sdk/issues/14
3333
- [ ] (Re-)Implement missing Notification & Request Handlers https://github.com/modelcontextprotocol/php-sdk/issues/9
3434

35-
---
36-
37-
Examples working
38-
- [x] 01-discovery-stdio-calculator
39-
- [ ] 02-discovery-http-userprofile
40-
- [x] 03-manual-registration-stdio
41-
- [ ] 04-combined-registration-http
42-
- [ ] 05-stdio-env-variables
43-
- [ ] 06-custom-dependencies-stdio
44-
- [ ] 07-complex-tool-schema-http
45-
- [ ] 08-schema-showcase-streamable
46-
- [ ] 09-standalone-cli
47-
4835
## Installation
4936

5037
```bash

examples/01-discovery-stdio-calculator/McpElements.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* @phpstan-type Config array{precision: int, allow_negative: bool}
2121
*/
22-
class McpElements
22+
final class McpElements
2323
{
2424
/**
2525
* @var Config

examples/02-discovery-http-userprofile/McpElements.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@
1818
use Mcp\Capability\Attribute\McpTool;
1919
use Psr\Log\LoggerInterface;
2020

21-
class McpElements
21+
/**
22+
* @phpstan-type User array{name: string, email: string, role: string}
23+
*/
24+
final class McpElements
2225
{
23-
// Simulate a simple user database
26+
/**
27+
* Simulate a simple user database.
28+
*
29+
* @var array<int, User>
30+
*/
2431
private array $users = [
2532
'101' => ['name' => 'Alice', 'email' => '[email protected]', 'role' => 'admin'],
2633
'102' => ['name' => 'Bob', 'email' => '[email protected]', 'role' => 'user'],
2734
'103' => ['name' => 'Charlie', 'email' => '[email protected]', 'role' => 'user'],
2835
];
2936

3037
public function __construct(
31-
private LoggerInterface $logger,
38+
private readonly LoggerInterface $logger,
3239
) {
3340
$this->logger->debug('HttpUserProfileExample McpElements instantiated.');
3441
}
@@ -38,7 +45,7 @@ public function __construct(
3845
*
3946
* @param string $userId the ID of the user (from URI)
4047
*
41-
* @return array user profile data
48+
* @return User user profile data
4249
*
4350
* @throws McpServerException if the user is not found
4451
*/
@@ -64,7 +71,7 @@ public function getUserProfile(
6471
/**
6572
* Retrieves a list of all known user IDs.
6673
*
67-
* @return array list of user IDs
74+
* @return int[] list of user IDs
6875
*/
6976
#[McpResource(
7077
uri: 'user://list/ids',
@@ -86,7 +93,7 @@ public function listUserIds(): array
8693
* @param string $userId the ID of the user to message
8794
* @param string|null $customMessage an optional custom message part
8895
*
89-
* @return array status of the operation
96+
* @return array<string, bool|string> status of the operation
9097
*/
9198
#[McpTool(name: 'send_welcome')]
9299
public function sendWelcomeMessage(string $userId, ?string $customMessage = null): array
@@ -106,6 +113,9 @@ public function sendWelcomeMessage(string $userId, ?string $customMessage = null
106113
return ['success' => true, 'message_sent' => $message];
107114
}
108115

116+
/**
117+
* @return array<string, bool|string>
118+
*/
109119
#[McpTool(name: 'test_tool_without_params')]
110120
public function testToolWithoutParams(): array
111121
{
@@ -118,7 +128,7 @@ public function testToolWithoutParams(): array
118128
* @param string $userId the user ID to generate the bio for
119129
* @param string $tone Desired tone (e.g., 'formal', 'casual').
120130
*
121-
* @return array prompt messages
131+
* @return array<string, string>[] prompt messages
122132
*
123133
* @throws McpServerException if user not found
124134
*/

examples/02-discovery-http-userprofile/UserIdCompletionProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
use Mcp\Capability\Prompt\Completion\ProviderInterface;
1515

16-
class UserIdCompletionProvider implements ProviderInterface
16+
final class UserIdCompletionProvider implements ProviderInterface
1717
{
18+
private const AVAILABLE_USER_IDS = ['101', '102', '103'];
19+
1820
public function getCompletions(string $currentValue): array
1921
{
20-
$availableUserIds = ['101', '102', '103'];
21-
22-
return array_filter($availableUserIds, fn (string $userId) => str_contains($userId, $currentValue));
22+
return array_filter(self::AVAILABLE_USER_IDS, fn (string $userId) => str_contains($userId, $currentValue));
2323
}
2424
}

examples/02-discovery-http-userprofile/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function (float $a, float $b, string $operation = 'add'): array {
5454
function (): array {
5555
$memoryUsage = memory_get_usage(true);
5656
$memoryPeak = memory_get_peak_usage(true);
57-
$uptime = time() - $_SERVER['REQUEST_TIME_FLOAT'] ?? time();
57+
$uptime = time() - ($_SERVER['REQUEST_TIME_FLOAT'] ?? time());
5858
$serverSoftware = $_SERVER['SERVER_SOFTWARE'] ?? 'CLI';
5959

6060
return [

examples/03-manual-registration-stdio/SimpleHandlers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Psr\Log\LoggerInterface;
1515

16-
class SimpleHandlers
16+
final class SimpleHandlers
1717
{
1818
private string $appVersion = '1.0-manual';
1919

@@ -54,7 +54,7 @@ public function getAppVersion(): string
5454
*
5555
* @param string $userName the name of the user
5656
*
57-
* @return array the prompt messages
57+
* @return array<string, string>[] the prompt messages
5858
*/
5959
public function greetingPrompt(string $userName): array
6060
{
@@ -70,7 +70,7 @@ public function greetingPrompt(string $userName): array
7070
*
7171
* @param string $itemId the ID of the item
7272
*
73-
* @return array item details
73+
* @return array<string, string> item details
7474
*/
7575
public function getItemDetails(string $itemId): array
7676
{

examples/04-combined-registration-http/DiscoveredElements.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Mcp\Capability\Attribute\McpResource;
1515
use Mcp\Capability\Attribute\McpTool;
1616

17-
class DiscoveredElements
17+
final class DiscoveredElements
1818
{
1919
/**
2020
* A tool discovered via attributes.

examples/04-combined-registration-http/ManualHandlers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
use Psr\Log\LoggerInterface;
1515

16-
class ManualHandlers
16+
final class ManualHandlers
1717
{
1818
public function __construct(
19-
private LoggerInterface $logger,
19+
private readonly LoggerInterface $logger,
2020
) {
2121
}
2222

examples/04-combined-registration-http/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
chdir(__DIR__);
1515

1616
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
17-
use Mcp\CombinedHttpExample\Manual\ManualHandlers;
17+
use Mcp\Example\CombinedHttpExample\ManualHandlers;
1818
use Mcp\Server;
1919
use Mcp\Server\Session\FileSessionStore;
2020
use Mcp\Server\Transport\StreamableHttpTransport;

examples/05-stdio-env-variables/EnvToolHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
use Mcp\Capability\Attribute\McpTool;
1515

16-
class EnvToolHandler
16+
final class EnvToolHandler
1717
{
1818
/**
1919
* Performs an action that can be modified by an environment variable.
2020
* The MCP client should set 'APP_MODE' in its 'env' config for this server.
2121
*
2222
* @param string $input some input data
2323
*
24-
* @return array the result, varying by APP_MODE
24+
* @return array<string, string|int> the result, varying by APP_MODE
2525
*/
2626
#[McpTool(name: 'process_data_by_mode')]
2727
public function processData(string $input): array

0 commit comments

Comments
 (0)