MCP 工具實戰:5 個開源神器讓 AI Agent 連線全世界

MCP 工具實戰:5 個開源神器讓 AI Agent 連線全世界

一、為什麼需要 MCP 工具?

在 MCP 協議出現之前,讓 AI 存取外部資料就像在黑暗中摸索:

  • 每個工具都有自己的 API 格式
  • 認證方式千差萬別(OAuth、API Key、JWT…)
  • 錯誤處理沒有統一標準
  • 除錯?基本靠猜

MCP 的出現改變了這一切。它定義了一套標準介面,讓任何 AI 模型都能以相同的方式與外部工具互動。就像 USB-C 統一了充電介面,MCP 正在統一 AI 與世界的連線方式。


二、5 款必試的 MCP 開源工具

1️⃣ MCP FileSystem Server - 讓 AI 讀寫你的檔案

GitHub: https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem

這是最基礎也最實用的 MCP 工具之一。安裝後,AI 可以安全地存取指定目錄的檔案。

安裝步驟

# 使用 npm 安裝
npm install -g @modelcontextprotocol/server-filesystem

# 建立配置檔案 ~/.mcp-config.json
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/yourname/documents"],
      "env": {}
    }
  }
}

功能列表

  • read_file - 讀取檔案內容
  • write_file - 寫入檔案
  • list_directory - 列出目錄內容
  • search_files - 搜尋檔案
  • create_directory - 建立目錄

使用範例

# AI 可以透過 MCP 呼叫
result = mcp_client.call_tool("filesystem", "read_file", {
    "path": "/home/yourname/documents/notes.md"
})
print(result.content)

安全提示:只開放必要的目錄,不要直接掛載根目錄 /


2️⃣ MCP PostgreSQL Server - 資料庫自然語言查詢

GitHub: https://github.com/modelcontextprotocol/servers/tree/main/src/postgres

讓 AI 直接用自然語言查詢資料庫,無需編寫 SQL。

安裝配置

npm install -g @modelcontextprotocol/server-postgres

# 配置檔案
{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://user:***@localhost:5432/mydb"],
      "env": {}
    }
  }
}

實戰場景

使用者:幫我找出上個月銷售額最高的前 10 個產品

AI (透過 MCP): 
  → 呼叫 postgres.read_query
  → 自動生成並執行 SQL
  → 返回結構化結果

支援的操作

  • 執行唯讀查詢(安全模式)
  • 獲取表結構資訊
  • 列出所有表名
  • 引數化查詢防止 SQL 注入

3️⃣ MCP GitHub Server - 程式碼倉庫智慧管理

GitHub: https://github.com/modelcontextprotocol/servers/tree/main/src/github

讓 AI 幫你管理 GitHub 倉庫,從檢視 Issue 到建立 PR。

安裝步驟

npm install -g @modelcontextprotocol/server-github

# 需要 GitHub Personal Access Token
# 存取 https://github.com/settings/tokens 建立 token
# 權限:repo, read:user, user:email

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "«redacted:ghp_…»"
      }
    }
  }
}

核心功能

  • search_repositories - 搜尋倉庫
  • get_issue - 獲取 Issue 詳情
  • create_issue - 建立新 Issue
  • list_pull_requests - 檢視 PR 列表
  • get_file_contents - 讀取檔案內容
  • create_branch - 建立新分支

自動化範例

場景:自動整理 Issue

AI 工作流:
1. 呼叫 github.list_issues(repo="myproject", state="open")
2. 分析每個 Issue 的內容和標籤
3. 呼叫 github.update_issue() 新增分類標籤
4. 為高優先順序 Issue 建立摘要文件

4️⃣ MCP Puppeteer Server - 網頁抓取與自動化

GitHub: https://github.com/modelcontextprotocol/servers/tree/main/src/puppeteer

讓 AI 能夠存取實時網頁內容,進行資料採集和自動化操作。

安裝配置

npm install -g @modelcontextprotocol/server-puppeteer

{
  "mcpServers": {
    "puppeteer": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-puppeteer"],
      "env": {}
    }
  }
}

功能亮點

  • puppeteer_navigate - 開啟網頁
  • puppeteer_screenshot - 截取頁面截圖
  • puppeteer_click - 點擊元素
  • puppeteer_fill - 填寫表單
  • puppeteer_evaluate - 執行 JavaScript

