
MCP即模型上下文协议(Model Context Protocol),是由Anthropic公司于2024年11月推出的一种开放标准协议。它旨在为AI模型与外部数据源、工具之间构建统一的交互接口,类似于智能交互领域的“通用插头”,通过制定统一规范,实现AI模型与外部资源的无缝对接。
在MCP出现之前,大模型与数据的交互存在诸多问题,如物理隔离、协议碎片化、时效性缺失等。而MCP的提出,为AI交互带来了革命性的变化,它提供了一种标准化的框架,使得AI模型与外部工具和数据源的交互变得更加高效、安全和易于实现。

MCP遵循客户端 - 服务器架构,主要包含以下三个核心组件:

MCP Server当前支持三种传输类型,分别是stdio 传输、SSE 传输、Streamable HTTP,这两者区别如下:
MCP的SSE通信过程可以分为以下几个关键步骤:

(三)双向通信与采样特性
与传统API的单向通信不同,MCP支持双向交互。这不仅让AI应用能从服务器获取数据,还能让服务器利用客户端的AI能力(如LLM的补全或生成)。例如,服务器可以通过MCP请求客户端的LLM完成任务,并将上下文数据纳入提示中,从而实现嵌套的智能行为(如代理式行为)。
此外,MCP还具备采样特性。如果需要,服务器可以利用客户端的AI能力(例如调用LLM生成文本或图像),而无需直接持有API密钥。客户端保留对模型访问和权限的控制权,确保安全性与灵活性。
MCP在设计上注重安全性,数据访问受到严格控制。所有操作都需要用户授权,并且MCP服务通常部署在本地,避免了数据外泄的风险。同时,MCP的架构灵活,支持模块化开发,允许开发者扩展MCP,创建更多的数据源支持。
MCP的传输层支持多种传输方法,如HTTP/2或WebSocket。所有传输都使用JSON - RPC 2.0进行消息交换,这为MCP Clients和MCP Servers之间的通信提供了统一的消息格式。



不同的搭建方式可能需要不同的环境和工具,以下是一些常见的准备要求:
pip、uv(Rust版,需要Python 3.8+环境),Node.js的npm、npx等。fastapi、uvicorn、@modelcontextprotocol/sdk、zod等。以下是一些常见软件的安装步骤:
node -v
npm -v在云上一键部署MCP Server,无需配置运行环境,支持社区热门MCP Server和自定义MCP Server,提供完整托管功能。云端或本地的AI应用可随时随地调用MCP Server。

# 在Windows下
python -m venv venv
venv\Scripts\activate
# 在Linux下
python3 -m venv venv
source venv/bin/activate激活虚拟环境后,安装所需的依赖:
pip install mcp[cli] httpx创建helpers.py文件,编写辅助函数用于处理API请求和格式化数据:
from textwrap import dedent
import httpx
from typing import Any
import logging
USER_AGENT = "weather-app/1.0"
async def make_nws_request(url: str) -> dict[str, Any] | None:
"""向NWS API发出请求,并进行适当的错误处理。"""
headers = {
"User-Agent": USER_AGENT,
"Accept": "application/geo+json"
}
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, headers=headers, timeout=30.0)
response.raise_for_status()
return response.json()
except Exception as e:
logging.error(e)
return None创建tools.py文件,实现MCP工具:
from textwrap import dedent
from helpers import make_nws_request, format_alert
NWS_API_BASE = "https://api.weather.gov"
async def get_alerts(state: str) -> str:
"""获取美国某州的天气警报。
参数:
state: 两位字母的美国州代码(例如CA, NY)
"""
url = f"{NWS_API_BASE}/alerts/active/area/{state}"
data = await make_nws_request(url)
if not data or "features" not in data:
return "无法获取警报或没有找到警报。"
if not data["features"]:
return "该州目前没有活跃的警报。"
alerts = [format_alert(feature) for feature in data["features"]]
return "\n---\n".join(alerts)将如下代码添加到weather.py文件中:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("weather")
@mcp.tool()
async def get_alerts(state: str) -> str:
# 调用辅助函数获取数据
pass
@mcp.tool()
async def get_forecast(location: str) -> str:
# 调用辅助函数获取数据
pass
if __name__ == "__main__":
mcp.run(transport='stdio')uv run weather.py从nodejs.org下载并安装Node.js和npm,然后验证Node.js是否正确安装:
node --version
npm --versionmkdir pixabay
cd pixabay
npm init -ynpm install @modelcontextprotocol/sdk zod
npm install -D @types/node typescript添加以下主要配置项:
{
"type": "module",
"bin": {
"pixabay": "./build/index.js"
},
"scripts": {
"build": "tsc && chmod 755 build/index.js"
},
"files": [
"build"
]
}mkdir src
touch src/index.ts{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}在src/index.ts文件中添加以下代码:
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const baseUrl = "https://pixabay.com/api/";
const server = new McpServer({ name: "pixabay", version: "1.0.0", capabilities: { resources: {}, tools: {} } });
server.tool('pixabay-image-search', { query: z.string(), type: z.string() }, async ({ query, type = 'all' }) => {
try {
if (!process.env.PIXABAY_KEY) {
console.error("PIXABAY_KEY environment variable is not set");
process.exit(1);
}
const requestUrl = `${baseUrl}?key=${process.env.PIXABAY_KEY}&q=${query}&image_type=${type}&per_page=3`;
const response = await fetch(requestUrl);
const json = await response.json();
return { content: [{ type: 'text', text: JSON.stringify({ images: json.hits || [], total_results: json.total, query }, null, 2) }] };
} catch (e) {
// 错误处理
}
});
const transport = new StdioServerTransport();
server.start(transport);编译TypeScript代码:
npm run build运行服务器:
node build/index.jspip install uvicorn fastapifrom fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def 首页():
return { "message": "MCP 太酷了" }uvicorn main:app --reload安装fastapi-mcp:
pip install fastapi-mcp在FastAPI代码中添加:
from fastapi import FastAPI
from fastapi_mcp import add_mcp_server
app = FastAPI()
add_mcp_server(app, mount_path = "/mcp", name = "My API MCP")不同的MCP服务器在Cursor中的配置方式可能有所不同,以下是一些常见的配置步骤:
npm install -g @modelcontextprotocol/server-filesystemnode [包的安装路径]\node_modules@modelcontextprotocol\server-filesystem\dist\index.js [允许访问的目录路径]。设置完成后,点击保存,如果小按钮变为绿色,表示添加成功。以MySQL MCP Server为例,将以下配置添加到claude_desktop_config.json:
{
"mcpServers": {
"mysql": {
"command": "uv",
"args": [
"--directory",
"path/to/mysql_mcp_server",
"run",
"mysql_mcp_server"
],
"env": {
"MYSQL_HOST": "localhost",
"MYSQL_PORT": "3306",
"MYSQL_USER": "your_username",
"MYSQL_PASSWORD": "your_password",
"MYSQL_DATABASE": "your_database"
}
}
}
}1. 通过预置的 MCP Server 市场,支持一键进行安装

