Skip to content

he426100/mcp-sdk-php

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Installation

You can install the package via composer:

composer require he426100/mcp-sdk-php

Requirements

  • PHP 8.1 or higher
  • ext-curl
  • ext-pcntl (optional, recommended for CLI environments)
  • ext-swow (for sse and websocket transport)

Basic Usage

Creating an MCP Server

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

Using Annotations (Alternative Approach)

<?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);

Sample Project

Documentation

For detailed information about the Model Context Protocol, visit the official documentation.

Credits

This PHP SDK was developed by:

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.

License

The MIT License (MIT). Please see License File for more information.

About

Model Context Protocol SDK for PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 91.3%
  • JavaScript 8.7%