返回 MCP 目录
public公开dns本地运行

example-mcp

该项目展示了Model Context Protocol(MCP)的示例实现,MCP是一种标准化协议,用于增强AI助手的能力,通过自定义工具和数据源扩展其功能。项目包含多个MCP服务器示例,如任务管理器、文件浏览器和天气服务,并提供了如何设置和使用这些服务器的指南。

article

README

🚀 光标模型上下文协议 (MCP) 示例

本仓库提供了模型上下文协议 (MCP) 服务器的示例实现,可用于增强光标 IDE 的功能。MCP 标准化了应用程序向大型语言模型 (LLMs) 提供上下文信息的方式,让您能创建自定义工具,与光标中的 AI 助手协同工作,强化人工智能功能。

🚀 快速开始

先决条件

  • Node.js(版本 >= 14)
  • npm 或 yarn 包管理器
  • 光标 IDE(版本 >= 2023.12)

设置 MCP 服务器

  1. 克隆此仓库:
git clone https://github.com/yourusername/example-mcp.git
cd example-mcp
  1. 安装依赖项:
npm install @modelcontextprotocol/server express
  1. 启动 MCP 服务器:
node index.js

在光标中连接 MCP 服务器

  1. 打开光标 IDE。
  2. 点击右上角的齿轮图标,进入设置菜单。
  3. 选择“上下文” > “模型上下文协议”。
  4. 点击“添加服务器”,输入以下信息:
    • 名称:示例 MCP 服务器
    • 类型:HTTP
    • 地址:http://localhost:3000
  5. 点击“保存”。

✨ 主要特性

什么是模型上下文协议?

模型上下文协议 (MCP) 是一个开放协议,它标准化了应用程序向大型语言模型 (LLMs) 提供上下文信息的方式。借助 MCP,您可以创建自定义工具,与光标中的 AI 助手配合使用,增强人工智能功能。

可以将 MCP 类比为设备和外设的 USB - C 端口:

  • 如同 USB - C 为设备连接各种外围设备提供了标准化方式,MCP 为 AI 模型连接不同数据源和工具提供了标准化方式。
  • 它使 AI 助手能够访问实时信息、执行专门功能,突破其训练数据的限制。

📦 安装指南

见“快速开始”部分的设置 MCP 服务器步骤。

💻 使用示例

基础用法

以下是启动 MCP 服务器的基本步骤,在快速开始部分已提及:

  1. 克隆仓库:
git clone https://github.com/yourusername/example-mcp.git
cd example-mcp
  1. 安装依赖:
npm install @modelcontextprotocol/server express
  1. 启动服务器:
node index.js

高级用法

示例 MCP 服务器

任务管理器
  • 名称task-manager
  • 描述创建、更新和删除任务。
  • 参数 schema
{
    "type": "object",
    "properties": {
        "action": { "type": "string", "enum": ["create", "update", "delete"] },
        "task": {
            "type": "object",
            "properties": {
                "id": { "type": "integer" },
                "title": { "type": "string" },
                "description": { "type": "string" }
            }
        }
    }
}
文件浏览器
  • 名称file-explorer
  • 描述浏览文件系统并执行操作。
  • 参数 schema
{
    "type": "object",
    "properties": {
        "action": { "type": "string", "enum": ["list", "open"] },
        "path": { "type": "string" }
    }
}
天气服务
  • 名称weather-service
  • 描述获取天气信息。
  • 参数 schema
{
    "type": "object",
    "properties": {
        "action": { "type": "string", "enum": ["get_weather"] },
        "location": { "type": "string" }
    }
}

📚 详细文档

项目结构

example-mcp/
├── mcp-servers/                # 不同的 MCP 服务器实现
│   ├── task-manager/           # 任务管理器 MCP 服务器示例
│   ├── file-explorer/          # 文件浏览器 MCP 服务器示例
│   └── weather-service/        # 天气服务 MCP 服务器示例
├── README.md                   # 项目文档
└── LICENSE                     # 许可证信息

MCP 架构

组件

  • MCP 主机:提供上下文信息的组件。
  • MCP 客户端:调用 MCP 服务的组件。
  • MCP 服务器:实现具体功能并响应请求的组件。

传输方式

Stdio
const { createServer } = require('@modelcontextprotocol/server');

createServer()
  .registerTool('task-manager', {
    name: 'Task Manager',
    description: 'Create, update and delete tasks.',
    paramsSchema: {
      type: 'object',
      properties: {
        action: { type: 'string', enum: ['create', 'update', 'delete'] },
        task: {
          type: 'object',
          properties: {
            id: { type: 'integer' },
            title: { type: 'string' },
            description: { type: 'string' }
          }
        }
      }
    }
  })
  .listen(3000, () => console.log('Server is running on port 3000'));
HTTP (SSE)
const express = require('express');
const { createServer } = require('@modelcontextprotocol/server');

const app = express();

createServer(app).registerTool('file-explorer', {
  name: 'File Explorer',
  description: 'Browse file system and perform operations.',
  paramsSchema: {
    type: 'object',
    properties: {
      action: { type: 'string', enum: ['list', 'open'] },
      path: { type: 'string' }
    }
  }
});

app.listen(3000, () => console.log('Server is running on port 3000'));

创建自己的 MCP 服务器

  1. 初始化项目:
mkdir my-mcp-server && cd $_
npm init -y
  1. 安装依赖:
npm install @modelcontextprotocol/server express
  1. 创建主文件 index.js
const express = require('express');
const { createServer } = require('@modelcontextprotocol/server');

const app = express();

createServer(app).registerTool('my-tool', {
  name: 'My Tool',
  description: 'Perform custom operations.',
  paramsSchema: {
    type: 'object',
    properties: {
      action: { type: 'string' },
      params: { type: 'object' }
    }
  }
});

app.listen(3000, () => console.log('Server is running on port 3000'));
  1. 启动服务器:
node index.js

📄 许可证

本项目遵循 MIT 许可证。查看 LICENSE 文件获取详细信息。

🔗 贡献者

  • [Your Name]
help

运行方式说明

cloud

托管运行

托管运行通常表示这个 MCP Server 由服务方环境承载,用户一般按页面提供的连接方式或授权流程接入,不需要在本地长期启动一个 MCP 进程

  1. 打开服务方连接页
  2. 完成授权或复制端点
  3. 在 MCP 客户端中连接
terminal

本地运行 / 其它方式

本地运行通常需要用户在自己的电脑或服务器上安装依赖,把 server_config 复制到 MCP 客户端,并按 env_schema 补齐环境变量、密钥或其它配置

  1. 复制 server_config
  2. 安装所需依赖
  3. 补齐环境变量后重启客户端