Expose Your Laravel App to AI with Model Context Protocol
Learn how to build a self-describing MCP server in Laravel, allowing AI assistants to query your database securely.
For years, the formula for exposing application data has been set in stone: design a REST API, write OpenAPI specs, and hope the developer on the other end reads the documentation. But as LLMs and AI agents increasingly become the primary consumers of our software, that developer-centric model is showing its age. AI models do not want to read a static PDF of API endpoints; they need to discover capabilities dynamically at runtime.
This is where the Model Context Protocol (MCP) comes in. Originally introduced to let assistants like Claude interact with local and remote services, MCP turns the traditional API model on its head. Instead of hard-coding endpoints, you build a server that advertises what it can do, and the AI client figures out how to use it.
Let's look at how to bring this paradigm to the PHP ecosystem by building a Laravel-backed MCP server that allows an AI assistant to safely query real application data.
The Paradigm Shift: REST vs. MCP
To understand why we need a new protocol, it helps to contrast it with what we already know. A traditional REST API is built for human developers. You write the code, publish the docs, and the client hard-codes calls to specific endpoints like GET /orders?status=Shipped.
An MCP server is built for an AI model at runtime. The model connects and asks, "What can you do?" The server responds with a self-describing schema of its available actions and data sources.
| Feature | REST API | MCP Server |
|---|---|---|
| Primary Consumer | A developer writing code | An AI model at runtime |
| Discovery | Reading external documentation | Self-advertising tools and schemas |
| Coupling | Client hard-codes specific endpoints | Client dynamically adapts to what's available |
| Primitives | Endpoints and HTTP verbs | Tools, Resources, and Prompts |
| Transport | Custom HTTP/auth architectures | Standardized (stdio or streamable HTTP) |
Think of a REST API as a printed menu. An MCP server is more like a waiter: it knows what's in the kitchen, tells you the specials, and handles the order based on what you ask for.
The Three Primitives of MCP
An MCP server exposes three core capabilities to an AI client:
- Tools: These are the actionable, executable routines. When the model decides it needs to perform an action (like searching for a customer or running a report), it calls a Tool.
- Resources: These are read-only data sources that provide context. For example, a resource might expose your database schema so the model understands the underlying tables and columns before it attempts to query them.
- Prompts: These are pre-defined templates or workflows that help seed the model's reasoning process, guiding it through complex tasks.
Anatomy of a Laravel MCP Server
Because MCP is a standardized protocol, you don't need to write custom integration code for every AI client. In a Laravel application, you can define an MCP server class that registers these primitives while keeping full access to your existing Eloquent models, validation rules, and database configurations.
Here is what the structural skeleton of a Laravel MCP server looks like:
use Laravel\Mcp\Server;
use Laravel\Mcp\Server\Attributes\Instructions;
#[Instructions(<<<'TXT'
This server exposes a read-only sales dataset for ad-hoc querying.
Read the "database-schema" resource first to learn the tables, then answer with the curated tools, falling back to a read-only SQL query only when no curated tool fits.
TXT)]
class AssistantServer extends Server
{
protected array $tools = [
ListOrdersTool::class,
SearchCustomersTool::class,
RevenueReportTool::class,
RunAssistantQueryTool::class,
];
protected array $resources = [
DatabaseSchemaResource::class
];
protected array $prompts = [
RevenueInsightsPrompt::class
];
}
The most striking difference here compared to a standard controller is the #[Instructions] attribute. This block of text is sent directly to the LLM upon connection. It acts as system-level guidance, instructing the model on how to behave—for instance, telling it to prioritize curated tools like RevenueReportTool and only fall back to raw SQL queries as a last resort.
Each registered tool (like ListOrdersTool) is a class containing a handle() method. Because this runs directly inside your Laravel application, you retain all your standard framework features. You can use Eloquent to query your database, leverage Laravel's built-in validation to sanitize inputs, and respect your existing configuration files.
The Trade-offs of Going Agent-Native
While MCP is incredibly powerful for AI-driven workflows, it is not a silver bullet for every integration.
First, it is not designed for traditional frontends. If you are building a single-page application (SPA) or a mobile app where a human-written client needs to fetch a list of products, a REST or GraphQL API remains the correct tool. MCP is designed specifically for LLM reasoning.
Second, it introduces overhead for simple use cases. If you only need to trigger a single, straightforward action from an external service, setting up an MCP server is likely overkill.
Finally, the ecosystem is still young. While REST has decades of mature tooling around API gateways, caching layers, and documentation generators, MCP is a newer standard. However, for teams looking to build "agent-native" applications that allow models to safely explore and query real-world data, the ability to reuse your existing Laravel codebase makes MCP an incredibly compelling architectural choice.
Sources & further reading
Lenn writes about cloud platforms, Kubernetes internals, and the infrastructure decisions that quietly make or break engineering organizations. Based in Berlin's vibrant tech scene, they have a talent for turning dense platform-engineering topics into prose that people actually finish reading.
Discussion 0
No comments yet
Be the first to weigh in.