Function Calling是一种让大语言模型(LLM)能够与外部工具和系统交互的机制。它允许模型在需要时识别并调用适当的工具来获取信息或执行作。
json{
"name": "get_weather",
"arguments": {
"location": "北京"
}
}
MCP基于JSON-RPC 2.0标准:
json// 标准的MCP工具调用请求
{
"jsonrpc": "2.0",
"id": "1",
"method": "tools/call",
"params": {
"name": "get_weather",
"arguments": {
"location": "北京"
}
}
}
// 标准的响应格式
{
"jsonrpc": "2.0",
"id": "1",
"result": {
"temperature": "25°C",
"condition": "晴天"
}
}
// 标准的错误格式
{
"jsonrpc": "2.0",
"id": "1",
"error": {
"code": -32601,
"message": "Method not found"
}
}
json// 工具发现请求
{
"jsonrpc": "2.0",
"id": "2",
"method": "tools/list",
"params": {}
}
// 工具列表响应
{
"jsonrpc": "2.0",
"id": "2",
"result": [
{
"name": "get_weather",
"description": "获取指定地点的天气信息",
"inputSchema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "地点名称"
}
}
}
}
]
}
json// 初始化请求
{
"jsonrpc": "2.0",
"id": "init-1",
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {},
"resources": {}
},
"clientInfo": {
"name": "MyAIApp",
"version": "1.0.0"
}
}
}
Function Calling场景
MCP场景