过去一年里,Function Call 成为最常用的“让 AI 调用代码”的方式。
但是当你的业务变复杂,比如:
Function Call 就开始吃力了。
于是, MCP出现了,它解决了 Function Call 的所有局限,成为下一代 AI 工具调用标准。
本文将通过 Node.js 实例,让你直观理解两者区别。
Function Call:
模型提示你:“我要调用这个函数”,然后你负责执行。
MCP:
模型自动发现和调用已注册的“工具”,开发者只负责定义工具,不负责执行。
一句话:
Function
Call:模型说“我需要工具”,你来执行。
MCP:模型自己执行工具,你只负责提供工具。
我们以“查询北京天气”为例进行对比。
目标:
让用户问:“北京天气怎么样?”
模型自动获取天气数据,并返回结果。
function_call/
├── app.
js
└── weather.
js
weather.js:
import axios
from
"axios";
export
async
function
getWeather(
city) {
const apiKey =
"your_api_key_here";
const url =
`https://api.open-meteo.com/v1/forecast?latitude=39.9&longitude=116.4&hourly=temperature_2m`;
const resp =
await axios.
get(url);
return
`当前温度:
${resp.data.hourly.temperature_2m[
0]}°C`;
}
app.js:
import {
ChatOpenAI }
from
"@langchain/openai";
import { getWeather }
from
"./weather.js";
const tools = [
{
type:
"function",
function: {
name:
"get_weather",
description:
"获取指定城市的天气信息",
parameters: {
type:
"object",
properties: {
city: {
type:
"string" }
},
required: [
"city"]
}
}
}
];
const llm =
new
ChatOpenAI({
model:
"gpt-4o-mini"
}).
bind({ tools });
async
function
run(
) {
const res =
await llm.
invoke(
"北京天气怎么样");
if (res.
tool_calls?.
length >
0) {
const { city } = res.
tool_calls[
0].
args;
// 你负责执行工具
const weather =
await
getWeather(city);
console.
log(
"返回天气:", weather);
}
}
run();
你必须:
当你的工具越来越多时,维护成本越来越高。
在 MCP 中,你只需要定义工具,模型会:
✔ 自动发现工具
✔ 自动决定什么时候调用
✔ MCPClient 自动执行
✔ 你不需要任何其它代码
新建:
mcp-weather-server.js
import {
Server }
from
"@modelcontextprotocol/sdk/server";
import axios
from
"axios";
const server =
new
Server({
name:
"weather-service",
version:
"1.0",
});
// 注册自定义工具
server.
tool(
"get_weather",
"获取天气",
{
type:
"object",
properties: {
city: {
type:
"string" }
},
required: [
"city"]
},
async ({ city }) => {
// 你可以接数据库/公司内网 API 都行
const url =
`https://api.open-meteo.com/v1/forecast?latitude=39.9&longitude=116.4&hourly=temperature_2m`;
const resp =
await axios.
get(url);
const temp = resp.
data.
hourly.
temperature_2m[
0];
return {
weather:
`
${city}当前温度:
${temp}°C` };
}
);
server.
start();
app.js:
import {
ChatOpenAI }
from
"@langchain/openai";
import {
MCPClient }
from
"@langchain/langgraph/mcp";
import {
ChatPromptTemplate }
from
"@langchain/core/prompts";
// 连接 MCP server
const mcp =
new
MCPClient(
"stdio", {
command:
"node",
args: [
"./mcp-weather-server.js"]
});
// 绑定到模型(让模型自动调用 MCP 工具)
const llm =
new
ChatOpenAI({
model:
"gpt-4o-mini",
}).
bind({ mcp });
const prompt =
ChatPromptTemplate.
fromMessages([
[
"system",
"你可以使用 MCP 工具获取天气信息。"],
[
"human",
"{q}"]
]);
async
function
ask(
q) {
const res =
await prompt.
pipe(llm).
invoke({ q });
console.
log(
"模型回答:\n", res.
content);
}
ask(
"北京天气怎么样");
运行后输出类似:
模型回答:
北京当前温度:1°C
| 能力 | Function Call | MCP |
|---|---|---|
| 工具在哪里定义? | 代码里 | MCP Server 里 |
| 工具由谁调用? | 开发者手动调用 | 模型自动调用 |
| 工具能否自动发现? | ❌ 不行 | ✔️ 自动发现 |
| 是否支持文件系统 / DB / 本地脚本? | 你得自己写 | MCP 工具库直接支持 |
| 能否被多个模型共享? | ❌ 每个模型都要重新写 | ✔️ MCP 工具可复用 |
| 安全性和权限? | 手动处理 | MCP 内置权限管理 |
一句话:
Function
Call = 模型请求你,你再帮它调工具
MCP = 模型自己调工具,你只提供工具
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048945/viewspace-3109194/ ,如需转载,请注明出处,否则将追究法律责任。