全部文章

MCP 模型上下文协议

Model Context Protocol 概念、服务器配置、常用 MCP 服务器与自定义开发

目录 28 节

MCP 模型上下文协议

MCP(Model Context Protocol) 是 Anthropic 推出的开放协议,让 AI 助手能够连接外部数据源和工具。

这页适合作为“MCP 概念与接入入口”。很多人第一次接触 MCP 时最容易混淆的是:它不是某个具体工具,而是一套把模型、工具、数据和客户端连接起来的标准方式。

核心概念

MCP 采用客户端-服务器架构:

AI 助手(客户端)  ←→  MCP 服务器  ←→  外部服务/数据

MCP 服务器可以提供三种能力:

  • Resources:向 AI 暴露数据和内容(文件、数据库记录等)
  • Tools:让 AI 调用外部功能(API 调用、计算等)
  • Prompts:预定义的提示词模板

Skills 和 MCP 的区别

很多人在刚接触 AI Agent 时,会把 skillsMCP 混在一起。最简单的理解方式是:

概念更像什么解决的问题
Skills可复用的方法论 / 操作手册告诉 Agent“该怎么做”
MCP标准化工具接口让 Agent“能够做什么”

也就是说:

  • Skills 适合封装固定工作流、代码规范、发布检查单、排障步骤
  • MCP 适合连接文件系统、GitHub、数据库、浏览器、Docker、SaaS API 等外部能力
  • 两者通常一起使用:Skills 管流程,MCP 管执行能力

如果你想把重复任务沉淀成 SKILL.md、脚本和参考资料,可以继续看 AI Agent Skills 指南

配置

Kiro

// .kiro/settings/mcp.json
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"],
      "disabled": false,
      "autoApprove": ["read_file", "list_directory"],
    },
  },
}

Cursor

// .cursor/mcp.json
{
  "mcpServers": {
    "sqlite": {
      "command": "uvx",
      "args": ["mcp-server-sqlite", "--db-path", "./data.db"],
    },
  },
}

Claude Desktop

// %APPDATA%/Claude/claude_desktop_config.json (Windows)
// ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)
{
  "mcpServers": {
    "brave-search": {
      "command": "npx",
      "args": ["-y", "@anthropic-ai/mcp-server-brave-search"],
      "env": {
        "BRAVE_API_KEY": "your-api-key",
      },
    },
  },
}

常用 MCP 服务器

开发工具

文件系统

{
  "filesystem": {
    "command": "npx",
    "args": ["-y", "@modelcontextprotocol/server-filesystem", "./"],
  },
}

Git

{
  "git": {
    "command": "uvx",
    "args": ["mcp-server-git", "--repository", "."],
  },
}

GitHub

{
  "github": {
    "command": "npx",
    "args": ["-y", "@modelcontextprotocol/server-github"],
    "env": {
      "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxx",
    },
  },
}

Playwright(浏览器自动化)

{
  "playwright": {
    "command": "npx",
    "args": ["-y", "@anthropic-ai/mcp-server-playwright"],
  },
}

Context7(代码文档)

为 AI 提供最新的库文档和代码示例:

{
  "context7": {
    "command": "npx",
    "args": ["-y", "@upstash/context7-mcp@latest"],
  },
}

Sequential Thinking(思维链)

让 AI 进行结构化的多步推理:

{
  "sequential-thinking": {
    "command": "npx",
    "args": ["-y", "@modelcontextprotocol/server-sequential-thinking"],
  },
}

容器化 MCP(Docker)

当某个 MCP Server 依赖复杂、团队成员系统不一致,或你希望把依赖隔离在容器里时,可以直接让客户端通过 docker run 启动 MCP Server。

{
  "my-dockerized-server": {
    "command": "docker",
    "args": ["run", "--rm", "-i", "ghcr.io/acme/my-mcp-server:latest"]
  }
}

这样做的优点:

  • 本机不需要额外安装一堆运行时依赖
  • 团队可以共享统一镜像,降低“我这里能跑”的问题
  • 更容易把访问权限、环境变量和版本固定到同一份配置中

如果你使用较新的 Docker Desktop,还可以关注官方提供的 Docker MCP Toolkit。相关内容可参考 Docker Desktop 安装与使用

数据库

PostgreSQL

{
  "postgres": {
    "command": "npx",
    "args": ["-y", "@modelcontextprotocol/server-postgres"],
    "env": {
      "POSTGRES_CONNECTION_STRING": "postgresql://user:pass@localhost:5432/mydb",
    },
  },
}

Supabase

{
  "supabase": {
    "command": "npx",
    "args": [
      "-y",
      "@supabase/mcp-server-supabase@latest",
      "--access-token",
      "your-token",
    ],
  },
}

SQLite

{
  "sqlite": {
    "command": "uvx",
    "args": ["mcp-server-sqlite", "--db-path", "./data.db"],
  },
}

搜索与网络

{
  "brave-search": {
    "command": "npx",
    "args": ["-y", "@anthropic-ai/mcp-server-brave-search"],
    "env": {
      "BRAVE_API_KEY": "your-key",
    },
  },
}

Fetch(网页抓取)

{
  "fetch": {
    "command": "uvx",
    "args": ["mcp-server-fetch"],
  },
}

