这是一个实现了 模型上下文协议 (MCP) 的 Laravel 包,它使您的 Laravel 应用程序能够通过标准化的 API 与 AI 助手或其他系统进行无缝通信。 请注意,此包仍在开发中,尚未准备好用于生产环境。
您可以通过 Composer 安装该包:
composer require innoge/laravel-mcp
当前此包仅支持通过 STDIO 运输方式创建 MCP 服务器。 HTTP 运输方式尚未支持,但将在未来添加。
创建一个命令来启动您的 MCP 服务器:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use InnoGE\LaravelMcp\Commands\ServesMcpServer;
class McpServerCommand extends Command
{
use ServesMcpServer;
protected $signature = 'mcp:serve';
protected $description = '启动 MCP 服务器';
public function handle(): int
{
return $this->serveMcp('your-app-name', '1.0.0');
}
private function getTools(): array
{
return [
// 在这里列出您的工具类
];
}
private function getResources(): array
{
return [
// 在这里列出您的资源提供者
];
}
}
资源允许您通过 MCP 协议公开应用程序的数据模型。此包提供了两种类型的资源提供者:
use InnoGE\LaravelMcp\Resources\EloquentResourceProvider;
use App\Models\User;
// 在您的 getResources() 方法中:
return [
new EloquentResourceProvider(User::query(), 'users', '应用中的用户')
];
use InnoGE\LaravelMcp\Resources\InMemoryResourceProvider;
use InnoGE\LaravelMcp\Types\Resources\ResourceContent;
use InnoGE\LaravelMcp\Types\Resources\ResourceItem;
// 创建一个资源提供者
$resourceProvider = new InMemoryResourceProvider();
// 添加示例文档作为资源
$resourceProvider->addResource(
new ResourceItem('doc://example/document1', '示例文档 1', '这是一个示例文档', 'text/plain', 1024),
new ResourceContent('doc://example/document1', 'text/plain', '这是文档的内容')
);
$resourceProvider->addResource(
new ResourceItem('doc://example/document2', '示例文档 2', '这是示例文档 2', 'text/plain', 1024),
new ResourceContent('doc://example/document2', 'text/plain', '这是文档 2 的内容')
);
// 在您的 getResources() 方法中:
return [
$resourceProvider
];
工具定义了可以通过 MCP 协议执行的操作:
内置示例工具:
HelloTool: 一个简单的“Hello World”示例ClockTool: 返回当前时间自定义工具:通过实现 ToolInterface 来创建自己的工具
use InnoGE\LaravelMcp\Tools\Examples\HelloTool;
use InnoGE\LaravelMcp\Tools\Examples\ClockTool;
use App\MCP\Tools\YourCustomTool;
// 在您的 getTools() 方法中:
return [
HelloTool::class,
ClockTool::class,
YourCustomTool::class,
];
工具是 MCP 的核心功能,允许 AI 助手与您的 Laravel 应用程序交互。它们提供了一种通过明确定义的接口执行特定操作的方法。
MCP 工具的实际例子包括:
示例工具:
<?php
namespace App\MCP\Tools;
use Illuminate\Support\Facades\Artisan;
use InnoGE\LaravelMcp\Tools\Tool;
use Symfony\Component\Console\Output\BufferedOutput;
class CallArtisanCommandTool implements Tool
{
/**
* 获取工具名称
*/
public function getName(): string
{
return 'call-artisan-command';
}
/**
* 获取工具描述
*/
public function getDescription(): string
{
return '调用 Laravel Artisan 命令';
}
/**
* 获取工具的输入模式
*/
public function getInputSchema(): array
{
return [
'type' => 'object',
'properties' => [
'command' => [
'type' => 'string',
'description' => '要调用的 Artisan 命令(例如 "migrate")',
],
],
'required' => ['command'],
];
}
/**
* 使用提供的参数执行工具
*/
public function execute(array $arguments): string
{
$command = $arguments['command'];
$outputBuffer = new BufferedOutput;
Artisan::call($command, [], $outputBuffer);
return $outputBuffer->fetch();
}
}
使用 Modelcontext Protocol Inspector 来测试 MCP 服务器:
npx @modelcontextprotocol/inspector php /path/to/your/app/artisan mcp:serve
编辑您的 Claude Desktop 配置文件:
~/Library/Application Support/Claude/claude_desktop_config.json
在配置文件中添加您的 MCP 服务器:
{
"mcpServers": {
"laravel-mcp": {
"command": "php",
"args": [
"/path/to/your/app/artisan",
"mcp:serve"
]
}
}
}
现在您可以在 Claude Desktop 中使用您的 MCP 服务器。请注意,Claude 目前不使用 MCP 资源。如果您想访问应用程序的数据,可以使用工具调用。
composer test
请参阅 更新日志 以获取更多信息。
请参阅 贡献指南 以获取详细信息。
请查阅 我们的安全政策 了解如何报告安全漏洞。
MIT 许可证 (MIT)。请参阅 许可证文件 以获取更多信息。