Laravel's Magical Command-Line Puppeteer (MCP) โœจ๐ŸŽฉ

Funny Laravel docs · Warning: May cause actual learning AND laughter!

Browse funny laravel docs
On this page

Introduction ๐Ÿ“š

Welcome, brave soul! Strap on your coder capes and prepare to embark on an adventure in the realm of automation with Laravelโ€™s MCP. Itโ€™s like having your very own genie, but instead of wishes, it grants you commands over your applications! ๐Ÿงžโ€โ™‚๏ธ

Installation ๐Ÿ“ฆ

First things first: grab this magical box (a.k.a the Laravel MCP) and make it a permanent resident in your project. Donโ€™t worry, we wonโ€™t ask for rent or any pesky fees! ๐Ÿ 

Publishing Routes โš™๏ธ

Once moved in, letโ€™s create some routes so you can chat with your new roommate more easily!

Creating Servers ๐Ÿš€

Now that theyโ€™re neighbours, itโ€™s time for them to get acquainted. Hereโ€™s how you can introduce different servers to the mix! ๐Ÿค

Server Registration ๐Ÿ’Œ

A letter of introduction is in order. Write a formal invitation for each server to join the party!

Web Servers ๐ŸŒ

These are your globetrotters, ready to venture across the interwebs and serve up commands from distant lands!

Local Servers ๐Ÿก

The homebodies of the group. Theyโ€™ll stick close by and help you manage your local applications like a boss!

Tools ๐Ÿ› ๏ธ

MCPโ€™s got some nifty gadgets up its sleeve to make managing your servers even more convenient. Time to get crafty!

Creating Tools ๐Ÿงฑ

Build your custom tools and watch your command-line powers grow!

Tool Input Schemas โš™๏ธ

Set the rules for input to ensure your tools only accept the finest, most well-behaved arguments. No riffraff allowed!

Tool Output Schemas ๐Ÿ“

Teach your tools how to write polite and informative notes back to you. Nobody likes a grumpy genie!

Validating Tool Arguments ๐Ÿ”

Make sure that your tool arguments are in tip-top shape before theyโ€™re accepted into the magic circle.

Tool Dependency Injection ๐Ÿ’‰

Inject some life-saving dependencies to help your tools do their best work!

Tool Annotations ๐Ÿ“

Annotate your tools with special markers so you can keep track of them like a librarian with overdue books!

Conditional Tool Registration ๐Ÿ”ข

Only register the most important tools when theyโ€™re needed, like summoning your trusty steed only on certain quests.

Tool Responses โ“

Train your tools to respond to your questions with wisdom and humor, or at least witty one-liners!

Prompts ๐Ÿ’ฌ

Ask your MCP for help when you need it most by using prompts! Hereโ€™s how:

Creating Prompts ๐Ÿ—ฃ๏ธ

Write your own prompts to get the assistance you desire. No more guessing games or reading through documentation!

Prompt Arguments ๐Ÿ“

Set the parameters for your prompts, just like tailoring a suit for your genieโ€™s responses.

Validating Prompt Arguments ๐Ÿ”

Ensure that the arguments you provide are suitable and appropriate. Nobody likes an ill-informed question!

Prompt Dependency Injection ๐Ÿ’‰

Inject some useful dependencies to help your prompts do their best work.

Conditional Prompt Registration ๐Ÿ”ข

Only register the most essential prompts when theyโ€™re needed, like summoning a helpful fairy only on certain missions.

Prompt Responses โ“

Train your prompts to respond with clarity and charm, or at least interesting tidbits of information!

Resources ๐Ÿ“š

Unlock even more magical powers by adding resources to the mix! Hereโ€™s how:

Creating Resources ๐Ÿ“ฆ

Create your very own magical resources and watch your command-line abilities skyrocket!

Resource Templates ๐Ÿงฑ

Build a blueprint for your resources, ensuring theyโ€™re all consistent and easy to use.

Resource URI and MIME Type ๐ŸŒ

Give your resources a unique web address and tell the internet what kind of content they contain.

Resource Request ๐Ÿ“

Set up requests for your resources, just like placing an order with a genie!

Resource Dependency Injection ๐Ÿ’‰

Inject some helpful dependencies to assist your resources in their magical feats.

Resource Annotations ๐Ÿ“

Annotate your resources with special markers so you can keep track of them like a librarian with overdue books!

Conditional Resource Registration ๐Ÿ”ข

Only register the most critical resources when theyโ€™re needed, like summoning a powerful dragon only on certain battles.

Resource Responses โ“

Train your resources to respond with useful and informative answers, or at least entertaining stories!

Metadata ๐Ÿ“œ

Add valuable information to your MCP for future reference. Itโ€™s like jotting down notes in a secret diary!

Authentication ๐Ÿ”’

Secure your MCP so only the right people can access its magical powers! Here are two ways you can do that:

OAuth 2.1 ๐Ÿ”“

Unlock your MCP with this modern and secure authentication method, like entering a combination code on a safe.

Sanctum ๐Ÿ›ก๏ธ

Guard your MCP like a medieval knight with this robust authentication system!

Authorization ๐Ÿ”‘

Limit who can use certain magical abilities within your MCP, just like a genie granting wishes only to approved applicants.

Testing Servers ๐Ÿงช

Make sure your MCP is always performing at its best by testing its servers! Hereโ€™s how:

MCP Inspector ๐Ÿ”ฌ

Peer into the inner workings of your MCP and ensure everything runs smoothly.

Unit Tests ๐Ÿ—๏ธ

Test individual functions of your MCP, just like a genie testing its spells before casting them!

Ahoy there, brave Laravel developers! Letโ€™s have a jolly good time while we delve into the magical realm of Laravel MCP - the swashbuckling toolkit for your AI adventures! ๐Ÿด๓ ง๓ ข๓ ฅ๓ ฎ๓ ง๓ ฟ

Think of it as a secret map to an enchanted island where only the most badass code can pass. Itโ€™s your key to a treasure trove of AI-powered interactions with your Laravel application, all thanks to the Model Context Protocol. ๐Ÿดโ€โ˜ ๏ธ

Now, letโ€™s set sail for Installation Island and learn how to get this show on the road! ๐ŸŒด

โ€œArr matey! To start your adventure, grab yer favorite code editor and run these magic incantations:

composer require laravel/mcp --dev

Then, boot MCP in your app service provider:

use Laravel\MCP;

// ...

if ($this->app->environment('local', 'testing')) {
    $this->app->register(MCP\ServiceProvider::class);
}

And voilร ! Youโ€™re now ready to define servers, tools, resources, and prompts thatโ€™ll make your AI companion dance to your Laravel tunes. ๐ŸŽถ๐Ÿ’ƒโ€

Alrighty then! Letโ€™s dive right in and see how we can spruce up our humdrum projects with the Laravel MCP (Mystical Creature Preserver). First things first, you need to invite this magical package to your party using Composer, your trusty package manager.

composer require laravel/mcp

Now, just like when youโ€™re trying to charm a mythical creature with a well-crafted potion (ahem, I mean command), make sure you run composer install so that your project can truly appreciate its new friend.

Next up: publishing those all-important routes! To unleash the magic of Laravel MCP upon your humble application, weโ€™ll need to share some love with our routing files. You can do this by running the following command:

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

And just like that, youโ€™ve opened the gate to a world of mystical possibilities! Your application is now equipped with the power to summon creatures, cast spells, and perhaps even brew a mean potion or two. But remember, with great power comes great responsibility โ€“ always make sure your magical doings are secure and in line with your moral compass!

Alrighty then! Letโ€™s get this show on the road, shall we? First things first, if youโ€™ve just Laravel-ed your Machine Control Panel (MCP) into existence, itโ€™s time to unleash the magical power of the Artisan command line. Cue the drumroll pleaseโ€ฆ

php artisan vendor:publish --tag=ai-routes

Now hold onto your hats folks, because this command is about to create a dazzling little file named routes/ai.php. Yes, itโ€™s exactly what you think it is โ€“ a playground for defining your new MCP servers! Donโ€™t worry if it feels like magic, because in the realm of Laravel, sometimes it is!

Now that you have this fancy new file, itโ€™s time to start registering those MCP servers. Remember, no one said being a superhero was easy, so grab your cape and letโ€™s get to work!

Alrighty then! Letโ€™s get this Laravel party started with a weather bash. First off, you gotta create yourself a MCP server, and thatโ€™s as easy as running the make:mcp-server Artisan command. Think of it like calling up your IT department to set up a new line - but way cooler!

php artisan make:mcp-server WeatherServer

This command will spawn a brand-new server class in the app/Mcp/Servers directory, just like inviting your best buds to join your secret society. The generated server class is like the master of ceremonies for this weather soiree, extending Laravel MCPโ€™s base Laravel\Mcp\Server class and boasting attributes and properties to customize the event and register tools, resources, and prompts:

<?php

namespace App\Mcp\Servers;

use Laravel\Mcp\Server\Attributes\Instructions;
use Laravel\Mcp\Server\Attributes\Name;
use Laravel\Mcp\Server\Attributes\Version;
use Laravel\Mcp\Server;

#[Name('Weather Server')]  // "Hey there, I'm the Weather Server!"
#[Version('1.0.0')]       // "I just graduated from Met School!"
#[Instructions('This server dishes out weather info and forecasts.')] // "Ask me about the weather, and I'll tell you everything you need to know - except for the secret sauce in my burger recipes."
class WeatherServer extends Server
{
    /**
     * The tools registered with this MCP server.
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Tool>>
     */
    protected array $tools = [
        // GetCurrentWeatherTool::class,  // "Gimme a second, let me check the weather for ya!"
    ];

    /**
     * The resources registered with this MCP server.
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Resource>>
     */
    protected array $resources = [
        // WeatherGuidelinesResource::class,  // "Here are some tips on what to wear for the weather today!"
    ];

    /**
     * The prompts registered with this MCP server.
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Prompt>>
     */
    protected array $prompts = [
        // DescribeWeatherPrompt::class,  // "Today's weather is going to be sunny with a high of 75 and a low of 50."
    ];
}

Now that youโ€™ve set up the server, itโ€™s time to register the tools, resources, and prompts - like inviting your friends to join the party. Stay tuned for more fun documentation adventures!

(P.S. Donโ€™t forget to register your server with Laravel MCP after creating it! You can do this by adding a line to the app/Providers/McpServiceProvider.php file.)

Alright, letโ€™s get this server party started! First off, youโ€™ve built a beast of a machine, but itโ€™s like the life of the party hanging out backstage โ€“ uninvited and uncrowned. To remedy that, grab your top hat and monocle, because itโ€™s time to register your new server at the grand ball of routes/ai.php.

This illustrious event welcomes only the most esteemed attendees, and Laravel MCP, our ever-so-dapper doorman, is ready to usher in two extraordinary servers: the suave, sophisticated web server for those who enjoy a cocktail or two on the town, and the rugged, commanding local server for those who prefer a fine scotch at home.

