MCP-Server

bdlabs/MCP-Server

3.2

If you are the rightful owner of MCP-Server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

MCP Server is a modular communication protocol server designed for easy integration and management of various tools and communication methods.

MCP Server - Modular Communication Protocol Server

Overview

MCP Server is a powerful and flexible server solution that enables easy integration and management of various tools and communication methods. Designed according to SOLID principles, it offers a modular architecture allowing rapid extension of functionality without modifying existing code.

Project Structure

mcp-server/
ā”œā”€ā”€ Server/
│   ā”œā”€ā”€ Registry/
│   │   ā”œā”€ā”€ MethodRegistry.php         # Registry of available API methods
│   │   └── ToolRegistry.php           # Registry of available tools
│   │
│   ā”œā”€ā”€ ResponseFormatter/
│   │   ā”œā”€ā”€ ResponseFormatterInterface.php  # Response formatter interface
│   │   ā”œā”€ā”€ JsonResponseFormatter.php       # JSON response formatter
│   │   └── EventStreamResponseFormatter.php # Event stream formatter
│   │
│   ā”œā”€ā”€ Tool/
│   │   ā”œā”€ā”€ Response/
│   │   │   ā”œā”€ā”€ ToolResponsable.php         # Tool response interface
│   │   │   ā”œā”€ā”€ TextContentResponse.php     # Text response
│   │   │   ā”œā”€ā”€ ImageContentResponse.php    # Image response
│   │   │   ā”œā”€ā”€ AudioContentResponse.php    # Audio response
│   │   │   └── ResourceContentResponse.php # Resource response
│   │   │
│   │   └── ToolInterface.php          # Interface for tools
│   ā”œā”€ā”€ MCPServer.php                      # Main server class
│   ā”œā”€ā”€ MCPServerBuilder.php               # Builder for server instance creation
│   ā”œā”€ā”€ MCPServerMethodParams.php          # Server method parameters
│
ā”œā”€ā”€ Tool/
│   ā”œā”€ā”€ AbstractTool.php               # Abstract class for tools
│
ā”œā”€ā”€ LICENSE                            # Project license (GPL-3.0)
ā”œā”€ā”€ README.md                          # Project documentation
└── config.php                         # Application configuration
└── index.php                          # Application entry point
└── .gitignore                         # Git ignored files configuration

Core Components

MCPServer

The main server class responsible for processing requests and managing tool and method registration. It uses the facade pattern to simplify complex internal logic.

Registries

  • ToolRegistry - manages the collection of available tools
  • MethodRegistry - manages available API methods

Response Formatters

Implement the strategy pattern, enabling flexible response formatting:

  • JsonResponseFormatter - formats responses as JSON
  • EventStreamResponseFormatter - formats responses as event streams

Response Types

A set of classes representing different types of tool responses:

  • TextContentResponse - text responses
  • ImageContentResponse - responses containing images
  • AudioContentResponse - responses containing audio
  • ResourceContentResponse - responses containing various resources

Usage Example

<?php

use MCP\MCPServer;
use MCP\MCPServerBuilder;
use MCP\Tool\MyCustomTool\Tool as MyCustomTool;

// Creating a server using the builder
$server = (new MCPServerBuilder())
    ->build([new MyCustomTool()]);

// Running the server
$server->handleRequest();

Server run.

php -S localhost:9876 index.php

Creating Custom Tools

To create a new tool:

  1. Create a class implementing the ToolInterface
  2. Register the tool using MCPServerBuilder::addTool()
<?php

namespace MCP\Tool\MyCustomTool;

use MCP\Tool\Response\ToolResponsable;

class Tool extends AbstractTool
{
    public function __construct()
    {
        $this->name = '';
        $this->description = '';
        $this->inputSchema = [];
    }
    
    public function execute(array $params): ToolResponsable
    {
        // Tool logic implementation
        return new TextContentResponse('Tool execution result');
    }
}

License

This project is licensed under the GNU General Public License v3.0 (GPL-3.0). See the LICENSE file for details.

Requirements

  • PHP 8.1 or newer

Contributing

We welcome contributions to this project! Please review our contribution guidelines in the CONTRIBUTING.md file.

Contact

If you have any questions or issues, please create an issue in our GitHub repository.