> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# MCP

> 说明如何通过 Laravel Copilot SDK 的会话配置和 Server RPC API 来配置与管理 MCP 服务器。

## MCP

只要在 SessionConfig 的 `mcpServers` 中指定 MCP 服务器就可以使用。

在 Copilot CLI 中使用 Laravel Boost 时，也请一起使用 [laravel-boost-copilot-cli](https://github.com/invokable/laravel-boost-copilot-cli)。
已知 `'type' => 'stdio'`（或 `'local'`）和 `'tools' => ['*']` 是必须的。没有它们就不会被识别为 MCP 服务器。

```php theme={null}
Artisan::command('copilot:mcp', function () {
    $config = new SessionConfig(
        mcpServers: [
            'laravel-boost' => [
                'type' => 'stdio',
                'command' => 'php',
                'args' => ['artisan', 'boost:mcp'],
                'tools' => ['*'],
            ],
        ],
    );

    Copilot::start(function (CopilotSession $session) {
        info('Starting Copilot with Laravel Boost MCP: '.$session->id());

        $prompt = '加载了哪些 MCP？如果可以使用 laravel-boost，请用 application-info 获取应用信息。';

        warning($prompt);

        $response = spin(
            callback: fn () => $session->sendAndWait($prompt),
            message: 'Thinking...',
        );

        note($response->content());
    }, config: $config);
});
```

## MCP 服务器的状态

会话内 MCP 服务器的状态可能是以下值：

| 状态               | 说明   |
| ---------------- | ---- |
| `connected`      | 已连接  |
| `failed`         | 连接失败 |
| `needs-auth`     | 需要认证 |
| `pending`        | 等待连接 |
| `disabled`       | 已禁用  |
| `not_configured` | 未配置  |

## MCP 服务器配置的管理（Server RPC）

除了会话之外，还可以在服务器级别管理 MCP 服务器的配置。

```php theme={null}
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\Rpc\McpConfigAddRequest;
use Revolution\Copilot\Types\Rpc\McpConfigUpdateRequest;
use Revolution\Copilot\Types\Rpc\McpConfigRemoveRequest;
use Revolution\Copilot\Types\Rpc\McpServerValue;

// 获取已配置的 MCP 服务器列表
$result = Copilot::client()->rpc()->mcp()->list();
foreach ($result->servers as $name => $config) {
    dump($name, $config->type, $config->command);
}

// 添加 MCP 服务器
Copilot::client()->rpc()->mcp()->add(new McpConfigAddRequest(
    name: 'laravel-boost',
    config: new McpServerValue(
        type: 'stdio',
        command: 'php',
        args: ['artisan', 'boost:mcp'],
        tools: ['*'],
    ),
));

// 更新 MCP 服务器配置
Copilot::client()->rpc()->mcp()->update(new McpConfigUpdateRequest(
    name: 'laravel-boost',
    config: new McpServerValue(
        type: 'stdio',
        command: 'php',
        args: ['artisan', 'boost:mcp', '--verbose'],
        tools: ['*'],
    ),
));

// 删除 MCP 服务器
Copilot::client()->rpc()->mcp()->remove(new McpConfigRemoveRequest(name: 'laravel-boost'));
```

<Info>
  最新信息请参考 [GitHub 仓库](https://github.com/invokable/laravel-copilot-sdk)。
</Info>


## Related topics

- [Laravel MCP](/zh/mcp.md)
- [用 Laravel 构建 MCP 服务器](/zh/advanced/mcp-server.md)
- [创建 Boost 的自定义 Agent](/zh/advanced/boost-custom-agent.md)
- [Laravel AI SDK](/zh/ai-sdk.md)
- [Laravel Boost Custom Agent for PhpStorm with GitHub Copilot](/zh/packages/laravel-boost-phpstorm-copilot.md)
