Search by

rasuvaeff / yii3-mcp

rasuvaeff

MCP server for Yii3 on the official mcp/sdk: attribute tools, OpenAPI bridge, per-user RBAC and audit log for everything the model touches

Package info

github.com/rasuvaeff/yii3-mcp

pkg:composer/rasuvaeff/yii3-mcp

Statistics

Installs: 2 565

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v6.1.1 2026-11-25 13:24 UTC

README

Stable Version Total Downloads Build Static analysis Psalm level PHP License Docs

Русская версия

Documentation — the full guide, API reference and cookbook live in rasuvaeff/mcp.

Model Context Protocol server integration for Yii3 over the official mcp/sdk (PHP Foundation + Symfony): expose your application's domain operations as MCP tools/resources for AI agents (Claude Code, Claude Desktop, …) through a PSR-15 Streamable HTTP endpoint, with tools resolved through the Yii3 DI container.

The runtime itself lives in rasuvaeff/mcp and knows nothing about Yii; the OpenAPI bridge is in rasuvaeff/mcp-openapi. This package adds the Yii3 side: config-plugin wiring, params and mcp:doctor. Upgrading from 5.x: UPGRADE.md.

Using an AI coding assistant? llms.txt contains a compact API reference you can share with the model. Contributors: see AGENTS.md. Projects using the llm/skills Composer plugin also get this package's agent skill synced into .agents/skills/ automatically on install.

Requirements

Requirement Version
PHP 8.3 – 8.6
mcp/sdk ~0.10.0 (experimental until 1.0 — hence the tilde pin)
rasuvaeff/mcp, rasuvaeff/mcp-openapi ^0.1 (installed automatically)
firebase/php-jwt optional, ^7.0: JWT validation for the OAuth resource server
MCP protocol 2024-11-05 … 2025-11-25 over initialize (negotiated), 2026-07-28 stateless
ext-fileinfo optional: MIME detection of file content

Installation

composer require rasuvaeff/yii3-mcp

Usage

1. Declare a tool

Tools are ordinary Yii3 services. Capability methods are annotated with the SDK's own attributes — this package invents no protocol structures:

use Mcp\Server\Capability\Attribute\McpTool;

final readonly class OrderTools
{
    public function __construct(private OrderRepository $orders) {}

    /**
     * Returns the current status of an order.
     */
    #[McpTool(name: 'order.status')]
    public function status(string $orderId): string
    {
        return $this->orders->get($orderId)->status->value;
    }
}

Input schemas are generated by the SDK from the method signature and DocBlock. #[McpResource], #[McpResourceTemplate] and #[McpPrompt] methods work the same way — all four SDK capability attributes are recognized.

2. Register it

// config/params.php
return [
    'rasuvaeff/yii3-mcp' => [
        'server_name' => 'my-app',
        'server_version' => '1.0.0',
        'tools' => [OrderTools::class],
        'endpoint_secret' => getenv('MCP_SECRET'),
    ],
];

Handlers are registered as [class, method] references — the SDK resolves the instance through the Yii3 container on call, so constructor dependencies are injected the normal way.

3. Route the endpoint

// config/routes.php
Route::methods(['POST', 'GET', 'DELETE', 'OPTIONS'], '/mcp')
    ->middleware(SharedSecretMiddleware::class)
    ->action(McpAction::class),

An MCP client connects with the secret header:

{
    "mcpServers": {
        "my-app": {
            "type": "http",
            "url": "https://example.com/mcp",
            "headers": { "X-Mcp-Secret": "..." }
        }
    }
}

Clients that authenticate with OAuth 2.1 instead route BearerTokenMiddleware in front of the action — see OAuth 2.1 resource server.

stdio for local development

./yii mcp:serve

Claude Code config: claude mcp add my-app -- ./yii mcp:serve.

