- PHP 99.8%
- Makefile 0.2%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
* Cast variadic tool parameters element-by-element SchemaGenerator::buildVariadicParameterSchema() already advertises a variadic parameter (e.g. `int ...$scores`) as a JSON "array" schema, so a spec-conformant client sends an array for it. But ReferenceHandler::prepareArguments() never special-cased ReflectionParameter::isVariadic(): it fell through to the regular single-value branch and passed the whole array to castArgumentType(), which tried to cast it as one scalar and failed - e.g. "Cannot cast value to integer" for `int ...$scores` given [1, 2, 3]. Any tool handler declaring a variadic parameter was therefore unreachable through the advertised schema, confirmed by direct reproduction against ReferenceHandler::handle() before this change. Casts each array element individually via the existing castArgumentType() (unchanged - it already inspects the variadic parameter's element type, not "array", per PHP reflection semantics) and appends to the result in order; variadic is always the last parameter, so this preserves correct positional ordering. A non-array value for a variadic argument now fails clearly with an invalid-params error instead of the confusing cast message. * Address chr-hertel's review: wrap scalars, reject objects, index cast errors - Non-array variadic argument is now wrapped as a single-element list instead of being rejected: MCP prompt arguments are always Record<string,string> per protocol, so a variadic prompt parameter legitimately receives a plain string, not an array. Rejecting it regressed behaviour that worked on main. - A JSON object (non-list array) is now explicitly rejected instead of being silently flattened into positional args by array_values(). - Cast-failure errors now include the parameter name and element index, so a failure inside a large array is actually actionable. - Trimmed the inline rationale comment; the PR description already covers it. Tests updated: replaced the "throws on non-array" test (no longer correct) with tests for the wrap-as-single-element, reject-object, and indexed-error-message behaviours. * Pin argument order for an injectable before a variadic --------- Co-authored-by: Christopher Hertel <mail@christopher-hertel.de> |
||
| .github | ||
| .phpdoc/template | ||
| adr | ||
| docs | ||
| examples | ||
| src | ||
| tests | ||
| .gitattributes | ||
| .gitignore | ||
| .php-cs-fixer.dist.php | ||
| CHANGELOG.md | ||
| composer.json | ||
| CONTRIBUTING.md | ||
| LICENSE | ||
| Makefile | ||
| mkdocs.yml | ||
| phpdoc.dist.xml | ||
| phpstan-baseline.neon | ||
| phpstan.dist.neon | ||
| phpunit.xml.dist | ||
| README.md | ||
| requirements-docs.txt | ||
| ROADMAP.md | ||
| SECURITY.md | ||
MCP PHP SDK
The official PHP SDK for the Model Context Protocol (MCP). It provides a framework-agnostic API for implementing MCP
servers and clients in PHP — tools, resources, prompts, STDIO and HTTP transports, sessions, authorization, and both
protocol eras (the initialize handshake and the stateless 2026-07-28 revision).
This project represents 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, please see the roadmap for planned next steps and features.
Installation
composer require mcp/sdk
Build a server
A server is a plain PHP class plus three lines of wiring:
use Mcp\Capability\Attribute\McpResource;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Server;
use Mcp\Server\Transport\StdioTransport;
class Calculator
{
/**
* Adds two numbers.
*/
#[McpTool]
public function add(int $a, int $b): int
{
return $a + $b;
}
#[McpResource(uri: 'config://calculator/settings')]
public function settings(): array
{
return ['precision' => 2];
}
}
exit(Server::builder()
->setServerInfo('Calculator', '1.0.0')
->setDiscovery(__DIR__, ['.'], excludeDirs: ['vendor'])
->build()
->run(new StdioTransport()));
The walkthrough in First server explains each piece, and Try it with the Inspector shows it running.
Build a client
use Mcp\Client;
use Mcp\Client\Transport\StdioTransport;
$client = Client::builder()
->setClientInfo('My Application', '1.0.0')
->build();
$client->connect(new StdioTransport(command: 'php', args: ['/path/to/server.php']));
$tools = $client->listTools();
$result = $client->callTool('add', ['a' => 5, 'b' => 3]);
$client->disconnect();
See Connecting to a server for transports, timeouts, and the handlers that answer server-initiated requests.
Documentation
The full documentation is published at php.sdk.modelcontextprotocol.io.
- Get started — Install the SDK and build your first server
- Servers — Tools, resources, resource templates, prompts, and how to register them
- Inside your handler — Talking back to the client, logging, and asking for input
- Running your server — Server builder, STDIO and HTTP transports, framework integration, sessions, authorization
- Clients — Client SDK for connecting to and communicating with MCP servers
- Protocol versions — The two protocol eras, and what revision
2026-07-28changed - Advanced — Events, protocol extensions (including MCP Apps), and custom message handlers
- Examples — Runnable server and client examples
- API Reference — Generated class reference
External Resources
- Model Context Protocol Documentation — Official MCP documentation
- Model Context Protocol Specification — Protocol specification
- Officially Supported Servers — Reference server implementations
PHP Libraries Using the MCP SDK
- api-platform/mcp — MCP integration for API Platform
- bnomei/kirby-mcp — MCP server for the Kirby CMS
- drupal/mcp_server — MCP server for Drupal exposing configuration and entities as MCP elements
- josbeir/cakephp-synapse — CakePHP plugin exposing application functionality over MCP
- nette/mcp-inspector — MCP server for introspecting Nette applications
- symfony/ai-mate — AI development assistant MCP server for Symfony projects
- symfony/mcp-bundle — Symfony integration bundle
Building something on top of the SDK? Open a pull request to add it to this list.
Contributing
We are passionate about supporting contributors of all levels of experience and would love to see you get involved in the project. Start by reporting issues or sending pull requests. See CONTRIBUTING.md for development setup, coding standards, and what to run before opening a PR.
Credits
The starting point for this SDK was the PHP-MCP project, initiated by Kyrian Obikwelu, and the Symfony AI initiative. We are grateful for the work done by both projects and their contributors, which created a solid foundation for this SDK.
License
This project is licensed under the Apache License, Version 2.0 for new contributions, with existing code under the MIT License — see the LICENSE file for details.