> ## 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 的 session 設定與 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 伺服器狀態

Session 內 MCP 伺服器的狀態可能為以下值：

| 狀態               | 說明    |
| ---------------- | ----- |
| `connected`      | 已連線   |
| `failed`         | 連線失敗  |
| `needs-auth`     | 需要認證  |
| `pending`        | 等待連線中 |
| `disabled`       | 已停用   |
| `not_configured` | 未設定   |

## 管理 MCP 伺服器設定（Server RPC）

除了 session 之外，也可以在伺服器層級管理 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-TW/mcp.md)
- [用 Laravel 構建 MCP 伺服器](/zh-TW/advanced/mcp-server.md)
- [Laravel AI Agent 支援 MCP 伺服器](/zh-TW/blog/ai-sdk-mcp-client.md)
- [Laravel AI SDK](/zh-TW/ai-sdk.md)
- [建立 Boost 的自定義 Agent](/zh-TW/advanced/boost-custom-agent.md)
