定制 dtyq/php-mcp 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dtyq/php-mcp

Composer 安装命令:

composer require dtyq/php-mcp

包简介

PHP implementation of MCP server and client

README 文档

README

A complete PHP implementation of the Model Context Protocol (MCP), providing both server and client functionality with support for multiple transport protocols.

CI codecov PHP Version License Latest Version

Language: English | 简体中文

✨ Key Features

  • 🚀 Latest MCP Protocol - Supports MCP 2025-03-26 specification
  • 🔧 Complete Implementation - Tools, resources, and prompts support
  • 🔌 Multiple Transports - STDIO ✅, HTTP ✅, Streamable HTTP 🚧
  • 🌐 Framework Compatible - Works with any PSR-compliant framework, built-in Hyperf integration
  • 📚 Well Documented - Comprehensive guides in English and Chinese

🚀 Quick Start

Installation

composer require dtyq/php-mcp

Hyperf Framework Quick Integration

If you're using Hyperf framework, integration is extremely simple:

// Just one line of code!
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
    return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handle('default');
});

Annotation-Based Registration:

class CalculatorService
{
    #[McpTool(description: 'Mathematical calculations')]
    public function calculate(string $operation, int $a, int $b): array
    {
        return ['result' => match($operation) {
            'add' => $a + $b,
            'multiply' => $a * $b,
            default => 0
        }];
    }
    
    #[McpResource(description: 'System information')]
    public function systemInfo(): TextResourceContents
    {
        return new TextResourceContents('mcp://system/info', 
            json_encode(['php' => PHP_VERSION]), 'application/json');
    }
}

Advanced Options:

  • 🔐 AuthenticatorInterface - Custom authentication
  • 📊 HttpTransportAuthenticatedEvent - Dynamic tool/resource registration
  • 📝 Annotation System - Auto-register tools, resources and prompts

👉 View Complete Hyperf Integration Guide

Basic Server Example

<?php
require_once 'vendor/autoload.php';

use Dtyq\PhpMcp\Server\McpServer;
use Dtyq\PhpMcp\Shared\Kernel\Application;

// Create server with simple container
$container = /* your PSR-11 container */;
$app = new Application($container, ['sdk_name' => 'my-server']);
$server = new McpServer('my-server', '1.0.0', $app);

// Add a tool
$server->registerTool(
    new \Dtyq\PhpMcp\Types\Tools\Tool('echo', [
        'type' => 'object',
        'properties' => ['message' => ['type' => 'string']],
        'required' => ['message']
    ], 'Echo a message'),
    function(array $args): array {
        return ['response' => $args['message']];
    }
);

// Start server
$server->stdio(); // or $server->http($request)

Basic Client Example

<?php
use Dtyq\PhpMcp\Client\McpClient;
use Dtyq\PhpMcp\Client\Configuration\StdioConfig;

$client = new McpClient('my-client', '1.0.0', $app);

// ✅ Recommended: Use shortcut methods with typed configuration
$config = new StdioConfig('php server.php');
$session = $client->stdio($config);
$session->initialize();

// Call a tool
$result = $session->callTool('echo', ['message' => 'Hello, MCP!']);
echo $result->getContent()[0]->getText();

Alternative Methods:

// HTTP shortcut method
use Dtyq\PhpMcp\Client\Configuration\HttpConfig;
$httpConfig = new HttpConfig('http://localhost:8080/mcp');
$session = $client->http($httpConfig);

// ⚠️ Legacy method (deprecated)
$session = $client->connect('stdio', ['command' => 'php server.php']);

📖 Documentation

Working Examples

Check the /examples directory:

  • stdio-server-test.php - Complete STDIO server
  • http-server-test.php - HTTP server with tools
  • stdio-client-test.php - STDIO client example
  • http-client-test.php - HTTP client example

🌟 Transport Protocols

Protocol Status Description
STDIO Process communication
HTTP JSON-RPC over HTTP
Streamable HTTP 🚧 HTTP + Server-Sent Events

🛠️ Requirements

  • PHP: 7.4+ (8.0+ recommended)
  • Extensions: json, mbstring, openssl, pcntl, curl
  • Composer: For dependency management

🤝 Contributing

We welcome contributions! Please see our issues page for areas where you can help.

git clone https://github.com/dtyq/php-mcp.git
cd php-mcp
composer install
composer test

📄 License

MIT License - see LICENSE file for details.

🙏 Acknowledgments

⭐ Star this repository if you find it useful!

dtyq/php-mcp 适用场景与选型建议

dtyq/php-mcp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.44k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2025 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 dtyq/php-mcp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dtyq/php-mcp 我们能提供哪些服务?
定制开发 / 二次开发

基于 dtyq/php-mcp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 28
  • 点击次数: 9
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 28
  • Watchers: 1
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-03