mcpField
M
MCP for Laravel

MCP for Laravel

github.com/settledcoフレームワーク・SDK681 週間前に更新

MCP for Laravel について

MCP for Laravelは、LaravelアプリケーションでAIエージェントを構築するためのPHPパッケージです。会話メモリ、ツール呼び出し、RAGコンポーネントを備え、MCPサーバーのツールにも接続できます。

主な機能

  1. 01Laravelエージェント基底クラス
  2. 02会話メモリ
  3. 03ツールと関数呼び出し
  4. 04システムプロンプトの構築
  5. 05RAGコンポーネント
  6. 06MCPサーバーツール接続機能

活用シーン

  1. 01Laravelで会話型エージェントを構築
  2. 02エージェントに具体的なタスク用ツールを追加
  3. 03エージェントのツールをMCPサーバーに接続

MCP 対応

McpConnectorは、MCPサーバーが公開するツールにエージェントを接続します。ツールを手動で実装せずに利用できます。

詳細

Laravel 用 MCP

Latest Stable Version License

Laravel 用 MCP (Model Context Protocol) - インテリジェントなアプリケーションを構築するための強力な AI フレームワーク。inspector-apm/neuron-ai をベースとしています。

要件

  • PHP: ^8.0
  • Laravel: ^11.0

公式ドキュメント

公式ドキュメントへ移動

インストール

最新バージョンのパッケージをインストールします:

composer require settled/mcp-laravel

エージェントを作成する

MCP for Laravel は、フレームワークの主な機能を継承して完全に機能するエージェントを作成できる Agent クラスを提供します。このクラスはメモリ、ツール、関数呼び出し、RAG システムまで、高度なメカニズムを自動的に管理します。Settled\MCP\Agent クラスを拡張して最初のエージェントを作成しましょう:

use Settled\MCP\Agent;
use Settled\MCP\SystemPrompt;
use Settled\MCP\Providers\AIProviderInterface;
use Settled\MCP\Providers\Anthropic\Anthropic;

class YouTubeAgent extends Agent
{
    public function provider(): AIProviderInterface
    {
        return new Anthropic(
            key: 'ANTHROPIC_API_KEY',
            model: 'ANTHROPIC_MODEL',
        );
    }

    public function instructions(): string
    {
        return new SystemPrompt(
            background: ["You are an AI Agent specialized in writing YouTube video summaries."],
            steps: [
                "Get the url of a YouTube video, or ask the user to provide one.",
                "Use the tools you have available to retrieve the transcription of the video.",
                "Write the summary.",
            ],
            output: [
                "Write a summary in a paragraph without using lists. Use just fluent text.",
                "After the summary add a list of three sentences as the three most important take away from the video.",
            ]
        );
    }
}

SystemPrompt クラスは、ベースとなる指示を受け取り、基盤となるモデルに対して一貫したプロンプトを構築するように設計されており、プロンプトエンジニアリングの労力を軽減します。

エージェントと対話する

エージェントにプロンプトを送信して、基盤となる LLM から応答を取得します:

$agent = YouTubeAgent::make();

$response = $agent->run(new UserMessage("Hi, I'm Valerio. Who are you?"));
echo $response->getContent();
// I'm a friendly YouTube assistant to help you summarize videos.


$response = $agent->run(
    new UserMessage("Do you know my name?")
);
echo $response->getContent();
// Your name is Valerio, as you said in your introduction.

上記の例でわかるように、エージェントは進行中の会話のメモリを自動的に保有しています。メモリの詳細については、ドキュメントを参照してください。

サポートされている LLM プロバイダー

Neuron を使用すると、わずか 1 行のコードで LLM プロバイダーを切り替えることができ、エージェント実装への影響はありません。サポートされているプロバイダー:

ツール & 関数呼び出し

Tool の配列を使用して、エージェントに具体的なタスクを実行する機能を追加できます:

use NeuronAI\Agent;
use NeuronAI\SystemPrompt;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\Anthropic\Anthropic;
use NeuronAI\Tools\Tool;
use NeuronAI\Tools\ToolProperty;

class YouTubeAgent extends Agent
{
    public function provider(): AIProviderInterface
    {
        return new Anthropic(
            key: 'ANTHROPIC_API_KEY',
            model: 'ANTHROPIC_MODEL',
        );
    }