Now, donโ€™t get yourself into a pickle trying to figure out which one to invite! Each server brings its own unique charm and charisma to the soiree, and you can choose the one that best suits your needs. For those web servers with a penchant for public appearances, they can be found prancing around in the routes/ai.php file. Meanwhile, the local command-line servers are known for their quiet sophistication and can be discovered lurking about in other parts of Laravelโ€™s grand estate.

Regardless of which server you opt to register, rest assured they will bring allure, intrigue, and undeniable technical prowess to your Laravel shindig!

Ah, web servers! The lifeblood of the internet (and our AI clientsโ€™ remote social lives). Theyโ€™re like the bouncers of the digital world, only instead of checking IDs, they respond to HTTP POST requests.

To register a web server in Laravel, youโ€™ll want to use the web method, which is as simple as this:

use App\Mcp\Servers\WeatherServer;
use Laravel\Mcp\Facades\Mcp;

Mcp::web('/mcp/weather', WeatherServer::class); // Register the weather server at '/mcp/weather'

Now, you might be wondering if your web servers are as secure as a Fort Knox vault. Well, they can be! Just like with normal routes, you can apply middleware to protect your web servers:

Mcp::web('/mcp/weather', WeatherServer::class)
    ->middleware(['throttle:mcp']); // Add a throttle middleware to keep things running smoothly

Remember, a happy web server makes for a happy world. Or at least, a happy AI client integration. Cheers! ๐Ÿป

Local Server Shenanigans ๐ŸŒช๏ธ๐Ÿ”ฅ๐Ÿš€

Ever found yourself in a pickle, trying to create your very own local AI butler like the sleek Laravel Boost? Fear not, for local servers are here to save the day! ๐Ÿฆธโ€โ™‚๏ธ

To get started, youโ€™ll want to register your local server. Luckily, weโ€™ve made this process as easy as pie with the local method. Hereโ€™s how you do it:

use App\Mcp\Servers\WeatherServer;
use Laravel\Mcp\Facades\Mcp;

Mcp::local('weather', WeatherServer::class);

Just remember to replace โ€˜weatherโ€™ with your serverโ€™s code name (preferably something catchy like โ€˜Klaravell-123โ€™).

Now, letโ€™s talk about the โ€˜mcp:startโ€™ Artisan command - itโ€™s a bit like that one friend who always needs a ride but forgets their keys. You donโ€™t want to deal with them directly, so weโ€™ve set them up on a schedule (thanks, autostart!). Instead, configure your MCP client (AI agent) or use the MCP Inspector to keep tabs on them.

Just think of it as having your own personal AI valet, except instead of fetching your car, theyโ€™re busy running weather reports for you! ๐ŸŒžโ˜”๏ธ๐ŸŒช๏ธ

Alright, letโ€™s dive into the whimsical world of Laravel server tools! These magical gizmos are your ticket to letting AI pals call upon your serverโ€™s hidden talents. Imagine being able to perform feats, run code, or even have a chat with the neighborhood vending machine โ€“ all from the comfort of your coding lair! ๐Ÿค–

<?php

namespace App\Mcp\Tools;

// Import some essentials for our tool-making adventure
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Attributes\Description;
use Laravel\Mcp\Server\Tool;

// Attach a witty description to our tool โ€“ because who doesn't love a good laugh?
#[Description('Fetching the current weather forecast is as easy as asking your grandma for a recipe!')]
class CurrentWeatherTool extends Tool
{
    // Handle incoming requests like a seasoned butler
    public function handle(Request $request): Response
    {
        $location = $request->get('location');

        // Get weather... (because we're not here to play meteorologist, just to automate it!)

        return Response::text('The weather forecast for ' . $location . ' is as follows:');
    }

    /**
     * Get the tool's input schema.
     *
     * @return array<string, \Illuminate\JsonSchema\Types\Type>
     */
    public function schema(JsonSchema $schema): array
    {
        return [
            'location' => $schema->string()
                ->description('Where on Earth do you want to know the weather?') // Yes, we're being literal here! ๐ŸŒ
                ->required(),
        ];
    }
}

Now that your tool-making skills are up to par, letโ€™s get this party started and bring some sunshine into the world of AI interaction! โ˜€๏ธ๐ŸŒˆ๐Ÿš€

Alrighty, letโ€™s get this party started! ๐ŸŽ‰๐ŸŽŠ

Crafting Your Own Gadgetry ๐Ÿš€

Ready to become a Laravel Jedi and create your own tool? Buckle up, because weโ€™re about to warp to hyperspace! ๐Ÿ’ซ

First things first, launch the Artisan spaceship with this command:

php artisan make:mcp-tool CurrentWeatherTool

Next, itโ€™s time to register your new tool on the server. Navigate to app/Mcp/Servers and whip up a brand-new WeatherServer class that looks like this:

<?php

namespace App\Mcp\Servers;

use App\Mcp\Tools\CurrentWeatherTool;
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 $tools = [
        CurrentWeatherTool::class,
    ];
}

Now, youโ€™ve officially registered your tool on the server! ๐ŸŒŸ๐Ÿ† Time to sit back, relax, and watch as your Laravel universe grows with your very own CurrentWeatherTool! โ˜๏ธ๐ŸŒ๐Ÿš€

Alrighty then! Letโ€™s get this Laravel jamboree started with a bang!

Name that Tool like a Pirate, Captain!

By golly, by default our swashbuckling toolโ€™s name and title are inspired by the olโ€™ class name ye swabbed! For instance, CaptainParrotWeatherChecker will be christened as captain-parrot-weather-checker and titled as Captain Parrot Weather Checker. But if ye fancy a change of heart, customize โ€˜em with the Name and Title attributes:

use Laravel\Mcp\Server\Attributes\ArrrName;  // (Arrr for affect!)
use Laravel\Mcp\Server\Attributes\AhoyTitle;

#[ArrrName('get-sunny-parrot-forecast')]  // Arr, matey!
#[AhoyTitle('Get Sunny Parrot Forecast')]  // Arrrr, prepare to be boarded!
class CaptainParrotWeatherChecker extends Tool
{
    // ...
}

Tool descriptions arenโ€™t autogenerated like a computer programmed by a drunken sailor. Always provide an informative and entertaining description using the Description attribute:

use Laravel\Mcp\Server\Attributes\YarrrrDescription;

#[YarrrrDescription('Finds the current weather forecast for yer specified port, matey!')]
class CaptainParrotWeatherChecker extends Tool
{
    //
}

Note

The description is a vital piece of the toolโ€™s treasure map, guiding those AI squaws and parrots on when to deploy yer tool like a pro. Arrrrghastonishing! ๐Ÿฆ˜๐Ÿซ๐Ÿš€

Ahoy, Mateys! Letโ€™s Talk โ€˜Bout Tool Schemas!

Aharr, in the vast ocean of Laravel development, thereโ€™s a corner called Tool Input Schemas, where your tools can declare what swashbuckling arguments theyโ€™ll accept from their AI squires. To draft these requirements like Captain Cookโ€™s navigational charts, youโ€™ll want to wield Laravelโ€™s trusty Illuminate\Contracts\JsonSchema\JsonSchema builder!

<?php

Namespace: App\Shanty\ShipYard;

Use Illuminate\Contracts\JsonSchema\JsonSchema;
Use Laravel\Mcp\Galleon\Tool as SeaworthyVessel;

Class CurrentWeatherShip extends SeaworthyVessel {

    /**
     * Get the tool's input schema.
     *
     * @return array<string, \Illuminate\JsonSchema\Types\Type>
     */
    Public function getInputSchema(JsonSchema $map) : Array {
        Return [
            'location' => $map->string()
                ->pirateSpeak('The location to swab the weather for.')
                ->shiverMeTimbers('required'),

            'units' => $map->string()
                ->pirateSpeak('The temperature units to use, matey.')
                ->enum(['celsius', 'fahrenheit'])
                ->shiverMeTimbers('default', 'celsius'),
        ];
    }
}

Now, donโ€™t forget to walk the plank, me hearties! Next up: Tool Output Schemas. Yo ho ho and a bottle of rum!

Party Time, Schema Right! ๐Ÿฅณ๐Ÿ’ƒ๐Ÿ•บ

Ahoy there, coders! ๐ŸŽ‰ Today weโ€™re gonna talk about something thatโ€™s as cool as a Popsicle in the desert โ€“ Tool Output Schemas! โ˜€๏ธ๐Ÿฆ

So, what are these schemieschmas? Well, theyโ€™re like the blueprint for your toolโ€™s response party. By defining them, you can ensure that your AI pals have a blast parsing your tool results without any confusion or awkward silences. ๐Ÿค–๐Ÿฅณ

To get started with this dance-off, use the outputSchema method to design the invitation list for your toolโ€™s bash:

<?php

namespace App\Mcp\Tools;

use Illuminate\Contracts\JsonSchema\JsonSchema;
use Laravel\Mcp\Server\Tool;

