Sandbox
@modelcontextprotocol/php-sdk

PHP SDK for MCP servers and clients

This library provides the core PHP building blocks for Model Context Protocol apps. You can use it to define tools and resources on a server, or to connect a client to an MCP server over STDIO or HTTP. It also covers sessions, authorization, and both current protocol eras.

1,605 stars170 forksPHPUpdated 9d ago
Who it's for

Builders who want to add MCP support to a PHP app or connect PHP code to MCP tools.

What it delivers

You can build MCP servers and clients in PHP without wiring the protocol by hand.

What it does

Server builder

Create an MCP server from a plain PHP class with `Server::builder()`, discovery, and a transport like STDIO.

Client SDK

Connect to MCP servers, list tools, call tools, and handle server-initiated requests.

Tools, resources, and prompts

Expose server capabilities through attributes and protocol handlers for tools, resources, resource templates, and prompts.

STDIO and HTTP transports

Run MCP traffic over local process streams or HTTP.

Sessions and authorization

Support session handling and authorization flows in MCP servers and clients.

Protocol version support

Work with both the `initialize` handshake era and the stateless `2026-07-28` revision.

Conformance coverage

Track server and client behavior against the MCP Conformance Test Framework.

How to get it

  1. 1Run
    composer require mcp/sdk

README

MCP PHP SDK

Latest Version CI PHP Version License

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.

Protocol Conformance

Measured weekly against the MCP Conformance Test Framework, as passing checks out of total. The 2026-07-28 scores run against the framework's alpha releases, so they move as upstream publishes new scenarios.

RevisionServerClient
2026-07-28Server conformance 2026-07-28Client conformance 2026-07-28
2025-11-25Server conformance 2025-11-25Client conformance 2025-11-25

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-28 changed
  • Advanced — Events, protocol extensions (including MCP Apps), and custom message handlers
  • Examples — Runnable server and client examples
  • API Reference — Generated class reference

External Resources

PHP Libraries Using the MCP SDK

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.

Files in the repo

Repository payload24 top-level entries
  • .github
  • .phpdoc
  • 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

Discussion (0)

Ask about usage, or say what you built with it

Sign in to join the discussion.

No comments yet. Be the first to say what this is good for.

More frameworks & sdks

HKUDS/nanobotFrameworks & SDKs

Ultra-lightweight, open-source, self-hosted personal AI agent framework in Python with WebUI, tools, memory, MCP, multi-agent workflows, automation, and chat apps

48k
microsoft/
SkillOpt
microsoft/SkillOptFrameworks & SDKs

SkillOpt is a text-space optimizer that trains reusable natural-language skills for frozen LLM agents through trajectory-driven edits, validation-gated updates, and deployable best_skill.md artifacts.

17k
omnigent-ai/omnigentFrameworks & SDKs

Omnigent is an open-source AI agent framework and meta-harness: orchestrate Claude Code, Codex, Cursor, Pi, and custom agents — swap harnesses without rewriting, enforce policies and sandboxing, and collaborate in real time from any device.

9.8k
kyegomez/
OpenMythos
kyegomez/OpenMythosFrameworks & SDKs

A theoretical reconstruction of the Claude Mythos architecture, built from first principles using the available research literature.

15k
D4Vinci/ScraplingFrameworks & SDKs

🕷️ An adaptive Web Scraping framework that handles everything from a single request to a full-scale crawl!

80k