Discover Laravel MCP – Supercharge Your Laravel Apps with AI Integration

Hey PHP community! If you’re a Laravel developer eager to ride the AI wave, buckle up. Today, we’re diving into Laravel MCP, a game-changing package that brings the power of the Model Context Protocol (MCP) to your Laravel applications. Fresh from the Laravel team, this tool lets you seamlessly integrate AI agents—like Claude, Cursor, or even ChatGPT—directly into your app, enabling users to interact with it conversationally. Imagine customers creating feedback, managing orders, or querying data through natural language, all powered by your Laravel backend.

In this 2,000+ word deep dive, we’ll unpack what Laravel MCP is, walk through its setup with hands-on code examples, and explore real-world use cases. Whether you’re a Laravel newbie or a seasoned pro prepping for the next big thing in web development, this guide— inspired by Nuno Maduro’s enthusiastic announcement—has you covered. Let’s build the future of AI-native apps together!

What Is Model Context Protocol (MCP)? The Big Picture

Before we hit the code, let’s break down the core concept. Model Context Protocol (MCP), pioneered by Anthropic, is an open standard for letting large language models (LLMs) interact with external applications. Think of it as an “API for AI”: a unified way to expose your app’s functionality—tools, resources, or prompts—to AI agents securely and scalably.

For Laravel developers, MCP is a bridge to a new world. Historically, integrating AI meant wrestling with custom API calls to OpenAI or Anthropic, juggling tokens, and handling edge cases. Laravel MCP changes that by providing a structured, Laravel-native way to:

  • Expose Tools: Define actions like “create feedback” that AI can trigger.
  • Share Resources: Let AI read or list structured data (e.g., user lists).
  • Craft Prompts: Guide AI responses with contextual templates.

The result? Your Laravel app becomes a conversational hub. Users can tell Claude, “Report to Dev that the Laravel team rocks,” and it’ll persist data in your database. As of 2025, with AI handling over 20 billion daily messages, MCP positions Laravel as a leader in “AI-first” development.

Why Laravel MCP? Benefits for PHP Developers