實戰案例:競品價格監控

# AI 自動執行
pages = [
    "https://example.com/product/1",
    "https://example.com/product/2"
]

for url in pages:
    mcp.call("puppeteer", "navigate", {"url": url})
    content = mcp.call("puppeteer", "evaluate", {
        "script": "document.querySelector('.price').textContent"
    })
    # 記錄價格資料

5️⃣ MCP Git Server - 版本控制自動化

GitHub: https://github.com/modelcontextprotocol/servers/tree/main/src/git

讓 AI 理解並操作 Git 倉庫,實現智慧程式碼管理。

安裝

npm install -g @modelcontextprotocol/server-git

{
  "mcpServers": {
    "git": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-git"],
      "env": {}
    }
  }
}

支援命令

  • git_status - 檢視倉庫狀態
  • git_diff - 檢視程式碼變更
  • git_log - 檢視提交歷史
  • git_commit - 建立提交
  • git_branch - 管理分支

智慧提交範例

場景:自動生成 Commit Message

AI 工作流:
1. 呼叫 git.diff() 獲取變更內容
2. 分析程式碼變更型別(功能/修復/重構)
3. 生成符合 Conventional Commits 的提交資訊
4. 呼叫 git.commit() 執行提交

三、快速開始:10 分鐘搭建你的第一個 MCP Agent

步驟 1:安裝 MCP Host

推薦使用 Claude Desktop 或自訂 Host:

# 使用官方 CLI
npm install -g @modelcontextprotocol/cli

# 或使用 Python
pip install mcp

步驟 2:配置 MCP Servers

建立 ~/.config/claude/mcp.json

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/yourname/projects"]
    },
    "git": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-git"]
    }
  }
}

步驟 3:測試連線

# 列出可用的工具
mcp list-tools

# 測試單個工具
mcp call-tool filesystem read_file --path /home/yourname/projects/README.md

步驟 4:開始對話

現在你可以用自然語言與 AI 互動:

"幫我檢視 projects 目錄下所有 Python 檔案,統計程式碼行數"
"找出最近修改的 5 個檔案,生成變更摘要"
"為 README.md 新增安裝說明部分"

四、最佳實踐與安全建議

✅ 推薦做法

  1. 最小權限原則:只開放必要的目錄和資源
  2. 環境變數管理:使用 .env 檔案儲存敏感資訊
  3. 日誌審計:記錄所有 MCP 工具呼叫
  4. 版本鎖定:使用固定版本號而非 latest

❌ 避免的陷阱

  1. 不要開放根目錄/ 是禁區
  2. 不要硬編碼密碼:使用環境變數或金鑰管理
  3. 不要信任所有輸入:驗證檔案路徑和查詢引數
  4. 不要忽略錯誤處理:MCP 呼叫可能失敗

五、進階:自訂 MCP Server

如果現有工具不滿足需求,可以自己編寫:

// 最簡單的 MCP Server 範例
import { Server } from "@modelcontextprotocol/sdk/server";

const server = new Server({
  name: "my-custom-server",
  version: "1.0.0"
});

server.setRequestHandler("tools/call", async (request) => {
  if (request.params.name === "hello") {
    return {
      content: [{ type: "text", text: "Hello from MCP!" }]
    };
  }
});

server.listen();

官方 SDK


六、總結與展望

MCP 生態系統正在快速發展:

工具型別成熟度推薦指數
檔案系統⭐⭐⭐⭐⭐必裝
資料庫⭐⭐⭐⭐強烈推薦
GitHub⭐⭐⭐⭐開發者必備
網頁抓取⭐⭐⭐按需使用
Git⭐⭐⭐⭐開發者必備

未來趨勢

  • 更多官方 Servers 發佈(Docker、Kubernetes、AWS…)
  • 企業級 MCP 閘道和權限管理
  • MCP 協議標準化組織成立
  • AI Agent 市場出現(可組合的 MCP 工具鏈)

資源連結


下一步行動

  1. 選擇 1-2 個工具立即嘗試
  2. 配置到你的 AI助手(Claude Desktop / Cursor / Windsurf)
  3. 分享你的使用案例到社群

MCP 不是未來,而是現在。開始構建你的智慧 Agent 吧!