Part 2: Full Demo – AI Prompts Across Two Laravel Apps Using MCP

Goal:

User types a prompt in Client App, AI interprets it, sends a structured command to MCP Server App, which executes backend logic and returns JSON. The AI then presents a user-friendly response back in the client.

Step 1: Setup the MCP Server App (App 1)

1️⃣ Install Laravel MCP

composer require laravel/mcp
php artisan vendor:publish --tag=mcp-config

2️⃣ Create Service for Business Logic

// app/Services/OrderService.php
namespace App\Services;use App\Models\Order;class OrderService
{
    public function create(int $userId, int $productId)
    {
        return Order::create([
            'user_id' => $userId,
            'product_id' => $productId,
        ]);
    }
}

3️⃣ Create MCP Tool

// app/Mcp/Tools/CreateOrder.php
namespace App\Mcp\Tools;use Laravel\Mcp\Tool;
use App\Services\OrderService;class CreateOrder extends Tool
{
    public string $name = 'createOrder';
    public string $description = 'Create a new order';    public function handle(array $arguments): array
    {
        $service = app(OrderService::class);
        $order = $service->create(
            $arguments['user_id'],
            $arguments['product_id']
        );        return [
            'id' => $order->id,
            'status' => 'created'
        ];
    }
}

4️⃣ Register Tool

// config/mcp.php
'tools' => [
App\Mcp\Tools\CreateOrder::class,
],

5️⃣ Start MCP Server

php artisan mcp:serve

MCP server is now running and ready to receive structured JSON commands.


Step 2: Setup the Client App (App 2)

1️⃣ Create a Form to Send Prompts

<!-- resources/views/prompt.blade.php -->
<form method="POST" action="{{ route('sendPrompt') }}">
@csrf
<input type="text" name="prompt" placeholder="Type your command">
<button type="submit">Send</button>
</form>
@if(session('response'))
<p><strong>AI Response:</strong> {{ session('response') }}</p>
@endif

2️⃣ Route

// routes/web.php
Route::view('/prompt', 'prompt');
Route::post('/send-prompt', [App\Http\Controllers\PromptController::class, 'send'])->name('sendPrompt');

3️⃣ Controller to Send Prompt to MCP Server

// app/Http/Controllers/PromptController.php
namespace App\Http\Controllers;use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;class PromptController extends Controller
{
public function send(Request $request)
{
$userPrompt = $request->input('prompt'); // Simulate AI deciding which tool to call
// In real case, you could integrate OpenAI function calling
$toolCall = [
'tool' => 'createOrder',
'arguments' => [
'user_id' => 5,
'product_id' => 10,
]
]; // Call MCP server API
$response = Http::post('http://mcp-server-app.test/mcp', $toolCall)->json(); // Convert structured response to user-friendly feedback
$userFriendly = "Order #{$response['id']} has been {$response['status']}."; return back()->with('response', $userFriendly);
}
}

Here, we’re simulating AI interpretation. In real integration, the AI would generate $toolCall automatically from the user prompt.


Step 3: Test the Flow

  1. Open Client App at /prompt.
  2. Type: “Create a new order for John with product ID 10”
  3. Click Send.
  4. Client app sends prompt → AI interprets → MCP server executes → Returns structured JSON → AI converts → Display to user:
AI Response: Order #123 has been created.

Diagram of Flow

[User Prompt in Client App] 
|
v
[AI interprets prompt → generates tool call JSON]
|
v
[MCP Server App (Laravel)]
|
| Executes Tool -> Calls Service -> Updates DB
v
[Structured JSON Response]
|
v
[AI converts response to friendly message]
|
v
[Displayed back to user in Client App]

Key Points

  1. Client App doesn’t need backend logic; MCP server handles it.
  2. MCP Tools are reusable for multiple apps.
  3. JSON structured calls make AI integration safe and predictable.
  4. You can integrate real AI (OpenAI, Claude, etc.) instead of hardcoded tool selection.

✅ This setup gives you a working end-to-end MCP integration between two Laravel apps, showing how AI prompts can trigger real database actions and return friendly responses.