1. Go to this page and download the library: Download elliottlawson/mcp-php-sdk 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/ */
elliottlawson / mcp-php-sdk example snippets
lliottLawson\McpPhpSdk\Server\McpServer;
use ElliottLawson\McpPhpSdk\Server\ServerCapabilities;
use ElliottLawson\McpPhpSdk\Transport\StdioTransport;
// Create server capabilities
$capabilities = ServerCapabilities::create()
->withResources(true)
->withTools(true);
// Create an MCP server
$server = new McpServer([
'name' => 'Example MCP Server',
'version' => '1.0.0',
'capabilities' => $capabilities
]);
// Register a simple echo resource
$server->resource(
'echo',
'echo://{message}',
function (string $uri, array $params) {
return [
'contents' => [
[
'uri' => $uri,
'text' => "You said: {$params['message']}"
]
]
];
}
);
// Register a calculator tool
$server->tool(
'calculator',
[
'type' => 'object',
'properties' => [
'operation' => ['type' => 'string', 'enum' => ['add', 'subtract', 'multiply', 'divide']],
'a' => ['type' => 'number'],
'b' => ['type' => 'number']
],
'rt
try {
$transport = new StdioTransport();
$server->connect($transport);
// The server will now run until the process is terminated
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage() . PHP_EOL;
exit(1);
}
$server->resource(
'name', // The resource name
'uri://{pattern}', // The URI pattern (with placeholders)
function (string $uri, array $params) {
// Handler function that returns resource content
return [
'contents' => [
[
'uri' => $uri,
'text' => 'Content goes here'
// Can also