Laravel MCP
Introduction
Laravel MCP は、AI クライアントが Model Context Protocol を通じて Laravel アプリケーションと対話するためのシンプルかつエレガントな方法を提供します。サーバー、ツール、リソース、プロンプトを定義するための表現力豊かで流暢なインターフェイスを提供し、AI を活用したアプリケーションとの対話を可能にします。
Installation
まず、Composer パッケージ マネージャーを使用して Laravel MCP をプロジェクトにインストールします。
composer require laravel/mcp
Publishing Routes
Laravel MCP をインストールした後、vendor:publish Artisan コマンドを実行して、MCP サーバーを定義する routes/ai.php ファイルを公開します。
php artisan vendor:publish --tag=ai-routes
このコマンドは、アプリケーションの routes ディレクトリに routes/ai.php ファイルを作成します。このファイルは、MCP サーバーの登録に使用されます。
Creating Servers
make:mcp-server Artisan コマンドを使用して MCP サーバーを作成できます。サーバーは、ツール、リソース、プロンプトなどの MCP 機能を AI クライアントに公開する中央の通信ポイントとして機能します。
php artisan make:mcp-server WeatherServer
このコマンドは、app/Mcp/Servers ディレクトリに新しいサーバー クラスを作成します。生成されたサーバークラスは、Laravel MCP の基本 Laravel\Mcp\Server クラスを拡張し、ツール、リソース、およびプロンプトを登録するためのプロパティを提供します。
<?php
namespace App\Mcp\Servers;
use Laravel\Mcp\Server;
class WeatherServer extends Server
{
/**
* The tools registered with this MCP server.
*
* @var array<int, class-string<\Laravel\Mcp\Server\Tool>>
*/
protected array $tools = [
// ExampleTool::class,
];
/**
* The resources registered with this MCP server.
*
* @var array<int, class-string<\Laravel\Mcp\Server\Resource>>
*/
protected array $resources = [
// ExampleResource::class,
];
/**
* The prompts registered with this MCP server.
*
* @var array<int, class-string<\Laravel\Mcp\Server\Prompt>>
*/
protected array $prompts = [
// ExamplePrompt::class,
];
}
Server Registration
サーバーを作成したら、それを routes/ai.php ファイルに登録してアクセスできるようにする必要があります。 Laravel MCP は、サーバーを登録するための 2 つの方法を提供します。HTTP アクセス可能なサーバー用の web と、コマンドライン サーバー用の local です。
Web Servers
Web サーバーは最も一般的なタイプのサーバーであり、HTTP POST リクエスト経由でアクセスできるため、リモート AI クライアントまたは Web ベースの統合に最適です。 web メソッドを使用して Web サーバーを登録します。
use App\Mcp\Servers\WeatherServer;
use Laravel\Mcp\Facades\Mcp;
Mcp::web('/mcp/weather', WeatherServer::class);
通常のルートと同様に、ミドルウェアを適用して Web サーバーを保護できます。
Mcp::web('/mcp/weather', WeatherServer::class)
->middleware(['throttle:mcp']);
Local Servers
ローカル サーバーは Artisan コマンドとして実行され、開発、テスト、またはローカル AI アシスタントの統合に最適です。 local メソッドを使用してローカル サーバーを登録します。
use App\Mcp\Servers\WeatherServer;
use Laravel\Mcp\Facades\Mcp;
Mcp::local('weather', WeatherServer::class);
登録したら、通常は mcp:start を自分で手動で実行する必要はありません。代わりに、サーバーを起動するように MCP クライアント (AI エージェント) を構成します。 mcp:start コマンドはクライアントによって呼び出されるように設計されており、必要に応じてサーバーの起動と停止を処理します。
php artisan mcp:start weather
Tools
ツールを使用すると、サーバーは AI クライアントが呼び出すことができる機能を公開できます。これらにより、言語モデルがアクションを実行したり、コードを実行したり、外部システムと対話したりできるようになります。
Creating Tools
ツールを作成するには、make:mcp-tool Artisan コマンドを実行します。
php artisan make:mcp-tool CurrentWeatherTool
ツールを作成したら、それをサーバーの $tools プロパティに登録します。
<?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 array $tools = [
CurrentWeatherTool::class,
];
}
Tool Name, Title, and Description
デフォルトでは、ツールの名前とタイトルはクラス名から派生します。たとえば、CurrentWeatherTool の名前は current-weather、タイトルは Current Weather Tool になります。これらの値は、ツールの $name プロパティと $title プロパティを定義することでカスタマイズできます。
class CurrentWeatherTool extends Tool
{
/**
* The tool's name.
*/
protected string $name = 'get-optimistic-weather';
/**
* The tool's title.
*/
protected string $title = 'Get Optimistic Weather Forecast';
// ...
}
ツールの説明は自動生成されません。ツールで $description プロパティを定義して、常に意味のある説明を提供する必要があります。
class CurrentWeatherTool extends Tool
{
/**
* The tool's description.
*/
protected string $description = 'Fetches the current weather forecast for a specified location.';
//
}
説明はツールのメタデータの重要な部分であり、AI モデルがツールをいつどのように効果的に使用するかを理解するのに役立ちます。
Tool Input Schemas
ツールは入力スキーマを定義して、AI クライアントから受け入れる引数を指定できます。 Laravel の Illuminate\JsonSchema\JsonSchema ビルダを使用して、ツールの入力要件を定義します。
<?php
namespace App\Mcp\Tools;
use Illuminate\JsonSchema\JsonSchema;
use Laravel\Mcp\Server\Tool;
class CurrentWeatherTool extends Tool
{
/**
* Get the tool's input schema.
*
* @return array<string, JsonSchema>
*/
public function schema(JsonSchema $schema): array
{
return [
'location' => $schema->string()
->description('The location to get the weather for.')
->required(),
'units' => $schema->enum(['celsius', 'fahrenheit'])
->description('The temperature units to use.')
->default('celsius'),
];
}
}
Validating Tool Arguments
JSON スキーマ定義はツール引数の基本構造を提供しますが、より複雑な検証ルールを強制することもできます。
Laravel MCP は、Laravel の validation features とシームレスに統合されます。ツールの handle メソッド内で受信ツール引数を検証できます。
<?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
{
$validated = $request->validate([
'location' => 'required|string|max:100',
'units' => 'in:celsius,fahrenheit',
]);
// Fetch weather data using the validated arguments...
}
}
検証が失敗すると、AI クライアントは提供されたエラー メッセージに基づいて動作します。したがって、明確で実用的なエラー メッセージを提供することが重要です。
$validated = $request->validate([
'location' => ['required','string','max:100'],
'units' => 'in:celsius,fahrenheit',
],[
'location.required' => 'You must specify a location to get the weather for. For example, "New York City" or "Tokyo".',
'units.in' => 'You must specify either "celsius" or "fahrenheit" for the units.',
]);
Tool Dependency Injection
Laravel service container は、すべてのツールを解決するために使用されます。その結果、ツールのコンストラクターで必要な依存関係をタイプヒントで指定できるようになります。宣言された依存関係は自動的に解決され、ツール インスタンスに挿入されます。
<?php
namespace App\Mcp\Tools;
use App\Repositories\WeatherRepository;
use Laravel\Mcp\Server\Tool;
class CurrentWeatherTool extends Tool
{
/**
* Create a new tool instance.
*/
public function __construct(
protected WeatherRepository $weather,
) {}
// ...
}
コンストラクターの注入に加えて、ツールの handle() メソッドでタイプヒントの依存関係を指定することもできます。サービスコンテナーは、メソッドが呼び出されるときに依存関係を自動的に解決して挿入します。
<?php
namespace App\Mcp\Tools;
use App\Repositories\WeatherRepository;
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, WeatherRepository $weather): Response
{
$location = $request->get('location');
$forecast = $weather->getForecastFor($location);
// ...
}
}
Tool Annotations
annotations を使用してツールを強化し、AI クライアントに追加のメタデータを提供できます。これらの注釈は、AI モデルがツールの動作と機能を理解するのに役立ちます。注釈は属性を介してツールに追加されます。
<?php
namespace App\Mcp\Tools;
use Laravel\Mcp\Server\Tools\Annotations\IsIdempotent;
use Laravel\Mcp\Server\Tools\Annotations\IsReadOnly;
use Laravel\Mcp\Server\Tool;
#[IsIdempotent]
#[IsReadOnly]
class CurrentWeatherTool extends Tool
{
//
}
利用可能な注釈は次のとおりです。
| 注釈 | タイプ | 説明 |
|---|---|---|
#[IsReadOnly] | ブール値 | ツールが環境を変更しないことを示します。 |
#[IsDestructive] | ブール値 | ツールが破壊的な更新を実行する可能性があることを示します (読み取り専用でない場合にのみ意味があります)。 |
#[IsIdempotent] | ブール値 | 同じ引数で繰り返し呼び出しても追加の効果がないことを示します (読み取り専用でない場合)。 |
#[IsOpenWorld] | ブール値 | ツールが外部エンティティと対話する可能性があることを示します。 |
Conditional Tool Registration
ツール クラスに shouldRegister メソッドを実装することで、実行時に条件付きでツールを登録できます。この方法を使用すると、アプリケーションの状態、構成、またはリクエスト パラメーターに基づいてツールを使用可能にするかどうかを決定できます。
<?php
namespace App\Mcp\Tools;
use Laravel\Mcp\Request;
use Laravel\Mcp\Server\Tool;
class CurrentWeatherTool extends Tool
{
/**
* Determine if the tool should be registered.
*/
public function shouldRegister(Request $request): bool
{
return $request?->user()?->subscribed() ?? false;
}
}
ツールの shouldRegister メソッドが false を返した場合、そのツールは使用可能なツールのリストに表示されず、AI クライアントから呼び出すことはできません。
Tool Responses
ツールは Laravel\Mcp\Response のインスタンスを返す必要があります。 Response クラスには、さまざまな種類の応答を作成するための便利なメソッドがいくつか用意されています。
単純なテキスト応答の場合は、text メソッドを使用します。
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
/**
* Handle the tool request.
*/
public function handle(Request $request): Response
{
// ...
return Response::text('Weather Summary: Sunny, 72°F');
}
ツールの実行中にエラーが発生したことを示すには、error メソッドを使用します。
return Response::error('Unable to fetch weather data. Please try again.');
Multiple Content Responses
ツールは、Response インスタンスの配列を返すことで、複数のコンテンツを返すことができます。
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
/**
* Handle the tool request.
*
* @return array<int, \Laravel\Mcp\Response>
*/
public function handle(Request $request): array
{
// ...
return [
Response::text('Weather Summary: Sunny, 72°F'),
Response::text('**Detailed Forecast**\n- Morning: 65°F\n- Afternoon: 78°F\n- Evening: 70°F')
];
}
Streaming Responses
長時間実行される操作またはリアルタイム データ ストリーミングの場合、ツールは handle メソッドから generator を返すことができます。これにより、最終応答の前に中間更新をクライアントに送信できるようになります。
<?php
namespace App\Mcp\Tools;
use Generator;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Tool;
class CurrentWeatherTool extends Tool
{
/**
* Handle the tool request.
*
* @return \Generator<int, \Laravel\Mcp\Response>
*/
public function handle(Request $request): Generator
{
$locations = $request->array('locations');
foreach ($locations as $index => $location) {
yield Response::notification('processing/progress', [
'current' => $index + 1,
'total' => count($locations),
'location' => $location,
]);
yield Response::text($this->forecastFor($location));
}
}
}
Web ベースのサーバーを使用する場合、ストリーミング応答は自動的に SSE (Server-Sent Events) ストリームを開き、生成された各メッセージをイベントとしてクライアントに送信します。
Prompts
Prompts を使用すると、AI クライアントが言語モデルと対話するために使用できる再利用可能なプロンプト テンプレートをサーバーで共有できるようになります。これらは、一般的なクエリと対話を構造化するための標準化された方法を提供します。
Creating Prompts
プロンプトを作成するには、make:mcp-prompt Artisan コマンドを実行します。
php artisan make:mcp-prompt DescribeWeatherPrompt
プロンプトを作成したら、それをサーバーの $prompts プロパティに登録します。
<?php
namespace App\Mcp\Servers;
use App\Mcp\Prompts\DescribeWeatherPrompt;
use Laravel\Mcp\Server;
class WeatherServer extends Server
{
/**
* The prompts registered with this MCP server.
*
* @var array<int, class-string<\Laravel\Mcp\Server\Prompt>>
*/
protected array $prompts = [
DescribeWeatherPrompt::class,
];
}
Prompt Name, Title, and Description
デフォルトでは、プロンプトの名前とタイトルはクラス名から派生します。たとえば、DescribeWeatherPrompt の名前は describe-weather、タイトルは Describe Weather Prompt になります。プロンプトで $name プロパティと $title プロパティを定義することで、これらの値をカスタマイズできます。
class DescribeWeatherPrompt extends Prompt
{
/**
* The prompt's name.
*/
protected string $name = 'weather-assistant';
/**
* The prompt's title.
*/
protected string $title = 'Weather Assistant Prompt';
// ...
}
プロンプトの説明は自動的には生成されません。プロンプトで $description プロパティを定義して、常に意味のある説明を提供する必要があります。
class DescribeWeatherPrompt extends Prompt
{
/**
* The prompt's description.
*/
protected string $description = 'Generates a natural-language explanation of the weather for a given location.';
//
}
説明は、AI モデルがプロンプトをいつどのように最大限に活用するかを理解するのに役立つため、プロンプトのメタデータの重要な部分です。
Prompt Arguments
プロンプトでは、AI クライアントが特定の値を使用してプロンプト テンプレートをカスタマイズできるようにする引数を定義できます。 arguments メソッドを使用して、プロンプトが受け入れる引数を定義します。
<?php
namespace App\Mcp\Prompts;
use Laravel\Mcp\Server\Prompt;
use Laravel\Mcp\Server\Prompts\Argument;
class DescribeWeatherPrompt extends Prompt
{
/**
* Get the prompt's arguments.
*
* @return array<int, \Laravel\Mcp\Server\Prompts\Argument>
*/
public function arguments(): array
{
return [
new Argument(
name: 'tone',
description: 'The tone to use in the weather description (e.g., formal, casual, humorous).',
required: true,
),
];
}
}
Validating Prompt Arguments
プロンプト引数はその定義に基づいて自動的に検証されますが、より複雑な検証ルールを適用することもできます。
Laravel MCP は、Laravel の validation features とシームレスに統合されます。プロンプトの handle メソッド内で受信プロンプト引数を検証できます。
<?php
namespace App\Mcp\Prompts;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Prompt;
class DescribeWeatherPrompt extends Prompt
{
/**
* Handle the prompt request.
*/
public function handle(Request $request): Response
{
$validated = $request->validate([
'tone' => 'required|string|max:50',
]);
$tone = $validated['tone'];
// Generate the prompt response using the given tone...
}
}
検証が失敗すると、AI クライアントは提供されたエラー メッセージに基づいて動作します。したがって、明確で実用的なエラー メッセージを提供することが重要です。
$validated = $request->validate([
'tone' => ['required','string','max:50'],
],[
'tone.*' => 'You must specify a tone for the weather description. Examples include "formal", "casual", or "humorous".',
]);
Prompt Dependency Injection
Laravel service container は、すべてのプロンプトを解決するために使用されます。その結果、プロンプトのコンストラクターで必要な依存関係をタイプヒントで指定できるようになります。宣言された依存関係は自動的に解決され、プロンプト インスタンスに挿入されます。
<?php
namespace App\Mcp\Prompts;
use App\Repositories\WeatherRepository;
use Laravel\Mcp\Server\Prompt;
class DescribeWeatherPrompt extends Prompt
{
/**
* Create a new prompt instance.
*/
public function __construct(
protected WeatherRepository $weather,
) {}
//
}
コンストラクターの注入に加えて、プロンプトの handle メソッドでタイプヒントの依存関係を使用することもできます。サービスコンテナーは、メソッドが呼び出されるときに依存関係を自動的に解決して挿入します。
<?php
namespace App\Mcp\Prompts;
use App\Repositories\WeatherRepository;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Prompt;
class DescribeWeatherPrompt extends Prompt
{
/**
* Handle the prompt request.
*/
public function handle(Request $request, WeatherRepository $weather): Response
{
$isAvailable = $weather->isServiceAvailable();
// ...
}
}
Conditional Prompt Registration
プロンプト クラスに shouldRegister メソッドを実装することで、実行時にプロンプトを条件付きで登録できます。この方法を使用すると、アプリケーションの状態、構成、または要求パラメーターに基づいてプロンプトを使用可能にするかどうかを決定できます。
<?php
namespace App\Mcp\Prompts;
use Laravel\Mcp\Request;
use Laravel\Mcp\Server\Prompt;
class CurrentWeatherPrompt extends Prompt
{
/**
* Determine if the prompt should be registered.
*/
public function shouldRegister(Request $request): bool
{
return $request?->user()?->subscribed() ?? false;
}
}
プロンプトの shouldRegister メソッドが false を返す場合、そのプロンプトは使用可能なプロンプトのリストに表示されず、AI クライアントによって呼び出すことはできません。
Prompt Responses
プロンプトは、単一の Laravel\Mcp\Response または反復可能な Laravel\Mcp\Response インスタンスを返す場合があります。これらの応答は、AI クライアントに送信されるコンテンツをカプセル化します。
<?php
namespace App\Mcp\Prompts;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Prompt;
class DescribeWeatherPrompt extends Prompt
{
/**
* Handle the prompt request.
*
* @return array<int, \Laravel\Mcp\Response>
*/
public function handle(Request $request): array
{
$tone = $request->string('tone');
$systemMessage = "You are a helpful weather assistant. Please provide a weather description in a {$tone} tone.";
$userMessage = "What is the current weather like in New York City?";
return [
Response::text($systemMessage)->asAssistant(),
Response::text($userMessage),
];
}
}
asAssistant() メソッドを使用すると、通常のメッセージがユーザー入力として扱われる一方で、応答メッセージが AI アシスタントから送信されたものとして扱われる必要があることを示すことができます。
Resources
Resources を使用すると、サーバーが AI クライアントが言語モデルと対話するときにコンテキストとして読み取って使用できるデータとコンテンツを公開できるようになります。これらは、ドキュメント、構成、AI 応答の通知に役立つデータなどの静的または動的な情報を共有する方法を提供します。
Creating Resources
リソースを作成するには、make:mcp-resource Artisan コマンドを実行します。
php artisan make:mcp-resource WeatherGuidelinesResource
リソースを作成したら、それをサーバーの $resources プロパティに登録します。
<?php
namespace App\Mcp\Servers;
use App\Mcp\Resources\WeatherGuidelinesResource;
use Laravel\Mcp\Server;
class WeatherServer extends Server
{
/**
* The resources registered with this MCP server.
*
* @var array<int, class-string<\Laravel\Mcp\Server\Resource>>
*/
protected array $resources = [
WeatherGuidelinesResource::class,
];
}
Resource Name, Title, and Description
デフォルトでは、リソースの名前とタイトルはクラス名から派生します。たとえば、WeatherGuidelinesResource の名前は weather-guidelines、タイトルは Weather Guidelines Resource になります。これらの値は、リソースで $name プロパティと $title プロパティを定義することでカスタマイズできます。
class WeatherGuidelinesResource extends Resource
{
/**
* The resource's name.
*/
protected string $name = 'weather-api-docs';
/**
* The resource's title.
*/
protected string $title = 'Weather API Documentation';
// ...
}
リソースの説明は自動的には生成されません。リソースに $description プロパティを定義して、常に意味のある説明を提供する必要があります。
class WeatherGuidelinesResource extends Resource
{
/**
* The resource's description.
*/
protected string $description = 'Comprehensive guidelines for using the Weather API.';
//
}
説明は、AI モデルがリソースをいつどのように効果的に使用するかを理解するのに役立つため、リソースのメタデータの重要な部分です。
Resource URI and MIME Type
各リソースは一意の URI によって識別され、AI クライアントがリソースの形式を理解するのに役立つ MIME タイプが関連付けられています。
デフォルトでは、リソースの URI はリソース名に基づいて生成されるため、WeatherGuidelinesResource の URI は weather://resources/weather-guidelines になります。デフォルトの MIME タイプは text/plain です。
これらの値は、リソースで $uri プロパティと $mimeType プロパティを定義することでカスタマイズできます。
<?php
namespace App\Mcp\Resources;
use Laravel\Mcp\Server\Resource;
class WeatherGuidelinesResource extends Resource
{
/**
* The resource's URI.
*/
protected string $uri = 'weather://resources/guidelines';
/**
* The resource's MIME type.
*/
protected string $mimeType = 'application/pdf';
}
URI と MIME タイプは、AI クライアントがリソース コンテンツを適切に処理および解釈する方法を決定するのに役立ちます。
Resource Request
ツールやプロンプトとは異なり、リソースは入力スキーマや引数を定義できません。ただし、リソースの handle メソッド内でリクエスト オブジェクトと対話することはできます。
<?php
namespace App\Mcp\Resources;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Resource;
class WeatherGuidelinesResource extends Resource
{
/**
* Handle the resource request.
*/
public function handle(Request $request): Response
{
// ...
}
}
Resource Dependency Injection
Laravel service container は、すべてのリソースを解決するために使用されます。その結果、リソースがコンストラクターで必要とする依存関係をタイプヒントで指定できるようになります。宣言された依存関係は自動的に解決され、リソース インスタンスに挿入されます。
<?php
namespace App\Mcp\Resources;
use App\Repositories\WeatherRepository;
use Laravel\Mcp\Server\Resource;
class WeatherGuidelinesResource extends Resource
{
/**
* Create a new resource instance.
*/
public function __construct(
protected WeatherRepository $weather,
) {}
// ...
}
コンストラクターのインジェクションに加えて、リソースの handle メソッドでタイプヒントの依存関係を指定することもできます。サービスコンテナーは、メソッドが呼び出されるときに依存関係を自動的に解決して挿入します。
<?php
namespace App\Mcp\Resources;
use App\Repositories\WeatherRepository;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Resource;
class WeatherGuidelinesResource extends Resource
{
/**
* Handle the resource request.
*/
public function handle(WeatherRepository $weather): Response
{
$guidelines = $weather->guidelines();
return Response::text($guidelines);
}
}
Conditional Resource Registration
リソース クラスに shouldRegister メソッドを実装することで、実行時に条件付きでリソースを登録できます。このメソッドを使用すると、アプリケーションの状態、構成、またはリクエスト パラメーターに基づいてリソースを利用可能にするかどうかを決定できます。
<?php
namespace App\Mcp\Resources;
use Laravel\Mcp\Request;
use Laravel\Mcp\Server\Resource;
class WeatherGuidelinesResource extends Resource
{
/**
* Determine if the resource should be registered.
*/
public function shouldRegister(Request $request): bool
{
return $request?->user()?->subscribed() ?? false;
}
}
リソースの shouldRegister メソッドが false を返すと、そのリソースは使用可能なリソースのリストに表示されず、AI クライアントはアクセスできません。
Resource Responses
リソースは Laravel\Mcp\Response のインスタンスを返す必要があります。 Response クラスには、さまざまな種類の応答を作成するための便利なメソッドがいくつか用意されています。
単純なテキスト コンテンツの場合は、text メソッドを使用します。
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
/**
* Handle the resource request.
*/
public function handle(Request $request): Response
{
// ...
return Response::text($weatherData);
}
Blob Responses
BLOB コンテンツを返すには、blob メソッドを使用して、BLOB コンテンツを指定します。
return Response::blob(file_get_contents(storage_path('weather/radar.png')));
BLOB コンテンツを返すとき、MIME タイプはリソース クラスの $mimeType プロパティの値によって決まります。
<?php
namespace App\Mcp\Resources;
use Laravel\Mcp\Server\Resource;
class WeatherGuidelinesResource extends Resource
{
/**
* The resource's MIME type.
*/
protected string $mimeType = 'image/png';
//
}
Error Responses
リソースの取得中にエラーが発生したことを示すには、error() メソッドを使用します。
return Response::error('Unable to fetch weather data for the specified location.');
Authentication
ルートの場合と同様に、ミドルウェアを使用して Web MCP サーバーを認証できます。この場合、ユーザーはサーバーの機能を使用する前に認証する必要があります。
MCP サーバーへのアクセスを認証するには 2 つの方法があります。Laravel Sanctum を介した単純なトークン ベースの認証、または Authorization HTTP ヘッダーを介して渡されるその他の任意の API トークンです。または、Laravel Passport を使用して OAuth 経由で認証することもできます。
OAuth 2.1
Web ベースの MCP サーバーを保護する最も堅牢な方法は、Laravel Passport を介した OAuth を使用することです。
OAuth 経由で MCP サーバーを認証する場合、routes/ai.php ファイル内の Mcp::oauthRoutes メソッドを呼び出して、必要な OAuth2 検出ルートとクライアント登録ルートを登録します。次に、Passport の auth:api ミドルウェアを routes/ai.php ファイル内の Mcp::web ルートに適用します。
use App\Mcp\Servers\WeatherExample;
use Laravel\Mcp\Facades\Mcp;
Mcp::oauthRoutes();
Mcp::web('/mcp/weather', WeatherExample::class)
->middleware('auth:api');
New Passport Installation
アプリケーションがまだ Laravel Passport を使用していない場合は、まず Passport の installation and deployment steps に従ってください。次に進む前に、OAuthenticatable モデル、新しい認証ガード、およびPassport キーを用意する必要があります。
次に、Laravel MCP が提供する Passport 認可ビューを公開する必要があります。
php artisan vendor:publish --tag=mcp-views
次に、Passport::authorizationView メソッドを使用して、このビューを使用するように Passport に指示します。通常、このメソッドはアプリケーションの AppServiceProvider の boot メソッドで呼び出す必要があります。
use Laravel\Passport\Passport;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Passport::authorizationView(function ($parameters) {
return view('mcp.authorize', $parameters);
});
}
このビューは、AI エージェントの認証試行を拒否または承認するための認証中にエンドユーザーに表示されます。
このシナリオでは、基盤となる認証可能なモデルへの変換レイヤーとして OAuth を使用しているだけです。スコープなど、OAuth の多くの側面を無視しています。
Using an Existing Passport Installation
アプリケーションがすでに Laravel Passport を使用している場合、Laravel MCP は既存の Passport インストール内でシームレスに動作するはずですが、OAuth は主に基盤となる認証可能なモデルへの変換レイヤーとして使用されるため、カスタム スコープは現在サポートされていません。
Laravel MCP は、上で説明した Mcp::oauthRoutes() メソッドを介して、単一の mcp:use スコープを追加、アドバタイズ、および使用します。
Passport vs. Sanctum
OAuth2.1 はモデル コンテキスト プロトコル仕様で文書化された認証メカニズムであり、MCP クライアント間で最も広くサポートされています。そのため、可能な限りPassportを使用することをお勧めします。
アプリケーションがすでに Sanctum を使用している場合、Passport の追加は面倒な場合があります。この場合、OAuth のみをサポートする MCP クライアントを使用するための明確な必要な要件が得られるまで、Passport なしで Sanctum を使用することをお勧めします。
Sanctum
Sanctum を使用して MCP サーバーを保護したい場合は、routes/ai.php ファイルで Sanctum の認証ミドルウェアをサーバーに追加するだけです。次に、MCP クライアントが Authorization: Bearer <token> ヘッダーを提供して、認証が成功したことを確認します。
use App\Mcp\Servers\WeatherExample;
use Laravel\Mcp\Facades\Mcp;
Mcp::web('/mcp/demo', WeatherExample::class)
->middleware('auth:sanctum');
Custom MCP Authentication
アプリケーションが独自のカスタム API トークンを発行する場合、必要なミドルウェアを Mcp::web ルートに割り当てることで、MCP サーバーを認証できます。カスタム ミドルウェアは、Authorization ヘッダーを手動で検査して、受信 MCP リクエストを認証できます。
Authorization
$request->user() メソッドを介して現在認証されているユーザーにアクセスすると、MCP ツールおよびリソース内で authorization checks を実行できるようになります。
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
/**
* Handle the tool request.
*/
public function handle(Request $request): Response
{
if (! $request->user()->can('read-weather')) {
return Response::error('Permission denied.');
}
// ...
}
Testing Servers
組み込みの MCP インスペクターを使用するか、単体テストを作成することによって、MCP サーバーをテストできます。
MCP Inspector
MCP Inspector は、MCP サーバーをテストおよびデバッグするための対話型ツールです。これを使用してサーバーに接続し、認証を確認し、ツール、リソース、プロンプトを試します。
登録されている任意のサーバー (たとえば、「weather」という名前のローカル サーバー) に対してインスペクタを実行できます。
php artisan mcp:inspector weather
このコマンドは MCP インスペクターを起動し、すべてが正しく構成されていることを確認するために MCP クライアントにコピーできるクライアント設定を提供します。 Web サーバーが認証ミドルウェアによって保護されている場合は、接続時に Authorization ベアラー トークンなどの必要なヘッダーを必ず含めてください。
Unit Tests
MCP サーバー、ツール、リソース、プロンプトの単体テストを作成できます。
まず、新しいテスト ケースを作成し、それを登録するサーバー上で目的のプリミティブを呼び出します。たとえば、WeatherServer でツールをテストするには:
test('tool', function () {
$response = WeatherServer::tool(CurrentWeatherTool::class, [
'location' => 'New York City',
'units' => 'fahrenheit',
]);
$response
->assertOk()
->assertSee('The current weather in New York City is 72°F and sunny.');
});
/**
* Test a tool.
*/
public function test_tool(): void
{
$response = WeatherServer::tool(CurrentWeatherTool::class, [
'location' => 'New York City',
'units' => 'fahrenheit',
]);
$response
->assertOk()
->assertSee('The current weather in New York City is 72°F and sunny.');
}
同様に、プロンプトとリソースをテストできます。
$response = WeatherServer::prompt(...);
$response = WeatherServer::resource(...);
プリミティブを呼び出す前に actingAs メソッドをチェーンすることで、認証されたユーザーとして動作することもできます。
$response = WeatherServer::actingAs($user)->tool(...);
応答を受信したら、さまざまなアサーション メソッドを使用して、応答の内容とステータスを確認できます。
assertOk メソッドを使用して、応答が成功したことをアサートできます。これにより、応答にエラーがないことがチェックされます。
$response->assertOk();
assertSee メソッドを使用して、応答に特定のテキストが含まれていることをアサートできます。
$response->assertSee('The current weather in New York City is 72°F and sunny.');
assertHasErrors メソッドを使用して、応答にエラーが含まれていることをアサートできます。
$response->assertHasErrors();
$response->assertHasErrors([
'Something went wrong.',
]);
assertHasNoErrors メソッドを使用して、応答にエラーが含まれていないことを主張できます。
$response->assertHasNoErrors();
assertName()、assertTitle()、および assertDescription() メソッドを使用して、応答に特定のメタデータが含まれていることをアサートできます。
$response->assertName('current-weather');
$response->assertTitle('Current Weather Tool');
$response->assertDescription('Fetches the current weather forecast for a specified location.');
通知が assertSentNotification および assertNotificationCount メソッドを使用して送信されたと主張できます。
$response->assertSentNotification('processing/progress', [
'step' => 1,
'total' => 5,
]);
$response->assertSentNotification('processing/progress', [
'step' => 2,
'total' => 5,
]);
$response->assertNotificationCount(5);
最後に、生の応答コンテンツを検査したい場合は、dd メソッドまたは dump メソッドを使用して、デバッグ目的で応答を出力できます。
$response->dd();
$response->dump();