🚀 光标模型上下文协议 (MCP) 示例
本仓库提供了模型上下文协议(MCP)服务器的示例实现,可用于增强光标 IDE 的功能。MCP 能让应用程序向大型语言模型(LLMs)提供上下文信息,助力打造更强大的 AI 工具。
🚀 快速开始
先决条件
- Node.js(版本 >= 14)
- npm 或 yarn 包管理器
- 光标 IDE(版本 >= 2023.12)
设置 MCP 服务器
- 克隆此仓库:
git clone https://github.com/yourusername/example-mcp.git
cd example-mcp
- 安装依赖项:
npm install @modelcontextprotocol/server express
- 启动 MCP 服务器:
node index.js
在光标中连接 MCP 服务器
- 打开光标 IDE。
- 点击右上角的齿轮图标,进入设置菜单。
- 选择“上下文” > “模型上下文协议”。
- 点击“添加服务器”,输入以下信息:
- 名称:
示例 MCP 服务器
- 类型:
HTTP
- 地址:
http://localhost:3000
- 点击“保存”。
✨ 主要特性
模型上下文协议(MCP)是一个开放协议,它标准化了应用程序向大型语言模型(LLMs)提供上下文信息的方式。可以将 MCP 类比为 USB - C 端口:
- 如同 USB - C 为设备连接各种外设提供了标准化方式,MCP 为 AI 模型连接不同数据源和工具提供了标准化途径。
- 借助 MCP,AI 助手能访问实时信息、执行特定功能,突破训练数据的限制。
📦 安装指南
先决条件
- Node.js(版本 >= 14)
- npm 或 yarn 包管理器
- 光标 IDE(版本 >= 2023.12)
设置 MCP 服务器
- 克隆此仓库:
git clone https://github.com/yourusername/example-mcp.git
cd example-mcp
- 安装依赖项:
npm install @modelcontextprotocol/server express
- 启动 MCP 服务器:
node index.js
在光标中连接 MCP 服务器
- 打开光标 IDE。
- 点击右上角的齿轮图标,进入设置菜单。
- 选择“上下文” > “模型上下文协议”。
- 点击“添加服务器”,输入以下信息:
- 名称:
示例 MCP 服务器
- 类型:
HTTP
- 地址:
http://localhost:3000
- 点击“保存”。
💻 使用示例
基础用法
任务管理器
{
"type": "object",
"properties": {
"action": { "type": "string", "enum": ["create", "update", "delete"] },
"task": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"title": { "type": "string" },
"description": { "type": "string" }
}
}
}
}
文件浏览器
{
"type": "object",
"properties": {
"action": { "type": "string", "enum": ["list", "open"] },
"path": { "type": "string" }
}
}
天气服务
{
"type": "object",
"properties": {
"action": { "type": "string", "enum": ["get_weather"] },
"location": { "type": "string" }
}
}
高级用法
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'));
📚 详细文档
项目结构
example-mcp/
├── mcp-servers/ # 不同的 MCP 服务器实现
│ ├── task-manager/ # 任务管理器 MCP 服务器示例
│ ├── file-explorer/ # 文件浏览器 MCP 服务器示例
│ └── weather-service/ # 天气服务 MCP 服务器示例
├── README.md # 项目文档
└── LICENSE # 许可证信息
示例 MCP 服务器
任务管理器
- 名称:
task-manager
- 描述:
创建、更新和删除任务。
- 参数 schema:见使用示例部分。
文件浏览器
- 名称:
file-explorer
- 描述:
浏览文件系统并执行操作。
- 参数 schema:见使用示例部分。
天气服务
- 名称:
weather-service
- 描述:
获取天气信息。
- 参数 schema:见使用示例部分。
创建自己的 MCP 服务器
- 初始化项目:
mkdir my-mcp-server && cd $_
npm init -y
- 安装依赖:
npm install @modelcontextprotocol/server express
- 创建主文件
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'));
- 启动服务器:
node index.js
🔧 技术细节
MCP 架构
组件
- MCP 主机:提供上下文信息的组件。
- MCP 客户端:调用 MCP 服务的组件。
- MCP 服务器:实现具体功能并响应请求的组件。
传输方式
- Stdio:见使用示例中的 Stdio 代码示例。
- HTTP (SSE):见使用示例中的 HTTP (SSE) 代码示例。
📄 许可证
本项目遵循 MIT 许可证。查看 LICENSE 文件获取详细信息。
贡献者