06 Function Calling&MCP
Summary
This material introduces Function Calling as a mechanism to extend large language models' capabilities by allowing them to invoke external tools for real-world interactions. It then delves into Model
Function Calling
Function Calling enables Large Language Models (LLMs) to interact with external systems and perform actions beyond their inherent linguistic abilities.
Role of Function Calling in LLMs
-
Extend Model Capabilities: Allows LLMs to acquire real-time data (weather, stock), perform complex calculations, or operate external systems (send emails, control devices).
-
Structured Output: Transforms natural language requests into structured function calls with specific parameters.
CodeExample:* "明天北京天气如何?" → `get_weather(location="北京", date="tomorrow")` -
Dynamic Decision Flow: Enables LLMs to decide when and how to call functions, even chaining multiple calls. It acts as a "bridge" between language understanding and concrete action.
Function Calling vs. MCP
Dimension | Function Calling | MCP (Model Context Protocol)
Positioning | Model vendor's private API (e.g., OpenAI, Qwen) | Open protocol (similar to HTTP/USB-C)
Scalability | Requires adaptation for each specific model | Develop once, compatible with multiple models
Complexity | Suitable for simple, single-call tasks | Supports multi-turn conversations, complex context management
Ecosystem Depend | Relies on a specific model (e.g., GPT-4) | Cross-model, cross-platform (e.g., Claude, Cursor)
Security | Relies on cloud API keys | Supports localized data control
When to Use Function Calling
Function Calling is more convenient for simple, atomic tasks:
- Querying weather:
get_weather(city="北京") - Calculating math:
calculate(expression="3+5") - Sending single notifications:
send_email(to="user@example.com")
Advantage: Fast development (no MCP Server needed), low latency. While MCP may become mainstream, Function Calling remains a fundamental capability.
CASE: Ticket Assistant
This case demonstrates building a ticket assistant using Qwen-Agent and Function Calling to query ticket sales data.
Overall Workflow:
- System Initialization: Set system prompt describing the
tkt_orderstable and common queries. - Tool Registration: Register an SQL query tool (
exc_sql). - Assistant Instantiation: Use
Qwen-Agent'sAssistantclass with LLM config, system prompt, andfunction_list(exc_sql). - Interaction Mode: Use WebUI for user input, where LLM generates SQL,
exc_sqlexecutes it, and results are displayed.
system_prompt Example:
system_prompt = """我是门票助手,以下是关于门票订单表相关的字段,我可能会编写对应的SQL,对数据进行查询
-- 门票订单表
CREATE TABLE tkt_orders (
order_time DATETIME, -- 订单日期
account_id INT, -- 预定用户ID
gov_id VARCHAR(18), -- 商品使用人ID(身份证号)
gender VARCHAR(10), -- 使用人性别
age INT, -- 年龄
province VARCHAR(30), -- 使用人省份
SKU VARCHAR(100), -- 商品SKU名
product_serial_no VARCHAR(30), -- 商品ID
eco_main_order_id VARCHAR(20), -- 订单ID
sales_channel VARCHAR(20), -- 销售渠道
status VARCHAR(30), -- 商品状态
order_value DECIMAL(10, 2), -- 订单金额
quantity INT -- 商品数量
);
"""
exc_sql Tool Registration:
from qwen_agent.tools.base import BaseTool, register_tool
import pandas as pd
import json
# Assuming 'engine' is pre-configured for database connection
@register_tool('exc_sql')
class ExcSQLTool(BaseTool):
"""
SQL查询工具,执行传入的SQL语句并返回结果。
"""
description = '对于生成的SQL,进行SQL查询'
parameters = [{
'name': 'sql_input',
'type': 'string',
'description': '生成的SQL语句',
'required': True
}]
def call(self, params: str, **kwargs) -> str:
args = json.loads(params)
sql_input = args['sql_input']
database = args.get('database', 'ubr') # Placeholder for actual DB connection logic
# Create database connection (engine = create_engine(...))
try:
# For demonstration, assume engine is available
df = pd.read_sql(sql_input, engine)
return df.head(10).to_markdown(index=False)
except Exception as e:
return f"SQL执行出错: {str(e)}"
Assistant Initialization:
from qwen_agent.agent import Assistant
def init_agent_service():
llm_cfg = {
'model': 'qwen-turbo-2025-04-28',
'timeout': 30,
'retry_count': 3,
}
try:
bot = Assistant(
llm=llm_cfg,
name='门票助手',
description='门票查询与订单分析',
system_message=system_prompt,
function_list=['exc_sql'], # Register tools by name
)
print("助手初始化成功!")
return bot
except Exception as e:
print(f"助手初始化失败: {str(e)}")
raise
WebUI Integration:
from qwen_agent.gui import WebUI
def app_gui():
bot = init_agent_service()
chatbot_config = {
'prompt.suggestions': [
'2023年4、5、6月一日门票,二日门票的销量多少?帮我按照周进行统计',
'2023年7月的不同省份的入园人数统计',
'帮我查看2023年10月1-7日销售渠道订单金额排名',
]
}
WebUI(
bot,
chatbot_config=chatbot_config
).run()
Visualizing Query Results
To enhance the ticket assistant, the exc_sql tool can be extended to include data visualization.
Strategy: Integrate plotting functionality directly into the exc_sql function rather than creating a separate tool. This avoids issues with passing large dataframes or complex plotting parameters between tools.
Implementation Steps (within exc_sql's call method):
-
SQL Query & Data Retrieval: Execute SQL, get a Pandas DataFrame (
df), and generate Markdown table (md). -
Automatic Field Inference:
-
X-axis: Prioritize the first
object(string/category) column. -
Y-axis: Select all
numbertype columns (supports multiple series).
-
-
Bar Chart Plotting: Use
matplotlibto create a bar chart. Adjust bar width and position for multiple Y-axis series. -
Chart Styling: Set labels, title, rotate X-axis labels for readability, add legend, and adjust layout.
-
Save & Return: Save the plot as a PNG image in a unique filename, generate a Markdown image reference (
), and return both the Markdown table and the image Markdown.
This integrated approach (assistant_ticket_bot-3) supports basic charts and multi-category pivot visualizations.
Model Context Protocol (MCP)
What is MCP?
Model Context Protocol (MCP) is an open protocol standard introduced by Anthropic in November 2024. Its goal is to standardize how LLMs interact with external data sources, tools, and services, often described as the "USB-C interface for AI."
MCP vs. Function Calling (Revisited)
MCP offers a more general, open, and platform-agnostic approach compared to Function Calling, which is typically proprietary to specific LLM providers. MCP aims for higher reusability and flexibility across different models and environments.
Core Concepts of MCP
-
Architecture and Components:
Code_MCP Host:_* The environment running the AI model (e.g., Claude Desktop, Cursor IDE). _MCP Client:_* An embedded component within the Host that initiates requests and communicates with the MCP Server. _MCP Server:_* A lightweight service providing specific functionalities (e.g., database queries, API calls) that AI models can invoke. -
Core Capabilities:
Code_Resources (Knowledge Extension):_* Provides structured data (databases, documents) to enhance AI's contextual understanding. _Tools (Tool Calling):_* Allows AI to execute external operations (send emails, query GitHub, call smart contracts). _Prompts (Prompt Templates):_* Pre-defined instruction templates to optimize AI task execution.
CASE: Travel Itinerary MCP (Amap Maps)
This case demonstrates using the Amap Maps MCP for generating travel itineraries.
Steps:
-
Obtain Amap Maps MCP Authorization: Register as a developer on lbs.amap.com, create an application, select Web services, and get an API KEY.
-
Configure MCP Client: Use a client with MCP capabilities (e.g., Cursor, Cherry Studio, GitHub Copilot).
Code_`mcp.json` Configuration Example (for Cursor/VS Code):_* ```json { "mcpServers": { "amap-maps": { "command": "npx", "args": [ "-y", "@amap/amap-maps-mcp-server" ], "env": { "AMAP_MAPS_API_KEY": "你的key" } } } } ``` _Client Setup (e.g., Cherry Studio):_* Install `uv`, `bun`. Search for `@amap` MCP service, add it, and ensure it's enabled. Configure an LLM (e.g., `qwen-turbo`) that supports tool calling and enable the MCP server for it. _Client Setup (e.g., GitHub Copilot in VS Code):_* Open `settings.json`, locate `mcpServers` section, and add the Amap Maps configuration. -
Interact: Ask the LLM (e.g., "用高德MCP,做上海一天旅游攻略") through the client, and it will use the Amap Maps MCP to generate the itinerary.
CASE: Tavily Search MCP
This case shows how to integrate Tavily, a real-time web search tool, via MCP.
Steps:
-
Add Tavily MCP Configuration:
JSON{ "mcpServers": { "tavily-mcp": { "args": [ "-y", "tavily-mcp@0.1.4" ], "autoApprove": [], "command": "npx", "disabled": false, "env": { "TAVILY_API_KEY": "your-api-key-here" } } } } -
Use MCP Client: In a client like Cursor, ask: "用Tavily MCP,查找黄金相关的新闻."
-
Integrate with Qwen-Agent: Add Tavily to the
function_listand provide relevantprompt.suggestionsinchatbot_config.
Tavily Tools Summary:
-
tavily-search(Real-time web search):-
query(string, required): Search keywords. -
max_results(int, optional): Number of results. -
search_depth(enum: 'basic' / 'advanced', optional): 'advanced' grabs and summarizes page content. -
include_answer(bool, optional): Request direct answer.
-
-
tavily-extract(Web page content extraction):-
urls(string or array of strings, required): Page addresses to extract. -
extract_depth(enum: 'basic' / 'advanced', optional): 'advanced' parses more details. -
format(enum: 'text' / 'markdown' / 'json', optional): Return format.
-
CASE: Desktop TXT Counter (MCP SDK Usage)
This case demonstrates building a local MCP server using Python MCP SDK to count and list desktop .txt files.
Python MCP SDK
pip install mcp
- Create MCP Server: Provides standardized API.
- Register Tools: Use
@mcp.tool()decorator to expose Python functions. - Secure Interaction: Supports permission control.
- Cross-platform: Integrates with LLMs like OpenAI, Anthropic Claude.
FastMCP
FastMCP is a lightweight server framework within the Python MCP SDK.
- Simple to Use: Start an MCP Server with few lines of code.
- Multiple Transport Options: Supports stdio, HTTP.
- Auto Tool Discovery: Automatically registers functions annotated with
@mcp.tool().
Code Example:
import os
from pathlib import Path
from mcp.server.fastmcp import FastMCP
# Create MCP Server
mcp = FastMCP("桌面TXT 文件统计器")
@mcp.tool()
def count_desktop_txt_files() -> int:
"""统计桌面上.txt 文件的数量"""
desktop_path = Path(os.path.expanduser("~/Desktop"))
txt_files = list(desktop_path.glob("*.txt"))
return len(txt_files)
@mcp.tool()
def list_desktop_txt_files() -> str:
"""获取桌面上所有.txt 文件的列表"""
desktop_path = Path(os.path.expanduser("~/Desktop"))
txt_files = list(desktop_path.glob("*.txt"))
if not txt_files:
return "桌面上没有找到.txt 文件。"
file_list = "\n".join([f"- {file.name}" for file in txt_files])
return f"在桌面上找到{len(txt_files)} 个.txt 文件:\n{file_list}"
if __name__ == "__main__":
# Initialize and run the server
mcp.run() # This typically runs in stdio mode by default
Running and Interacting:
- Run the server:
mcp dev txt_counter.py - Access MCP Inspector in browser:
http://localhost:5173/ - Connect, select tools (
count_desktop_txt_files,list_desktop_txt_files), and run them. Results and history are displayed.
MCP Inspector
MCP Inspector is an open-source debugging tool designed for MCP Servers.
- Server Connection Management: Supports local/remote servers, various transport methods (STDIO, HTTP+SSE), environment variables, and execution history.
- Service Browsing and Invocation: Provides a visual UI (default
http://localhost:5173) to display Tools, Resources, Prompts. Developers can directly call tools and observe results.
UV Tool
In the MCP protocol context, uv is often used to launch and manage MCP servers, particularly for running Python scripts as MCP servers within a specified directory.
Key Takeaways
- Function Calling extends LLM capabilities by allowing interaction with external systems for real-time data, complex computations, and operations.
- MCP (Model Context Protocol) is an open standard designed to unify how AI models interact with tools and data, aiming for broader compatibility and reusability across different platforms.
- Choose Function Calling for simple, atomic tasks due to its quick development and low latency, while MCP is better suited for complex, multi-turn interactions and cross-platform integration.
- MCP clients (e.g., Cursor, Cherry Studio, VS Code with Copilot) provide interfaces to integrate and use MCP services, configuring them via
mcp.jsonor built-in settings. - Python MCP SDK (with FastMCP) allows developers to easily create custom MCP servers and expose Python functions as tools for AI models to consume locally.