Laravel with MCP — Complete WorkFlow

Laravel MCP (Model Context Protocol) allows your Laravel application to connect directly with AI systems like ChatGPT, Claude, Gemini, etc.
What Can AI Do With MCP?
Using MCP, AI can: • Call functions inside your Laravel app • Read and write database data • Use tools (actions) • Use prompts (templates) • Access resources (documents or files)
Table of Contents
1. What Is Laravel MCP? 2. Install Laravel 3. Install MCP Package 4. Publish MCP Routes 5. Create MCP Server 6. Register MCP Server Route 7. Create a Tool 8. Add Tool to Server 9. Test with MCP Inspector 10. Real Example: AI Creates a User 11. Create Prompts 12. Create Resources 13. AI Reading or Writing Database 14. Summary
What Is Laravel MCP?
Laravel MCP is a bridge between AI and your Laravel application. It allows AI to: • Run actions (tools) • Read data (resources) • Use templates (prompts) • Update your database Install Laravel composer create-project laravel/laravel my-app cd my-appInstall MCP Packagecomposer require laravel/mcp
Publish MCP Routes
php artisan vendor:publish --tag=ai-routes This creates: routes/ai.php This file is used to register MCP servers.
Create MCP Server
A server is the main controller for MCP. php artisan make:mcp-server WeatherServer Example server:
<?php
namespace App\Mcp\Servers;
use Laravel\Mcp\Server;
class WeatherServer extends Server
{
protected string $name = 'Weather Server';
protected string $version = '1.0.0';
protected string $instructions = 'This server provides weather information.';
protected array $tools = [];
protected array $resources = [];
protected array $prompts = [];
}Register MCP Server Route
Open routes/ai.php:
<?php
use App\Mcp\Servers\WeatherServer;
use Laravel\Mcp\Facades\Mcp;
Mcp::web('/mcp/weather', WeatherServer::class);
AI can now connect via: POST /mcp/weather
Create a Tool
Tools are actions AI can execute. php artisan make:mcp-tool CurrentWeatherTool Example:
<?php
namespace App\Mcp\Tools;
use Laravel\Mcp\Tool;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\JsonSchema;
class CurrentWeatherTool extends Tool
{
protected string $description = 'Get current weather for a location.';
public function handle(Request $request): Response
{
$location = $request->get('location');
return Response::text("The weather in $location is sunny.");
}
public function schema(JsonSchema $schema): array
{
return [
'location' => $schema->string()->required(),
]; }}Add Tool to Server
Edit WeatherServer.php:
protected array $tools = [
CurrentWeatherTool::class,
];Test With MCP Inspector
php artisan mcp:inspector mcp/weather Inspector lets you test all MCP features interactively.
Real Example: AI Creates a User
Create tool:php artisan make:mcp-tool CreateUserTool Implementation:
<?php
namespace App\Mcp\Tools;
use App\Models\User;
use Laravel\Mcp\Tool;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\JsonSchema;
class CreateUserTool extends Tool
{
protected string $description = 'Create a new user.';
public function handle(Request $request): Response
{
$data = $request->validate([
'name' => 'required',
'email' => 'required|email',]);
$user = User::create($data);
return Response::text("User created: {$user->name}");
}
public function schema(JsonSchema $schema): array
{
return [
'name' => $schema->string()->required(),
'email' => $schema->string()->required(),
];
}
}
AI can now say: “Create a user named John with email john@gmail.com”
Create Prompts
Prompts are predefined AI templates. php artisan make:mcp-prompt DescribeWeatherPrompt Example:
<?php
namespace App\Mcp\Prompts;
use Laravel\Mcp\Prompt;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
class DescribeWeatherPrompt extends Prompt
{
protected string $description = 'Describe weather naturally.';
public function handle(Request $request): Response
{
return Response::text("Explain the weather in simple words.");
}
}Create Resources
Resources provide AI with static or dynamic content. php artisan make:mcp-resource WeatherGuidelinesResource Example:
<?php
namespace App\Mcp\Resources;
use Laravel\Mcp\Resource;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
class WeatherGuidelinesResource extends Resource
{
protected string $description = 'Weather safety guidelines.';
public function handle(Request $request): Response
{
return Response::text("Always check temperature before traveling.");
}
}AI Reading or Writing Database
Example: Fetch all users
<?php
namespace App\Mcp\Tools;
use App\Models\User;
use Laravel\Mcp\Tool;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
class GetUsersTool extends Tool
{
public function handle(Request $request): Response
{
return Response::structured([
'users' => User::all()
]); }
}
AI can ask: “Show me all users.”
Summary
Component Purpose Server Main entry point for MCP Tool AI actions (database write, fetch, etc.) Prompt Predefined AI instructions Resource Documentation or data for AI Routes Expose MCP server endpoints Inspector Testing tool
Conclusion
Laravel MCP makes your Laravel project AI-powered. AI can automate tasks, read data, and interact with your system just like a real user — but smarter.