2. 自定义配置MCP SERVER ,以Time MCP Server为例,在 Craft_mcp_settings.json 配置文件中添加 Time MCP Server 服务器的配置

{
"mcpServers": {
"mcp-server-time": {
"command": "python",
"args": [
"-m",
"mcp_server_time",
"--local-timezone",
"Asia/Shanghai"
],
"disabled": false
}
}
}1. 通过预置的 MCP Server 市场,支持一键进行安装

2. 自定义配置MCP SERVER

名称 | 网址 | 特点 |
|---|---|---|
阿里云百炼 | https://bailian.console.aliyun.com/ | 2025 年 4 月 9 日上线业界首个全生命周期 MCP 服务,支持一键部署、无需运维,具备高可用与低成本特点,提供多类型供给、低成本托管及全链路工具兼容,帮助企业快速构建专属智能体,首批上线了高德、无影、Fetch、Notion 等 50 多款主流 MCP 服务。 |
腾讯云 | https://cloud.tencent.com.cn/ | 2025 年 4 月 12 日,腾讯云发布 Agent 之 MCP 协议,其知识引擎平台作为企业级大模型应用构建平台,升级支持 MCP 协议接入 MCP Server,工作文档精选的 MCP Server 包括腾讯生态内的多款 APP,如腾讯位置共享服务、腾讯云 Edge One Pages、微信读书等,也包括 Fetch 和 Airbnb 等第三方应用,内容覆盖专业知识获取、网页部署和预览、网页爬虫解析等场景。 |
字节跳动火山引擎 | https://www.volcengine.com/mcp-marketplace | 字节跳动推出 MCP Servers,即大模型生态广场,通过 “MCP Market(工具广场)+ 火山方舟(大模型服务)+ Trae(应用开发环境)” 的深度协同,实现工具调用、模型推理到应用部署的全链路开发闭环。其集成了众多高质量的 MCP 协议适配工具,涵盖搜索、数据库、业务系统 API 等高频场景,并支持开发者将自研工具按 MCP 协议封装并上传共享。 |
MCPmarket.cn | https://mcpmarket.cn/ | 已收录超过 7000 + 标准化的 MCP 工具,覆盖数据处理、内容生成、消息调度、系统控制等多个典型场景,为开发者汇集了全球范围内可接入的 MCP Server 工具资源。同时,该平台还为开发者提供中文教程文档和中文社群支持,并将陆续提供开箱即用的开发模板、调试工具,帮助开发者快速构建和测试 MCP 工具链,降低接入门槛、提升构建效率。 |
PulseMCP | https://github.com/pulse-mcp/pulse-mcp-server | 目前最大的 MCP 服务器集合平台,提供了超过 1150 个 MCP 服务器。 |
ClineMCP | https://cline-mcp-marketplace.vercel.app/ | 用户可以发现和使用各种 MCP 服务器资源,并且该平台提供详细的服务器描述和使用统计。如果在使用 Cline 插件,使用 Cline MCP Marketplace 安装 MCP 应用会很简便,只需点击 “下载” 按钮,Cline 会自动完成 MCP 服务器的安装和配置,无需手动处理复杂的 JSON 文件和依赖关系。 |
mcp.so | https://mcp.so/ | 专注于提供高质量 MCP 服务器的集合网站。 |
MCP Store | https://mcpstore.co/ | 聚合全球的 MCP Servers,为用户提供了丰富的选择,目前已经收录了 8632 个 MCP Servers。这些服务器涵盖多种功能,如网页搜索、地图服务、文件转换等。该网站是一个开放的 MCP 市场,对于 MCP 开发者来说,这是一个展示和分享自己开发的 MCP 服务器的平台。 |
ModelScope | https://modelscope.cn/ | 是国际化的模型开放平台,其 MCP 市场提供了多种多样的 MCP 服务。 |
Cursor Directory | https://cursor.dev/ | 主要是与 Cursor IDE 相关的 MCP 服务器集合。 |
Smithery.ai | https://smithery.ai/ | 提供了 500 + 开箱即用的 MCP 工具,涵盖 API、数据库和无头 CMS 工具等,如 google-docs、forms、maps 等 GCP 服务,还可以直接在平台上构建和部署应用程序。 |

sudo命令获取更高权限,但使用sudo时需谨慎操作。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。