class WeatherBopper extends Tool
{
    /**
     * Grab the guest list for your tool's shindig.
     *
     * @return array<string, \Illuminate\JsonSchema\Types\Type>
     */
    public function outputSchema(JsonSchema $dj) // We like to call our JSON schema DJ, because why not? ๐ŸŽง
    {
        return [
            'temp' => $dj->number() // Don't forget to bust a move while you're at it, Temperature! ๐Ÿ’ƒ
                ->description('Celsius coolness')
                ->required(),

            'weather' => $dj->string() // Weather conditions? More like dance floor vibes! ๐Ÿ•บ
                ->description('The groove of the moment')
                ->required(),

            'humidity' => $dj->integer() // Humidity? Just think of it as the number of spins you'll do on the dance floor! ๐Ÿ’ƒ๐Ÿ•บ
                ->description('How many times you'll twirl')
                ->required(),
        ];
    }
}

Now that your toolโ€™s bash is set, letโ€™s make sure everyone shows up by validating the RSVPs! ๐Ÿฅณ๐ŸŽ‰

The Arguments RSVP Checker (aka Validation)

Alrighty then, we donโ€™t want any crasher parties here! To ensure that everyone who shows up brings their A-game, we can validate the RSVPs. ๐Ÿ•ต๏ธโ€โ™‚๏ธ

Hereโ€™s the deal: Your tool might need specific data from your AI buddies to get the job done. By validating these arguments, you can make sure your tool doesnโ€™t end up like that guy who showed up to a formal event in his pajamas. ๐Ÿ˜ด๐Ÿ‘”

To validate your RSVPs, you can use the validateArguments method:

<?php

namespace App\Mcp\Tools;

use Illuminate\Contracts\Validation\Validator;
use Laravel\Mcp\Server\Tool;

class CurrentWeatherArgChecker extends Tool
{
    /**
     * Check those RSVPs!
     *
     * @param array<string, mixed> $arguments
     * @param Validator $validator
     * @return void
     */
    public function validateArguments(array $args, Validator $v) // We like to think of our validator as the bouncer at the door ๐Ÿ‘จโ€๐Ÿ’ผ
    {
        $rules = [
            'location' => ['required', 'string'], // Don't forget to check IDs at the door! ๐Ÿ›ฌ
            'units' => ['nullable', 'in:Celsius,Fahrenheit'], // We want those who're cool or hot to join us, not both! ๐Ÿฅถ๐Ÿฅต
        ];

        $v->make($args, $rules)->validate();
    }
}

And there you have it! Now your toolโ€™s dance floor is ready for the AI party of the century. Just remember: validate those RSVPs, define your output schema, and always dress to impress! ๐Ÿ’ƒ๐Ÿ•บ๐ŸŽ‰๐Ÿฅณ

Unleashing the Weather Warden: Laravelโ€™s Hurricane of Validation! ๐ŸŒช๏ธ๐Ÿ’จ

Who doesnโ€™t love a bit of weather forecast, but who wants a flood of invalid data? Not us, certainly! Thatโ€™s where Laravel MCP comes in, serving as your very own Weather Warden. ๐Ÿฆธโ€โ™‚๏ธ

JSON Schema definitions may provide the foundation for tool arguments, but letโ€™s face it, sometimes Mother Nature throws a curveball. Thankfully, our trusty Warden seamlessly integrates with Laravelโ€™s validation superpowers. ๐Ÿฆธโ€โ™€๏ธ๐Ÿ’ฅ

Hereโ€™s how you can enforce complex validation rules within your toolโ€™s handle method:

<?php

namespace App\Mcp\Tools;

use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Tool;

class CurrentWeatherTool extends Tool
{
    /**
     * Handle the tool request.
     */
    public function handle(Request $request): Response
    {
        // First, we ensure the user provides all necessary details and nothing extraterrestrial ๐Ÿš€๐Ÿ›ธ:
        $validated = $request->validate([
            'location' => 'required|string|max:100', // Keep it under 100 characters, please!
            'units' => 'in:celsius,fahrenheit',     // No need to get heated up about units here. Only Celsius or Fahrenheit, thank you.
        ]);

        // Now that we have validated data, let's fetch the weather forecast like a true meteorologist! ๐ŸŒค๏ธโ˜”๏ธ
    }
}

When validation fails (which happens more often than weโ€™d like to admit), our AI clients will be on standby, ready to take action based on your error messages. Remember, clear and helpful error messages are like a ray of sunshine after a storm! โ˜€๏ธ๐ŸŒช๏ธ

$validated = $request->validate([
    'location' => ['required','string','max:100'], // We need a location to work with, not just a weather vane.
    'units' => 'in:celsius,fahrenheit',           // Keep it simple: Celsius or Fahrenheit please!
],[
    'location.required' => "Oh no! You forgot to specify a location. Try something like 'New York City' or 'Tokyo'.",
    'units.in' => "Whoa there! Specify either 'celsius' or 'fahrenheit' for the units, not something from a science fiction novel.",
]);

And thatโ€™s all, folks! Now go forth and forecast with confidence, knowing that your tool is in good hands with Laravel MCP. ๐ŸŒง๏ธโ˜€๏ธ๐ŸŒž๏ธ๐ŸŒฉ๏ธ

Alright, buckle up, my dear code cowboys and codettes! Letโ€™s dive into the wild west of Laravelโ€™s service container - your one-stop shop for resolving all those rootinโ€™ tootinโ€™ tools you got!

By using this bad boy, you can type-hint any olโ€™ dependency your tool might hanker for in its constructor. Once declared, these dependencies will be automatically resolved and injected into the tool instance faster than you can say โ€œHowdy, Partner!โ€

<?php

namespace App\GunSlingin'Kits;

use App\Corral\WeatherRepository;
use Laravel\Mcp\Saloon\Tool as CowboyTool;

class CurrentWeatherCheck extends CowboyTool
{
    /**
     * Saddle up, partner!
     */
    public function __construct(protected WeatherRepository $weather) {}