    public function instructions(): string
    {
        return new SystemPrompt(
            background: ["You are an AI Agent specialized in writing YouTube video summaries."],
            steps: [
                "Get the url of a YouTube video, or ask the user to provide one.",
                "Use the tools you have available to retrieve the transcription of the video.",
                "Write the summary.",
            ],
            output: [
                "Write a summary in a paragraph without using lists. Use just fluent text.",
                "After the summary add a list of three sentences as the three most important take away from the video.",
            ]
        );
    }

    public function tools(): array
    {
        return [
            Tool::make(
                'get_transcription',
                'Retrieve the transcription of a youtube video.',
            )->addProperty(
                new ToolProperty(
                    name: 'video_url',
                    type: 'string',
                    description: 'The URL of the YouTube video.',
                    required: true
                )
            )->setCallable(function (string $video_url) {
                // ... retrieve the video transcription
            })
        ];
    }
}

ツールについてさらに詳しくは、ドキュメントを参照してください。

MCP サーバーコネクター

ツールを手動で実装する代わりに、McpConnector コンポーネントを使用して MCP サーバーによって公開されたツールを接続できます:

use NeuronAI\Agent;
use NeuronAI\MCP\McpConnector;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\Anthropic\Anthropic;
use NeuronAI\Tools\Tool;
use NeuronAI\Tools\ToolProperty;

class SEOAgent extends Agent
{
    public function provider(): AIProviderInterface
    {
        return new Anthropic(
            key: 'ANTHROPIC_API_KEY',
            model: 'ANTHROPIC_MODEL',
        );
    }

    public function instructions(): string
    {
        return new SystemPrompt(
            background: ["Act as an expert of SEO (Search Engine Optimization)."]
            steps: [
                "Analyze a text of an article.",
                "Provide suggestions on how the content can be improved to get a better rank on Google search."
            ],
            output: ["Structure your analysis in sections. One for each suggestion."]
        );
    }

    public function tools(): array
    {
        return [
            // Connect an MCP server
            ...McpConnector::make([
                'command' => 'npx',
                'args' => ['-y', '@modelcontextprotocol/server-everything'],
            ])->tools(),

            // Implement your custom tools
            Tool::make(
                'get_transcription',
                'Retrieve the transcription of a youtube video.',
            )->addProperty(
                new ToolProperty(
                    name: 'video_url',
                    type: 'string',
                    description: 'The URL of the YouTube video.',
                    required: true
                )
            )->setCallable(function (string $video_url) {
                // ... retrieve the video transcription
            })
        ];
    }
}

MCP コネクターについてさらに詳しくは、ドキュメントを参照してください。

RAG システムを実装する

RAG ユースケースの場合、デフォルトの Agent クラスの代わりに NeuronAI\RAG\RAG クラスを拡張する必要があります。

RAG を作成するには、AI プロバイダー以外に、ベクトルストアや埋め込みプロバイダーなどの追加コンポーネントをアタッチする必要があります。

RAG 実装の例は以下の通りです:

use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\Anthropic\Anthropic;
use NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface;
use NeuronAI\RAG\Embeddings\VoyageEmbeddingProvider;
use NeuronAI\RAG\RAG;
use NeuronAI\RAG\VectorStore\PineconeVectoreStore;
use NeuronAI\RAG\VectorStore\VectorStoreInterface;

class MyChatBot extends RAG
{
    public function provider(): AIProviderInterface
    {
        return new Anthropic(
            key: 'ANTHROPIC_API_KEY',
            model: 'ANTHROPIC_MODEL',
        );
    }

    public function embeddings(): EmbeddingsProviderInterface
    {
        return new VoyageEmbeddingProvider(
            key: 'VOYAGE_API_KEY',
            model: 'VOYAGE_MODEL'
        );
    }

    public function vectorStore(): VectorStoreInterface
    {
        return new PineconeVectoreStore(
            key: 'PINECONE_API_KEY',
            indexUrl: 'PINECONE_INDEX_URL'
        );
    }
}

RAG についてさらに詳しくは、ドキュメントを参照してください。

公式ドキュメント

公式ドキュメントへ移動

貢献

Neuron AI Framework の開発への貢献をお勧めします! 進め方については、貢献ガイドラインを確認してください。ぜひご参加ください!

ライセンス

このバンドルは MIT ライセンスの下でライセンスされています。

コメント

MCP は初めてですか?

プロトコルの概要から最初のサーバーの導入まで、よくある疑問にまとめて答えます。

AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。