> For the complete documentation index, see [llms.txt](https://docs.neuron-ai.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.neuron-ai.dev/neuron-v3-zh/agent/mcp-connector.md).

# MCP

MCP（模型上下文协议）是 Anthropic 设计的一项开源标准，用于将你的代理连接到外部服务提供商，例如你的应用数据库或外部 API。

借助该协议，你可以让外部服务器暴露的工具供你的代理使用。

公司可以构建 MCP 服务器，让开发者将 Agents 连接到他们的平台。以下是几个包含常用 MCP 服务器的目录：

* MCP 官方 GitHub - <https://github.com/modelcontextprotocol/servers>
* MCP-GET 注册表 - <https://mcp-get.com/>

### 工作原理

Neuron 为你提供 `McpConnector` 类，你可以传入 MCP 服务器配置来实例化它。

```php
use NeuronAI\MCP\McpConnector;

class MyAgent extends Agent 
{
    ...
    
    protected function tools(): array
    {
        return [
            ...McpConnector::make([
                'command' => 'php',
                'args' => ['/home/code/mcp_server.php'],
            ])->tools(),
        ];
    }
}
```

你应该为 `McpConnector` 你希望交互的每个 MCP 服务器创建一个实例。

Neuron 会自动发现服务器暴露的工具，并将它们连接到你的代理。

当代理决定运行某个工具时，Neuron 会生成适当的请求，在 MCP 服务器上调用该工具，并将结果返回给 LLM 以继续任务。它的体验与使用你自己定义的工具完全一样，但你只需一行代码，就能访问代理可执行的大量预定义操作库。

### 本地 MCP 服务器

如果你想连接安装在本机或虚拟机上的 MCP 服务器，可以使用“command”样式配置。

```php
use NeuronAI\MCP\McpConnector;

class MyAgent extends Agent 
{
    ...
    
    protected function tools(): array
    {
        return [
            ...McpConnector::make([
                'command' => 'php',
                'args' => ['/home/code/mcp_server.php'],
            ])->tools(),
        ];
    }
}
```

## 远程 MCP 服务器

### 可流式 HTTP 服务器

远程服务器可通过 URL 访问，通常需要身份验证。你可以在配置数组中使用 `token` 字段，它将作为在服务器上进行身份验证的授权令牌：

```php
use NeuronAI\MCP\McpConnector;

class MyAgent extends Agent 
{
    ...
    
    protected function tools(): array
    {
        return [
            ...McpConnector::make([
                'url' => 'https://mcp.example.com',
                'token' => 'BEARER_TOKEN',
                'timeout' => 30,
                'headers' => [
                    //'x-cutom-header' => 'value'
                ]
            ])->tools(),
        ];
    }
}
```

### SSE HTTP 传输

SSE（[服务器发送事件](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events)）是一种允许 Web 客户端从服务器接收自动更新的机制。这些更新称为“事件”，并通过单个长连接的 HTTP 连接发送。

要使用 SSE 传输，你需要设置 `async ⇒ true` 在配置参数中。

```php
use NeuronAI\MCP\McpConnector;

class MyAgent extends Agent 
{
    ...
    
    protected function tools(): array
    {
        return [
            ...McpConnector::make([
                'url' => 'https://mcp.example.com',
                'token' => 'BEARER_TOKEN',
                'timeout' => 30,
                'async' => true
            ])->tools(),
        ];
    }
}
```

## 监控与调试

使用 Neuron 构建的许多应用程序会包含多个步骤以及对 LLM 的多次调用。随着这些应用程序变得越来越复杂，能够检查你的代理系统内部到底发生了什么就变得至关重要。实现这一点的最佳方式是使用 [Inspector](https://inspector.dev/).

{% embed url="<https://docs.inspector.dev/guides/neuron-ai>" %}

## 过滤工具列表

在与复杂的 MCP 服务器连接时，它们可能包含在特定上下文中会导致不良行为的工具。 `exclude()` 和 `only()` 这些方法优雅地解决了这一挑战，使开发者能够连接到功能全面的 MCP 服务器，同时对你想提供给代理的可用能力保持细粒度控制。

当你处理需要特定能力的专用代理时，这一点尤其有用，因为你希望降低代理出错的概率，并减少 token 消耗。

这些方法接受一个工具名称列表，你可以选择将其关联或不关联到代理。

```php
class MyAgent extends Agent 
{
    ...
    
    protected function tools()
    {
        return [
            // EXCLUDE: 丢弃某些工具
            ...McpConnector::make([
                'url' => 'https://mcp.example.com',
            ])->exclude([
                'tool_name_1',
                'tool_name_2',
            ])->tools(),
            
            // ONLY: 选择你想要包含的工具
            ...McpConnector::make([
                'url' => 'https://mcp.example.com',
            ])->only([
                'tool_name_1',
                'tool_name_2',
            ])->tools(),
        ];
    }
}
```
