🚀 光標模型上下文協議 (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 文件獲取詳細信息。
貢獻者