Firecrawl(结构化网页数据)

{
  "firecrawl": {
    "command": "npx",
    "args": ["-y", "firecrawl-mcp"],
    "env": {
      "FIRECRAWL_API_KEY": "your-key",
    },
  },
}

生产力工具

Notion

{
  "notion": {
    "command": "npx",
    "args": ["-y", "@notionhq/notion-mcp-server"],
    "env": {
      "OPENAPI_MCP_HEADERS": "{\"Authorization\":\"Bearer ntn_xxx\",\"Notion-Version\":\"2022-06-28\"}",
    },
  },
}

Slack

{
  "slack": {
    "command": "npx",
    "args": ["-y", "@anthropic-ai/mcp-server-slack"],
    "env": {
      "SLACK_BOT_TOKEN": "xoxb-xxx",
    },
  },
}

知识与记忆

Memory(知识图谱)

{
  "memory": {
    "command": "npx",
    "args": ["-y", "@modelcontextprotocol/server-memory"],
  },
}

热门服务器排行

根据社区使用量和 GitHub Stars 排名:

服务器用途推荐度
Context7代码库文档查询⭐⭐⭐⭐⭐
Playwright浏览器自动化⭐⭐⭐⭐⭐
Sequential Thinking结构化推理⭐⭐⭐⭐⭐
GitHub代码仓库管理⭐⭐⭐⭐
Filesystem文件读写⭐⭐⭐⭐
Brave Search网络搜索⭐⭐⭐⭐
Supabase数据库操作⭐⭐⭐⭐
Notion知识库管理⭐⭐⭐⭐
Fetch网页内容获取⭐⭐⭐
Memory持久化记忆⭐⭐⭐

自定义 MCP 服务器

TypeScript 实现

pnpm add @modelcontextprotocol/sdk
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

const server = new McpServer({
  name: "my-mcp-server",
  version: "1.0.0",
});

// 定义工具
server.tool(
  "get_weather",
  "获取天气信息",
  {
    city: z.string().describe("城市名称"),
  },
  async ({ city }) => {
    const weather = await fetchWeather(city);
    return {
      content: [{ type: "text", text: JSON.stringify(weather) }],
    };
  },
);

// 定义资源
server.resource("config", "config://app", async (uri) => ({
  contents: [
    {
      uri: uri.href,
      mimeType: "application/json",
      text: JSON.stringify(config),
    },
  ],
}));

// 启动
const transport = new StdioServerTransport();
await server.connect(transport);

Python 实现

pip install mcp
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("my-server")

@mcp.tool()
def get_weather(city: str) -> str:
    """获取天气信息"""
    # 实现逻辑
    return f"{city} 的天气:晴,25°C"

@mcp.resource("config://app")
def get_config() -> str:
    """应用配置"""
    return json.dumps(config)

if __name__ == "__main__":
    mcp.run()

调试

MCP Inspector

npx @modelcontextprotocol/inspector npx -y my-mcp-server

浏览器打开 Inspector 界面,可以测试工具调用和资源访问。

日志

{
  "my-server": {
    "command": "node",
    "args": ["server.js"],
    "env": {
      "DEBUG": "mcp:*",
      "FASTMCP_LOG_LEVEL": "DEBUG",
    },
  },
}

安全注意事项

  • MCP 服务器以本地进程运行,拥有系统权限
  • 谨慎配置 autoApprove,敏感操作应手动确认
  • 不要在 MCP 配置中硬编码密钥,使用环境变量
  • 审查第三方 MCP 服务器的源码

推荐上手顺序

建议按这个顺序理解和落地:

  1. 先理解 MCP 的客户端 / 服务器关系
  2. 再接一个最简单的文件系统或搜索服务器
  3. 再接 GitHub、数据库、浏览器等更强工具
  4. 再开始写自己的服务器
  5. 最后再做团队级配置和权限治理

常见问题

MCP 和插件 / SDK 是什么关系

可以把 MCP 理解成更偏“标准化接线方式”,而插件和 SDK 更像“具体实现和接入载体”。

为什么接了很多 MCP,代理还是不好用

通常不是工具太少,而是:

  • 工具太多但没有明确用途
  • 权限太大或太小
  • 规则和工作流没跟上
  • 客户端没有把工具调用策略管好

一开始该接哪些 MCP

大多数开发者的起步组合通常是:

  • 文件系统
  • GitHub
  • 搜索 / Fetch
  • 文档类服务

延伸阅读

参考链接

阅读建议
  • - 先读标题和摘要,再结合目录决定从哪个章节开始精读。
  • - 看到具体命令、配置或步骤时,尽量在自己的环境里同步验证。
  • - 如果你只是快速查资料,可先看目录和相关文档,再决定是否深入全文。
适合谁看
  • - 希望把零散经验整理成长期可复用工作流的人
  • - 正在使用 AI 工具、Agent 或自动化工作流的人
  • - 希望阅读时顺手建立自己的操作清单或收藏体系的人
执行前检查
  • - 先浏览标题、摘要和目录,带着问题阅读会更高效
  • - 确认模型供应商、API Key、CLI 工具链与本地资源是否已准备好
  • - 如果页面里提到相关文档,尽量一起打开对照,效果通常更完整
同类内容
← 上一篇MCP 服务器开发指南