mcp-sdk-php
Model Context Protocol SDK for PHP
claude mcp add --transport stdio logiscape-mcp-sdk-php php server.php
How to use
The MCP SDK for PHP provides a PHP-based implementation of the Model Context Protocol, allowing you to build both MCP servers and clients using native PHP. This means you can run an MCP server within typical PHP hosting environments (Apache, FPM, or CLI) and communicate with MCP clients over standard transports like STDIO or HTTP. The server exposes resources, prompts, and tools that clients can invoke via JSON-RPC, and it includes a simple API surface for defining tools and handling lifecycle events. To get started, install the package via Composer, create a server script that instantiates McpServer, define tools, and call run() to start listening for requests. You can also use the provided client classes to connect over STDIO or HTTP transports to interact with your server.
Typical usage involves two pieces: a server script that defines the MCP server and its capabilities, and a client that connects to the server to call tools or query resources. The examples show how to create a basic addition tool and expose it via the server, and how to write a client that connects via the stdio transport to invoke the tool. The HTTP transport and OAuth 2.1 authorization are supported, enabling web-based deployments and secure access patterns. The web client in the repository demonstrates a web UI for testing prompts, tools, and resources against your server.
How to install
Prerequisites before installing:
- PHP 8.1 or higher
- PHP extensions: curl (ext-curl), json (ext-json)
- Optional: ext-pcntl (recommended for CLI environments)
- Optional: monolog/monolog (for logging in examples)
Install steps:
- Ensure PHP and Composer are installed on your system.
- Create a new project directory or navigate to your project.
- Install the MCP PHP SDK package via Composer:
composer require logiscape/mcp-sdk-php
- (Optional) Install logging dependency for examples:
composer require monolog/monolog
- Create your MCP server script (for example server.php) that wires up McpServer, defines tools, and calls run(). See the README for examples of defining a tool:
<?php
require 'vendor/autoload.php';
use Mcp\Server\McpServer;
$server = new McpServer('my-mcp-server');
$server
->tool('add-numbers', 'Adds two numbers together', function (float $a, float $b): string {
return 'Sum: ' . ($a + $b);
})
->run();
- Run the server via the PHP CLI:
php server.php
If you prefer HTTP-based transports or other clients, configure your server accordingly (for example, through the provided HTTP transport examples in the repo).
Additional notes
Tips and notes:
- This PHP MCP SDK is designed to work well in shared hosting environments where long-running processes may be restricted. The library includes transport options that can fit typical hosting setups, with attention to lifecycle management.
- If using the CLI, consider enabling pcntl (if available) for improved process control in longer-running CLI sessions.
- For testing, the repository includes a web client (webclient) that can connect to a local PHP MCP server. Ensure you expose the correct transport (stdio or HTTP) consistent with your server script.
- When deploying to production, review OAuth 2.1 integration if you plan to protect HTTP endpoints. The repository mentions an OAuth example; adapt it to your hosting environment and security requirements.
- If you encounter issues with autoloading or dependencies, running composer dump-autoload can help refresh the autoloader after changes.
Related MCP Servers
drift
Codebase intelligence for AI. Detects patterns & conventions + remembers decisions across sessions. MCP server for any IDE. Offline CLI.
laravel
A Laravel package for implementing secure Model Context Protocol servers using Streamable HTTP and SSE transport, providing real-time communication and a scalable tool system for enterprise environments.
php
基于 PHP 实现的 MCP (Model Control Protocol) 服务器框架,通过注解优雅地定义 MCP 服务,支持 Tool、Prompt、Resource 三种处理器,支持Stdio、Sse 两种 transport。
mcp-profiler-bundle
The Symfony MCP Profiler Bundle mimics the WebProfiler Bundle. It bridges the gap between Profiler data and your favorite MCP-enabled AI-powered IDE.
elementor
Comprehensive Elementor MCP Server plugin.
mcp
Model Context Protocol for Hyperf