Fix & clean up examples (#78)

This commit is contained in:
Christopher Hertel 2025-09-24 21:50:06 +02:00 • committed by GitHub
commit 8defd9b17f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 136 additions and 397 deletions

View file

@ -13,16 +13,19 @@ namespace Mcp\Example\DependenciesStdioExample;
use Mcp\Capability\Attribute\McpResource;
use Mcp\Capability\Attribute\McpTool;
use Mcp\DependenciesStdioExample\Services\StatsServiceInterface;
use Mcp\DependenciesStdioExample\Services\TaskRepositoryInterface;
use Mcp\Example\DependenciesStdioExample\Service\StatsServiceInterface;
use Mcp\Example\DependenciesStdioExample\Service\TaskRepositoryInterface;
use Psr\Log\LoggerInterface;
class McpTaskHandlers
/**
* @phpstan-import-type Task from TaskRepositoryInterface
*/
final class McpTaskHandlers
{
public function __construct(
private TaskRepositoryInterface $taskRepo,
private StatsServiceInterface $statsService,
private LoggerInterface $logger,
private readonly TaskRepositoryInterface $taskRepo,
private readonly StatsServiceInterface $statsService,
private readonly LoggerInterface $logger,
) {
$this->logger->info('McpTaskHandlers instantiated with dependencies.');
}
@ -33,7 +36,7 @@ class McpTaskHandlers
* @param string $userId the ID of the user
* @param string $description the task description
*
* @return array the created task details
* @return Task the created task details
*/
#[McpTool(name: 'add_task')]
public function addTask(string $userId, string $description): array
@ -48,7 +51,7 @@ class McpTaskHandlers
*
* @param string $userId the ID of the user
*
* @return array a list of tasks
* @return Task[] a list of tasks
*/
#[McpTool(name: 'list_user_tasks')]
public function listUserTasks(string $userId): array
@ -63,7 +66,7 @@ class McpTaskHandlers
*
* @param int $taskId the ID of the task to complete
*
* @return array status of the operation
* @return array<string, mixed> status of the operation
*/
#[McpTool(name: 'complete_task')]
public function completeTask(int $taskId): array
@ -77,7 +80,7 @@ class McpTaskHandlers
/**
* Provides current system statistics.
*
* @return array system statistics
* @return array<string, int> system statistics
*/
#[McpResource(uri: 'stats://system/overview', name: 'system_stats', mimeType: 'application/json')]
public function getSystemStatistics(): array

View file

@ -13,17 +13,20 @@ namespace Mcp\Example\DependenciesStdioExample\Service;
use Psr\Log\LoggerInterface;
class InMemoryTaskRepository implements TaskRepositoryInterface
/**
* @phpstan-import-type Task from TaskRepositoryInterface
*/
final class InMemoryTaskRepository implements TaskRepositoryInterface
{
/**
* @var array<int, Task>
*/
private array $tasks = [];
private int $nextTaskId = 1;
private LoggerInterface $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
public function __construct(
private readonly LoggerInterface $logger,
) {
// Add some initial tasks
$this->addTask('user1', 'Buy groceries');
$this->addTask('user1', 'Write MCP example');

View file

@ -13,5 +13,8 @@ namespace Mcp\Example\DependenciesStdioExample\Service;
interface StatsServiceInterface
{
/**
* @return array<string, int>
*/
public function getSystemStats(): array;
}

View file

@ -11,13 +11,11 @@
namespace Mcp\Example\DependenciesStdioExample\Service;
class SystemStatsService implements StatsServiceInterface
final class SystemStatsService implements StatsServiceInterface
{
private TaskRepositoryInterface $taskRepository;
public function __construct(TaskRepositoryInterface $taskRepository)
{
$this->taskRepository = $taskRepository;
public function __construct(
private readonly TaskRepositoryInterface $taskRepository,
) {
}
public function getSystemStats(): array

View file

@ -11,12 +11,24 @@
namespace Mcp\Example\DependenciesStdioExample\Service;
/**
* @phpstan-type Task array{id: int, userId: string, description: string, completed: bool, createdAt: string}
*/
interface TaskRepositoryInterface
{
/**
* @return Task
*/
public function addTask(string $userId, string $description): array;
/**
* @return Task[]
*/
public function getTasksForUser(string $userId): array;
/**
* @return Task[]
*/
public function getAllTasks(): array;
public function completeTask(int $taskId): bool;

View file

@ -13,7 +13,10 @@
require_once dirname(__DIR__).'/bootstrap.php';
chdir(__DIR__);
use Mcp\DependenciesStdioExample\Services;
use Mcp\Example\DependenciesStdioExample\Service\InMemoryTaskRepository;
use Mcp\Example\DependenciesStdioExample\Service\StatsServiceInterface;
use Mcp\Example\DependenciesStdioExample\Service\SystemStatsService;
use Mcp\Example\DependenciesStdioExample\Service\TaskRepositoryInterface;
use Mcp\Server;
use Mcp\Server\Transport\StdioTransport;
@ -21,11 +24,11 @@ logger()->info('Starting MCP Custom Dependencies (Stdio) Server...');
$container = container();
$taskRepo = new Services\InMemoryTaskRepository(logger());
$container->set(Services\TaskRepositoryInterface::class, $taskRepo);
$taskRepo = new InMemoryTaskRepository(logger());
$container->set(TaskRepositoryInterface::class, $taskRepo);
$statsService = new Services\SystemStatsService($taskRepo);
$container->set(Services\StatsServiceInterface::class, $statsService);
$statsService = new SystemStatsService($taskRepo);
$container->set(StatsServiceInterface::class, $statsService);
$server = Server::make()
->setServerInfo('Task Manager Server', '1.0.0')