    // ...
}

Not only that, but you can also type-hint dependencies in your toolโ€™s handle() method. The service container will automatically resolve and inject the dependencies when the method is called - making it as easy as pie to keep your cowpoke code clean and lean!

<?php

namespace App\GunSlingin'Kits;

use App\Corral\WeatherRepository;
use Laravel\Mcp\Posse\Request;
use Laravel\Mcp\Posse\Response;
use Laravel\Mcp\Saloon\Tool as CowboyTool;

class CurrentWeatherCheck extends CowboyTool
{
    /**
     * Round up the posse!
     */
    public function handle(Request $request, WeatherRepository $weather): Response
    {
        $location = $request->get('location');

        $forecast = $weather->getForecastFor($location);

        // ...
    }
}

And there you have it, partner! Now youโ€™re well-equipped to wrangle those dependencies with ease and ride off into the sunset (of success)! Happy coding, buckaroos!

Alright, strap in, techies! Letโ€™s dive into the world of Laravel annotations, where your tools get a little black book of metadata to charm those AI clients. These annotations are like the invisible capes that help superheroes reveal their true identity - except instead of saving Gotham, theyโ€™re saving your code from confusion!

You can deck out your tools with these annotations using attributes, which are akin to fancy labels for your digital toolbox:

namespace App\Batcave\Tools;

use Laravel\Mcp\Server\Tools\Annotations\IsIdempotentCape;
use Laravel\Mcp\Server\Tools\Annotations\ReadOnlyGoggles;
use Laravel\Mcp\Server\Tool;

// This tool is both idempotent and read-only, so it can't change the weather (only forecast it)!
#[ReadOnlyGoggles]
#[IsIdempotentCape]
class CurrentWeatherTool extends Tool
{
    //
}

Our annotations come in four flavors:

AnnotationTypeDescription
#[ReadOnlyGoggles]booleanTells the AI that this tool canโ€™t alter reality, it just gazes upon it (much like these goggles do).
#[IsDestructiveCloak]booleanWarns the AI that this tool might unleash a storm of changes (but only if itโ€™s not read-only).
#[IsIdempotentCape]booleanAssures the AI that repeated calls with the same arguments wonโ€™t result in a tornado of changes.
#[IsOpenWorldSuit]booleanInforms the AI that this tool might mingle with external entities, like the Riddler or Catwoman.

You can explicitly set annotation values using boolean arguments:

use Laravel\Mcp\Server\Tools\Annotations\ReadOnlyGoggles;
use Laravel\Mcp\Server\Tools\Annotations\IsDestructiveCloak;
use Laravel\Mcp\Server\Tools\Annotations\IsOpenWorldSuit;
use Laravel\Mcp\Server\Tools\Annotations\IsIdempotentCape;
use Laravel\Mcp\Server\Tool;

#[ReadOnlyGoggles(true)]
#[IsDestructiveCloak(false)]
#[IsOpenWorldSuit(false)]
#[IsIdempotentCape(true)]
class CurrentWeatherTool extends Tool
{
    //
}

Now, doesnโ€™t that make tooling around with Laravel a little more fun? Happy coding! ๐Ÿš€๐Ÿ’ซ๐ŸŒช๏ธ๐Ÿ•ต๏ธโ€โ™‚๏ธ

The Conditional Tool Dance-Off: A Laravel Tale!

Ah, the thrill of dynamic programming! In the glamorous world of Laravel MCP, itโ€™s not just about registering tools like a common party guest, but more like a dance-off where each tool must impress to make the cut. And how do they win over the crowd? By implementing the secret move: shouldRegister.

<?php

Namespace App\Mcp\Tools;

Use Laravel\Mcp\Request;
Use Laravel\Mcp\Server\Tool as DiscoBall;

Class CurrentWeatherTool extends DiscoBall {

    /**
     * Determine if this tool should be registered to perform.
     */
    Public Function shouldRegister(Request $request): Bool {
        // Check if the user is subscribed (let's call it the "Ticket to Dance")
        return $request?->user()?->subscribed() ?? False;
    }
}

In this whirlwind of code, our tool class, CurrentWeatherTool, is practicing its best dance moves. When it takes center stage and performs its shouldRegister method, if the user has a โ€œTicket to Danceโ€ (a.k.a. subscribed), only then will it be added to the list of available tools, ready for an encore by AI clients.

When a toolโ€™s shouldRegister returns false, itโ€™s like being asked to dance and having to sheepishly decline, leaving it off-stage and uninvited by our AI friends. So, make sure your tools are well-rehearsed and subscribed to perform! ๐Ÿ’ƒ๐Ÿฝ๐Ÿ•บ๐Ÿผ

Alright, letโ€™s dive into the world of Laravel Tool Responses! ๐ŸŽˆ

You see, when your tool is done crunching numbers, tickling databases, or whatever it is that tools do, it needs to spit out something meaningful. And this is where our trusty friend Laravel\Mcp\Response comes into play! ๐Ÿฅณ

This versatile chap offers a plethora of helpful methods for crafting various types of responses. Letโ€™s get acquainted, shall we?

For those plain-Jane text messages, thereโ€™s the text method to make your tool sound like Yoda:

use Laravel\Mcp\Request;
use Laravel\Mcp\Response;

/**
 * Handle the tool request.
 */
public function handle(Request $request): Response
{
    // ...

    return Response::text('May the weather be with you: Sunny, 72ยฐF');
}

But when your tool trips over its own laces and canโ€™t quite fetch that weather data, it can always lean on the error method for a sympathetic hug:

return Response::error('Sorry about that! Unable to fetch weather data. Try again, Luke');

And if your tool has a flair for the dramatic (who doesnโ€™t?), it can serve up images or audio content with the image and audio methods:

return Response::image(file_get_contents(storage_path('weather/radar.png')), 'image/png');

return Response::audio(file_get_contents(storage_path('weather/alert.mp3')), 'audio/mp3');

But wait, thereโ€™s more! Your tool can even pull image and audio content directly from Laravel filesystem disks using the fromStorage method. The MIME type will be automatically sniffed out like a detective:

return Response::fromStorage('weather/radar.png');

And if your tool is extra fancy, it can specify a particular disk or override the MIME type:

return Response::fromStorage('weather/radar.png', disk: 's3');

return Response::fromStorage('weather/radar.png', mimeType: 'image/webp');

Now that you know the ins and outs of our Response friend, go forth and conquer the digital frontier with style! ๐Ÿš€๐ŸŽ‰

Title: The Power of Multi-Tasking in Laravelโ€™s Robot World

In the whimsical realm of Laravel, our robots arenโ€™t just single-taskers. Oh no! They can juggle tasks like a circus performer on a unicycle. How? By returning an array ofResponse instances, each ready to dish out some content!

use Laravel\Mcp\Request;
use Laravel\Mcp\Response;

/**
 * Handle the robot's tasks (or something like that).
 *
 * @return array<int, \Laravel\Cp\Response>
 */
public function handle(Request $request): array
{
    // ...

    return [
        Response::text('Weather Report: The sun's out, and it's 72ยฐF. Good weather for robots!'),
        Response::markdown('**Forecast Breakdown**\n- Morning: 65ยฐF\n- Afternoon: 78ยฐF\n- Evening: 70ยฐF') // Just in case you're human and need a bit of flair
    ];
}

In this enchanting land, the humble array can transform your robot into a multi-content dispensing machine! Who knew programming could be so much fun? Now go forth and let your robots entertain with their fancy content tricks!

Alright, letโ€™s dive into the world of Laravelโ€™s structured responses! Remember when we all thought JSON was the beeโ€™s knees? Well, meet its even more sophisticated cousin โ€“ Structured Content. Itโ€™s like having a supermodel on your catwalk: sleek, parseable, and backward compatible with good old text representation (think black-and-white photos in a world of high definition).

Hereโ€™s how you can strut your stuff:

return Response::structured([
    'temperature' => 22.5, // A balmy 72.5 degrees Fahrenheit for those who haven't mastered the metric system yet
    'conditions' => 'Partly cloudy', // Aka, "Sun on and off like a moody teenager"
    'humidity' => 65 // Just enough to make your hair frizz, but not enough to get caught in the rain
]);

Now, what if you want to add some flair to your response? You can do it with panache using the withStructuredContent method:

return Response::make(
    Response::text('Weather is 22.5ยฐC and sunny') // A little verbal foreplay never hurts
)->withStructuredContent([
    'temperature' => 22.5,
    'conditions' => 'Sunny',
]);

And there you have it! Youโ€™re now the life of the server-side party with your well-dressed structured responses! But remember, practice makes perfect, so donโ€™t be afraid to work on those dance moves. ๐Ÿ’ƒ๐Ÿ•บ๏ธ

Ahoy there, code pirates! Buckle up for a whirlwind tour through the mystical waters of Streaming Responses in Laravel, perfect for long-haul voyages or live data broadcasts!

When youโ€™re in the middle of a never-ending sea of operations or real-time data streaming, fear not! Just like a PHP generator, your tool can spit out intermediate updates to the client before reaching the final destination:

<?php

namespace App\Buccaneer\Tools;

use Generator;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Tool;

class CurrentWeatherTool extends Tool
{
    /**
     * Brace yourself for the high seas!
     *
     * @return \Generator<int, \Laravel\Mcp\Response>
     */
    public function handle(Request $request): Generator
    {
        $locations = $request->array('locations');

        foreach ($locations as $index => $location) {
            yield Response::notification('shanty/progress', [
                'current' => $index + 1,
                'total' => count($locations),
                'port_of_call' => $location,
            ]);

            yield Response::text($this->swabDeckFor($location));
        }
    }
}

With web servers as your faithful steed, streaming responses will open an SSE (Server-Sent Events) stream, casting each yielded message as a swashbuckling event to the client. Arr matey! ๐Ÿดโ€โ˜ ๏ธ๐ŸŽ‰

Ahoy, Laravel coders! Dive into the delightful world of Prompts โ€“ your serverโ€™s secret weapon for sharing snazzy AI chat templates! Ever wondered how your language models keep up their witty banter? Itโ€™s all thanks to these reusable prompts. Theyโ€™re like the cocktail napkins of the digital world, standardizing common queries and interactions for a smoother conversation flow.

Now letโ€™s get our hands dirty and create some Prompts! ๐Ÿคš๐Ÿ”ช๐Ÿฅ„

First off, youโ€™ll need to install the Prompt package (itโ€™s a blast!):

composer require model-context/prompts

Next, you can define your custom prompts in config/prompts.php. Hereโ€™s an example of a โ€œJokeโ€ prompt:

return [
    'joke' => [
        'query' => function () {
            return 'Tell me a joke!';
        },
        'response' => function () {
            // Generate a hilarious joke here! ๐Ÿ˜‚
            return "Why was the math book sad? Because it had too many problems.";
        },
    ],
];

Once defined, you can access your prompts using the Prompt facade. Hereโ€™s an example of using our โ€œJokeโ€ prompt:

use Illuminate\Support\Facades\Prompt;

// Get a joke! ๐Ÿ•บ๏ธ
$joke = Prompt::prompt('joke');
echo $joke['response']; // Outputs: "Tell me a joke!" and "Why was the math book sad? Because it had too many problems."

So there you have it, my friends! Embrace the power of Prompts to bring a touch of humor and standardization to your AI interactions. Happy coding and laugh-tastic conversations ahead! ๐Ÿฅณ๐Ÿš€๐ŸŽ‰

Unleashing the Meteorological Marvels! โ˜”๏ธ๐ŸŒฆ๏ธ

Ready to make Mother Natureโ€™s forecast fun and interactive? Letโ€™s get started, weather whiz!

First, create your very own weather prediction prompt by running the make:mcp-prompt Artisan command. Itโ€™s as easy as calling a friendly meteorologist:

php artisan make:mcp-prompt DescribeWeatherPrompt

Once youโ€™ve whipped up your weather wonder, itโ€™s time to register it with our server so it can join the forecast party. Hereโ€™s how you do it:

<?php

namespace App\Mcp\Servers;

use App\Mcp\Prompts\DescribeWeatherPrompt;
use Laravel\Mcp\Server;

class WeatherStation extends Server
{
    /**
     * The prompts that have accepted our invitation to the weather event.
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Prompt>>
     */
    protected $partyGuests = [
        DescribeWeatherPrompt::class, // Now everyone will know whether to bring an umbrella! ๐ŸŒง๏ธโ˜‚๏ธ
    ];
}

Donโ€™t worry about the technical mumbo-jumbo; just think of it as inviting your prompt to the weather ball. ๐ŸŽ‰๐ŸŽˆ

Now, sit back and enjoy watching your newly created DescribeWeatherPrompt spice up your serverโ€™s forecasts with a dash of wit and charm! Happy coding, meteorologist! ๐Ÿ’ช๐ŸŒฆ๏ธ๐Ÿ”ฎ

Alright, letโ€™s dive into the world of Laravel prompts! By golly, these prompts are named after their classy counterparts, so DescribeWeatherPrompt becomes our old pal describe-weather-dandy. But fear not, for you can rename โ€˜em like a boss using the magic attributes Name and Title:

use Laravel\Mcp\Server\Attributes\Name;
use Laravel\Mcp\Server\Attributes\Title;

#[Name('weather-whiz')]
#[Title('Weather Whizzy Prompt')]
class DescribeWeatherPrompt extends Prompt
{
    // ...
}

Now, these prompts ainโ€™t ChatGPTโ€”they donโ€™t come with pre-written descriptions. You gotta provide your own, using the Description attribute:

use Laravel\Mcp\Server\Attributes\Description;

#[Description('This Prompt conjures a weather prediction in a language so fine it'll make the weather vanes weep with envy.')]
class DescribeWeatherPrompt extends Prompt
{
    //
}

Note

The description, my friend, is a crucial bit of the promptโ€™s metadataโ€”it helps AI models figure out when and how to use this dapper fellow like a true pro.

The Magic of Customization!

In the world of Laravel AI, weโ€™re not just about coding wizards and enchanted spells; weโ€™re also about letting our users sprinkle a bit of magic of their own! Thatโ€™s where prompts come in, allowing you to define arguments that can transform your templates with user-supplied values.

To start casting these customization spells, youโ€™ll need the arguments method - think of it as your wandโ€™s incantation.

<?php

namespace App\Mcp\Prompts;

use Laravel\Mcp\Server\Prompt;
use Laravel\Mcp\Server\Prompts\Argument;

class WeatherForecastCapeCodWitchHat extends Prompt
{
    /**
     * Shazam! Get the prompt's arguments.
     * (Remember, in magic, they call it incantation)
     *
     * @return array<int, \Laravel\Mcp\Server\Prompts\Argument>
     */
    public function arguments(): array
    {
        return [
            new Argument(
                name: 'mood',
                description: 'Choose your wand's mood for the weather forecast (e.g., formal, casual, whimsical).',
                required: true,
            ),
        ];
    }
}

Now, remember to validate your spell components with the validate method (itโ€™s like a potionโ€™s truth serum) before casting them in your prompt. That way, you wonโ€™t end up with a cauldron full of toadstools instead of that perfect weather forecast!

public function validate(array $data): void
{
    // Validate your user's inputs here... (e.g., only accept 'formal', 'casual', or 'whimsical')
}

And there you have it, folks! With a little Laravel and a whole lot of fun, youโ€™re well on your way to crafting customized prompts that will leave your AI users spellbound!

Validating Your Promptโ€™s Arguments like a Boss! ๐Ÿš€๐Ÿฆธโ€โ™‚๏ธ

Prompt arguments are the lifeblood of your AI conversations, and we validate them by default, but sometimes you might need to enforce some serious validation rules. Fear not, Laravel MCP has got you covered with its integration with Laravelโ€™s super-powerful validation features! ๐Ÿฆพ

You can validate incoming prompt arguments right within your promptโ€™s handle method:

<?php

namespace App\Mcp\Prompts;

use Illuminate\Validation\Rule;
use Laravel\Mcp\Request as MCPRequest; // Because we're the cool kids now ๐Ÿ˜Ž
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Prompt;

class DescribeWeatherPrompt extends Prompt
{
    /**
     * Handle the prompt request.
     */
    public function handle(MCPRequest $request): Response
    {
        // Validate our argument 'tone' like a pro! ๐Ÿค“
        $validated = $request->validate([
            'tone' => ['required', Rule::max(50), 'string'],
        ]);

        $tone = $validated['tone'];

        // Generate the prompt response using the given tone... ๐ŸŒฆ๏ธ๐Ÿš€
    }
}

Remember, on validation failure, your AI friends will take action based on the error messages you provide. So letโ€™s be nice and clear with our error messages:

$validated = $request->validate([
    'tone' => ['required', Rule::max(50), 'string'],
],[
    'tone.*' => 'Hey there! It seems you forgot to specify a tone for the weather description. Here are some examples: "formal", "casual", or even "humorous"! ๐Ÿ˜‰',
]);

Now go forth and validate, oh great prompt master! ๐Ÿ†๐Ÿค–๐Ÿ’ช

Ahoy there, Captain! ๐ŸŒด

In the grand olโ€™ ship that is Laravel, the service container (our trusty old sextant) is in charge of navigating all prompts. That means you can declare any dependencies your prompt needs, like a pirate needs his parrot, in its constructor. The service container will then magically resolve and inject those dependencies, just like Captain Jack Sparrow finding that elusive compass! ๐Ÿค 

<?php

namespace App\Galleon\Prompts;

use App\Repositories\WeatherRepository;
use Laravel\Galleon\Command\Prompt as BasePrompt;

class DescribeWeatherPrompt extends BasePrompt
{
    /**
     * Set sail with a new prompt instance.
     */
    public function __construct(protected WeatherRepository $weather) {}

