ITPub博客

首页 > 应用开发 > Javascript > Function Call 和 MCP 的区别(举例子,一文说明白)

Function Call 和 MCP 的区别(举例子,一文说明白)

Javascript 作者:vvxdl844 时间:2026-01-28 10:01:13 0 删除 编辑

一、前言

过去一年里,Function Call 成为最常用的“让 AI 调用代码”的方式。
但是当你的业务变复杂,比如:

  • 访问数据库
  • 操作本地文件系统
  • 管理多个工具
  • 调用公司内部 API
  • 多个模型复用同一套工具

Function Call 就开始吃力了。

于是, MCP出现了,它解决了 Function Call 的所有局限,成为下一代 AI 工具调用标准。

本文将通过  Node.js 实例,让你直观理解两者区别。

二、概念总结:一句话说清两者区别

Function Call:
模型提示你:“我要调用这个函数”,然后你负责执行。

MCP:
模型自动发现和调用已注册的“工具”,开发者只负责定义工具,不负责执行。

一句话:


Function 
Call:模型说“我需要工具”,你来执行。
MCP:模型自己执行工具,你只负责提供工具。

三、用例对比:查询某个城市的天气

我们以“查询北京天气”为例进行对比。

目标:
让用户问:“北京天气怎么样?”
模型自动获取天气数据,并返回结果。

3.1 Function Call 示例(Node.js)

代码结构

function_call/
  ├── app.
js
  └── weather.
js

步骤1:你需要手写工具函数(核心)

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`;
}

步骤2:把工具绑定给模型(你要写)

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();

Function Call 的特点

你必须:

  • ❗手写 getWeather
  • ❗手动执行工具
  • ❗手动把结果反馈给模型
  • ❗每个模型都需要绑一遍 tools

当你的工具越来越多时,维护成本越来越高。

3.2 MCP 示例(Node.js)

在 MCP 中,你只需要定义工具,模型会:

✔ 自动发现工具
✔ 自动决定什么时候调用
✔ MCPClient 自动执行
✔ 你不需要任何其它代码

步骤1:定义一个 MCP 天气服务

新建: 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();

步骤2:模型自动调用 MCP 工具

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

MCP 的特点

  • 模型自动识别并调取 get_weather
  • MCPClient 负责执行工具并返回结果
  • 开发者不用写其它code(如 res.tool_calls)
  • 所有 MCP 支持的模型都能直接复用这个工具

四、核心区别(通过例子直接看出来)

能力 Function Call MCP
工具在哪里定义? 代码里 MCP Server 里
工具由谁调用? 开发者手动调用 模型自动调用
工具能否自动发现? ❌ 不行 ✔️ 自动发现
是否支持文件系统 / DB / 本地脚本? 你得自己写 MCP 工具库直接支持
能否被多个模型共享? ❌ 每个模型都要重新写 ✔️ MCP 工具可复用
安全性和权限? 手动处理 MCP 内置权限管理

一句话:

Function 
Call = 模型请求你,你再帮它调工具  

MCP = 模型自己调工具,你只提供工具

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048945/viewspace-3109194/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2026-01-05

  • 博文量
    40
  • 访问量
    41838