PHP MCP Server Generator

php-mcp-server-generator skill for programming & development

A generator for creating MCP server implementations in PHP with tool definitions, resource handlers, and JSON-RPC communication.

What Is This?

Overview

This skill generates complete MCP server implementations in PHP with server initialization, tool registration, and stdio transport using modern PHP 8.1+ features.

Who Should Use This

Perfect for PHP developers building AI integrations, Laravel teams creating MCP services, and organizations exposing existing PHP functionality to AI assistants.

Why Use It?

Problems It Solves

Building MCP servers in PHP requires understanding JSON-RPC and tool schemas. This skill generates compliant scaffolding so developers focus on tool logic.

Core Highlights

  • Complete Scaffolding - Generates server structure with routing and initialization
  • Typed Tool Schemas - PHP 8.1+ enums and typed properties for parameters
  • Framework Support - Works standalone or integrates with Laravel and Symfony
  • Resource Handlers - File, database, and API resource implementations
  • Composer Ready - Proper package structure with autoloading configuration

How to Use It?

Basic Usage

Ask Claude to generate a PHP MCP server, and this skill creates complete implementations.

Scenario 1: Basic MCP Server

Ask Claude: "Generate a PHP MCP server with a database query tool"

Claude will create:

<?php

declare(strict_types=1);

use MCP\Server\MCPServer;
use MCP\Server\Tool;
use MCP\Server\ToolResult;

class DatabaseQueryServer
{
    private MCPServer $server;
    private PDO $db;

    public function __construct(string $dsn, string $user, string $pass)
    {
        $this->db = new PDO($dsn, $user, $pass);
        $this->server = new MCPServer('database-query', '1.0.0');

        $this->server->addTool(new Tool(
            name: 'query_table',
            description: 'Query a database table with optional filters',
            parameters: [
                'table' => ['type' => 'string', 'description' => 'Table name'],
                'where' => ['type' => 'string', 'description' => 'WHERE clause'],
                'limit' => ['type' => 'integer', 'default' => 50],
            ],
            required: ['table'],
            handler: $this->handleQuery(...)
        ));
    }

    private function handleQuery(array $params): ToolResult
    {
        $table = preg_replace('/[^a-zA-Z0-9_]/', '', $params['table']);
        $sql = "SELECT * FROM {$table}";
        if (isset($params['where'])) {
            $sql .= " WHERE " . $params['where'];
        }
        $sql .= " LIMIT " . ($params['limit'] ?? 50);
        $rows = $this->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
        return ToolResult::json($rows);
    }

    public function run(): void
    {
        $this->server->startStdio();
    }
}

Scenario 2: Laravel Integration

Tell Claude: "Create an MCP server as a Laravel Artisan command"

Claude will generate:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use MCP\Server\MCPServer;
use MCP\Server\Tool;

class MCPServeCommand extends Command
{
    protected $signature = 'mcp:serve';
    protected $description = 'Start the MCP server';

    public function handle(): int
    {
        $server = new MCPServer('laravel-tools', '1.0.0');
        $server->addTool(new Tool(
            name: 'search_users',
            description: 'Search users by name or email',
            parameters: [
                'query' => ['type' => 'string', 'description' => 'Search term'],
            ],
            required: ['query'],
            handler: fn(array $p) => ToolResult::json(
                \App\Models\User::where('name', 'like', "%{$p['query']}%")->limit(20)->get()
            )
        ));
        $server->startStdio();
        return 0;
    }
}

Real-World Examples

Legacy System Integration

A company built an MCP server exposing their PHP inventory system to Claude, querying stock levels through natural language without rewriting existing logic.

WordPress Plugin Tools

A WordPress agency wrapped WP-CLI operations in an MCP server, letting editors manage content through Claude.

Advanced Tips

Error Handling

Map domain exceptions to MCP error codes for clear client-side error handling.

Testing with PHPUnit

Unit test tool handlers independently by mocking database and external services.

When to Use It?

Use Cases

  • Database Tools - Expose database queries as MCP tools for AI assistants
  • Laravel Integration - Add MCP capabilities to Laravel applications
  • Legacy Bridging - Connect existing PHP systems to modern AI workflows
  • CMS Operations - Manage WordPress or Drupal content through AI
  • API Wrapping - Expose existing PHP APIs as MCP tool interfaces

Related Topics

When you ask Claude these questions, this skill will activate:

  • "Create MCP server in PHP"
  • "Generate PHP tool handler for Claude"
  • "Build Laravel MCP server"
  • "PHP MCP resource handler"

Important Notes

Requirements

  • PHP 8.1+ for typed properties, enums, and first-class callables
  • Composer for dependency management and autoloading
  • MCP SDK for PHP package installed via Composer
  • PDO or framework ORM for database tool implementations

Usage Recommendations

Do:

  • Use typed parameters - Leverage PHP 8.1+ types for tool schema accuracy
  • Validate inputs - Sanitize all tool parameters before database operations
  • Handle errors gracefully - Return structured MCP errors, not raw exceptions
  • Test handlers independently - Unit test tool logic separate from transport

Don't:

  • Don't skip input sanitization - SQL injection risks apply to tool parameters
  • Don't block the event loop - Use async patterns for long-running operations
  • Don't expose raw errors - Sanitize error messages returned to AI clients

Limitations

  • PHP's synchronous nature may limit concurrent tool handling
  • Stdio transport requires process management by the MCP client
  • Composer dependency conflicts possible with framework packages
  • Limited community tooling compared to Node.js MCP ecosystem