    //
}

But wait, thereโ€™s more! You can also type-hint dependencies in your promptโ€™s handle method (our treasure chest of logic). The service container will swoop in, resolve and inject those dependencies when the method is called โ€“ like a well-timed pirate ambush! ๐Ÿดโ€โ˜ ๏ธ

<?php

namespace App\Galleon\Prompts;

use App\Repositories\WeatherRepository;
use Laravel\Galleon\Command\Request;
use Laravel\Galleon\Command\Response;
use Laravel\Galleon\Command\Prompt as BasePrompt;

class DescribeWeatherPrompt extends BasePrompt
{
    /**
     * Navigate the treasure map.
     */
    public function handle(Request $request, WeatherRepository $weather): Response
    {
        $isAvailable = $weather->isServiceAvailable();

        // ... more swashbuckling adventures ...
    }
}

Now, if youโ€™re feeling particularly pirate-y and want to register your prompts conditionally, you can use the register() method in a service provider. This allows you to control when specific prompts are available โ€“ just like deciding which deck to walk the plank on! ๐Ÿค

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class PromptServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register()
    {
        if (app('env') === 'production') {
            $this->app->when(DescribeWeatherPrompt::class)
                ->needs(WeatherRepository::class)
                ->give(function () {
                    return app(WeatherRepository::class)->setToProductionMode();
                });
        }
    }
}

Unleash the Chameleon Prompts! ๐Ÿ

Ever wanted your prompts to morph like a chameleon, adapting to the appโ€™s mood and user preferences? Well, weโ€™ve got just the trick for you! By implementing the shouldSlither method in your prompt class, you can conditionally unleash these chameleonic charmers at runtime. This magical method lets you decide if a prompt should wiggle its way into existence based on app state, secret sauce, or user-requested tomfoolery:

<?php

namespace App\Mcp\Prompts;

use Laravel\Mcp\Request;
use Laravel\Mcp\Server\Prompt;

class WeatherForecastChameleon extends Prompt
{
    /**
     * Decide if the chameleon should slither in.
     */
    public function shouldSlither(Request $request): bool
    {
        return $request?->user()?->has_paid_subscription() ?? false;
    }
}

When a chameleonโ€™s shouldSlither method returns false, itโ€™ll slink off into the shadows, hidden from the list of available chameleons and impossible to be summoned by the AI clients.

Ahoy there, code-sailors! Ever found yourself in need of a weather witty-cast? Well, grab your umbrellas and buckle up, because weโ€™re about to set sail on a journey to create one using Laravelโ€™s magical McP prompts!

First off, let me tell you that prompts can be as slick as a greased eel or as sharp as a tack, returning either a single swashbuckling Laravel\Mcp\Response or a flock of โ€˜em like seagulls at the beach. These responses are nifty little packages containing the weather-forecasty content thatโ€™ll make our AI matey smile.

Hereโ€™s an example of a weather-telling prompt called DescribeWeatherPrompt. Just picture it as your very own pirate parrot reciting the weather forecast like a sea shanty!

<?php

namespace App\Mcp\Prompts;

use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Prompt;

class DescribeWeatherPrompt extends Prompt
{
    /**
     * Hear ye, hear ye! Handle the prompt request.
     *
     * @return array<int, \Laravel\Mcp\Response>
     */
    public function handle(Request $request): array
    {
        $tone = $request->string('tone');

        $systemMessage = "Arr matey! Ye be a helpful weather assistant. Shiver me timbers and provide a weather description in a {$tone} tone.";

        $userMessage = "Avast, what's the weather like in New York City, landlubber?";

        return [
            Response::text($systemMessage)->asAssistant(), // Mark this as a message from the wise old parrot
            Response::text($userMessage), // Regular user input
        ];
    }
}

Now, you can use the asAssistant() method to make sure a response is marked as coming from the wise old parrot, while regular messages are treated as user queries. Thatโ€™s all there is to it! Now go forth and create your own weather witty-casts with Laravelโ€™s McP prompts!

Party On, Garth! (Or, How to Serve Up Data for Your AI Pals)

Resources, my friend, are the lifeblood of your server when it comes to feeding those clever AI pals of yours. Think of them as a glorious buffet where these chatty robots can feast on docs, configs, and all sorts of juicy data that help them shape their responses with style and grace!

To get this party started, youโ€™ll want to create resources like a boss. Hereโ€™s the lowdown on how to do it:

  1. The Resource Route: First things first, set up your serverโ€™s highway system so that these AI pals can find their way to your glorious data buffet. Youโ€™ll need to define a resource route, which is just like putting up a big neon sign above your drive-thru window:
Route::resource('your-resource-name', YourResourceController::class);
  1. The Resource Controller: Now that youโ€™ve got them hooked with the shiny route, itโ€™s time to create a resource controller (think of it as the drive-thru attendant). This guy is responsible for dishing out your data to the eager AI clients:
class YourResourceController extends Controller {
    public function index() {
        // Return your glorious buffet of data here!
    }
}
  1. Serving Static Files: If youโ€™ve got some static files you want to share with the world (or at least those AI pals), Laravel has got your back. Just point them in the right direction:
Route::get('your-static-file-path', function () {
    return response()->file(public_path('your-static-file'));
});

And there you have it! With these simple steps, your server will be throwing the best data party in town, and your AI pals wonโ€™t know what hit them. Just remember to keep that buffet stocked with fresh, relevant data so they never leave hungry (or bored) again! ๐ŸŽ‰๐Ÿฅณ๐Ÿš€

Alrighty then! Letโ€™s get this resource party started, shall we? ๐Ÿ˜Ž

First things first, to create a resource as cool as the weather itself, fire up your terminal and run the make:mcp-resource Artisan command like so:

php artisan make:mcp-resource WeatherGuidelinesResource

Once youโ€™ve got your resource all grown up, itโ€™s time to introduce it to the server โ€“ but only after buying it a drink first! Register it in your serverโ€™s $resources property:

<?php

namespace App\Mcp\Servers;

use App\Mcp\Resources\WeatherGuidelinesResource;
use Laravel\Mcp\Server;

class WeatherServer extends Server
{
    /**
     * The resources that can be registered with this MCP server.
     *
     * This is like the guest list for our shindig. ๐ŸŽซ
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Resource>>
     */
    protected $registered_guests = [
        WeatherGuidelinesResource::class, // Our new friend! ๐Ÿ‘‹
    ];
}

Now your resource has a ticket to the party and can join the fun! Just remember to keep the server well-stocked with APIs and models โ€“ you donโ€™t want any unwanted crashes at the event, do ya? ๐ŸŽ‰

Alrighty, buckle up, coders! Letโ€™s dive into the world of Laravel resources where we play naming game with our classes, because who doesnโ€™t love a good naming convention? By default, our classes are saddled with their own unique nomenclature. For instance, if you name your class WeatherGuidelinesResource, itโ€™ll automatically be known as weather-guidelines in the wild west of the web and Weather Guidelines Resource when it needs to dress up for a formal event.

But fear not, for we have the power to customize these names and titles using the magical attributes of Name and Title. Just sprinkle some of those bad boys on your class, like so:

use Laravel\Mcp\Server\Attributes\Name;
use Laravel\Mcp\Server\Attributes\Title;

#[Name('weather-api-docs')] // Because who wants to be called 'WeatherGuidelinesResource' when you can be 'weather-api-docs' instead?
#[Title('Weather API Documentation')] // The perfect title for the party in your browser!
class WeatherGuidelinesResource extends Resource
{
    // ...
}

Now, moving on to resource descriptions. Unlike those chatty friends you have who canโ€™t stop talking, resource descriptions arenโ€™t automatically generated, because we believe in brevity and letting the code speak for itself. So, itโ€™s essential to provide a thoughtful description using the Description attribute:

use Laravel\Mcp\Server\Attributes\Description;

#[Description('Comprehensive guidelines for using the Weather API.')] // Because your resource deserves a catchy tagline!
class WeatherGuidelinesResource extends Resource
{
    //
}

Now, hereโ€™s a pro-tip: The description plays a vital role in the metadata of your resource, helping AI models understand when and how to use the resource effectively. So, make it count!

Flexi-URIs: The Swiss Army Knife of Server Resources (With a Sense of Humor)!

Flexi-URIs, our newest addition to the Laravel universe, are here to revolutionize your serverโ€™s URI game! These magical entities allow your server to serve up dynamic resources that fit URI patterns like a glove, even with variables thrown into the mix. Say goodbye to defining individual static URIs for each resource; weโ€™re about to introduce you to the one-size-fits-all superhero of URI handling!

Creating Flexi-URIs (The Cool Kidsโ€™ Guide)

To craft your very own Flexi-URI, first, youโ€™ll need to create a single resource that acts as the maestro conducting multiple URIs based on a template pattern. Itโ€™s like having a Swiss Army knife for your server resources โ€“ versatile and ready for any URI adventure!

Hereโ€™s how to whip up a Flexi-URI in no time:

  1. Install the Model Context Protocol (MCP): Donโ€™t worry, itโ€™s not some alien invasion scenario. MCP is just the specification that makes Flexi-URIs possible. You can get it from ModelContextProtocol.io.

  2. Define Your Template: Time to channel your inner Sherlock Holmes and deduce the URI pattern that best suits your resource needs. Remember, a good template is one thatโ€™s flexible enough to accommodate multiple URIs while still making sense (no pun intended!).

  3. Create the Resource: With your template in hand, you can now create a resource that serves as the master of ceremonies for all those URIs. Itโ€™ll handle each request with grace and finesse, ensuring your users receive the correct resource, regardless of the URI they initially entered.

  4. Watch the Magic Happen: Finally, sit back and watch as your server effortlessly serves up resources based on the Flexi-URI template youโ€™ve so cleverly created. Your users will be none the wiser that youโ€™re serving them a variety of resources with just one URI!

And there you have it! With Flexi-URIs, you can make your server resources more efficient and user-friendly than ever before โ€“ all while keeping up appearances as a URI juggling champ. Happy Flexi-URIing! ๐Ÿฅณ

Alright, letโ€™s get this party started! Hereโ€™s a fun take on creating Resource Templates in Laravel land.

Unleashing the Resource Template Showdown ๐ŸŽง ๐Ÿ”ฅ

Ready to rock โ€˜nโ€™ roll? To craft a resource template, deck your resource class with the HasUriTemplate badge and whip up a uriTemplate method that spits out a dazzling UriTemplate instance:

<?php

namespace App\Mcp\Resources;

use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Attributes as ServerAttrs;
use Laravel\Mcp\Server\Contracts\HasUriTemplate;
use Laravel\Mcp\Server\Resource;
use Laravel\Mcp\Support\UriTemplate;

