1. Go to this page and download the library: Download logiscape/mcp-sdk-php 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/ */
logiscape / mcp-sdk-php example snippets
// A basic example server with a list of prompts for testing
rompt;
use Mcp\Types\PromptArgument;
use Mcp\Types\PromptMessage;
use Mcp\Types\ListPromptsResult;
use Mcp\Types\TextContent;
use Mcp\Types\Role;
use Mcp\Types\GetPromptResult;
use Mcp\Types\GetPromptRequestParams;
// Create a server instance
$server = new Server('example-server');
// Register prompt handlers
$server->registerHandler('prompts/list', function($params) {
$prompt = new Prompt(
name: 'example-prompt',
description: 'An example prompt template',
arguments: [
new PromptArgument(
name: 'arg1',
description: 'Example argument',
red: true
)
]
);
return new GetPromptResult(
messages: [
new PromptMessage(
role: Role::USER,
content: new TextContent(
text: "Example prompt text with argument: $argValue"
)
)
],
description: 'Example prompt'
);
});
// Create initialization options and run server
$initOptions = $server->createInitializationOptions();
$runner = new ServerRunner($server, $initOptions);
$runner->run();
// A basic example client that connects to example_server.php and outputs the prompts
tent;
// Create server parameters for stdio connection
$serverParams = new StdioServerParameters(
command: 'php', // Executable
args: ['example_server.php'], // File path to the server
env: null // Optional environment variables
);
// Create client instance
$client = new Client();
try {
echo("Starting to connect\n");
// Connect to the server using stdio transport
$session = $client->connect(
commandOrUrl: $serverParams->getCommand(),
args: $serverParams->getArgs(),
env: $serverParams->getEnv()
);
echo("Starting to get available prompts\n");
// List available prompts
$promptsResult = $session->listPrompts();
// Output the list of prompts
if (!empty($promptsResult->prompts)) {
echo "Available prompts:\n";
foreach ($promptsResult->prompts as $prompt) {
echo " - Name: " . $prompt->name . "\n";
echo " Description: " . $prompt->description . "\n";
echo " Arguments:\n";
if (!empty($prompt->arguments)) {
foreach ($prompt->arguments as $argument) {
echo " - " . $argument->name . " (" . ($argument->
// A version of the basic example server with extra logging
// Enable comprehensive error logging
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/php_errors.log'); // Logs errors to a file
error_reporting(E_ALL);
er;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Formatter\LineFormatter;
// Create a logger
$logger = new Logger('mcp-server');
// Delete previous log
@unlink(__DIR__ . '/server_log.txt');
// Create a handler that writes to server_log.txt
$handler = new StreamHandler(__DIR__ . '/server_log.txt', Logger::DEBUG);
// Optional: Create a custom formatter to make logs more readable
$dateFormat = "Y-m-d H:i:s";
$output = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
$formatter = new LineFormatter($output, $dateFormat);
$handler->setFormatter($formatter);
// Add the handler to the logger
$logger->pushHandler($handler);
// Create a server instance
$server = new Server('example-server', $logger);
// Register prompt handlers
$server->registerHandler('prompts/list', function($params) {
$prompt = new Prompt(
name: 'example-prompt',
description: 'An example prompt template',
arguments: [
new PromptArgument(
name: 'arg1',
description: 'Example argument',
// A version of the basic example client with extra logging and output
// Enable comprehensive error reporting and logging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/php_errors.log'); // Logs errors to a file
error_reporting(E_ALL);
R__ . '/client_log.txt');
// Create a handler that writes to client_log.txt
$handler = new StreamHandler(__DIR__ . '/client_log.txt', Logger::DEBUG);
// Optional: Create a custom formatter to make logs more readable
$dateFormat = "Y-m-d H:i:s";
$output = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
$formatter = new LineFormatter($output, $dateFormat);
$handler->setFormatter($formatter);
// Add the handler to the logger
$logger->pushHandler($handler);
// Create server parameters for stdio connection
$serverParams = new StdioServerParameters(
command: 'php', // Executable
args: ['debug_example_server.php'], // File path to the server
env: null // Optional environment variables
);
echo("Creating client\n");
// Create client instance
$client = new Client($logger);
try {
echo("Starting to connect\n");
// Connect to the server using stdio transport
$session = $client->connect(
commandOrUrl: $serverParams->getCommand(),
args: $serverParams->getArgs(),
env: $serverParams->getEnv()
);
echo("Starting to get available prompts\n");
// List available prompts
$promptsResult = $session->listPrompts();
// Output the list of prompts
if (!empty($promptsResult->prompts)) {
echo "Available prompts:\n";
foreach ($promptsResult->prompts as $prompt) {
echo " - Name: " . $prompt->name . "\n";
echo " Description: " . $prompt->description . "\n";
echo " Arguments:\n";
if (!empty($prompt->arguments)) {
foreach ($prompt->arguments as $argument) {
echo " - " . $argument->name . " (" . ($argument->
bash
composer
bash
php example_client.php
bash
php debug_example_client.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.