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

Exploring Laravel MCP: Build AI-Ready Servers with Ease

Md. Mostafijur RahmanMMd. Mostafijur Rahman

Md. Mostafijur Rahman

•1 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.

Md. Mostafijur RahmanMMd. Mostafijur Rahman

WRITTEN BY

Md. Mostafijur Rahman

    Latest Posts

    View All

    Handling Concurrent Writes with Laravel Pessimistic Locking

    Handling Concurrent Writes with Laravel Pessimistic Locking

    Hardening a Linux VPS with Custom Port and Keys

    Hardening a Linux VPS with Custom Port and Keys

    Next.js Server Actions Concurrency and Stale Data

    Next.js Server Actions Concurrency and Stale Data

    FastAPI Dependency Injection Scope Leaks in Production

    FastAPI Dependency Injection Scope Leaks in Production

    Mastering Next.js Streaming and Suspense

    Mastering Next.js Streaming and Suspense

    Preventing Race Conditions in Laravel Seeders

    Preventing Race Conditions in Laravel Seeders

    Linux Sysctl Tuning for High-Throughput Web Servers

    Linux Sysctl Tuning for High-Throughput Web Servers

    Local Embedding Models in Production RAG

    Local Embedding Models in Production RAG

    Preventing nextjs middleware cache poisoning

    Preventing nextjs middleware cache poisoning

    Fixing Async Python Memory Leaks from Unclosed Sessions

    Fixing Async Python Memory Leaks from Unclosed Sessions