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

    React Context: When to Reach for a Store

    React Context: When to Reach for a Store

    Custom React Hooks: Abstraction vs Indirection

    Custom React Hooks: Abstraction vs Indirection

    Stop Wasting React Performance on memo and useMemo

    Stop Wasting React Performance on memo and useMemo

    Next.js Partial Prerendering: Static Shells, Dynamic Holes

    Next.js Partial Prerendering: Static Shells, Dynamic Holes

    Stop Re-rendering: When to Drop useEffect in React 19

    Stop Re-rendering: When to Drop useEffect in React 19

    Fixing Next.js Hydration Mismatch Errors in React 19

    Fixing Next.js Hydration Mismatch Errors in React 19

    Generating Open Graph Images at the Edge in Next.js 16

    Generating Open Graph Images at the Edge in Next.js 16

    Fix Next.js Third Party Script Performance

    Fix Next.js Third Party Script Performance

    Next.js 16 Testing Strategy: Unit, Component, E2E

    Next.js 16 Testing Strategy: Unit, Component, E2E

    Shrinking Next.js Bundle Size: Analyzer & Barrel Fixes

    Shrinking Next.js Bundle Size: Analyzer & Barrel Fixes