If you’re new to Laravel and AI integration, you may have heard of MCP servers and wondered: “What is it, and why do I need it?”
This post will explain MCP servers step by step, show how it works, and include working code examples and diagrams.
1️⃣ What is an MCP Server?
MCP stands for Model Context Protocol.
Think of it as:
A structured bridge between your AI agent and your Laravel backend.
It allows AI to:
- Call functions (tools) in your backend safely
- Receive structured responses
- Know exactly what actions it can take
💡 MCP is not the AI itself. It’s the “middleman” that executes your backend logic based on AI’s commands.
✅ Why MCP is Better Than Regular APIs for AI
| Feature | Regular API | MCP Server |
|---|---|---|
| AI knows available actions? | ❌ | ✅ |
| AI sends structured calls? | ❌ | ✅ |
| Validation & safe execution? | ❌ | ✅ |
| Auto-discovery of tools for AI? | ❌ | ✅ |
2️⃣ How MCP Works – Conceptually
Let’s visualize the flow:
User Prompt → Laravel Client App
↓
AI Agent interprets → generates JSON tool call
↓
Laravel MCP Server (App 1) → Executes tool
↓
Structured Response (JSON)
↓
AI Agent converts → Human-readable message
↓
Displayed back to user in Laravel Client App
3️⃣ Setup MCP Server in Laravel 11
Step 1: Install Laravel MCP
composer require laravel/mcp
php artisan vendor:publish --tag=mcp-config
Step 2: Create a Tool
MCP tools are thin classes that map to your backend logic.
Example: Create a new order.
// app/Mcp/Tools/CreateOrder.php
namespace App\Mcp\Tools;use Laravel\Mcp\Tool;
use App\Services\OrderService;class CreateOrder extends Tool
{
public string $name = 'createOrder';
public string $description = 'Create a new order'; public function handle(array $arguments): array
{
$service = app(OrderService::class); $order = $service->create(
$arguments['user_id'],
$arguments['product_id']
); return [
'id' => $order->id,
'status' => 'created'
];
}
}
Step 3: Register Tool in Config
// config/mcp.php
'tools' => [
App\Mcp\Tools\CreateOrder::class,
],
Step 4: Start MCP Server
php artisan mcp:serve
Now your Laravel app exposes a JSON-based MCP endpoint that AI clients can call.
4️⃣ Backend Logic – Keep It Clean
Use Services for actual business logic, not Controllers.
// app/Services/OrderService.php
namespace App\Services;use App\Models\Order;class OrderService
{
public function create(int $userId, int $productId)
{
return Order::create([
'user_id' => $userId,
'product_id' => $productId,
]);
}
}
MCP Tool calls this service — Controllers don’t need to be involved.
5️⃣ How AI Interacts
- AI receives available tools from MCP server.
- It chooses the right tool based on user prompt.
- Sends structured JSON call:
{
"tool": "createOrder",
"arguments": {
"user_id": 5,
"product_id": 10
}
}
- MCP executes
CreateOrdertool - Returns JSON response:
{
"id": 123,
"status": "created"
}
- AI converts response into human-readable message:
“Order #123 has been successfully created.”
6️⃣ Multi-App Use Case
You can use one MCP server across multiple Laravel apps:
- App 1: MCP server with tools and services
- App 2: Client app (can be Laravel or anything)
- Flow:
App 2 sends prompt → AI interprets → MCP call to App 1 → Response → AI feedback → Display in App 2
MCP is safer and structured than just calling APIs.
7️⃣ Key Takeaways
- MCP = structured AI bridge, not AI itself
- AI sends JSON-based tool calls
- MCP executes backend logic via tools
- Keep business logic in Services
- Use MCP for multi-app integration, AI-driven automation, and safe execution
8️⃣ Quick Diagram
[User Prompt]
|
v
[Laravel Client App]
|
v
[AI Agent] --(structured JSON tool call)--> [Laravel MCP Server]
| |
| Tool Classes
| |
v v
Feedback <------------------------------- Business Logic / Services
9️⃣ Conclusion
If you want AI to safely interact with your Laravel app, MCP is the right solution.
- Easier than manually parsing JSON
- Safer than exposing raw APIs to AI
- Supports multiple clients and apps
This is beginner-friendly, uses Laravel 11, and includes code examples and diagrams to get you started.
MCP is a Laravel package that, once installed, lets you write logic to handle structured commands sent by an AI model (based on user prompts). The MCP tool executes your backend logic, interacts with the database, and returns a structured JSON response. The AI then interprets this response and presents it back to the user in a friendly, human-readable format.
