Download the PHP package php-mcp/server without Composer

On this page you can find all versions of the php package php-mcp/server. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package server

PHP MCP Server SDK

Latest Version on Packagist Total Downloads Tests

A comprehensive PHP SDK for building Model Context Protocol (MCP) servers. Create production-ready MCP servers in PHP with modern architecture, extensive testing, and flexible transport options.

This SDK enables you to expose your PHP application's functionality as standardized MCP Tools, Resources, and Prompts, allowing AI assistants (like Anthropic's Claude, Cursor IDE, OpenAI's ChatGPT, etc.) to interact with your backend using the MCP standard.

๐Ÿš€ Key Features

This package supports the 2025-03-26 version of the Model Context Protocol with backward compatibility.

๐Ÿ“‹ Requirements

๐Ÿ“ฆ Installation

๐Ÿ’ก Laravel Users: Consider using php-mcp/laravel for enhanced framework integration, configuration management, and Artisan commands.

โšก 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:

2. Create the Server Script

Create mcp-server.php:

3. Configure Your MCP Client

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

4. Test the Server

Your AI assistant can now call:

๐Ÿ—๏ธ Architecture Overview

The PHP MCP Server uses a modern, decoupled architecture:

Core Components

Transport Options

  1. StdioServerTransport: Standard I/O for direct client launches
  2. HttpServerTransport: HTTP + Server-Sent Events for web integration
  3. StreamableHttpServerTransport: Enhanced HTTP with resumability and event sourcing

โš™๏ธ Server Configuration

Basic Configuration

Advanced Configuration with Dependencies

Session Management Options

๐ŸŽฏ Defining MCP Elements

The server provides two powerful ways to define MCP elements: Attribute-Based Discovery (recommended) and Manual Registration. Both can be combined, with manual registrations taking precedence.

Element Types

1. ๐Ÿท๏ธ Attribute-Based Discovery (Recommended)

Use PHP 8 attributes to mark methods or invokable classes as MCP elements. The server will discover them via filesystem scanning.

Discovery Process:

Available Attributes:

2. ๐Ÿ”ง Manual Registration

Register elements programmatically using the ServerBuilder before calling build(). Useful for dynamic registration or when you prefer explicit control.

Key Features:

๐Ÿ† Element Precedence & Discovery

Precedence Rules:

Discovery Process:

Caching Behavior:

๐Ÿš€ Running the Server (Transports)

The server core is transport-agnostic. Choose a transport based on your deployment needs:

1. ๐Ÿ“Ÿ Stdio Transport

Best for: Direct client execution, command-line tools, simple deployments

Client Configuration:

โš ๏ธ Important: When using stdio transport, never write to STDOUT in your handlers (use STDERR for debugging). STDOUT is reserved for JSON-RPC communication.

2. ๐ŸŒ HTTP + Server-Sent Events Transport (Deprecated)

โš ๏ธ Note: This transport is deprecated in the latest MCP protocol version but remains available for backwards compatibility. For new projects, use the StreamableHttpServerTransport which provides enhanced features and better protocol compliance.

Best for: Legacy applications requiring backwards compatibility

Client Configuration:

Endpoints:

3. ๐Ÿ”„ Streamable HTTP Transport (Recommended)

Best for: Production deployments, remote MCP servers, multiple clients, resumable connections

JSON Response Mode:

The enableJsonResponse option controls how responses are delivered:

Features:

๐Ÿ“‹ Schema Generation and Validation

The server automatically generates JSON schemas for tool parameters using a sophisticated priority system that combines PHP type hints, docblock information, and the optional #[Schema] attribute. These generated schemas are used both for input validation and for providing schema information to MCP clients.

Schema Generation Priority

The server follows this order of precedence when generating schemas:

  1. #[Schema] attribute with definition - Complete schema override (highest precedence)
  2. Parameter-level #[Schema] attribute - Parameter-specific schema enhancements
  3. Method-level #[Schema] attribute - Method-wide schema configuration
  4. PHP type hints + docblocks - Automatic inference from code (lowest precedence)

When a definition is provided in the Schema attribute, all automatic inference is bypassed and the complete definition is used as-is.

Parameter-Level Schema Attributes

Method-Level Schema

Complete Schema Override (Method-Level Only)

