
在人工智能飞速发展的今天,AI Agent(人工智能代理)正成为连接大模型与现实世界的关键桥梁。然而,传统的Agent开发长期面临一个核心挑战:模型(如ChatGPT、Claude、Gemini)与外部工具、数据源和环境之间缺乏一种标准化、高效的通信方式。
开发者常需为不同的模型重复编写适配代码,工具生态碎片化严重,这就像《圣经》中的巴别塔项目,因语言不通而难以协同。正是在这样的背景下,Anthropic 提出了 Model Context Protocol(MCP,模型上下文协议),旨在成为AI Agent开发领域的“通用语言”,打破隔阂,开启开发新范式。
本文将带你从零开始,全面解读MCP,并通过多场景实战,展示如何利用MCP构建强大、高效且可移植的AI Agent。
MCP是一个开放标准协议,它定义了大语言模型(LLM)与外部服务器(Servers)(如工具、数据库、API等)之间如何进行通信。其核心思想是解耦与标准化。
一个典型的MCP系统包含三个部分:
search_web, execute_code)。file:///path/to/file.txt, redis://key)。让我们通过一个简单的“Hello World”示例来感受MCP的开发流程。我们将创建一个提供当前时间查询工具的MCP服务器。
bash
mkdir my-first-mcp-server
cd my-first-mcp-server
npm init -yAnthropic提供了官方的MCP SDK来简化开发。
bash
npm install @modelcontextprotocol/sdkserver.js)javascript
const { Server } = require('@modelcontextprotocol/sdk/server/server.js');
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
const { Tool } = require('@modelcontextprotocol/sdk/types.js');
// 1. 创建一个MCP服务器实例
const server = new Server(
{
name: 'my-first-mcp-server',
version: '1.0.0',
},
{
capabilities: {
tools: {}, // 声明本服务器提供工具
},
}
);
// 2. 定义一个获取当前时间的工具
const getCurrentTimeTool = new Tool(
{
name: 'get_current_time',
description: '获取当前的日期和时间,适用于需要知道确切时间的场景。',
inputSchema: {
type: 'object',
properties: {
timezone: {
type: 'string',
description: '时区,例如 Asia/Shanghai 或 UTC,默认为本地时区',
},
},
},
},
async (input) => {
const timezone = input.timezone || undefined;
const now = new Date();
let timeString;
try {
timeString = now.toLocaleString('en-US', { timeZone: timezone });
} catch (error) {
// 如果时区无效,回退到本地时间
timeString = now.toLocaleString();
return {
content: [
{
type: 'text',
text: `无效时区,已使用本地时间。当前时间是: ${timeString}`,
},
],
};
}
return {
content: [
{
type: 'text',
text: `当前时间是: ${timeString}`,
},
],
};
}
);
// 3. 将工具注册到服务器
server.toolRegistry.register(getCurrentTimeTool);
// 4. 启动服务器,使用标准输入输出作为传输层
const transport = new StdioServerTransport();
server.connect(transport).then(() => {
console.error('My First MCP Server is running on stdio...');
});在Claude Desktop的配置文件中添加你的服务器。
~/Library/Application Support/Claude/)。claude_desktop_config.json。json
{
"mcpServers": {
"my-first-server": {
"command": "node",
"args": ["/ABSOLUTE/PATH/TO/your/server.js"]
}
}
}重启Claude Desktop后,你就可以在和Claude聊天时直接使用@get_current_time工具了!
掌握了基础之后,我们来看几个更有实战价值的场景。
目标:构建一个MCP服务器,让AI能读取、分析我们本地代码库的文件,并进行智能问答。
实现思路:
read_file、list_directory等工具。关键技术点:
fs 模块读取文件和目录。目标:连接企业内部的JIRA、Confluence、CRM等系统,打造一个一站式信息查询和处理助手。
实现思路:
search_issues, get_issue_details, create_issue 等工具。关键技术点:
目标:将AI与操作系统级的自动化(如AppleScript、Shell)结合。
实现思路:
run_apple_script, execute_shell_command(需极其谨慎)等工具。关键技术点:
try...catch包裹,并返回清晰的错误信息给模型和用户。Model Context Protocol (MCP) 通过引入标准化和解耦的设计哲学,真正为AI Agent开发带来了范式革新。它降低了开发门槛,促进了工具生态的繁荣,并最终让AI的能力更无缝、更安全地融入我们的数字生活。
从简单的数据查询到复杂的多系统 orchestration,MCP 为我们提供了实现这一切的统一蓝图。现在,正是深入学习并投身于这一新范式的最佳时机,共同构建下一代智能互联的应用体验。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。