rasuvaeff / yii3-mcp
MCP server for Yii3 on the official mcp/sdk: attribute tools, OpenAPI bridge, per-user RBAC and audit log for everything the model touches
Requires
- php: 8.3 - 8.6
- mcp/sdk: ~0.10.0
- psr/cache: ^2.0 || ^3.0
- psr/container: ^2.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.1
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/simple-cache: ^2.0 || ^3.0
- rasuvaeff/mcp: ^0.1.2
- rasuvaeff/mcp-openapi: ^0.1.3
- symfony/console: ^7.0 || ^8.0
- symfony/uid: ^7.0 || ^8.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.51
- firebase/php-jwt: ^7.0
- friendsofphp/php-cs-fixer: ^3.95
- infection/infection: ^0.35
- maglnet/composer-require-checker: ^4.17
- nyholm/psr7: ^1.8
- rasuvaeff/property-testing-testo: ^1.0
- rasuvaeff/rector-named-literals: ^1.0
- rasuvaeff/understudy-testo: ^1.0
- rector/rector: ^2.4
- roave/backward-compatibility-check: ^8.0
- testo/bridge-infection: ^0.1.6
- testo/testo: ^0.10.25
- vimeo/psalm: ^7.0
- yiisoft/di: ^1.4
- yiisoft/test-support: ^3.0
Suggests
- firebase/php-jwt: JWT access-token validation for the OAuth resource server (BearerTokenMiddleware)
- guzzlehttp/guzzle: PSR-18 HTTP client for the OpenAPI bridge executor and spec loader
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-25 13:25:38 UTC
README
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
SharedSecretMiddlewareorOAuth\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 leakedMcp-Session-Idalone does not let another authenticated client act in — or destroy — a foreign session. Session files are owner-only (0700dir,0600files) 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 +$refdepth/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-triprequestStateis 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.