Laravel MCP isn’t just a wrapper—it’s a love letter to the Laravel ecosystem, built on top of the php-mcp/server library and integrated with Laravel’s service container, routing, and Artisan CLI. Here’s why it’s a must-have:

  1. Developer-Friendly: Define tools with PHP 8 attributes (#[McpTool]) or facades—minimal boilerplate.
  2. Enterprise Security: Leverage Laravel middleware for authentication, secure HTTP/SSE transports, and JsonSchema input validation.
  3. Scalable Performance: Integrates with Laravel Octane for concurrency, Redis caching, and database sessions.
  4. Broad Compatibility: Works with Claude, ChatGPT (via DeepResearch mode), Cursor IDE, and more.
  5. Rapid Prototyping: Auto-generate tools from OpenAPI specs or codebase introspection.

With AI-driven apps projected to dominate by 2026, Laravel MCP lets you ship features that delight users and scale effortlessly. Plus, it’s open-source under MIT—free and community-driven.

Getting Started: Installation and Setup

Let’s roll up our sleeves and set up Laravel MCP in a fresh Laravel 11+ project (Laravel 12 recommended for the latest features). If you don’t have a project, create one:

composer create-project laravel/laravel mcp-demo

Step 1: Install the Package

Run:

composer require laravel/mcp

This pulls in the official Laravel MCP package from GitHub. Publish the config:

php artisan vendor:publish --provider="Laravel\Mcp\McpServiceProvider"

This generates config/mcp.php, where you can tweak settings like transport (STDIO/HTTP), caching, and pagination limits.

Step 2: Set Up AI Routes

Create a routes/ai.php file (or use routes/web.php for simplicity):

<?php
// routes/ai.php
use Illuminate\Support\Facades\Route;
use Laravel\Mcp\Facades\Mcp;

Route::mcp(‘/mcp’, function () {
return Mcp::serve();
});

Update bootstrap/app.php to register the AI routes:

<?php
use Illuminate\Foundation\Application;

return Application::configure(basePath: dirname(DIR))
->withRouting(
web: DIR.’/../routes/web.php’,
commands: DIR.’/../routes/console.php’,
health: ‘/up’,
ai: DIR.’/../routes/ai.php’, // Add this
)
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();

The /mcp endpoint now exposes your MCP server to AI clients.

Step 3: Start the Server

For development, use Laravel Octane with FrankenPHP for Server-Sent Events (SSE):

composer require laravel/octane
php artisan octane:install --server=frankenphp
php artisan octane:start --host=0.0.0.0 --port=8000

Your MCP server is live at http://localhost:8000/mcp, ready for AI interaction.

Building Your First Tool: A Feedback System

Let’s recreate the demo: a tool that lets users submit feedback via AI, like “Report to Dev that the Laravel team is awesome.” MCP tools mimic Laravel controllers—you receive AI requests, process them, and respond.

Create the Tool

Generate a tool class:

php artisan make:mcp-tool CreateFeedbackTool

Edit app/Mcp/Tools/CreateFeedbackTool.php:

<?php
namespace App\Mcp\Tools;

use App\Models\Feedback; // Assume an Eloquent model
use Illuminate\Http\Request;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Request as McpRequest;
use Laravel\Mcp\Response as McpResponse;
use Illuminate\JsonSchema\JsonSchema;

class CreateFeedbackTool extends Tool
{
/**
* Description for AI to understand when to use this tool.
*/
public function description(): string
{
return ‘Creates a new feedback entry in the system, reporting content to a specific recipient.’;
}

/**
 * Input schema for validation.
 */
public function inputSchema(): JsonSchema
{
    return JsonSchema::make()
        ->string('content', 'The feedback content')
        ->string('recipient', 'The recipient (e.g., [email protected])')
        ->required(['content', 'recipient']);
}

/**
 * Handle the AI request.
 */
public function handle(McpRequest $request): McpResponse
{
    $content = $request->input('content');
    $recipient = $request->input('recipient');

    // Persist to DB
    $feedback = Feedback::create([
        'content' => $content,
        'recipient' => $recipient,
        'user_id' => auth()->id() ?? 1, // From session
    ]);

    // Success response
    return McpResponse::success("Feedback created with ID {$feedback->id} for {$recipient}.");
}

}

This defines a tool that validates input (content, recipient), saves feedback via Eloquent, and responds to the AI.

Register the Tool

In app/Providers/McpServiceProvider.php:

<?php
namespace App\Providers;

use App\Mcp\Tools\CreateFeedbackTool;
use Illuminate\Support\ServiceProvider;
use Laravel\Mcp\Facades\Mcp;

class McpServiceProvider extends ServiceProvider
{
public function boot()
{
Mcp::tool(new CreateFeedbackTool());
}
}

Or use PHP 8 attributes in the tool class:

#[McpTool]
class CreateFeedbackTool extends Tool { /* ... */ }

Laravel’s auto-discovery handles the rest.

Live Demo: Interacting with Claude

Let’s see it in action. Assume you’ve installed Claude’s CLI and added your MCP server:

claude mcp add laravel-feedback http://localhost:8000/mcp

In Claude’s terminal:

$ claude
> Report to Dev that the Laravel team is awesome.

Claude detects the CreateFeedbackTool, calls it, persists the feedback, and replies: “Feedback reported successfully!”

Verify in your app with a custom Artisan command:

php artisan feedback:list
// Output: "The Laravel team is awesome" -> [email protected]

This showcases MCP’s power: clients add your server with one command, enabling native AI interactions. For production, secure the endpoint:

Route::mcp('/mcp', function () {
    return Mcp::serve()->middleware('auth:sanctum');
});

Beyond Tools: Resources and Prompts

MCP isn’t just about tools. Add resources for data retrieval:

<?php
namespace App\Mcp\Resources;

use App\Models\Feedback;
use Laravel\Mcp\Server\Resource;

class FeedbackResource extends Resource
{
public function list(): array
{
return Feedback::paginate(10)->toArray();
}
}

And prompts to guide AI:

<?php
namespace App\Mcp\Prompts;

use Laravel\Mcp\Server\Prompt;

class FeedbackPrompt extends Prompt
{
public function content(): string
{
return ‘You are an assistant collecting constructive feedback. Validate content before creating.’;
}
}

These let AI list feedback or respond contextually, enhancing user experience.

Real-World Use Cases

Laravel MCP unlocks endless possibilities:

  • E-commerce: AI handles orders via Claude—“Add product X to cart.”
  • CRM: Auto-generate reports or create leads from chats.
  • Development: Introspect codebases for Cursor—query routes, models.
  • Admin Panels: Integrate with Filament for AI-driven dashboards.

Stay tuned for a future tutorial on building an AI-powered todo app with MCP.

Challenges and Best Practices

  • Security: Always use middleware; avoid public endpoint exposure.
  • Performance: Cache tools with Redis; limit resource pagination.
  • Debugging: Log MCP requests with Laravel Telescope.
  • Compatibility: Test with multiple AI clients—Claude leads, but ChatGPT’s growing fast.

Check the official GitHub repo for troubleshooting.

Laravel’s AI-Powered Future

Laravel MCP isn’t just a package—it’s a paradigm shift. It transforms your apps into conversational hubs, ready for the AI-driven world. As Nuno said, “The Laravel team is awesome!”—and now, you can build that into your app.


Posted

in

, , , ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *