关于 Laravel MCP (Model Context Protocol)
Laravel MCP 是一个已停止维护的概念验证包,用于在 Laravel 中构建 MCP 服务。它支持 STDIO 传输,并提供资源提供器和自定义工具;项目建议改用官方 laravel/mcp 包。
主要功能
- 01在 Laravel 中构建 MCP 服务
- 02公开 Eloquent 和内存资源
- 03定义自定义工具
- 04支持 STDIO 传输
适用场景
- 01在 Laravel 应用中试验 MCP 支持
- 02通过资源提供器公开应用数据
- 03为 MCP 客户端定义操作
MCP 相关
该包通过 STDIO 实现 MCP 服务,向 MCP 客户端公开配置的资源和工具。项目已停止维护,并由官方 laravel/mcp 包取代。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
Laravel MCP(Model Context Protocol)
警告
此软件包已不再维护。
它只是一个概念验证,不再继续开发。目前已由官方
laravel/mcp软件包完全取代。请改用该软件包。
这是一个 Laravel 软件包,实现了 Model Context Protocol (MCP),通过标准化的 API,让 Laravel 应用与 AI 助手或其他系统无缝通信。 请注意,此软件包仍在开发中,尚未准备好用于生产环境。
安装
你可以通过 Composer 安装此软件包:
composer require innoge/laravel-mcp
基本用法
设置 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 = 'Start an MCP server';
public function handle(): int
{
return $this->serveMcp('your-app-name', '1.0.0');
}
private function getTools(): array
{
return [
// List your tool classes here
];
}
private function getResources(): array
{
return [
// List your resource providers here
];
}
}
资源
资源可让你通过 MCP 协议公开应用的数据模型。此软件包提供两种资源提供程序:
- EloquentResourceProvider:公开 Eloquent 模型
use InnoGE\LaravelMcp\Resources\EloquentResourceProvider;
use App\Models\User;
// In your getResources() method:
return [
new EloquentResourceProvider(User::query(), 'users', 'A User of the Application')
];
- InMemoryResourceProvider:公开自定义数据结构或非 Eloquent 数据
use InnoGE\LaravelMcp\Resources\InMemoryResourceProvider;
use InnoGE\LaravelMcp\Types\Resources\ResourceContent;
use InnoGE\LaravelMcp\Types\Resources\ResourceItem;
// Create a resource provider
$resourceProvider = new InMemoryResourceProvider();
// Add example documents as resources
$resourceProvider->addResource(
new ResourceItem('doc://example/document1', 'Example Document 1', 'This is an example document', 'text/plain', 1024),
new ResourceContent('doc://example/document1', 'text/plain', 'This is the content of the document')
);
$resourceProvider->addResource(
new ResourceItem('doc://example/document2', 'Example Document 2', 'This is an example document 2', 'text/plain', 1024),
new ResourceContent('doc://example/document2', 'text/plain', 'This is the content of the document 2')
);
// In your getResources() method:
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;
// In your getTools() method:
return [
HelloTool::class,
ClockTool::class,
YourCustomTool::class,
];
创建工具
工具是 MCP 的核心功能,可让 AI 助手与 Laravel 应用交互。它们提供了一种通过定义明确的接口在应用中执行特定操作的方式。
MCP 工具的实际应用示例包括:
- 数据库操作:创建、读取、更新或删除记录
- 外部 API 集成:向第三方服务发起 API 调用
- 文件管理:上传、下载或处理文件
- 身份验证:验证用户凭据或生成令牌
- 报告:生成报告或导出数据
- 电子邮件/通知:向用户发送消息
工具示例:
<?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
{
/**
* Get the tool name
*/
public function getName(): string
{
return 'call-artisan-command';
}
/**
* Get the tool description
*/
public function getDescription(): string
{
return 'Call a Laravel Artisan command';
}
/**
* Get the input schema for the tool
*/
public function getInputSchema(): array
{
return [
'type' => 'object',
'properties' => [
'command' => [
'type' => 'string',
'description' => 'The Artisan command to call (e.g. "migrate")',
],
],
'required' => ['command'],
];
}
/**
* Execute the tool with the provided arguments
*/
public function execute(array $arguments): string
{
$command = $arguments['command'];
$outputBuffer = new BufferedOutput;
Artisan::call($command, [], $outputBuffer);
return $outputBuffer->fetch();
}
}
测试 MCP 服务端
使用 Modelcontext Protocol Inspector 测试 MCP 服务端:
npx @modelcontextprotocol/inspector php /path/to/your/app/artisan mcp:serve
将 MCP 服务端添加到 Claude Desktop
编辑 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
更新日志
有关近期变更的更多信息,请参阅 CHANGELOG。
贡献
详情请参阅 CONTRIBUTING。
安全漏洞
请查看我们的安全政策,了解如何报告安全漏洞。
致谢
License
The MIT License (MIT). Please see License File for more information.
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。