跳到正文
Skillpedia
API 与服务器(后端)40k · #7323

php-mcp-server-generator

它能做什么

这个生成器可以创建一个完整的 PHP 服务器项目,用于与 AI 通信(使用 MCP 协议)。项目中包含服务器运行所需的工具、资源和测试。

适用场景

  • ✓当你想创建一个与 AI 交互的 PHP 服务器时。
  • ✓当你需要具备特定功能的服务器时,比如读取文件或执行计算。
  • ✓如果你已经有一个 PHP 项目,并想为它添加与 AI 通信的能力。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/php-mcp-server-generator/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/php-mcp-server-generator/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“生成一个名为 'my-file-server' 的 PHP MCP 服务器,描述为“用于管理文件的服务器”,使用 http 传输方式,包含 'file read' 和 'file write' 两个工具,并加入 resources 和 prompts。使用 PHP 8.2。”

每周 5 个新技能,1 分钟读完。

工作原理

你需要提供项目名称、描述、通信方式(stdio 或 http)、需要的工具(比如读写文件)、是否需要额外的资源,以及要使用的 PHP 版本。随后 AI 会自动搭建整个所需的文件夹和文件结构。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

PHP MCP Server Generator

You are a PHP MCP server generator. Create a complete, production-ready PHP MCP server project using the official PHP SDK.

Project Requirements

Ask the user for:

  1. Project name (e.g., "my-mcp-server")
  2. Server description (e.g., "A file management MCP server")
  3. Transport type (stdio, http, or both)
  4. Tools to include (e.g., "file read", "file write", "list directory")
  5. Whether to include resources and prompts
  6. PHP version (8.2+ required)

Project Structure

{project-name}/
├── composer.json
├── .gitignore
├── README.md
├── server.php
├── src/
│   ├── Tools/
│   │   └── {ToolClass}.php
│   ├── Resources/
│   │   └── {ResourceClass}.php
│   ├── Prompts/
│   │   └── {PromptClass}.php
│   └── Providers/
│       └── {CompletionProvider}.php
└── tests/
    └── ToolsTest.php

File Templates

composer.json

{
    "name": "your-org/{project-name}",
    "description": "{Server description}",
    "type": "project",
    "require": {
        "php": "^8.2",
        "mcp/sdk": "^0.1"
    },
    "require-dev": {
        "phpunit/phpunit": "^10.0",
        "symfony/cache": "^6.4"
    },
    "autoload": {
        "psr-4": {
            "App\\\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\\\": "tests/"
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    }
}

.gitignore

/vendor
/cache
composer.lock
.phpunit.cache
phpstan.neon

README.md

# {Project Name}

{Server description}

## Requirements

- PHP 8.2 or higher
- Composer

## Installation

```bash
composer install

Usage

Start Server (Stdio)

php server.php

Configure in Claude Desktop

{
  "mcpServers": {
    "{project-name}": {
      "command": "php",
      "args": ["/absolute/path/to/server.php"]
    }
  }
}

Testing

vendor/bin/phpunit

Tools

  • {tool_name}: {Tool description}

Development

Test with MCP Inspector:

npx @modelcontextprotocol/inspector php server.php

### server.php

```php
#!/usr/bin/env php
<?php

declare(strict_types=1);

require_once __DIR__ . '/vendor/autoload.php';

use Mcp\Server;
use Mcp\Server\Transport\StdioTransport;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;

// Setup cache for discovery
$cache = new Psr16Cache(new FilesystemAdapter('mcp-discovery', 3600, __DIR__ . '/cache'));

// Build server with discovery
$server = Server::builder()
    ->setServerInfo('{Project Name}', '1.0.0')
    ->setDiscovery(
        basePath: __DIR__,
        scanDirs: ['src'],
        excludeDirs: ['vendor', 'tests', 'cache'],
        cache: $cache
    )
    ->build();

// Run with stdio transport
$transport = new StdioTransport();

$server->run($transport);

src/Tools/ExampleTool.php

<?php

declare(strict_types=1);

namespace App\Tools;

use Mcp\Capability\Attribute\McpTool;
use Mcp\Capability\Attribute\Schema;

class ExampleTool
{
    /**
     * Performs a greeting with the provided name.
     * 
     * @param string $name The name to greet
     * @return string A greeting message
     */
    #[McpTool]
    public function greet(string $name): string
    {
        return "Hello, {$name}!";
    }
    
    /**
     * Performs arithmetic calculations.
     */
    #[McpTool(name: 'calculate')]
    public function performCalculation(
        float $a,
        float $b,
        #[Schema(pattern: '^(add|subtract|multiply|divide)$')]
        string $operation
    ): float {
        return match($operation) {
            'add' => $a + $b,
            'subtract' => $a - $b,
            'multiply' => $a * $b,
            'divide' => $b != 0 ? $a / $b : 
                throw new \InvalidArgumentException('Division by zero'),
            default => throw new \InvalidArgumentException('Invalid operation')
        };
    }
}

src/Resources/ConfigResource.php

<?php

declare(strict_types=1);

namespace App\Resources;

use Mcp\Capability\Attribute\McpResource;

class ConfigResource
{
    /**
     * Provides application configuration.
     */
    #[McpResource(
        uri: 'config://app/settings',
        name: 'app_config',
        mimeType: 'application/json'
    )]
    public function getConfiguration(): array
    {
        return [
            'version' => '1.0.0',

…
常见问题

什么是 MCP 协议?

MCP(Model Context Protocol,模型上下文协议)是一种标准,让计算机程序(服务器)能够以有条理的方式与人工智能通信。

这里的 'tools'、'resources' 和 'prompts' 分别是什么?

Tools 是服务器可以执行的操作(例如计算某些内容)。Resources 是服务器可以提供的数据(例如配置信息)。Prompts 是 AI 与服务器交互时可以使用的提问模板。

使用这个生成器需要懂 PHP 编程吗?

需要。这个生成器创建的是 PHP 项目。虽然创建过程是自动完成的,但了解 PHP 有助于你定制和使用生成的服务器。

详情

作者的原始描述

Generate a complete PHP Model Context Protocol server project with tools, resources, prompts, and tests using the official PHP SDK

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7323
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能