PHPackages                             he426100/php-mcp-server - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. he426100/php-mcp-server

ActiveProject

he426100/php-mcp-server
=======================

0.0.3(1y ago)21137PHP

Since Apr 10Pushed 1y ago3 watchersCompare

[ Source](https://github.com/he426100/php-mcp-server)[ Packagist](https://packagist.org/packages/he426100/php-mcp-server)[ RSS](/packages/he426100-php-mcp-server/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (6)Used By (0)

PHP MCP Server
==============

[](#php-mcp-server)

[English Version](README.en.md)

这是一个基于 PHP 实现的 MCP (Model Control Protocol) 服务器框架，支持通过注解优雅地定义 MCP 服务。

项目概述
----

[](#项目概述)

本项目提供了一个完整的 MCP 服务器实现，特色功能：

- 基于注解的 MCP 服务定义
- 支持 Tool、Prompt、Resource 三种处理器
- 支持 Stdio、Sse 两种传输方式
- 支持 [Swow](https://github.com/swow/swow) 和 [Swoole](https://github.com/swoole/swoole-src) 两种环境
- 完整的日志系统
- Docker 支持

系统要求
----

[](#系统要求)

- PHP &gt;= 8.1
- Composer
- Swow 扩展 &gt; 1.5 或 Swoole &gt; 5.1
- Docker (可选)

快速开始
----

[](#快速开始)

### 安装

[](#安装)

```
# 1. 克隆项目
git clone https://github.com/he426100/php-mcp-server
cd php-mcp-server

# 2. 安装依赖
composer install

# 3. 可选，安装 Swow 扩展（如果没有）
./vendor/bin/swow-builder --install
```

> 关于 Swow 扩展的详细安装说明,请参考 [Swow 官方文档](https://github.com/swow/swow)

### 运行示例服务器

[](#运行示例服务器)

```
php bin/console mcp:test-server
```

#### 通用命令参数

[](#通用命令参数)

ParameterDescriptionDefault ValueOptions--transportTransport typestdiostdio, sse--portPort to listen on for SSE8000注解使用指南
------

[](#注解使用指南)

本框架提供三种核心注解用于定义 MCP 服务：

### 1. Tool 注解

[](#1-tool-注解)

用于定义工具类处理器：

```
use Mcp\Annotation\Tool;

class MyService {
    #[Tool(
        name: 'calculate-sum',
        description: '计算两个数的和',
        parameters: [
            'num1' => [
                'type' => 'number',
                'description' => '第一个数字',
                'required' => true
            ],
            'num2' => [
                'type' => 'number',
                'description' => '第二个数字',
                'required' => true
            ]
        ]
    )]
    public function sum(int $num1, int $num2): int
    {
        return $num1 + $num2;
    }
}
```

### 2. Prompt 注解

[](#2-prompt-注解)

用于定义提示模板处理器：

```
use Mcp\Annotation\Prompt;

class MyService {
    #[Prompt(
        name: 'greeting',
        description: '生成问候语',
        arguments: [
            'name' => [
                'description' => '要问候的人名',
                'required' => true
            ]
        ]
    )]
    public function greeting(string $name): string
    {
        return "Hello, {$name}!";
    }
}
```

### 3. Resource 注解

[](#3-resource-注解)

用于定义资源处理器：

```
use Mcp\Annotation\Resource;

class MyService {
    #[Resource(
        uri: 'example://greeting',
        name: 'Greeting Text',
        description: '问候语资源',
        mimeType: 'text/plain'
    )]
    public function getGreeting(): string
    {
        return "Hello from MCP server!";
    }
}
```

创建自定义服务
-------

[](#创建自定义服务)

1. 创建服务类：

```
namespace Your\Namespace;

use Mcp\Annotation\Tool;
use Mcp\Annotation\Prompt;
use Mcp\Annotation\Resource;

class CustomService
{
    #[Tool(name: 'custom-tool', description: '自定义工具')]
    public function customTool(): string
    {
        return "Custom tool result";
    }
}
```

2. 创建命令类：

```
namespace Your\Namespace\Command;

use He426100\McpServer\Command\AbstractMcpServerCommand;
use Your\Namespace\CustomService;

class CustomServerCommand extends AbstractMcpServerCommand
{
    protected string $serverName = 'custom-server';
    protected string $serviceClass = CustomService::class;

    protected function configure(): void
    {
        parent::configure();
        $this->setName('custom:server')
            ->setDescription('运行自定义 MCP 服务器');
    }
}
```

注解参数说明
------

[](#注解参数说明)

### Tool 注解参数

[](#tool-注解参数)

参数类型说明必填namestring工具名称是descriptionstring工具描述是parametersarray参数定义否### Prompt 注解参数

[](#prompt-注解参数)

参数类型说明必填namestring提示模板名称是descriptionstring提示模板描述是argumentsarray参数定义否### Resource 注解参数

[](#resource-注解参数)

参数类型说明必填uristring资源URI是namestring资源名称是descriptionstring资源描述是mimeTypestringMIME类型否注解函数返回类型说明
----------

[](#注解函数返回类型说明)

### Tool 注解函数支持的返回类型

[](#tool-注解函数支持的返回类型)

返回类型说明转换结果TextContent/ImageContent/EmbeddedResource直接返回内容对象原样保留TextContent/ImageContent/EmbeddedResource 数组内容对象数组原样保留ResourceContents资源内容对象转换为 EmbeddedResource字符串或标量类型如 string、int、float、bool转换为 TextContentnull空值转换为空字符串的 TextContent数组或对象复杂数据结构转换为 JSON 格式的 TextContent### Prompt 注解函数支持的返回类型

[](#prompt-注解函数支持的返回类型)

返回类型说明转换结果PromptMessage消息对象原样保留PromptMessage 数组消息对象数组原样保留Content 对象TextContent/ImageContent 等转换为用户角色的 PromptMessage字符串或标量类型如 string、int、float、bool转换为带 TextContent 的用户消息null空值转换为空内容的用户消息数组或对象复杂数据结构转换为 JSON 格式的用户消息### Resource 注解函数支持的返回类型

[](#resource-注解函数支持的返回类型)

返回类型说明转换结果TextResourceContents/BlobResourceContents资源内容对象原样保留ResourceContents 数组资源内容对象数组原样保留字符串或可转字符串对象文本内容根据 MIME 类型转换为对应资源内容null空值转换为空的 TextResourceContents数组或对象复杂数据结构转换为 JSON 格式的资源内容注意事项：

- 对于超过 2MB 的大文件内容会自动截断
- 文本类型 (text/\*) 的 MIME 类型会使用 TextResourceContents
- 其他 MIME 类型会使用 BlobResourceContents

日志配置
----

[](#日志配置)

服务器日志默认保存在 `runtime/server_log.txt`，可通过继承 `AbstractMcpServerCommand` 修改：

```
protected string $logFilePath = '/custom/path/to/log.txt';
```

Docker 支持
---------

[](#docker-支持)

构建并运行容器：

```
docker build -t php-mcp-server .
docker run --name=php-mcp-server -p 8000:8000 -itd php-mcp-server mcp:test-server --transport sse
```

sse地址：

通过 CPX 使用
---------

[](#通过-cpx-使用)

您可以通过 [CPX (Composer Package Executor)](https://github.com/imliam/cpx) 直接运行本项目，无需事先安装：

### 前提条件

[](#前提条件)

1. 全局安装 CPX：

```
composer global require cpx/cpx
```

2. 确保 Composer 的全局 bin 目录在您的 PATH 中

### 使用方式

[](#使用方式)

```
# 运行测试服务器
cpx he426100/php-mcp-server mcp:test-server

# 使用 SSE 传输模式
cpx he426100/php-mcp-server mcp:test-server --transport=sse

# 查看可用命令
cpx he426100/php-mcp-server list
```

许可证
---

[](#许可证)

[MIT License](LICENSE)

贡献
--

[](#贡献)

欢迎提交 Issue 和 Pull Request。

作者
--

[](#作者)

[he426100](https://github.com/he426100/)
[logiscape](https://github.com/logiscape/mcp-sdk-php)

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance49

Moderate activity, may be stable

Popularity17

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity31

Early-stage or recently created project

 Bus Factor1

Top contributor holds 66.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~16 days

Total

3

Last Release

372d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f496f5618977a1eeb3f64de28aa1a130e33c0c3f847043177f7496916cc8eecf?d=identicon)[he426100](/maintainers/he426100)

---

Top Contributors

[![he426100](https://avatars.githubusercontent.com/u/9689137?v=4)](https://github.com/he426100 "he426100 (2 commits)")[![webguosai](https://avatars.githubusercontent.com/u/2083784?v=4)](https://github.com/webguosai "webguosai (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/he426100-php-mcp-server/health.svg)

```
[![Health](https://phpackages.com/badges/he426100-php-mcp-server/health.svg)](https://phpackages.com/packages/he426100-php-mcp-server)
```

###  Alternatives

[saasykit/laravel-open-graphy

An awesome open graph image (social cards) generator package for Laravel.

13057.0k](/packages/saasykit-laravel-open-graphy)[crwlr/crawler

Web crawling and scraping library.

37214.8k2](/packages/crwlr-crawler)[vormkracht10/laravel-open-graph-image

Laravel package to generate dynamic Open Graph images

7217.7k](/packages/vormkracht10-laravel-open-graph-image)[backstage/laravel-og-image

Laravel package to generate dynamic Open Graph images

723.2k](/packages/backstage-laravel-og-image)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