โš ๏ธ Important: Complete schema definition override should rarely be used. It bypasses all automatic schema inference and requires you to define the entire JSON schema manually. Only use this if you're well-versed with JSON Schema specification and have complex validation requirements that cannot be achieved through the priority system. In most cases, parameter-level and method-level #[Schema] attributes provide sufficient flexibility.

๐ŸŽจ Return Value Formatting

The server automatically formats return values from your handlers into appropriate MCP content types:

Automatic Formatting

Advanced Content Types

File and Stream Handling

๐Ÿ”„ Batch Processing

The server automatically handles JSON-RPC batch requests:

๐Ÿ”ง Advanced Features

Completion Providers

Completion providers enable MCP clients to offer auto-completion suggestions in their user interfaces. They are specifically designed for Resource Templates and Prompts to help users discover available options for dynamic parts like template variables or prompt arguments.

Note: Tools and resources can be discovered via standard MCP commands (tools/list, resources/list), so completion providers are not needed for them. Completion providers are used only for resource templates (URI variables) and prompt arguments.

Completion providers must implement the CompletionProviderInterface:

Important: Completion providers only offer suggestions to users in the MCP client interface. Users can still input any value, so always validate parameters in your handlers regardless of completion provider constraints.

Custom Dependency Injection

Your MCP element handlers can use constructor dependency injection to access services like databases, APIs, or other business logic. When handlers have constructor dependencies, you must provide a pre-configured PSR-11 container that contains those dependencies.

By default, the server uses a BasicContainer - a simple implementation that attempts to auto-wire dependencies by instantiating classes with parameterless constructors. For dependencies that require configuration (like database connections), you can either manually add them to the BasicContainer or use a more advanced PSR-11 container like PHP-DI or Laravel's container.

Resource Subscriptions

Resumability and Event Store

For production deployments using StreamableHttpServerTransport, you can implement resumability with event sourcing by providing a custom event store:

Custom Session Handlers

Implement custom session storage by creating a class that implements SessionHandlerInterface:

SSL Context Configuration

For HTTPS deployments of StreamableHttpServerTransport, configure SSL context options:

SSL Context Reference: For complete SSL context options, see the PHP SSL Context Options documentation.

๐Ÿ” Error Handling & Debugging

The server provides comprehensive error handling and debugging capabilities:

Exception Handling

Tool handlers can throw any PHP exception when errors occur. The server automatically converts these exceptions into proper JSON-RPC error responses for MCP clients.

The server will convert these exceptions into appropriate JSON-RPC error responses that MCP clients can understand and display to users.

Logging and Debugging

๐Ÿš€ Production Deployment

Since $server->listen() runs a persistent process, you can deploy it using any strategy that suits your infrastructure needs. The server can be deployed on VPS, cloud instances, containers, or any environment that supports long-running processes.

Here are two popular deployment approaches to consider:

Option 1: VPS with Supervisor + Nginx (Recommended)

Best for: Most production deployments, cost-effective, full control

Supervisor Configuration:

Nginx Configuration with SSL:

Start Services:

Client Configuration:

Option 2: Docker Deployment

Best for: Containerized environments, Kubernetes, cloud platforms

Production Dockerfile:

docker-compose.yml:

Security Best Practices

  1. Firewall Configuration:

  2. SSL/TLS Setup:

๐Ÿ“š Examples & Use Cases

Explore comprehensive examples in the examples/ directory:

Available Examples

Running Examples

๐Ÿšง Migration from v2.x

If migrating from version 2.x, note these key changes:

Schema Updates

Session Management

Transport Changes

๐Ÿงช Testing

๐Ÿค Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

๐Ÿ“„ License

The MIT License (MIT). See LICENSE for details.

๐Ÿ™ Acknowledgments


Ready to build powerful MCP servers with PHP? Start with our Quick Start guide! ๐Ÿš€


All versions of server with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
opis/json-schema Version ^2.4
php-mcp/schema Version ^1.0
phpdocumentor/reflection-docblock Version ^5.6
psr/clock Version ^1.0
psr/container Version ^1.0 || ^2.0
psr/log Version ^1.0 || ^2.0 || ^3.0
psr/simple-cache Version ^1.0 || ^2.0 || ^3.0
react/event-loop Version ^1.5
react/http Version ^1.11
react/promise Version ^3.0
react/stream Version ^1.4
symfony/finder Version ^6.4 || ^7.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package php-mcp/server contains the following files

Loading the files please wait ....