前言
在了解**Model Context Protocol(MCP)**前,首先需要先了解function calling,一般也理解為Tool Use。
為什么會有function calling?
大模型有幾個問題:
1)沒有最新的信息,因為訓練的數據是某個時間點之前的,屬于歷史數據。
2)沒有真正的邏輯能力,因為大模型返回的結果是基于訓練數據的統計規律和概率匹配,而非嚴格的邏輯推理或形式化證明,即使加上了Jason Wei大佬提出的 ?鏈式思維(Chain of Thought,CoT?) 將大問題拆成小問題,以一定的邏輯順序解決問題,但實質還是數據驅動的模式匹配,而非真正的認知推理。對于復雜的數學邏輯問題還是較為吃力。
3)大模型沒與真實世界溝通的橋梁,就像人腦沒有雙手只能思考不能辦事。
基于上述的一些問題,業內提出了Function Calling的概念,緩解了上述的問題,比如通過Function Calling訪問最新的數據和私有的數據,為解決復雜的數據邏輯計算,可以通過tools use去調用專業的數學庫,因為打通了和現實世界溝通的橋梁,大模型能做很多實際的現實工作。Function Calling的大體工作邏輯如下圖:

一些說明
1,首先會開發好一個外部可調用的API function,這個服務可以是做Math復雜數據計算的工具,或是具體的業務操作,或是外部的最新數據源訪問等等能力。
2,這個function會定義為一個Tool,然后會有Prompt提示詞一起注冊到大模型中,讓模型明白這個Tool是干啥的。
3,用戶發起調用后,大模型按用戶的要求選擇function,并按情況填充function的參數請求API返回結果。
4,在大模型獲得function返回的結果后,按提示詞渲染和組裝結果返回給用戶。
這里會有一個問題,那就是不同的功能,不同的場景需要按情況開發不同的function,每個開發者或組織都會有自己的function,不一樣的規劃不一樣的格式。因此,MCP的主要目的就是這個,統一協議,統一標準。 官方文檔說明如下:
Think of MCP like a USB-C port for AI applications. Just as USB-C provides a standardized way to connect your devices to various peripherals and accessories,
MCP provides a standardized way to connect AI models to different data sources and tools.
關鍵字: like a USB-C (不能每個手機廠商都自己的接口吧~), standardized(標準化的;規范化的) 。簡言之,MCP是給function calling提供的一種標準化和規劃化的協議。
MCP技術是什么
上文已經說的很清楚了,MCP是給function call提供的一種標準化和規劃化的協議。下面是官方MCP架構的圖片:

