关于 MCP for Laravel
MCP for Laravel 是用于在 Laravel 应用中构建 AI Agent 的 PHP 框架包,提供对话记忆、工具调用和 RAG 组件,并可连接 MCP 服务中的工具。
主要功能
- 01Laravel Agent 基类
- 02对话记忆
- 03工具与函数调用
- 04系统提示词构建
- 05RAG 组件
- 06MCP 服务工具连接器
适用场景
- 01在 Laravel 中构建对话 Agent
- 02为 Agent 添加具体任务工具
- 03将 Agent 工具连接到 MCP 服务
MCP 相关
McpConnector 可连接 MCP 服务公开的工具,让 Agent 使用这些工具,而无需手动实现。
详细介绍
Laravel 的 MCP
Laravel 的 MCP(Model Context Protocol)— 一个用于构建智能应用的强大 AI 框架。基于 inspector-apm/neuron-ai
需求
- PHP: ^8.0
- Laravel: ^11.0
官方文档
安装
安装最新版本的软件包:
composer require settled/mcp-laravel
创建 Agent
MCP for Laravel 为您提供了 Agent 类,您可以扩展它来继承该框架的主要功能,
并创建功能完善的 Agent。该类自动为您管理一些高级机制,例如内存、
工具和函数调用,直至 RAG 系统。让我们创建第一个 Agent,扩展 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 类旨在接受您的基础指令并为底层模型构建一致的提示词
以减少提示词工程的工作量。
与 Agent 对话
向 Agent 发送提示词以从底层大模型获得响应:
$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.
如上例所示,Agent 自动拥有对正在进行的对话的内存。了解更多关于内存的信息,请参阅文档。
支持的大模型提供商
使用 Neuron,您可以仅用一行代码在大模型提供商之间切换,对您的 Agent 实现没有任何影响。 支持的提供商:
- Anthropic
- Ollama(也可用作嵌入提供商)
- OpenAI
- Mistral
- Deepseek
工具和函数调用
您可以通过工具数组向您的 Agent 添加执行具体任务的能力:
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 用例,您必须扩展 NeuronAI\RAG\RAG 类,而不是默认的 Agent 类。
要创建 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 框架的开发做出贡献! 请查看贡献指南了解如何进行。加入我们!
许可证
此软件包在 MIT 许可证下授权。
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。