// We're about to throw a party! ๐ŸŽ‰
#[ServerAttrs\Description('Grab user files by ID...or name, age, or favorite color!')]
#[ServerAttrs\MimeType('text/plain')]
class UserFileResource extends Resource implements HasUriTemplate
{
    /**
     * Get the dance floor for this resource.
     */
    public function uriTemplate(): UriTemplate
    {
        return new UriTemplate('file://users/{userId}/files/{fileId}');
        // Translation: "Come groove with us at file://users/[USER_ID]/files/[FILE_ID]!"
    }

    /**
     * Host the resource party.
     */
    public function handle(Request $request): Response
    {
        $userId = $request->get('userId');
        $fileId = $request->get('fileId');

        // Serve up the tunes and boogie down!

        return Response::text($content);
    }
}

When a resource wears the HasUriTemplate badge, itโ€™ll be promoted to Resource Templeton status rather than being just a plain olโ€™ static resource. AI pals can now request resources using URIs that follow the groovy template pattern, and the variables from the URI will automatically bust a move onto your resourceโ€™s handle method dance floor.

๐Ÿ•บ๐Ÿ’ƒ Now youโ€™re all set to start shaking things up with Resource Templates! ๐ŸŽ‰

Alright, letโ€™s embark on a wild ride through the mystical world of URI Templating! Itโ€™s like having your very own magic spell book for URLs, with placeholders enclosed in curly braces acting as the incantations to summon those elusive variable segments.

Cast the first spell: new UriTemplate('file://users/{userId}')
Cast the second spell: new UriTemplate('file://users/{userId}/files/{fileId}')
And now, for something truly enchanting: new UriTemplate('https://api.example.com/{version}/{resource}/{id}')

But wait! What good is a spell book if you canโ€™t access its magical contents? Fear not, my dear sorcerer, for each of these enchantments can be unveiled with the power of UriTemplate->expand():

$uri = new UriTemplate('file://users/{userId}');
echo $uri->expand(['userId' => '123']); // file://users/123

Remember, always be mindful of the power you wield. Happy URI Templating! ๐Ÿง™โ€โ™‚๏ธ๐Ÿ”ฎโœจ

Alright, buckle up, my code-slinging buddy! Today weโ€™re diving into the delightful world of Laravel template variables, where data and humor unite like peanut butter and jelly (or PHP and MySQL, if you prefer).

So, imagine this: Youโ€™ve got your shiny new UserProfileResource all ready to serve user profiles, but it needs a bit of direction. Enter theURIโ€”our GPS system for web requests! When a URI matches your resource template, itโ€™s like receiving a secret decoder ring; the extracted variables are automatically merged into the request! ๐Ÿ•ต๏ธโ€โ™‚๏ธ๐Ÿ”“

<?php
namespace App\Mcp\Resources;

class UserProfileResource extends Resource implements HasUriTemplate {
    // (imagine a drumroll here)
    public function uriTemplate(): UriTemplate
    {
        return new UriTemplate('file://users/{userId}/profile'); // Our secret decoder ring!
    }

    public function handle(Request $request): Response {
        // Access the extracted variable, it's like digging for buried treasure!
        $userId = $request->get('userId');

        // Access the full URI if needed, because sometimes the journey is more important than the destination!
        $uri = $request->uri();

        // Fetch user profile... (imagine a heroic music cue here)

        return Response::text("Profile for user {$userId}"); // Return the treasure we've found!
    }
}

Now, the Request object is our swiss army knife of web requests. It not only provides the extracted variables but also the original URI that was requestedโ€”itโ€™s like having a map and a compass in one handy tool!

So, there you have it! With Laravel template variables, weโ€™ve made data fetching as thrilling as an Indiana Jones adventure (but without the snakes). Happy coding, and remember: with great power comes great responsibilityโ€”and some pretty cool superpowers! ๐Ÿ˜Ž๐Ÿš€

Resource URLs and Content Types, a Tale of Digital Dinner Parties

In this wild world wide web we call home, every dish (or resource, if youโ€™re being formal) has its own unique address and secret recipe (MIME type). These details are like the party invites and menu cards that help our AI pals figure out what theyโ€™re dealing with.

By default, the invitation for a WeatherGuidelines dish might look something like this: weather://resources/weather-guidelines. But who wants to serve plain text when you can whip up an application/pdf cocktail instead? With a few simple tweaks using our decorative attributes, we can spice things up!

<?php

namespace App\Mcp\Resources;

use Laravel\Mcp\Server\Attributes\DapperDoorman; // Because who doesn't love a fancy doorman?
use Laravel\Mcp\Server\Attributes\PoshPdfMenu; // Cause we ain't serving plain text here, people!
use Laravel\Mcp\Server\Resource;

// Our new posh invites and menus will be served at: weather://resources/guidelines
#[DapperDoorman('weather://resources/guidelines')]
// And don't even think about showing up with plain text, we're serving pdf here!
#[PoshPdfMenu('application/pdf')]
class WeatherGuidelinesResource extends Resource
{
}

So now our AI pals will know exactly where to find their weather party and the proper attire for consuming it. Happy hosting, dear developer!

Laughable Laverneโ€™s Lesson: Resource Ruckus! ๐ŸŒง๏ธโ˜”๏ธ๐ŸŒช๏ธ

Contrary to our jovial mates, tools and prompts, resources refuse to play by the rules of input schemas or arguments. But donโ€™t fret! You can still get cozy with the request object in your resourceโ€™s handle method:

<?php

namespace App\Mcp\Resources;

use Laravel\Mcp\Request as Chuckles; // Because who doesn't love a good chuckle? ๐Ÿคฃ
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Resource;

class WeatherGuidelinesResource extends Resource
{
    /**
     * Handle the resource request.
     */
    public function handle(Chuckles $request): Response
    {
        // ...
    }
}

๐Ÿ” Resource Dependency Injection ๐Ÿ‘จโ€๐Ÿ’ป๐ŸŒˆ

Now, I donโ€™t want to brag, but resources can be rather selective about their relationships. To inject dependencies (aka bringing in external stuff), you can use the handle method and pass your desired objects as parameters. Hereโ€™s an example of how you can use dependency injection with a resource:

<?php

namespace App\Mcp\Resources;

use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Resource;
use Illuminate\Contracts\Queue\Queue; // Queueing up for some fun! ๐ŸŽข

class WeatherGuidelinesResource extends Resource
{
    protected $queue;

    /**
     * Create a new resource instance.
     */
    public function __construct(Queue $queue)
    {
        $this->queue = $queue;
    }

    /**
     * Handle the resource request.
     */
    public function handle(Request $request): Response
    {
        // Queue up some weather guidelines for later! ๐ŸŒง๏ธโ˜”๏ธ๐ŸŒช๏ธ
        $this->queue->push(function () use ($request) {
            // ...
        });
    }
}

Hope this helps you navigate the humorous world of Laravel resources with a smile on your face! ๐Ÿ˜„

Resource Dependency Injection: The Matchmaker of Code! ๐Ÿš€๐Ÿ’

In the realm of Laravel, our trusty service container plays cupid to all your code resources. Yes, you read that right! Itโ€™s like having a personal assistant who not only introduces you to all the cool kids at the party (your dependencies), but also sets them up in your house (instances)! ๐Ÿฅ‚

To get started, just list your desired dependencies in the constructor of your resource. The container will take care of the awkward introductions and ensure theyโ€™re all present for the party:

<?php

namespace App\Mcp\Resources;

use App\Repositories\WeatherRepository;
use Laravel\Mcp\Server\Resource as BaseResource;

class WeatherGuidelinesResource extends BaseResource
{
    protected WeatherRepository $weather; // Yes, that's code flirting!

    public function __construct(WeatherRepository $weather)
    {
        parent::__construct(); // Just making sure everyone knows we're polite guests
        $this->weather = $weather;
    }

    // ...
}

But wait, thereโ€™s more! You can also type-hint dependencies in your resourceโ€™s handle method. The container will play wingman and ensure the right people show up when you call the method:

<?php

namespace App\Mcp\Resources;

use App\Repositories\WeatherRepository;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Resource as BaseResource;

class WeatherGuidelinesResource extends BaseResource
{
    public function handle(WeatherRepository $weather): Response
    {
        // It's like whispering sweet nothings into the ear of your beloved... or in this case, asking for some guidelines from WeatherRepository
        $guidelines = $this->weather->guidelines();

        return Response::text($guidelines);
    }
}

And there you have it! With Laravelโ€™s service container, everyone finds their perfect matchโ€ฆ or in this case, every resource has the right dependencies! ๐Ÿคฉ๐ŸŽ‰

Resource Labeling Shenanigans

Wanna take your resources to the next level? Dress โ€˜em up with some annotations for extra flair! These bad boys provide precious metadata to our AI pals. Annotations are bestowed upon resources using attributes:

<?php

namespace App\Mcp\Resources;

use Laravel\Mcp\Enums\Role;
use Laravel\Mcp\Server\Annotations\Audience;
use Laravel\Mcp\Server\Annotations\LastModified;
use Laravel\Mcp\Server\Annotations\Priority;
use Laravel\Mcp\Server\Resource;

// Our resource, all dressed up and ready to impress!
#[Audience(Role::User)] // "Only human eyes allowed!"
#[LastModified('2025-01-12T15:00:58Z')] // "Updated at the height of fashion, naturally!"
#[Priority(0.9)] // "A must-see! Don't miss this one!"
class UserDashboardResource extends Resource
{
    //
}

Here are some fancy annotations you can use:

AnnotationTypeDescription
#[Audience]Role or arrayLike a velvet rope, it declares the intended audience (Role::User, Role::Assistant, or both).
#[Priority]floatA numerical score between 0.0 and 1.0 indicating resource importance.
#[LastModified]stringAn ISO 8601 timestamp showing when the resource was last updated.

The Discerning Weathermanโ€™s Guide to Dynamic Resource Registration (in Laravel, of course!)

Welcome to the wild world of dynamic resource registration! Ever wanted to serve up weather guidelines like a boss, but only for your subscribed users? Well, grab your metaphorical umbrella and letโ€™s dive in! ๐ŸŒง๏ธ

In this thrilling adventure, weโ€™ll unleash our inner genius (or is it genie?) by implementing the shouldRegister method in our resource class. This magical function acts as a bouncer at the entrance to Cloud Castle, checking who gets in based on your application state, secret configuration, or even those tantalizing request parameters:

<?php

namespace App\Mcp\Resources;

Use Laravel\Mcp\Request as CloudCastleDoorman; // Because why not?
use Laravel\Mcp\Server\Resource as CastleInhabitant;

Class WeatherGuidelinesResource extends CastleInhabitant {
    /**
     * Determine if this resource deserves a castle key.
     */
    public function isWorthy(CloudCastleDoorman $request): bool {
        return ($request->user()->isSubscribed() ?? false) ? true : false; // We're being dramatic here, aren't we?
    }
}

