1. Go to this page and download the library: Download dtyq/php-mcp library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dtyq / php-mcp example snippets
// Just one line of code!
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handler();
});
class CalculatorService
{
#[McpTool(description: 'Mathematical calculations')]
public function calculate(string $operation, int $a, int $b): array
{
return ['result' => match($operation) {
'add' => $a + $b,
'multiply' => $a * $b,
default => 0
}];
}
#[McpResource(description: 'System information')]
public function systemInfo(): TextResourceContents
{
return new TextResourceContents('mcp://system/info',
json_encode(['php' => PHP_VERSION]), 'application/json');
}
}
Dtyq\PhpMcp\Server\McpServer;
use Dtyq\PhpMcp\Shared\Kernel\Application;
// Create server with simple container
$container = /* your PSR-11 container */;
$app = new Application($container, ['sdk_name' => 'my-server']);
$server = new McpServer('my-server', '1.0.0', $app);
// Add a tool
$server->registerTool(
new \Dtyq\PhpMcp\Types\Tools\Tool('echo', [
'type' => 'object',
'properties' => ['message' => ['type' => 'string']],
'
use Dtyq\PhpMcp\Client\McpClient;
$client = new McpClient('my-client', '1.0.0', $app);
$session = $client->connect('stdio', ['command' => 'php server.php']);
$session->initialize();
// Call a tool
$result = $session->callTool('echo', ['message' => 'Hello, MCP!']);
echo $result->getContent()[0]->getText();
bash
composer
bash
git clone https://github.com/dtyq/php-mcp.git
cd php-mcp
composer install
composer test
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.