iT邦幫忙

2025 iThome 鐵人賽

DAY 15
0
AI & Data

30 天從 0 至 1 建立一個自已的 AI 學習工具人系列 第 15

30-15: [知識] - 可以讓 AI 工具人知道外面世界的工具 2 - MCP 原理

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20250929/20089358hEsWXYl6Ye.png

30-13: [知識] 可以讓 AI 工具人知道外面世界的工具 1 - Funcation Calling

Function Calling 讓我們 AI Agent 知道外部世界後,那接下來我們將來介紹另一個方法,那就是 :

Model Context Protocol

🚀 MCP(Model Context Protocol)的起源與幹啥用的

首先我們先來說說他的起源。

MCP ( Model Context Protocol ) 它是在 2024 年 11 月時,由 Anthropic 所提出的一個協定,它的官網寫這 :

MCP (Model Context Protocol) is an open-source standard for connecting AI applications to external systems.

簡單的說就是標準化 AI application 連到外部系統,這樣只要外部系統有根據這個協定來建立 sesrver,然後這些 AI application ( client ) 就可以用串接它了。

🤔 我用 Function calling 就好了,為啥還要用 MCP 呢?

對沒錯,很多功能你事實上用 Function Calling 就可以完成,那是你想想一個情境 :

如果其它公司或公司內其它 AI Agent 也想用你寫的 Function Calling 要怎麼辦呢 ?

對,這就是 MCP 的真的目的,它存在的目的就是為了 :

讓其它 Agent 可以更簡單的外接到外部的武器庫,而不用每一個都自已要重寫一次

就以 google calander 為範例,我們的確可以自已寫 function calling 然後裡面來呼叫 google calander api,來完成我們要的需求,但是如果你們公司其它 agent 也需要使用,那你是不是會 copy 一份過去 ?

但這時如果有個 google calander MCP,你只要去串他就好了。

https://ithelp.ithome.com.tw/upload/images/20250929/20089358B8gqLnjPzi.png

🚀 MCP Transports

https://modelcontextprotocol.io/specification/2025-06-18/basic/transports

這個就是說 client 與 server 要溝通的通信協定

然後在這裡支援以下兩個 :

  • Stdio : 它就是本地端 process 與 process 的溝通。
  • Streamable HTTP ( 以前是用 SSE ) : 就是我們常用的 client 與 server 透過 http 的溝通。

目前官方是建議儘量用 Stdio,雖然官網沒說詳細的原因,但主要的原因應該在於 :

我在猜是簡單、可靠、較安全與延遲表現都最好。

但是相對的,它的缺點就是像我們現在的桌面軟體,不論是更新、擴展都會比較慢一點。

然後順到說一下,大約在 2024-11-05 時,MCP 正式將原本的 HTTP + SSE 改成 Streamable HTTP,然後主要的原因應該是在性能,下面這一篇文章我覺得分析的很好,可以參考看看。

https://medium.com/@higress_ai/comparison-of-data-before-and-after-using-streamable-http-b094db8b414e

🚀 MCP Protocol 整個協定總列

整份主要的 protocol 內容在這份連結中 :

https://modelcontextprotocol.io/specification/2025-06-18

然後我們來理解一下協定的詳細內容 ~

首先根據這份連結,這個協定總共分五個部份 :

  1. Base Protocol: Core JSON-RPC message types
  2. Lifecycle Management: Connection initialization, capability negotiation, and session control
  3. Authorization: Authentication and authorization framework for HTTP-based transports
  4. Server Features: Resources, prompts, and tools exposed by servers
  5. Client Features: Sampling and root directory lists provided by clients
  6. Utilities: Cross-cutting concerns like logging and argument completion

然後接下來我們一個一個來看。

🚀 MCP Protocol - Base Protocol

整個 protocol 的溝通 payload 結構是以 JSON-RPC 2.0 為主

json-rpc 2.0 specification

下面就是 client 與 server 在初次見面時,server 回傳給 client 用 json-rpc 2.0 寫的 message payload 內容範例,內容就是和 client 說我這個 server 有支援那些功能。

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "protocolVersion": "2024-11-05",
    "capabilities": {
      "logging": {},
      "prompts": {
        "listChanged": true
      },
      "resources": {
        "subscribe": true,
        "listChanged": true
      },
      "tools": {
        "listChanged": true
      }
    },
    "serverInfo": {
      "name": "ExampleServer",
      "title": "Example Server Display Name",
      "version": "1.0.0"
    },
    "instructions": "Optional instructions for the client"
  }
}

🚀 MCP Protocol - Lifecycle Management

整個溝通流程 ( 生命周期 ),需要如下圖所示。

https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle

總共有三個階段 :

  • Initialization : 這個階段就是 client 與 server 雙方的自我介紹,client 會用 capabilities 這欄位說明支援什麼功能,而 server 也是會用 capabilities 說明支援什麼功能 ( 可以點進去看 payload 就知道 )。
  • Operation : 這個就是中間 client 與 server 互相溝通的階段,就是 client 呼叫 server 問有那些 tools 可以用,或是執行這個 tool 時的階段。
  • Shutdown : 就是 Shutdown。

https://ithelp.ithome.com.tw/upload/images/20250910/200893589HBgTAAVYx.png
圖片來源: 官網

🚀 MCP Protocol - Authorization

如果 MCP 需要 Authorization 功能,那要使用 OAuth 2.0

https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization

然後以下列幾個重點 :

  • 它是 optional。
  • 標準相容 OAuth 2.0

大概就降 Oauth 2.0 的流程就自已進去看看,然後下圖是我自已透過 AI 產生 Oauth 2.0 的流程,不熟的人可以看看,基本上和官網中的那個差不多,只是因為它不是圖片而且用是黑的,所以才再產一個。