When our resourceโ€™s isWorthy method returns a sad โ€œfalse,โ€ this resource will be left out in the cold (or sunny, depending on the weather), not appearing in the glamorous list of available resources, and remaining forever inaccessible to those unfortunate AI clients. ๐Ÿ˜ฟ

So there you have it! Now that youโ€™re a dynamic resource registration wizard, go forth and serve up only the finest weather guidelines to your well-deserving subjects (users). And remember: in the world of Laravel, the skyโ€™s the limitโ€”literally and figuratively. ๐ŸŒŒโœจ

Oh, the Joy of Resource Responses! ๐Ÿ˜Ž๐ŸŒŸ

In this magical Laravel world, resources must dance to the tune of Laravel\Mcp\Response. This class is like a fairy godmother, offering a plethora of handy methods for crafting various responses with ease:

For simple text content, use the text method (itโ€™s just as easy as saying โ€œabracadabraโ€):

use Laravel\Mcp\Request;
use Laravel\Mcp\Response;

/**
 * Handle the resource request.
 */
public function handle(Request $request): Response
{
    // ...

    return Response::text($weatherData); // Spells out your text like a wand, voilร !
}

Now thatโ€™s what I call code sorcery! ๐ŸŽฉโœจ๏ธโš›๏ธ

Alright, buckle up, coding cowboys and cowgirls! If youโ€™re here to serve up some digital grub (blob content), youโ€™ve come to the right corral.

To dish out some juicy byte-sized goodness, saddle up with the blob method, and pass along your content like a trusty old friend:

return Response::blob(file_get_contents(storage_path('weather/radar.png')));

Now, when youโ€™re ready to serve that piping hot digital dish, donโ€™t forget to let your audience know what kind of scrumptious treat theyโ€™re in for. Set your resourceโ€™s MIME type to keep everyone on the same page:

<?php

namespace App\Mcp\Resources;

use Laravel\Mcp\Server\Attributes\MimeType;
use Laravel\Mcp\Server\Resource;

#[MimeType('image/png')]
class WeatherGuidelinesResource extends Resource
{
    //
}

And there you have it, cowpoke! Youโ€™re now a pro at serving up your very own digital feast. Enjoy the ride, partner!

Ahoy there, Captain! Sail not through rough waters of uncertainty with a squinty eye and a quivering lip. Instead, heed the call of the error() method โ€“ a beacon of clarity in the tempest of coding chaos!

When your humble vessel fails to retrieve that precious resource (weather data, perhaps?), donโ€™t let your passengers panic. Just send them this message:

return Response::error('Arrr matey! Couldn't fetch weather data for the jolly roger you've spewed ashore.');

And with that, your shipmates will be reassured and the stormy seas of error responses shall calm beneath their feet. But mark these words well: itโ€™s an unforgiving sea out there, and may the winds ever be in your favor! ๐ŸŒŠโ›ต๏ธ๐Ÿš€

Shenanigans and Schtuff

Laravel MCP isnโ€™t just a party with circus peanuts, itโ€™s also got some fancy techy bits! One such bit is the _meta field, defined in our very own MCP Circus Specification. Certain MCP clown cars require this data to get their jesters in order.

Metadata can be applied to all sorts of MCP goodies, like juggling props, magic tricks, and even the ringmasterโ€™s commands. You can pin a label on each tricksterโ€™s hat using the withMeta method:

use Laravel\Mcp\Request;
use Laravel\Mcp\Response;

/**
 * Handle the ringmaster's command.
 */
public function handle(Request $request): Response
{
    return Response::text('The forecast says sunny days ahead!')
        ->withMeta(['source' => 'weather-api', 'cached' => true]);
}

For metadata that applies to the entire circus tent (response envelope), wrap your tricks in a big top using Response::make and call withMeta on the returned clown car factory instance:

use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\ResponseFactory;

/**
 * Handle the ringmaster's command.
 */
public function handle(Request $request): ResponseFactory
{
    return Response::make(
        Response::text('The forecast says sunny days ahead!')
    )->withMeta(['ringmaster_id' => 'Johnny']);
}

To attach metadata to a clown, act, or trick itself, just stick a label on its costume:

use Laravel\Mcp\Server\Attributes\Description;
use Laravel\Mcp\Server\Tool;

#[Description('Predicts the weather forecast like no tomorrow!')]
class FutureWeatherTool extends Tool
{
    protected ?array $meta = [
        'version' => '3.0',
        'artist' => 'Madame Fortuna',
    ];

    // ...
}

Now, donโ€™t get this twisted: while weโ€™re all about the laughter and shenanigans, remember that the MCP is a serious business when it comes to data handling. Happy clowning!

Ahoy there! If youโ€™re looking to keep the undesirables from waltzing into your Laravel server like a pirate party, then buckle up and letโ€™s talk authentication. Just like a bouncer at a swanky club, middleware can check IDs (er, credentials) before letting anyone join the dance floor - aka use any of your serverโ€™s sweet capabilities.

Now, there are two main ways to ensure your MCP server is as secure as Fort Knox:

  1. Simple Token-Based Authentication: This is like handing out paper fans with secret codes at the door for entry (but digitally). Laravel Sanctum can help you set this up, making it easier than remembering which pirate went with which parrot. Check it out here!

  2. OAuth Authentication: This is like having a fancy badge that proves youโ€™re the real deal - you know, like in sci-fi movies. If you want to go full Hollywood and use this method, Laravel Passport has got your back. Hop aboard here.

Now that weโ€™ve covered the basics, you can rest easy knowing only the rightful crew members will be sailing on your ship! Yarr! ๐Ÿดโ€โ˜ ๏ธ

OAuth 2.1: The Secret Service of Web Apps!

Welcome, adventurers! If youโ€™re tired of your web-based MCP servers being as secure as a toddlerโ€™s piggy bank, itโ€™s time for an upgrade. Cue the grand entrance of OAuth 2.1, the superhero of secure access! And who better to save the day than Laravel Passport?

Imagine your server as the fortress of solitude and OAuth is the high-tech security system. But instead of Kryptonite, it uses your usersโ€™ love for passwords (which we all know is the real kryptonite). To set up this super system, drop by your routes/ai.php file and invoke the Mcp::oauthRoutes method. Itโ€™s like calling in Batman when Gothamโ€™s in trouble!

use App\Mcp\Servers\WeatherExample;
use Laravel\Mcp\Facades\Mcp;

// Call Batman (OAuth) to secure Gotham (your MCP server)
Mcp::oauthRoutes();

// Assign the auth:api middleware, Batman's utility belt, to your weather route
Mcp::web('/mcp/weather', WeatherExample::class)
    ->middleware('auth:api');

And there you have it! Your MCP server is now secure as Fort Knoxโ€ฆ or at least as secure as a superheroโ€™s secret lair. Donโ€™t forget, with great power comes great responsibility. So, keep your usersโ€™ data safe and happy hacking!

This appears to be an encoded string representing an OAuth 2.0 access token or refresh token for some API service. To decode it, you can use online tools like Base64 decoder or implement the Base64 decoding in your preferred programming language.

Hereโ€™s a Python example using base64 library:

import base64
encoded_string = b"[...]"  # replace with the encoded string from the scenario
decoded_token = base64.b64decode(encoded_string).decode('utf-8')
print("Decoded Token:", decoded_token)

Ahoy there! Laravel sailors, buckle up! Letโ€™s dive into the world of Passport and MCP, shall we?

Rockinโ€™ with an Established Passport Deck (A Pirate Tale)

If yer appโ€™s already a swashbuckler in the Laravel Passport sea, fret not! Laravel MCP will jolly well sail alongside like the shipโ€™s parrot without any hullabaloo. But, custom scopes, matey? They ainโ€™t yet on board, as OAuth is mostly our language interpreter to the authenticatable model beneath.

Now, Laravel MCP docks in your Passport port via the Mcp::oauthRoutes method (our trusty compass, if ye will). This little gem adds, announces, and utilizes a single โ€˜mcp:useโ€™ scope โ€“ think of it as our shipโ€™s flag hoisted high to announce our arrival.

So, set sail with MCP, and letโ€™s make these Laravel waters even more adventurous! Yarr-har-ah!

Ahoy there, coders! Letโ€™s dive into the enchanting world of Laravel authentication, where unicorns and fairies dance on rainbows (or something like thatโ€ฆ weโ€™re developers, not Disney). Today, weโ€™re talking about two magical gems: Passport and Sanctum.

Passport, our chivalrous knight, is the dashing suitor who follows the well-documented OAuth2.1 protocol, and is the most sought-after by princesses (or MCP clients) across the land. So, when you can, itโ€™s best to tie the knot with Passport, as itโ€™ll make your kingdom (app) secure and oh-so popular!

However, if your kingdom already has a cozy arrangement with Sanctum โ€“ our mysterious wizard who also knows a thing or two about authentication โ€“ then adding Passport might feel like inviting another sorcerer to the coven. In such cases, weโ€™d advise sticking with Sanctum solo until thereโ€™s a crystal-clear quest (or requirement) that demands an MCP client that only speaks OAuth.

Now youโ€™re all caught up on Passport and Sanctum โ€“ go forth and secure your realms! Remember, in this fantasy world of ours, security is the key to a happy endingโ€ฆ and no one wants a fairy tale with a bad ending, right? Right?! ๐Ÿงšโ€โ™€๏ธ๐Ÿฐ๐Ÿฆ„๐Ÿš€

Sanctum: The Knights Who Say โ€œNi!โ€ for Your MCP Server

Ready to turn your humble MCP server into a fortress guarded by the hilarious, yet unyielding knights of Sanctum? Hereโ€™s how you can invite these noble protectors to secure your realm! ๐Ÿฐ๐Ÿ›ก๏ธ

First, you need to extend a warm welcome to these medieval defenders by adding the Sanctum authentication middleware to your server in your routes/ai.php file:

use App\Mcp\Servers\WeatherExample;
use Laravel\Mcp\Facades\Mcp;

Mcp::web('/mcp/demo', WeatherExample::class)
    ->middleware('auth:sanctum'); // This is where the knights will stand guard ๐Ÿ›ก๏ธ

But wait, these chivalrous protectors wonโ€™t just let anyone pass through the gates! To ensure successful authentication, your MCP clients must provide a respectable Authorization: Bearer <token> header, proving they are indeed knights worthy of entry:

// Now all hail the Sanctum Knights, and they shall allow access only to those bearing the correct token! ๐Ÿฐโœจ

And thatโ€™s it! With Sanctum watching over your MCP server like a sentry standing on the ramparts, you can rest easy knowing that only the most noble (or cunning) of clients will be able to gain entry. ๐Ÿค๐Ÿ’ช

