什么是MCP?
Model Context Protocol(MCP)
是一种开放协议,旨在标准化大型语言模型(LLM)与外部数据源和工具之间的通信。它由 Anthropic 推出,允许 LLM 与服务器进行交互,以获取信息、执行操作并完成更复杂的任务。
调用外部工具
安装MCP SDK
bash
pnpm add @modelcontextprotocol/sdk
创建MCP
服务器和客户端
ts
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
const transport = new StdioClientTransport({
command: "npx",
args: [
"-y",
"@modelcontextprotocol/server-filesystem",
`${process.cwd()}/test`
],
stderr: process.stderr,
});
const client = new Client(
{
name: "fs",
version: "1.0.0",
}
);
await client.connect(transport);
代码解释:
-
new StdioClientTransport()
- 创建一个新的
StdioClientTransport
实例,用于通过标准输入输出流(stdin/stdout)与 MCP 服务器进行通信。
- 创建一个新的
-
command: "npx"
- 指定要运行的命令是
npx
,它是 Node.js 包运行工具,用于运行安装在项目中的 npm 包的命令行工具。
- 指定要运行的命令是
-
args
-
args
是一个数组,包含传递给命令的参数:"-y"
:通常用于自动确认提示,避免交互式输入。"@modelcontextprotocol/server-filesystem"
:指定要运行的 MCP 文件系统服务器包。${process.cwd()}/test
:使用模板字符串插入当前工作目录下的test
文件夹路径,作为 MCP 文件系统服务器的工作目录。
-
-
stderr: process.stderr
- 将 MCP 服务器的标准错误输出(stderr)重定向到当前 Node.js 进程的标准错误输出,以便在控制台中查看错误信息。
在test
目录中创建一个index.js
文件,然后通过LLM
调用工具读取它的内容
ts
import { generateText, jsonSchema, tool } from "ai";
const fsTool = {
description: "读取一个本地文件的内容",
parameters: jsonSchema({
type: "object",
properties: {
path: { type: "string", description: "文件路径" },
},
required: ["path"],
}),
execute: async (args) => {
const result = await client.callTool({
name: "read_file",
arguments: args,
});
return JSON.stringify(result);
},
};
const model = this.agent.createQWenModel();
const result = await generateText({
model,
tools: {
fsTool
},
toolChoice: 'required',
prompt: "test文件夹中index.js的内容是什么?"
});
console.log(result.toolResults);
成功调用fsTool
读取了index.js
的内容
json
[
{
"type": "tool-result",
"toolCallId": "call-zpJwmR2spvsUYiIppilkzHi6m0xe9aLp",
"toolName": "fsTool",
"args": {
"path": "test/index.js"
},
"result": "{\"content\":[{\"type\":\"text\",\"text\":\"function main() {\\n console.log('main');\\n}\"}]}"
}
]
MCP Tool
转换成AI SDK Tool
AISDK MCP Bridge
是一个桥接包,它使得模型MCP
和AI SDK
之间能够实现无缝集成,允许MCP
服务器和AI模型之间进行高效的通信和工具调用。
安装AISDK MCP Bridge
bash
pnpm add aisdk-mcp-bridge
使用AISDK MCP Bridge
我们只需创建mcp.config.json
配置文件
json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"./test"
]
}
}
}
通过桥接器自动将MCP Tool
转换成AI SDK Tool
ts
try {
await initializeMcp({ debug: true });
const tools = await getMcpTools({ debug: true });
const model = this.agent.createQWenModel();
const result = await generateText({
model,
tools,
toolChoice: 'required',
prompt: "test文件夹中index.js的内容是什么?"
});
console.log(result.toolResults);
} finally {
await cleanupMcp();
}
实现了相同的效果
json
[
{
"type": "tool-result",
"toolCallId": "call-ZnX8AdWB32Pp4nT1unF5Dxpeh7AYcIo7",
"toolName": "read_file",
"args": {
"path": "test/index.js"
},
"result": {
"type": "function",
"function": {
"name": "read_file",
"arguments": "{\"path\":\"test/index.js\"}"
},
"content": [
{
"type": "text",
"text": "function main() {\n console.log('main');\n}"
}
]
}
}
]
从市场中寻找可用的MCP Servers
json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"./test"
]
},
"docker-mcp": {
"command": "uvx",
"args": [
"docker-mcp"
]
}
}
}
ts
try {
await initializeMcp({ debug: true });
const tools = await getMcpTools({ debug: true });
const model = this.agent.createQWenModel();
const result = await generateText({
model,
tools,
toolChoice: 'required',
prompt: "使用docker运行一个Nginx容器"
});
console.log(result.toolResults);
} finally {
await cleanupMcp();
}
json
[
{
"type": "tool-result",
"toolCallId": "call-RkyU2khsEoXlXVq6C5SH4DNw3CrCaQCu",
"toolName": "create-container",
"args": {
"image": "nginx",
"name": "nginx_container"
},
"result": {
"type": "function",
"function": {
"name": "create-container",
"arguments": "{\"image\":\"nginx\",\"name\":\"nginx_container\"}"
},
"content": [
{
"type": "text",
"text": "Created container 'nginx_container' (ID: b6bb7f0fa3a924f3d86b49de90561f3d0bff2172b49b42594bde0703948df269)"
}
]
}
}
]
成功运行了一个Nginx
容器
总结
MCP 协议为 LLM 与外部工具和数据源的集成提供了标准化、高效且安全的解决方案,通过 MCP SDK 和 AISDK MCP Bridge 等工具,可以方便地创建和调用各种 MCP 服务器,实现 LLM 在不同场景下的应用扩展,如文件操作、容器管理等,为智能体的开发和集成带来了新的可能性和便利性。