
A Laravel app already has what WebMCP tools need: routes, validation, policies and a session. You only add a small script to the page. New to the topic? Start with what WebMCP is.
Step 1: A small JSON route
Use a route in routes/web.php, so the visitor’s session applies, and add a rate limit:
Route::get('/agent/products', function (Illuminate\Http\Request $request) {
$data = $request->validate(['q' => 'required|string|max:80']);
return App\Models\Product::query()
->where('published', true)
->where('name', 'like', '%'.$data['q'].'%')
->limit(5)
->get(['name', 'price', 'slug']);
})->middleware('throttle:30,1');Select only the columns a customer may see. Never return a whole model.
Step 2: Register the tool in Blade
Add a script to your layout, or push it only to the pages that need it:
@push('scripts')
<script>
const mc = document.modelContext || navigator.modelContext;
if (mc && mc.registerTool) {
mc.registerTool({
name: 'search_products',
description: 'Search published products. Returns name, price and slug.',
inputSchema: {
type: 'object',
properties: { q: { type: 'string', description: 'Keywords' } },
required: ['q'],
},
annotations: { readOnlyHint: true },
execute: async ({ q }) => {
const res = await fetch('/agent/products?q=' + encodeURIComponent(q));
return await res.text();
},
});
}
</script>
@endpushStep 3: Blade forms as tools
Add toolname and tooldescription to a form you already have. Labelled inputs become the tool’s inputs. Keep @csrf in place. The browser fills in the form and, by default, the visitor presses the button.
Step 4: Sensitive actions
For a POST tool, send the CSRF token in the X-CSRF-TOKEN header, and authorize the action with a policy as usual. Mark the tool with consequentialHint: true.
Safety rules
- Start read-only. Search and details first; mark them with
readOnlyHint. - Ask before acting. Mark cart, booking and form actions with
consequentialHint, so the visitor confirms. - Check on the server. The browser is not a security boundary, so validate every input and permission in your backend.
- Return little. Send only what a customer may see, never admin data or other customers’ records.
Test it
- Use a recent Chrome with WebMCP turned on, through the origin trial or the testing flag.
- Call each tool with good and bad inputs.
- Check that normal visitors see no change.
- Run the free WebMCP checker on the exact page where your tools live.
Want it done for you?
We add WebMCP to Laravel and PHP apps. See our WebMCP integration services, or check your current PHP version with our free PHP version checker.
Frequently asked questions
Do I need a Laravel package for WebMCP?
No. WebMCP is a browser API, so a small script and a normal route are enough. A package only saves a little code.
How does authentication work with WebMCP in Laravel?
Tools run in the page, so same-origin requests carry the visitor’s session cookie. Your auth middleware and policies work as usual.
What about CSRF for POST tools?
Send the CSRF token from the page’s meta tag in the X-CSRF-TOKEN header, as you would for any JavaScript request.
Does this work with Livewire or Inertia?
Yes. Load the script in your main layout, or register tools in a Vue or React component when you use Inertia.
How we can help
- WebMCP Integration ServicesWebMCP development for any stack, from WordPress and Shopify to Next.js, Laravel and Django: search, FAQs and forms offered as secure tools for AI browser agents, with a free audit.
- PHP to Next.js & FastAPI MigrationMigration of legacy PHP, Laravel, CodeIgniter and WordPress apps to Next.js and Python FastAPI, with a fixed quote, URLs, SEO and data kept and a zero-downtime switch.
- Custom Web Application DevelopmentFull-stack custom web application development for SaaS, internal tools, dashboards, and enterprise portals across React, Next.js, Node, Laravel, and Django.
Talk to an engineer about your project
Tell us what you are building. We reply within one business day with a candid view on scope, approach and effort.
Book a free strategy callWritten by the UnlockLive IT engineering team. UnlockLive IT Limited works with clients through its Los Angeles headquarters and Toronto office and delivers engineering from its Dhaka delivery centre. About us