The mcp:serve, mcp:list and mcp:doctor commands are registered through the package's yiisoft/yii-console params, so they show up in ./yii list right after composer require — no application-side configuration. An application that maintains its own commands map merges with it recursively (the application layer wins on a name conflict, so a command can be renamed or rebound by re-declaring its name); an application not using yiisoft/config can still list the three classes in its console params the way previous versions documented it.

Check what is served

./yii mcp:list             # every tool, resource and prompt, without a client
./yii mcp:doctor           # configuration health check with stable exit codes

mcp:doctor is McpDoctorCommand over Doctor\McpDoctor, which returns a DoctorReport of CheckResults (CheckStatus, CheckCategory). See Operations.

Configuration

Everything is under params['rasuvaeff/yii3-mcp']; config/params.php documents each key. Yii3: configuration explains how the server is assembled.

Params Guide
server_name, server_version, instructions, pagination_limit, protocol_version, result_json server identity and server-wide knobs
tools, configurators tool classes and your own configurators
endpoint_secret, client_secrets, secret_header, external_auth shared secrets, several clients, rotation
oauth OAuth 2.1 resource server
allowed_hosts, expected_http_host, session hosts and session storage
modern_era, header_validation, cache_policy, request_state stateless era
notifications resource update notifications
interceptors, prompt_interceptors, resource_interceptors, result_decorators interceptor chains and decorators
tool_call_budget, limits, cache call budget, size limit, result cache
tool_visibility, prompt_visibility, resource_visibility, visibility what a session may see
openapi bridging an existing REST API
apps MCP Apps

Bridges

Audit log, RBAC and telemetry are ordinary interceptors: see Bridges. For tenants see Multi-tenant serving, for tests Testing your tools.

Security

  • The endpoint is trusted-only. MCP tools execute application code; treat the endpoint like an admin API. Ship it behind SharedSecretMiddleware or OAuth\BearerTokenMiddleware (an empty secret rejects every request with an explanatory 503) or an explicit network ACL.
  • Tool errors are returned as MCP error envelopes by the SDK — internals are not leaked as 500 traces.
  • The core registers no tools by default; every exposed operation is an explicit entry in params['rasuvaeff/yii3-mcp']['tools'].
  • Sessions are bound to the client that created them (immutable owner stamped at initialize, verified before every POST/DELETE, and the SDK's own principal binding behind it): a leaked Mcp-Session-Id alone does not let another authenticated client act in — or destroy — a foreign session. Session files are owner-only (0700 dir, 0600 files) in an application-specific directory.
  • Capability names are unique across the whole server, enforced. A collision between any registration paths (attribute tools, configurators, the OpenAPI bridge) fails the build with DuplicateCapabilityException — never the SDK's silent last-write-wins, which would leave visibility/cache/RBAC/audit rules describing a vanished handler.
  • All caller-influenced output is size-bounded before allocation: upstream response bodies (openapi.max_response_bytes, incremental read), spec documents (10 MiB + $ref depth/node budget) and tool results (limits.tool_result_bytes).
  • OAuth 2.1 through OAuth\BearerTokenMiddleware (the SDK's resource server pieces): token, audience, issuer and scopes are checked before the transport runs; see OAuth 2.1 resource server.
  • Nested tool arguments are masked at every depth, objects included (ArgumentMasker), and a multi round-trip requestState is bound to its request and client.

Examples

yii3-wiring.php builds the package config with a real yiisoft/di container. Runtime and bridge scripts are in rasuvaeff/mcp (runtime) and rasuvaeff/mcp-openapi (OpenAPI bridge); see examples/.

Roadmap

Planned direction (tool-call interceptors, AI audit trail, session budgets, tenant-scoped serving, per-session tool visibility): see ROADMAP.md.

Development

No PHP/Composer on the host — run in Docker via the composer:2 image:

docker run --rm -v "$PWD":/app -w /app composer:2 composer build

Or with Make: make build, make cs-fix, make psalm, make test.

License

BSD-3-Clause. See LICENSE.md.