# 策展 · X (Twitter) 🔥🔥🔥

> 作者：Philipp Schmid (@_philschmid) · 平台：X (Twitter) · 日期：2026-04-27

> 原始來源：https://x.com/_philschmid/status/2048781360643375434

## 中文摘要

# 如何正確地在你的 AI Agent 中使用 MCP servers

MCP servers 並沒有過時。但盲目地啟用它們會導致你的 context 變得臃腫，進而導致更高的成本與更差的效能。與 Agent Skills 不同，MCP servers 並沒有內建「漸進式揭露」(progressive disclosure) 的機制。你有責任為手邊的任務選擇所需的工具。以下是兩種已被驗證、能正確使用 MCP servers 並避免臃腫的模式。

## 1. 明確的 MCP Servers：內嵌工具注入 (Inline Tool Injection)

MCP servers 應保持「選擇性啟用」。伺服器在你的 Prompt 中透過 `@mention` 進行參照，Agent 會在模型請求發送前解析、獲取並注入這些工具。

![](https://pub-75d4fe1e4e80421b9ecb1245a7ae0d1a.r2.dev/curated/1777307597051-iaHG66Tzwa0AAJgfTjpg.jpg)

在 Prompt 中輸入 `@github` 或 `@slack` 會觸發 Agent 執行以下動作：

1. 將 `@mention` 解析為已註冊的 MCP server URL。

2. 從伺服器獲取工具的 schema。

3. 將它們注入到 API 請求的 `tools[]` 陣列中。

4. 將所有內容轉發給模型，由模型決定呼叫哪個工具。

除非有需求，否則不會載入任何東西。工具的表面積 (tool surface) 保持在最小範圍。

```typescript
// Pseudo-code
async function handlePrompt(prompt: string) {
  // Detect @mentions in the prompt
  const mentions = parseMentions(prompt); // ["@github", "@slack"]
  
  // Resolve each mention to an MCP server
  const servers = mentions.map(m => mcpServers.resolve(m));
  
  // Fetch tool schemas from each server
  const mcpTools = await Promise.all(
    servers.map(s => s.listTools())
  );
  
  // Inject into the API request alongside native tools
  const response = await llmCall({
    prompt,
    tools: [...nativeTools, ...mcpTools],
  });
  
  // handle response, call tools, etc ... 
}
```

使用時機：當 MCP 的使用是偶發且由使用者驅動時。當有人提出需要 Slack 或 GitHub 資料的問題時，他們會 `@mention` 該伺服器，而該伺服器僅會針對該次請求被載入。這能降低成本，並避免在不需要外部整合的任務中產生工具雜訊。

## 2. Subagent MCP Servers

MCP servers 在 Subagent 的定義中宣告，並在執行時期與 `read_file` 或 `bash` 等原生工具一同自動啟用。

![](https://pub-75d4fe1e4e80421b9ecb1245a7ae0d1a.r2.dev/curated/1777307597020-iaHG66d2yWsAAFjxTjpg.jpg)

每個 Subagent 都有自己的配置，宣告了其模型、工具以及選用的 MCP servers。你可以使用 `allowed_tools` 將 MCP servers 的範圍限制在特定的工具上。

```yaml
# Pseudo-code: code_reviewer.md
---
name: code-reviewer
model: gemini-3-flash
mcp_servers:
  - url: https://github-mcp.example
    allowed_tools:
      - list_pulls
      - list_reviews
      - get_diff
---
You are a code reviewer. Review open PRs...
```

主 Agent 會註冊這些 Subagents。當被呼叫時，每個 Subagent 會連接到其宣告的 MCP servers，並將這些工具與原生工具合併。

```typescript
// Pseudo-code

// Register subagents as tools for the orchestrator
const codeReviewer = createSubagent("./agents/code_reviewer.md");
const slackMonitor = createSubagent("./agents/slack_monitor.md");

async function handlePrompt(prompt: string) {

  // pre-processing ...

  const response = await llmCall({
    prompt,
    tools: [...nativeTools, codeReviewer, slackMonitor],
  });

  // handle response, call tools, etc ...
}
```

`allowed_tools` 讓你能在不分叉 (fork) MCP server 的情況下，實現最小權限原則的範圍限制。

使用時機：當使用情境決定了工具需求時。例如，程式碼審查 Agent 總是需要 GitHub，而客服 Agent 總是需要 Zendesk。此時 MCP servers 是 Agent 本身的一部分，而不是使用者在每次請求時才選擇啟用的項目。`allowed_tools` 確保了每個 Agent 的範圍僅限於它真正需要的工具。

## 標籤

MCP, Agent, 教學資源, Anthropic, Claude
