The Model Context Protocol is how an AI client — a desktop assistant, an editor, an agent — asks an application to do something. It offers three kinds of thing:
| Kind | Is | Example |
|---|---|---|
| Tool | something the client can call | “void invoice INV-42” |
| Resource | something the client can read, addressed by a URI | invoice://INV-42 |
| Prompt | a ready-made instruction the client can fill in | “draft a reminder for…” |
Your modules declare them, the same way they declare routes and commands:
$module->mcp(static function (McpCollector $mcp): void {
$mcp->tool('invoice.void', VoidInvoice::class, 'Void an unpaid invoice.', permission: 'invoice.void');
});
MCP is an interface to what your modules already do. It holds no business logic and exposes nothing a module has not named: there is no generic query tool, no filesystem browser, no shell, and no way to reach a class by name.
The framework serves it over STDIO for a local client and over HTTP for a remote one.
Module-facing classes are Stable; the server, registry and transports
are Internal. See STABILITY.md.
module.php ──$module->mcp()──► McpRegistry (name → handler class, owning module, permission)
stdin/stdout ─► StdioTransport ┐
POST /mcp ────► HttpTransport ─┴► McpServer ─► ToolRunner / ResourceReader / PromptProvider
│ authorize → mcp.tool.input → validate
│ → mcp.tool.before → your class → output filter
└► your module's service, which does the work
initialize, ping, tools/*,
resources/* and prompts/*.One module offering one read-only tool, one resource and one prompt.
// modules/Invoices/module.php
return static function (ModuleContext $module): void {
$module->name('Invoices');
$module->access(static function (AccessCollector $access): void {
$access->capability('invoice.read', 'See invoices.');
$access->role('accountant', ['invoice.read']);
});
$module->mcp(static function (McpCollector $mcp): void {
$mcp->tool('invoice.find', FindInvoice::class, 'Find an invoice by its number.', permission: 'invoice.read')
->resource('invoice://{number}', InvoiceResource::class, 'One invoice, as JSON.', permission: 'invoice.read')
->prompt('invoice.chase', ChaseLetter::class, 'Draft a reminder for an overdue invoice.');
});
};
final class FindInvoice implements Tool
{
public function __construct(private readonly InvoiceService $invoices) {}
public function inputSchema(): array
{
return [
'type' => 'object',
'properties' => ['number' => ['type' => 'string', 'pattern' => '^INV-[0-9]{1,10}$']],
'required' => ['number'],
];
}
public function call(array $arguments, McpContext $context): ToolResult
{
$invoice = $this->invoices->find($arguments['number'], $context->identity);
return $invoice === null
? ToolResult::error('No invoice has that number.')
: ToolResult::structured(['number' => $invoice->number, 'total' => $invoice->total, 'status' => $invoice->status]);
}
}
Working code to copy: tests/Fixtures/Modules/McpDemo/Plugins/McpDemo/ is a
module with one of each kind, plus a hook and a filter, and
tests/Feature/McpDemoModuleTest drives it over both transports.
tests/Fixtures/Modules/System/Plugins/Server/ shows
system operations as MCP tools.
Declare them inside $module->mcp(), while module.php loads. A capability
cannot be added later.
| Kind | Declared as | Name must be |
|---|---|---|
| Tool | ->tool(name, Handler::class, description, permission: ?) |
a-z, digits, ., _, -; up to 128 characters, starting with a letter |
| Resource | ->resource(uri, Handler::class, description, permission: ?) |
an absolute URI such as status://app, or a template such as invoice://{number} |
| Prompt | ->prompt(name, Handler::class, description, permission: ?) |
as for a tool |
php laika mcp:list [--module=Invoices] shows every capability, its
module, what a caller needs, and how each transport is configured.A permission is an ordinary auth capability, declared by the module that checks it and granted through roles.
mcp:list
shows it as “any user”.mcp.allow_guests is true — and even
then, a guest can use only capabilities that name no permission.$context->identity through to your
service, as FindInvoice does.Tool has two methods: inputSchema(): array and
call(array $arguments, McpContext $context): ToolResult.
ToolResult::text('Invoice INV-42 was voided.');
ToolResult::structured(['number' => 'INV-42', 'status' => 'void'], 'Voided INV-42.');
ToolResult::error('INV-42 is already paid and cannot be voided.');
structured() takes plain data only: scalars, null, and arrays of them.
A model or any other object is refused, because what an object serialises to
is whatever its properties happen to be. Build the array you mean to send.error() is for a failure worth telling the model about, which it may work
around. Throw an McpException (such as ToolException) only to refuse in
protocol terms.7.0 becomes 7.A strict subset of JSON Schema, and it fails closed: a keyword it does not support is an error when the tool is used, not something ignored.
| Supported | |
|---|---|
type |
object, string, integer, number, boolean, array, null |
| objects | properties, required, additionalProperties (boolean) |
| arrays | items, minItems, maxItems |
| values | enum, const, minLength, maxLength, pattern (PCRE, UTF-8), minimum, maximum |
| annotations | default, description, title |
A property you did not list is refused unless additionalProperties is
true, and a tool whose schema is just {"type": "object"} accepts no arguments
at all. Arguments are limited to 256 KiB and 32 levels of nesting. A validation
failure answers InvalidParams with up to ten {path, message} problems, and
never echoes the offending value.
For an operation that changes something important, ask the model to confirm in the call itself:
'confirm' => ['type' => 'boolean', 'const' => true]
That is not security — the permission is — but a client that shows tool calls shows the confirmation.
final class InvoiceResource implements Resource
{
public function __construct(private readonly InvoiceService $invoices) {}
public function read(string $uri, array $parameters, McpContext $context): ResourceContents
{
$invoice = $this->invoices->find($parameters['number'], $context->identity)
?? throw ResourceException::notFound($uri);
return ResourceContents::json($uri, ['number' => $invoice->number, 'total' => $invoice->total]);
}
}
Three ways to answer: ResourceContents::text($uri, $text, $mimeType),
::json($uri, $data) and ::blob($uri, $bytes, $mimeType).
A URI without placeholders is listed by resources/list; one with placeholders
is listed by resources/templates/list.
A placeholder matches one path segment of printable text. Before your class
runs it is URL-decoded, then refused if it contains a / or a control
character. So invoice://..%2F..%2Fetc never reaches you — but it is still the
client’s text: look it up, never use it as a path.
final class ChaseLetter implements Prompt
{
public function arguments(): array
{
return [new PromptArgument('number', 'The invoice number.', required: true)];
}
public function get(array $arguments, McpContext $context): PromptResult
{
return new PromptResult([
PromptMessage::user(\sprintf('Draft a polite reminder for invoice %s.', $arguments['number'])),
], 'Reminder for an overdue invoice');
}
}
Arguments are strings. A missing required argument, or one the prompt does not
declare, is refused before get() runs.
config/mcp.php returns the mcp block. Every key is optional:
return [
'enabled' => true,
'server' => ['name' => 'Acme ERP', 'version' => '4.2.0'],
'transports' => ['stdio' => true, 'http' => true],
'http' => ['path' => '/mcp'],
'allow_guests' => false,
];
| Key | Default | What it does |
|---|---|---|
enabled |
true |
The switch for all of MCP. Off, mcp:stdio refuses to start and the HTTP path returns 404 |
server.name, server.version |
lphp, the framework version |
What initialize reports to a client. One line, 1–100 characters |
transports.stdio |
true |
Whether mcp:stdio will serve. On its own this opens nothing |
transports.http |
false |
Whether the HTTP endpoint exists |
http.path |
/mcp |
The endpoint’s exact path. Surrounding slashes are ignored. A query, fragment, .., percent-escape or bare / is refused |
allow_guests |
false |
Whether a caller with no credentials is served as a guest, or refused |
log.enabled |
true |
Whether the mcp log channel records MCP traffic — see Logging |
Every value is checked when MCP is first used, and a wrong one raises a
ConfigurationException naming the key. The HTTP endpoint needs its settings on
every request, so an invalid mcp block fails every request until it is fixed,
just as a malformed security key does.
php laika mcp:stdio --user=ada
The MCP client starts this command and talks to it over the pipe. Most clients take the command as JSON:
{ "mcpServers": { "acme": { "command": "php", "args": ["/srv/app/laika", "mcp:stdio", "--user=ada"] } } }
--user, the session is a guest.
Whoever can run the console chooses the user — the same trust as running any
other command.Once mcp.transports.http is on, the endpoint takes one JSON-RPC message per
POST to mcp.http.path.
It is not a route. The kernel answers it before routing, as it does for
assets, so route meta, CSRF and the route guard do not apply. The request size
limit, the security headers and response.instance still run.
TokenProvider, as for REST. A request with no valid token gets
401 with WWW-Authenticate: Bearer realm="mcp". A token that is sent and
rejected is never downgraded to a guest, and a token in the query string is
ignored.Origin header names another
scheme, host or port gets 403, which blocks DNS rebinding. Clients that are
not browsers send no Origin and are unaffected.Mcp-Session-Id and no event stream. Each request
stands alone, at the version in MCP-Protocol-Version (2025-03-26 when the
header is absent). initialize is still answered.| Status | When |
|---|---|
| 200 | A request was answered. This includes a JSON-RPC error about the request, such as an unknown tool |
| 202 | A notification: accepted, with no body |
| 400 | Unreadable JSON, an invalid message or batch, a message over 1 MiB, or an unsupported protocol version |
| 401 | No valid bearer token, and guests are not allowed |
| 403 | The Origin header names another site |
| 405 | Any method but POST (Allow: POST) |
| 406 | Accept excludes application/json |
| 415 | The body is not application/json |
Every refusal still carries a JSON-RPC error, with a null id.
Apache does not pass the Authorization header to PHP on its own.
public/.htaccess has the rule that does, so keep it if you replace that file.
MCP traffic goes to the mcp log channel, so you can route it somewhere of its
own by channel name. Every record carries the MCP request id, transport, client,
principal (a user id, or null for a guest), capability, kind and owning module.
| Level | Record |
|---|---|
| warning | Access denied: authorization refused a capability. The client was told it does not exist |
| warning | A capability failed. The record holds the exception class only; the exception itself goes to the error log |
| notice | A capability refused in protocol terms |
| info | A tool called (with ms), a resource read (with its URI), a prompt loaded, a request that failed (with the JSON-RPC code) |
| debug | Every request, with its method |
Arguments, results, resource contents and prompt text are never logged,
because that is where a customer’s data or a secret would be. The logging
layer’s own redaction (logging.redact) still applies to what is logged.
Set mcp.log.enabled to false to stop these records; the hooks keep firing
for any other listener.
The mcp.* hooks and filters are listed in the
lifecycle table. Register
them from onBoot, as for any other hook:
$module->onBoot(static function (HookEngine $hooks): void {
$hooks->add('mcp.tool.after', static function (Capability $tool, ToolResult $result, McpContext $context): void {
// count, audit, notify
}, 10, 'Invoices');
});
They never run ahead of the security checks. A capability the caller may not use
reaches no listener. mcp.tool.input runs before schema validation, so a
filter cannot add input the schema forbids. An output filter must return the
same kind of value it was given, or the call fails and is reported. A listener
can refuse a call by throwing an MCP exception, but it cannot grant access to
anything.
These hold for every capability, and each has a test
(tests/Feature/McpSecurityTest and the architecture tests):
Class::method strings,
function names and near misses are all “Unknown tool.”, and nothing is
attempted.engine/MCP imports no database,
model or system code, and calls no file or process function. What a tool can
do is exactly what your service does.app.debug on.What remains yours: choose permissions, check the specific record in your
service, give tools names that say what they do, and prefer read-only tools. A
tool that changes something deserves a confirm argument and a narrow
permission.
There is no manifest to build. Capabilities are declared in module.php,
which runs on every boot anyway, as routes are. The module discovery cache that
cache:warm writes already removes the filesystem scan, and registering a
hundred capabilities takes about 0.15 ms (composer bench). What mcp:list
shows is exactly what is served.
An MCP call over HTTP costs less than an ordinary JSON request, and a page request with MCP enabled builds nothing of MCP.
Call the server the application built, as a client would:
$app = $this->application(['modules' => ['paths' => ['modules']]])->boot();
$session = new McpSession(new Identity('7', 'ada', ['accountant']), 'stdio');
$session->initialize('2025-06-18', 'test', '1');
$answer = $app->container()->get(McpServer::class)->handle(
'{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"invoice.find","arguments":{"number":"INV-42"}}}',
$session,
);
Over HTTP, switch the transport on in the test’s configuration and handle() a
POST /mcp with a bearer token, as tests/Feature/McpHttpSliceTest does.
Test the refusals as well as the answers: a caller without the permission, a guest, and input the schema refuses.
| Symptom | Cause |
|---|---|
| The client sees no tools | The user lacks every permission, or is a guest. mcp:list shows what each tool needs |
| “Unknown tool.” for a tool that exists | The caller lacks its permission, or its module is disabled. Look for a denied warning in the mcp log |
POST /mcp is 404 |
mcp.transports.http is off, or mcp.enabled is false, or the path differs from mcp.http.path |
| Every request is 401 | No token was sent, or Apache stripped the Authorization header, or the application’s user provider does not implement TokenProvider |
| 403 from a browser-based client | Its Origin is not this server’s scheme, host and port |
| The STDIO client says it received invalid JSON | Something wrote to stdout outside a handler, such as a module.php that echoes. Handler output is caught and reported on stderr |
| “The tool failed.” | The handler threw. The exception is in the error log, and the mcp log’s warning at the same moment names the tool and the exception class |
A ConfigurationException on every request |
The mcp block is invalid; the message names the key |