App集成MCP协议,实现大模型AI自定义工具

本文将以“在 App 中集成 MCP 协议并自定义 AI 工具调用”为主题,介绍如何使用 MCP(Model Context Protocol)协议,将用户的自然语言请求自动解析为工具函数调用,并通过远程大模型(如 DeepSeek 或 ChatGPT)完成智能化响应。

什么是 MCP(Model Context Protocol)?

MCP 是一种让大语言模型具备“结构化调用外部工具能力”的协议。它允许你定义工具(函数)并将其结构化描述注入模型上下文中,当模型理解用户意图后即可自动生成调用参数,执行工具逻辑并返回结果。

步骤

  • 在 iOS App 中集成 MCP SwiftSDK(Android Kotlin版本
  • 自定义工具,如天气查询 getWeather(city: String)
  • 用户自然语言如“查一下北京天气” → 自动匹配工具并解析参数
  • 最终调用远程模型(DeepSeek 或 ChatGPT)完成推理响应

工具调用流程简图

1
2
3
4
5
6
用户输入  -> MCP SDK 发送请求
-> MCP Server 构造 prompt
-> 调用远程 LLM(如 DeepSeek)
-> 模型生成 Tool 调用结构体
-> App 执行工具方法(如查询天气)
-> 返回结果渲染给用户

工程结构 & 架构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
+-----------------------------+
| Swift App (MCP SDK) |
| +------------------------+ |
| | getWeatherTool() | |
| +------------------------+ |
| | |
| MCP 协议 & Context |
+-------------|---------------+
v
+-----------------------------+
| MCP Server (Node.js) |
| -> 提供工具 JSON Schema |
| -> 构造上下文 Prompt |
| -> 调用 DeepSeek 或 ChatGPT |
+-------------|---------------+
v
+-----------------------------+
| 远程大模型服务 |
| (ChatGPT / DeepSeek / etc)|
+-----------------------------+

在服务端创建 MCP servers

可直接使用官网服务项目:https://github.com/modelcontextprotocol/servers。
快速启动方式(Node.js 环境):

1
2
3
4
5
git clone https://github.com/modelcontextprotocol/server
cd server
cp .env.example .env
npm install
npm run dev

然后编辑 config.ts 配置你的模型服务商(如 DeepSeek、OpenAI或本地olloma部署):

1
2
3
4
5
6
7
export const config = {
completion: {
provider: 'openai',
apiKey: '你的 API KEY',
apiBaseUrl: 'https://api.deepseek.com/v1'
}
}

Swift 中定义工具

使用 Swift SDK 定义一个工具,如 getWeather

1
2
3
4
5
6
7
8
9
10
11
12
13
struct GetWeatherTool: Tool {
static var name: String = "getWeather"
static var description: String = "获取某个城市的天气"

struct Input: Codable {
let city: String
}

func call(input: Input) async throws -> String {
// 这里代码省略,实际为需要自定义处理的代码逻辑
return "\(input.city) 今天多云,气温 25°C。"
}
}

App调用MCP SDK发送请求

1
2
3
4
5
6
7
8
9
10
11
let context = Context()
context.addTool(GetWeatherTool())

let connection = try await SSEConnection(url: URL(string: "https://yourdomain.com/mcp")!)
let client = try await Client(connection: connection)

let request = Request(messages: [.user("查一下杭州的天气")])

for try await output in try await client.generate(request: request, context: context) {
print(output.content)
}

模型如何自动识别工具和参数?

MCP 会将工具结构描述转换为 JSON Schema,注入上下文,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"name": "getWeather",
"description": "获取某个城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "要查询天气的城市"
}
}
}
}

这里可能大家有疑问,比如用户说:“请帮我查一下北京的天气”,模型怎么知道去调用 GetWeatherTool类呢?

首先我们在 GetWeatherTool中的定义一定要尽可能明确,比如:

1
2
static var name: String = "getWeather" // 不要定义 gWeather不明确定义
static var description: String = "获取某个城市的天气" // 这里尽可能描述清晰意图

大语言模型通过语义理解,分析:
- 动作是:查天气 → 匹配到工具 getWeather
- 城市是:“北京” → 匹配到 city 参数
就能推断出:

1
2
3
4
5
6
{
"tool_name": "getWeather",
"parameters": {
"city": "北京"
}
}

App 适用场景

  • 构建具备“智能操作能力”的 AI 助理
  • 模型驱动的业务自动化(如搜索、下单等业务场景)
  • 工具响应可插拔、可扩展
  • 模型自动“理解 + 调用 + 返回”全链路闭环