Tech Verse Logo
Enable dark mode
Exploring Laravel MCP: Build AI-Ready Servers with Ease

Exploring Laravel MCP: Build AI-Ready Servers with Ease

Tech Verse Daily

Tech Verse Daily

4 min read

The Laravel team has introduced Laravel MCP, now in public beta. This package provides a smooth way to build Model Context Protocol (MCP) servers inside your Laravel projects.

Following in the footsteps of Laravel Boost, MCP continues Laravel’s trend of making AI-focused development both enjoyable and productive.

What is Laravel MCP?

At its core, Laravel MCP gives you the building blocks to let AI clients talk to your Laravel application. Instead of wiring everything from scratch, you can define servers, tools, resources, and prompts that form the backbone of your AI-driven workflows.

Defining an MCP Server

An MCP server acts as the hub for communication. Here’s a minimal example:

namespace App\Mcp\Servers;
 
use Laravel\Mcp\Server;
 
class WeatherServer extends Server
{
    /**
     * The tools registered with this MCP server.
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Tool>>
     */
    protected array $tools = [
        // ExampleTool::class,
    ];
 
    /**
     * The resources registered with this MCP server.
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Resource>>
     */
    protected array $resources = [
        // ExampleResource::class,
    ];
 
    /**
     * The prompts registered with this MCP server.
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Prompt>>
     */
    protected array $prompts = [
        // ExamplePrompt::class,
    ];
}

Each section (tools, resources, prompts) represents capabilities that your server can provide to connected AI clients.

Adding a Tool

Tools are where the action happens. Here’s a simplified example of a weather-fetching tool:

namespace App\Mcp\Tools;
 
use Illuminate\JsonSchema\JsonSchema;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Tool;
 
class CurrentWeatherTool extends Tool
{
    protected string $description = 'Fetches the current weather forecast for a specified location.';
 
    public function handle(Request $request): Response
    {
        $location = $request->get('location');
 
        // Get weather...
 
        return Response::text('The weather is...');
    }
 
    public function schema(JsonSchema $schema): array
    {
        return [
            'location' => $schema->string()
                ->description('The location to get the weather for.')
                ->required(),
        ];
    }
}

Notice how the schema method defines the input requirements, and the handle method processes requests and returns responses.

    Latest Posts

    View All

    how to systematically optimize Laravel databases in production

    how to systematically optimize Laravel databases in production

    Optimize Images in Laravel with Intervention Image

    Optimize Images in Laravel with Intervention Image

    Common Security Mistakes in Laravel Apps and How to Fix Them Properly

    Common Security Mistakes in Laravel Apps and How to Fix Them Properly

    Clean, Reusable Query Logic the Right Way: Laravel Global Scopes & Local Scopes

    Clean, Reusable Query Logic the Right Way: Laravel Global Scopes & Local Scopes

    Mastering Custom Blade Directives in Laravel

    Mastering Custom Blade Directives in Laravel

    Laravel 12.44: Adds HTTP Client afterResponse() Callbacks

    Laravel 12.44: Adds HTTP Client afterResponse() Callbacks

    Laravel Artifact: Manage Your Media Easily

    Laravel Artifact: Manage Your Media Easily

    Handling Large File Uploads in Laravel: A Guide to Chunking & Resuming

    Handling Large File Uploads in Laravel: A Guide to Chunking & Resuming

    Next-Gen Laravel Deployment: FrankenPHP + Octane on Ubuntu VPS

    Next-Gen Laravel Deployment: FrankenPHP + Octane on Ubuntu VPS

    Speed Up Your Laravel App: Mastering Concurrent API Requests with Http::pool and Batch

    Speed Up Your Laravel App: Mastering Concurrent API Requests with Http::pool and Batch