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
xentixar 4db831778b
[Server] Implement discovery caching for improved performance (#39)
* feat: implement discovery caching with state-based approach

- Add DiscoveryState class to encapsulate discovered MCP capabilities
- Add exportDiscoveryState and importDiscoveryState methods to Registry
- Modify Discoverer to return DiscoveryState instead of void
- Create CachedDiscoverer decorator for caching discovery results
- Add importDiscoveryState method to ReferenceRegistryInterface
- Update ServerBuilder to use caching with withCache() method
- Update tests to work with new state-based approach
- Add example demonstrating cached discovery functionality
- Add PSR-16 SimpleCache and Symfony Cache dependencies

* docs: add comprehensive discovery caching documentation

- Add detailed documentation explaining discovery caching architecture
- Include usage examples for different cache implementations
- Document performance benefits and best practices
- Add troubleshooting guide and migration instructions
- Include complete API reference for all caching components
- Fix PHPStan issues by regenerating baseline
- Apply PHP CS Fixer formatting to all new files

* refactor: address reviewer feedback and clean up discovery caching

- Make DiscoveryState class final
- Cache DiscoveryState objects directly instead of arrays (no serialization needed)
- Rename exportDiscoveryState/importDiscoveryState to getDiscoveryState/setDiscoveryState
- Add getDiscoveryState method to ReferenceRegistryInterface
- Remove TTL parameter from CachedDiscoverer (no expiration by default)
- Remove unused methods from DiscoveryState (toArray, fromArray, merge)
- Simplify ServerBuilder to handle decoration internally
- Make Discoverer.applyDiscoveryState() internal (no longer public API)
- Simplify documentation to focus on user perspective
- Remove unnecessary development comments
- Update all tests to work with new architecture
- All tests pass, PHPStan clean, code formatting applied

* refactor: clean up comments and formatting in server.php example

* refactor: improve code formatting and consistency in discovery classes and example server

* Fix method names in cached discovery example

- Change withServerInfo() to setServerInfo()
- Change withDiscovery() to setDiscovery()
- Change withLogger() to setLogger()
- Fixes PHPStan static analysis errors

* refactor: update method names in ServerBuilder for consistency

- Change withCache() to setCache() in ServerBuilder
- Update example and documentation to reflect method name changes
- Ensure consistency across caching implementation in examples and documentation

* refactor: reorganize examples and fix PHPStan baseline

- Move cached discovery example from examples/10 to examples/09
- Update composer.json autoload-dev namespace mapping
- Fix PHPStan baseline by removing invalid entries for non-existent files
- Update server.php to follow proper example pattern with logging and lifecycle

* fix: correct formatting in server.php
2025-09-24 20:54:27 +02:00
.github Remove --no-scripts option from composer install (#30) 2025-09-07 01:47:53 +02:00
docs [Server] Implement discovery caching for improved performance (#39) 2025-09-24 20:54:27 +02:00
examples [Server] Implement discovery caching for improved performance (#39) 2025-09-24 20:54:27 +02:00
src [Server] Implement discovery caching for improved performance (#39) 2025-09-24 20:54:27 +02:00
tests [Server] Implement discovery caching for improved performance (#39) 2025-09-24 20:54:27 +02:00
.gitattributes Add Makefile to export-ignore in .gitattributes (#56) 2025-09-09 22:32:02 +02:00
.gitignore feat(server): Rework transport architecture and add StreamableHttpTransport (#49) 2025-09-21 12:12:40 +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 refactor: remove dead code and obsolete examples (#76) 2025-09-21 20:23:59 +02:00
composer.json [Server] Implement discovery caching for improved performance (#39) 2025-09-24 20:54:27 +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 [Server] Implement discovery caching for improved performance (#39) 2025-09-24 20:54:27 +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 Switch to set* and add* on ServerBuilder instead of with* (#71) 2025-09-15 08:30:41 +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()
    ->setServerInfo('Stdio Calculator', '1.1.0', 'Basic Calculator over STDIO transport.')
    ->setDiscovery(__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.