一些說明
1,不需要理解太復雜,主要注意三個元語:Tool(工具),Resource(資源),Prompt(提示模板)。
Tool(工具):允許服務器公開可執行的函數,這些函數可由客戶端調用并由 LLM 使用來執行操作。使服務器能夠向客戶端暴露可執行的功能。通過工具,LLM 可以與外部系統交互、執行計算并在現實世界中采取行動。
Resource(資源):服務器提供給客戶端的任何類型的只讀數據。包括不限于:文件內容、數據庫記錄、圖片、日志等等。
Prompt(提示模板):這個才開始有點難理解,其實就是動態提示詞,可以通過外部給AI模型溝通的“導航儀”,告訴模型“做什么”,明確“如何做”,確保輸出更精準。
2,?理解了MCP之前的RAG(Retrieval-Augmented Generation檢索增強生成)技術,Tool Use等需要和大模型交互的功能均統一可以使用MCP對接,而不需要單獨的能力?。
3,上圖的有些術語,MCP 客戶端(MCP Clients),MCP 服務器(MCP Servers),本地資源(Local Resources),遠程資源(Remote Resources)。
MCP 客戶端(MCP Clients):發起請求的AI應用程序,比如聊天機器人,Agent。
MCP 服務器(MCP Servers):為 MCP客戶端提供Tools,Resource和Prompt的服務。
本地資源(Local Resources):可供 MCP 服務器安全訪問的資源,如文件,數據庫。
遠程資源(Remote Resources):MCP 服務器可以連接到的遠程資源,如通過API提供的數據,比如天氣,股票,新聞熱點。
Demo實現
官方文檔提供了Python ,Typescript,Java,kotlin這4中語音的SDK。這里使用大家比較熟悉的Java Spring AI MCP簡單試用下Tools能力。
代碼
詳細例子可以查看SpringAI MCP 官方文檔 。首先要求JDK版本至少為17。
# 1,maven依賴
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
</dependency>
# 2,初始化
@SpringBootApplication
public class McpserverApplication {
public static void main(String[] args) {
SpringApplication.run(McpserverApplication.class, args);
}
@Bean
public ToolCallbackProvider wechatTools(WeChatService weChatService,EmailService emailService) {
return MethodToolCallbackProvider.builder().toolObjects(weChatService,emailService).build();
}
public record TextInput(String input) {
}
@Bean
public ToolCallback toUpperCase() {
return FunctionToolCallback.builder("toUpperCase", (TextInput input) -> input.input().toUpperCase())
.inputType(TextInput.class)
.description("Put the text to upper case")
.build();
}
}
# 3,開發Tools
//--郵件Tool
@Service
public class EmailService {
@Resource
private EmailConfig emailConfig;
@Tool(description = "使用郵件工具給target發送郵件")
public String postEmailMsg(@ToolParam(description = "target目標郵箱") String target,
@ToolParam(description = "發送內容") String content) {
List<String> to = new ArrayList<>();
to.add(target);
String subject = "AI MCP郵箱測試";
boolean flag = EmailUtils.send(emailConfig, to, subject, content,
emailConfig.getContentType(), null);
System.out.println("郵箱發送結果flag=" + flag + "\n,target=" + target + "\n,content=" + content);
return "send email result:" + flag;
}
}
//--企微機器人Tool
@Service
public class WeChatService {
@Value("${wechat.reboot.url}")
private String wechatRebootUrl;
@Tool(description = "給微信群推送內容為content的消息")
public String postWeChatMsg(@ToolParam String content) {
RestClient restClient = RestClient.create();
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("msgtype", "text");
Map<String, Object> contentBody = new HashMap<>();
contentBody.put("content", content);
requestBody.put("text", contentBody);
// 發送 POST 請求
var responseTmp = restClient.post()
.uri(wechatRebootUrl)
.header(Headers.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(requestBody).retrieve();
System.out.println("wechat發送結果" + responseTmp.toString());
return responseTmp.toString();
}
}
# 4,配置文件
server.port=8113
# Server identification
spring.ai.mcp.server.name=aiagent-demo-server
spring.ai.mcp.server.version=0.0.1
# Server type (SYNC/ASYNC)
spring.ai.mcp.server.type=SYNC
# mcp有兩種通訊方式stdio(內部進程通訊)和Server-Sent Events (SSE 服務器發送事件),
spring.ai.mcp.server.stdio=false
spring.ai.mcp.server.sse-message-endpoint=/mcp/message
spring.ai.mcp.server.enabled=true
# Change notifications
spring.ai.mcp.server.resource-change-notification=true
spring.ai.mcp.server.tool-change-notification=true
spring.ai.mcp.server.prompt-change-notification=true
# Logging (required for STDIO transport)
spring.main.banner-mode=off
logging.file.name=../logs/server.log
# 5,啟動服務

測試
client調用
客戶端調用代碼同樣使用SpringAI,較為簡單,具體參考官方文檔例子。
# 1,maven依賴
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
# 2,初始化
@SpringBootApplication
public class McpclientApplication {
public static void main(String[] args) {
SpringApplication.run(McpclientApplication.class, args);
}
@Value("${ai.user.input}")
private String userInput;
@Bean
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools,
ConfigurableApplicationContext context) {
return args -> {
var chatClient = chatClientBuilder
.defaultTools(tools)
.build();
System.out.println("\n>>> QUESTION: " + userInput);
System.out.println("\n>>> ASSISTANT: " + chatClient.prompt(userInput).call().content());
context.close();
};
}
}
# 3,配置文件
server.port=8114
spring.main.web-application-type=none
spring.ai.ollama.base-url=${ollama服務的IP}:11434
spring.ai.ollama.chat.options.model=qwen2.5:14b
spring.ai.ollama.chat.options.temperature=0.7
spring.ai.mcp.client.sse.connections.server1.url=${mcp server的IP}:8113
ai.user.input=簡單說明下RAG結束并發送到郵箱xxx@chinatelecom.cn
# 4,啟動服務
結果


Langflow
Langflow需要配置MCP sse的Tool組件,如圖:



Dify
Dify需要安裝MCP sse的Tool,且要求版本在1.0.0+以上。如圖


總結
0,MCP源于function calling。
1,?MCP是給function calling提供的一種標準化和規劃化的協議。統一協議,統一標準。大模型的實際觸發還是走的function calling?。
2,MCP需要注意三個元語:?**Tool(工具),Resource(資源),Prompt(提示模板)**?。
3,MCP社區開發了大量統一的MCP Server,不過MCP概念尚未普及,主流的開放平臺還未兼容MCP協議,需要自己包裝和開發。