You can install the package via composer:
composer require he426100/mcp-sdk-php
- PHP 8.1 or higher
- ext-curl
- ext-pcntl (optional, recommended for CLI environments)
- ext-swow (for sse and websocket transport)
Here's a complete example of creating an MCP server that provides prompts:
<?php
// A basic example server with a list of prompts for testing
require 'vendor/autoload.php';
use Mcp\Server\Server;
use Mcp\Server\ServerRunner;
use Mcp\Types\Prompt;
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',
required: true
)
]
);
return new ListPromptsResult([$prompt]);
});
$server->registerHandler('prompts/get', function(GetPromptRequestParams $params) {
$name = $params->name;
$arguments = $params->arguments;
if ($name !== 'example-prompt') {
throw new \InvalidArgumentException("Unknown prompt: {$name}");
}
// Get argument value safely
$argValue = $arguments ? $arguments->arg1 : 'none';
$prompt = new Prompt(
name: 'example-prompt',
description: 'An example prompt template',
arguments: [
new PromptArgument(
name: 'arg1',
description: 'Example argument',
required: 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();
$runner->run($server, $initOptions);
Save this as example_server.php
<?php
require 'vendor/autoload.php';
use Mcp\Server\Server;
use Mcp\Server\ServerRunner;
use Mcp\Annotation\Prompt;
use Mcp\Tool\McpHandlerRegistrar;
$server = new Server('example-server');
// Define the prompt handling class using annotations
class ExamplePrompts
{
#[Prompt(
name: 'example-prompt',
description: 'An example prompt template',
arguments: [
'arg1' => [
'description' => 'Example argument',
'required' => true
]
]
)]
public function generatePrompt(string $arg1): string
{
return "Example prompt text with argument: $arg1";
}
}
// Register annotation processor
(new McpHandlerRegistrar)->registerHandler($server, new ExamplePrompts());
// Create initialization options and run server
$initOptions = $server->createInitializationOptions();
$runner = new ServerRunner();
$runner->run($server, $initOptions);
For detailed information about the Model Context Protocol, visit the official documentation.
This PHP SDK was developed by:
- Josh Abbott
- he426100
- Claude 3.5 Sonnet (Anthropic AI model)
Additional debugging and refactoring done by Josh Abbott using OpenAI ChatGPT o1 pro mode.
Based on the original Python SDK for the Model Context Protocol.
The MIT License (MIT). Please see License File for more information.