https://ithelp.ithome.com.tw/upload/images/20250929/20089358tn5vcI1kc0.png
圖片來源: 官網

🚀 MCP Protocol - Server Features

就是 MCP Server 端要實作支援的功能,主要有 Resources、Prompts、Tools

https://modelcontextprotocol.io/specification/2025-06-18/server

主要有三大項 :

  • Prompts: 它主要是 MCP Server 提供 prompt template 給 MCP Client,讓它可以用這個 prompt template 送給 LLM。
  • Resources: 它要主要是 MCP Server 提供 resource 給 MCP Client,然後它會將這些 resource 當成 context 送給 LLM。
  • Tools: 就是我們這個 MCP Server 要支援給 AI 呼叫的 tools,運作流程幾乎與 function calling 相同。

然後我們下面假設我們現在要開發一個需求 :

購買 hahow 課程 ( 先說好這個我們家沒做,只是我腦補如果要做了會長什麼樣子 )

然後來看一下,以上三個東西在整個流程的部份,來幫助我們更能理解這三個的實際功用 :

  1. 第一個藍色區塊,主要是準備 prompt template。
  2. 第二個橘色區塊,主要是準備 context 上下文 ( 就是讓 LLM 判斷的資訊 )。
  3. 第三個亮綠色區塊,就是將 1 與 2 準備好的東西送給 LLM,請他推薦要買的課程。
  4. 第四個藍色區塊,就將執行整個購買流程。

整個 MCP Server 所需支援的三大功能,大概在整個 MCP 流程中就是這樣使用。

https://ithelp.ithome.com.tw/upload/images/20250929/20089358WFRPFcIUIK.png

🚀 MCP Protocol - Client Features

就是 MCP Client 要支援的功能,主要有三個 Sampling、Root、Elicitation

  • Root : 允許 MCP Server 可以控制 client 的位置,例如 /data,就是指 server 可以去這個地方取得到資料。
  • Sampling : 允許 MCP Server 呼叫 client 端的 LLM 幫他做一些事情。
  • Elicitation : 允許 MCP Server 詢問 client 的用戶以獲取額外資訊。

下面這個是我以這個範例來當這三個東西的展示。

hahow 課程審核 ( 先說好這個我們家沒做,只是我腦補如果要做了會長什麼樣子 )

先說一下,下面的說明,不是每一個區塊都會說,只會說有用到RootSamplingElicitation的部份才會說說。

  1. 直接從橘色開始看,他就是 client 提供給 server 說他有那些 root 你可以用。這裡就是用到。
  2. 接下來亮綠色的部份,就是 server 去這些地方取得到檔案,這裡應該會有點困惑,為什麼只是給檔案位置 server 就可以取得到呢 ? 因為現在 MCP 實際上支援有種通信模式 StdioStreamable HTTP,然後這裡的範例是 Stdio,所以也就是 client 與 server 是在同一個地方,因此可以直接取得到檔案。( 可以想想用 claude desktop 時你使用 MCP 的情況,是不是要指定 MCP Server 的檔案位置 )
  3. 再來是粉紅色的部份,他就是 MCP Server 請 MCP client 叫 LLM 做些事情 ( MCP Server 事實上裡面可以沒有 LLM 就可以做事了,因為它是做給 LLM 用的 )。
  4. 亮藍色的部份就是,當 server 發現有些地方要問人類金主時,會用 Elicitation 這個來處理這件事情。

https://ithelp.ithome.com.tw/upload/images/20250929/20089358629phmLF3H.png

🚀 LangChain 的最簡單的使用範例

這個程式碼是直接從官網抓來的,事實上與使用 Function Calling 的用法沒差多少,只差在 tools 是自已寫,還是用 MCP Client 產出來的。

import { MultiServerMCPClient } from "@langchain/mcp-adapters";
import { ChatAnthropic } from "@langchain/anthropic";
import { createAgent } from "langchain";

const client = new MultiServerMCPClient({
    math: {
        transport: "stdio",  // Local subprocess communication
        command: "node",
        // Replace with absolute path to your math_server.js file
        args: ["/path/to/math_server.js"],
    },
    weather: {
        transport: "sse",  // Server-Sent Events for streaming
        // Ensure you start your weather server on port 8000
        url: "http://localhost:8000/mcp",
    },
});

const tools = await client.getTools();
const agent = createAgent({
    llm: new ChatAnthropic({ model: "claude-3-7-sonnet-latest" }),
    tools,
});

const mathResponse = await agent.invoke({
    messages: [{ role: "user", content: "what's (3 + 5) x 12?" }],
});

const weatherResponse = await agent.invoke({
    messages: [{ role: "user", content: "what is the weather in nyc?" }],
});

🚀 小總結

這篇文章中我們學會了整個 MCP 的原本,簡單的總結如下 :

  • MCP 是用來給 LLM 增加遠端與外部武器庫用的,他是可以替代 Function Calling 的一些功能,例如外面第三方場早就有人寫好的 MCP Server,那的確不一定要寫 Function Calling 自已幹一個。但也不代表不需要 function calling 例如你沒有要分享的功能之類的。
  • MCP Transports 有支援 Streamable HTTP 與 Stdio。
  • MCP Protocol 要實作的地方。

接下來我們明天將來實作看看 MCP 的功能到我們的 AI 工具人上。

🚀 參考資料


上一篇
30-14: [實作-6] 透過 Function Calling 來實現取得昨日學習狀況
下一篇
30-16: [實作-7] 將我們的學習記錄,記錄到 Notion 之 1 ( MCP Server )
系列文
30 天從 0 至 1 建立一個自已的 AI 學習工具人16
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言