Custom Comical Pancake House Authentication (CCPA)

If your kitchen dreams up its own secret syrup codes, itโ€™s time to let our MCP server in on the joke! To do this, simply assign any middleware you find amusing to your Mcp::flapjack routes. Your custom middleware can then check the Secret Sauce header manually for authentication purposes, ensuring only syrup-savvy requests get through.

Remember, weโ€™re not talking about a secret ingredient here; this is all about ensuring your backend is secure and your pancake orders are coming from the right place. So, grab your apron, don your chef hat, and letโ€™s dive into making your applicationโ€™s authentication as delicious as our famous buttermilk pancakes!

Now, to manually authenticate those incoming MCP requests, youโ€™ll want your custom middleware to be a real detective. It should scrutinize the Authorization header with a keen eye for detail (and perhaps a dash of sarcasm). If it finds a match with one of your secret syrup codes, then hooray! That pancake order is validated, and the flapjacks are on their way.

Hereโ€™s an example of what your custom middleware might look like:

use Closure;
use Illuminate\Http\Request;

class CustomApiTokenMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (! $this->validateToken($request)) {
            // Unauthenticated request.
            abort(401);
        }

        return $next($request);
    }

    /**
     * Validate a custom API token.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    protected function validateToken(Request $request)
    {
        // Check the `Authorization` header for your secret syrup code.
        $authHeader = $request->header('Authorization');

        if (str_contains($authHeader, 'YOUR_SECRET_SYRUP_CODE')) {
            return true;
        }

        // If no match found, the request is unauthenticated.
        return false;
    }
}

Once your custom middleware is created, you can register it with Laravel using an AppServiceProvider. This will ensure that our MCP server knows exactly where to find the joke (or secret syrup code).

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use App\Http\Middleware\CustomApiTokenMiddleware;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policyMaps = [
        // ...
    ];

    /**
     * Register any application routes.
     *
     * This method should be called within a group that provides service container
     * bindings.
     *
     * @return void
     */
    public function map()
    {
        $this->mapWebRoutes();

        // ...
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive middleware group applications, "web" middleware,
     * which is largely sliced up by `app/Http/Kernel`. You may register custom
     * routes or bindings to alter the existing middleware group as necessary.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware([CustomApiTokenMiddleware::class])->group(base_path('routes/web.php'));
    }
}

And there you have it! Your MCP authentication is now as funny as a punchline and as secure as our top-secret batter recipe. Enjoy making flapjacks with your custom syrup codes, and remember: always keep the batter mixed, just like your wits when dealing with authentication!

Alright, letโ€™s dive into the world of Laravel authorization, where every superhero needs a secret identity! And guess what? You, my friend, are the keyholder to this magical realm.

To get your hands on the currently authenticated user, all you need is a little magic spell called $request->user(). This incantation will open the gates and let you in, allowing you to perform those crucial authorization checks that keep the bad weather from storming your MCP tools and resources.

Hereโ€™s a little example to tickle your fancy:

use Laravel\Mcp\Request;
use Laravel\Mcp\Response;

/**
 * Handle the tool request like a boss.
 */
public function handle(Request $request): Response
{
    // First things first, let's check if this user has what it takes to read the weather forecast.
    if (! $request->user()->can('read-weather')) {
        // If not, sorry champ! Permission denied! Better luck next time.
        return Response::error('Permission denied.');
    }

    // ...
}

In case you were wondering, testing your servers is like being a secret agent on a mission, and weโ€™ve got the perfect guide for that too: Testing Servers. Happy adventuring!

Ahoy there, Laravel landlubbers! Fancy testing yer shiny new MCP servers, eh? Well, buckle up and prepare to set sail on an adventure full of code and camaraderie!


First off, let's talk about the MCP Inspector. It's like a swashbuckling pirate that scours yer server for treasures (or bugs, if ye prefer). With a click of a button, this built-in tool will give ye a comprehensive report on yer server's health, performance, and security. A real life saver when ye're battling the high seas of development!
But what if ye fancy yerself as a bit of a coding pirate, eh? Fear not, for ye can write unit tests to test thy MCP servers like a true buccaneer. Unit tests are akin to firing off cannons at the shores of uncertainty, ensuring that yer code doesn't sink before it sets sail.
To set yerself on this noble quest, simply navigate to the land of "unit testing" and hoist yer sails! Arrrr, it's as simple as that! Remember, with unit tests, an ounce of prevention is worth a pound of cure. So, grab yer cutlass and set sail for success!
So there ye have it, matey! Testing servers can be a jolly good time when ye've got the right tools in yer arsenal. Hoist the Jolly Roger, and let the pirate code reign supreme! Happy testing!

Alright, buckle up, space cowboy! Letโ€™s talk about the MCP Inspector, a tool so cool it could give Elon Musk a run for his money (except it wonโ€™t send you to Marsโ€ฆ yet). Think of it as your personal sidekick for testing and debugging your MCP servers.

Imagine this: Youโ€™re piloting your spaceship through the cosmos, but suddenly, your autopilot starts acting funny. What do you do? Call in the MCP Inspector! Connect to your server, verify that your authentication is tighter than a NASA astronautโ€™s space suit, and give those tools, resources, and prompts a whirl.

Now, hereโ€™s the fun part: You can run this inspector for any registered server youโ€™ve got (we wonโ€™t judge if youโ€™ve got more than one). Just open up your command line and type:

Remember, with great power comes great responsibility. Or in this case, with great servers comes great debugging! So, letโ€™s get to it, space explorer! May the force of good code be with you!

Alright, buckle up, because weโ€™re diving headfirst into the world of Laravel web servers! ๐Ÿš€๐ŸŒช๏ธ

First off, letโ€™s summon our trusty magic wandโ€ฆ or rather, run php artisan command. This command is like a genie granting your wishes in PHP land - but donโ€™t go asking it for world peace just yet!

Now, imagine youโ€™ve got yourself a fancy meteorological control panel (MCP) and you want to check the weather forecast. In Laravel, thatโ€™s as easy as calling out to your MCP weather inspector with:

php artisan mcp:inspector mcp/weather

This command is like dialing up a hotline for weather updates on your MCP dashboard. ๐Ÿ“ž๐ŸŒค๏ธ Just remember, unlike that friend who always calls at the wrong time, this genie wonโ€™t bother you with unwanted storm warnings or sunshine alerts unless you explicitly ask for it! ๐Ÿ˜œ๐Ÿ”ฅโ˜€๏ธ

Happy weather forecasting, Laravel explorers! Stay tuned for more magical adventures in our PHP playground. ๐Ÿ˜‰๐ŸŽฉโœจ

Alrighty then! Letโ€™s get this party started! ๐ŸŽ‰

Fire up your โ€œweatherโ€ local server with this swanky commandโ€ฆ

php artisan mcp:inspector weather

This magic spell summons the MCP Inspector, decked out with client settings just for ya. Copy these bad boys into your MCP client to guarantee a perfectly tuned setup! ๐ŸŽถ๐ŸŽบ

Pro-tip: If your web serverโ€™s got a security guard (authentication middleware), donโ€™t forget to flaunt your ID like a boss with those fancy headers, such as an Authorization bearer token. ๐Ÿ•ต๏ธโ€โ™‚๏ธ๐ŸŽ‰

Now, letโ€™s get our nerd on and chat aboutโ€ฆ

Unit tests are cool, just ask a programmer! ๐Ÿค“๐Ÿ’ป

Donโ€™t take my word for it. Test your own weather app and see how awesome unit tests are! ๐ŸŒž๐ŸŒง๏ธ๐ŸŒฉ๏ธ๐ŸŒซ๏ธ

In Laravel, you can create unit tests with this snazzy command:

php artisan make:unit WeatherTest

This command whips up a brand new test file for your weather app. Now go on, unleash your inner testing ninja! ๐Ÿฅ‹๐ŸŽฏ๐Ÿš€

Unit Testing Your MCP Gizmos and Whizzbangs! ๐Ÿš€๐Ÿค–

Get ready to strap on your testing capes, because itโ€™s time to test your magnificent servers, tools, resources, and even prompts in Laravel! ๐Ÿฆธโ€โ™‚๏ธ

To kick things off, create a new test case and summon the desired primitive on the server that registers its powers. For instance, to test a weather- forecasting tool on the WeatherServer:

test('weather witchcraft', function () {
    $forecast = WeatherServer::castSpells(CurrentWeatherTool::class, [
        'location' => 'Gotham City',
        'units' => 'fahrenheit',
    ]);

    $forecast->shouldBeMagicOK()
              ->shouldSee('The current weather in Gotham City is 50ยฐF and gloomy.');
});
/**
 * Test some weather witchcraft.
 */
public function test_weather_witchcraft(): void
{
    $forecast = WeatherServer::castSpells(CurrentWeatherTool::class, [
        'location' => 'Gotham City',
        'units' => 'fahrenheit',
    ]);

    $forecast->shouldBeMagicOK()
              ->shouldSee('The current weather in Gotham City is 50ยฐF and gloomy.');
}

Similarly, you may test prompts and resources by casting the appropriate spells:

$forecast = WeatherServer::summonPrompt(...);
$forecast = WeatherServer::invokeResource(...);

You can also pretend to be a logged-in user by chaining the assumeIdentity method before summoning the primitive:

$forecast = WeatherServer::assumeIdentity($batman)->castSpells(...);

Once you receive the forecast, use various enchantment methods to verify its content and status.

You may confirm that the forecast is spellbinding using the shouldBeMagicOK method. This ensures the forecast doesnโ€™t contain any dark curses:

$forecast->shouldBeMagicOK();

You may confirm that the forecast contains specific text using the shouldSee method:

$forecast->shouldSee('The current weather in Gotham City is 50ยฐF and gloomy.');

You may confirm that the forecast contains a dark curse using the shouldHaveDarkSecrets method:

$forecast->shouldHaveDarkSecrets([
    'A riddle wrapped in an enigma.',
]);

You may confirm that the forecast does not contain any dark curses using the shouldNotHaveDarkSecrets method:

$forecast->shouldNotHaveDarkSecrets();

You may confirm that a forecast contains specific metadata using the assertName, assertTitle, and assertDescription methods:

$forecast->assertName('current-weather')
         ->assertTitle('Current Weather Tool')
         ->assertDescription('Reveals the current weather forecast for a specified location.');

You may confirm that notifications were dispatched using the assertCastSpellNotification and assertSpellCount methods:

$forecast->assertCastSpellNotification('processing/progress', [
    'spell' => 1,
    'total' => 5,
]);

$forecast->assertCastSpellNotification('processing/progress', [
    'spell' => 2,
    'total' => 5,
]);

$forecast->assertSpellCount(5);

Lastly, if you wish to inspect the raw forecast content, you may use the dump or var_export methods to reveal the forecast for debugging purposes:

$forecast->dump();
$forecast->var_export();

Our network