Mirror of https://github.com/modelcontextprotocol/php-sdk — The official PHP SDK for Model Context Protocol servers and clients. Maintained in collaboration with The PHP Foundation.
  • PHP 99.8%
  • Makefile 0.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Pavel Buchnev 99bf56eef8
Registry Architecture Refactoring - Enhanced Separation of Concerns (#46)
* refactor: Separate Registry concerns following SOLID principles [WIP]

- Extract ReferenceProvider and ReferenceRegistryInterface interfaces
- Create DefaultToolExecutor with ReferenceHandlerInterface
- Remove execution responsibility from Registry class
- Enable custom handler and executor implementations

* refactor: implement SOLID principles with separated execution concerns

* Create DefaultResourceReader, DefaultPromptGetter
* Refactor JsonRpc Handler and RequestHandlers to use dedicated executors
* Update ServerBuilder to support custom executors via dependency injection

* chore: remove extra comments

* feat: introduce DispatchableRegistry for enhanced tool and resource management

* refactor: use proper interfaces

* refactor: implement HandlerInterface for improved abstraction and flexibility

* test: add unit tests for DispatchableRegistry and Registry classes

* refactor: cover with unit tests Resource reader, Prompt getter and Tool executor

* cs fix

* phpstan fix

* test: add unit tests for CallToolHandler, GetPromptHandler, PingHandler, and ReadResourceHandler

* refactor: revert Handler

* chore: remove style guide for tests

* refactor: remove "Default" prefix from classes

* refactor: remove DispatchableRegistry

* refactor: use package specific exception classes

* refactor: remove DispatchableRegistry

* refactor: add logger support to PromptGetter and ResourceReader classes

* Update src/Capability/Prompt/PromptGetter.php

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>

* refactor: Use FQN for Resource class to avoid cs-fixer misinterpreting it as `resource`

* refactor: rename ToolExecutor to ToolCaller and related classes for clarity and consistency

* refactor: add missed docblock

* refactor: rename ToolExecutor references to ToolCaller

* refactor: rename ToolCallerTest property and variable references

* cs fix

* ignore some phpstan errors

---------

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
2025-09-14 20:44:18 +02:00
.github Remove --no-scripts option from composer install (#30) 2025-09-07 01:47:53 +02:00
examples Registry Architecture Refactoring - Enhanced Separation of Concerns (#46) 2025-09-14 20:44:18 +02:00
src Registry Architecture Refactoring - Enhanced Separation of Concerns (#46) 2025-09-14 20:44:18 +02:00
tests Registry Architecture Refactoring - Enhanced Separation of Concerns (#46) 2025-09-14 20:44:18 +02:00
.gitattributes Add Makefile to export-ignore in .gitattributes (#56) 2025-09-09 22:32:02 +02:00
.gitignore Adopt server examples 2025-08-29 11:19:51 +02:00
.php-cs-fixer.dist.php Kick off the SDK based on Symfony's MCP SDK 2025-08-29 11:09:16 +02:00
CHANGELOG.md Kick off the SDK based on Symfony's MCP SDK 2025-08-29 11:09:16 +02:00
composer.json Remove suggest section in composer.json (#34) 2025-09-04 23:50:15 +02:00
LICENSE Kick off the SDK based on Symfony's MCP SDK 2025-08-29 11:09:16 +02:00
Makefile Kick off the SDK based on Symfony's MCP SDK 2025-08-29 11:09:16 +02:00
phpstan-baseline.neon Registry Architecture Refactoring - Enhanced Separation of Concerns (#46) 2025-09-14 20:44:18 +02:00
phpstan.dist.neon Fix typos in documentation and configuration files (#25) 2025-09-04 23:25:49 +02:00
phpunit.xml.dist Kick off the SDK based on Symfony's MCP SDK 2025-08-29 11:09:16 +02:00
README.md Updated README.md to clarify grammar in "Important" section (#41) 2025-09-06 22:34:51 +02:00

MCP PHP SDK

The official PHP SDK for Model Context Protocol (MCP). It provides a framework-agnostic API for implementing MCP servers in PHP.

Important

Currently, we are still in the process of merging Symfony's MCP SDK and PHP-MCP components. Not all code paths are fully tested or complete, and this package may still contain duplicate functionality or dead code.

If you want to help us stabilize the SDK, please see the issue tracker.

This project is a collaboration between the PHP Foundation and the Symfony project. It adopts development practices and standards from the Symfony project, including Coding Standards and the Backward Compatibility Promise.

Until the first major release, this SDK is considered experimental.

🚧 Roadmap

Features


Examples working

  • 01-discovery-stdio-calculator
  • 02-discovery-http-userprofile
  • 03-manual-registration-stdio
  • 04-combined-registration-http
  • 05-stdio-env-variables
  • 06-custom-dependencies-stdio
  • 07-complex-tool-schema-http
  • 08-schema-showcase-streamable
  • 09-standalone-cli

Installation

composer require mcp/sdk

Since this package has no tagged releases yet, it is required to extend your composer.json:

"minimum-stability": "dev",
"prefer-stable": true

⚡ Quick Start: Stdio Server with Discovery

This example demonstrates the most common usage pattern - a stdio server using attribute discovery.

1. Define Your MCP Elements

Create src/CalculatorElements.php:

<?php

namespace App;

use Mcp\Capability\Attribute\McpTool;

class CalculatorElements
{
    #[McpTool(name: 'add_numbers')]
    public function add(int $a, int $b): int
    {
        return $a + $b;
    }
}

2. Create the Server Script

Create mcp-server.php:

#!/usr/bin/env php
<?php

declare(strict_types=1);

require_once __DIR__ . '/vendor/autoload.php';

use Mcp\Server;
use Mcp\Server\Transport\StdioTransport;

Server::make()
    ->withServerInfo('Stdio Calculator', '1.1.0', 'Basic Calculator over STDIO transport.')
    ->withDiscovery(__DIR__, ['.'])
    ->build()
    ->connect(new StdioTransport());

3. Configure Your MCP Client

Add to your client configuration (e.g., mcp.json):

{
    "mcpServers": {
        "php-calculator": {
            "command": "php",
            "args": ["/absolute/path/to/your/mcp-server.php"]
        }
    }
}

4. Test the Server

Your AI assistant can now call:

  • add_numbers - Add two integers

Documentation

Examples of MCP Tools that use this SDK

Contributing

We are passionate about supporting contributors of all levels of experience and would love to see you get involved in the project. See the contributing guide to get started before you report issues and send pull requests.

Credits

The starting point for this SDK was the PHP-MCP project, initiated by Kyrian Obikwelu. We are grateful for the work done by Kyrian and other contributors to that repository, which created a solid foundation for this SDK.

License

This project is licensed under the MIT License - see the LICENSE file for details.