使用说明
📝 MCP服务器片段
一种模型上下文协议(MCP)服务器,通过stdio、HTTP(SSE)和流式HTTP传输提供对代码片段的访问。
✨ 特性
- 🔌 具有多种传输选项的MCP服务器:
- 流式HTTP -VS Code和其他MCP客户端的现代HTTP传输
- SSE(服务器发送事件) -使用SSE流的传统HTTP传输
- 工作室 -桌面应用程序的基于进程的传输
- 📚 代码段管理
- 🔒 具有最严格编译器设置的TypeScript
- ✅ ESLint和Prettier已配置
- 🧪 Jest用于单元测试
🚀 快速开始
先决条件
- Node.js>=18.0.0
从npm安装(推荐)
# Run directly without installing
npx snippets-mcp
# Or install globally
npm install -g snippets-mcp
snippets-mcp
从源代码安装
npm install
npm run build
发展
npm run dev:streamable-http # Streamable HTTP server with hot reload
npm run dev:http # SSE HTTP server with hot reload
npm run dev:stdio # Stdio server for testing
🔧 配置
对于使用HTTP传输的VS代码
要在VS Code中使用HTTP传输(由较新的MCP客户端支持):
- 首先,启动HTTP服务器:
npm run start:streamable-http
- 添加到您的VS代码设置(Ctrl+Shift+P→ “首选项:打开用户设置(JSON)”):
{
"mcp.servers": {
"snippets": {
"type": "http",
"url": "http://localhost:3003/mcp"
}
}
}
VS代码中的GitHub副本
- 按
Ctrl+Shift+P→ “首选项:打开用户设置(JSON)” - 添加:
{
"github.copilot.chat.mcp.servers": {
"snippets-mcp": {
"command": "npx",
"args": ["-y", "snippets-mcp"]
}
}
}
或者,如果您在本地克隆了仓库(替换为实际路径):
{
"github.copilot.chat.mcp.servers": {
"snippets-mcp": {
"command": "node",
"args": ["/absolute/path/to/snippets-mcp/dist/stdio.js"]
}
}
}
适用于克劳德桌面
编辑 %APPDATA%\Claude\claude_desktop_config.json (Windows)或 ~/Library/Application Support/Claude/claude_desktop_config.json (Mac):
{
"mcpServers": {
"snippets-mcp": {
"command": "npx",
"args": ["-y", "snippets-mcp"]
}
}
}
或者,如果您在本地克隆了仓库(替换为实际路径):
{
"mcpServers": {
"snippets-mcp": {
"command": "node",
"args": ["/absolute/path/to/snippets-mcp/dist/stdio.js"]
}
}
}
Cline VS代码扩展
添加 .vscode/settings.json:
{
"cline.mcpServers": {
"snippets-mcp": {
"command": "npx",
"args": ["-y", "snippets-mcp"]
}
}
}
或者,如果您在本地克隆了仓库(替换为实际路径):
{
"cline.mcpServers": {
"snippets-mcp": {
"command": "node",
"args": ["/absolute/path/to/snippets-mcp/dist/stdio.js"]
}
}
}
🛠️ 可用工具
list_snippets-列出所有可用的代码段get_snippet-按ID获取特定代码段search_snippets-按查询搜索片段
📦 可用资源
snippet://list-所有代码段的JSON列表- 使用路径格式的单个代码段内容:
snippet://{keyword0}/{keyword1}/{prefix}- 所有代码段都遵循这种一致的格式
- 缺少的关键字在路径中表示为空字符串
- 前缀始终包含在最后一段(唯一标识符)中
- 示例:
- 使用2+个关键字:
snippet://loop/iteration/for-loop - 使用1个关键字:
snippet://function//arrow-fn(第二个关键字为空) - 包含0个关键字:
snippet:////hello(两个关键字均为空)
- 使用2+个关键字:
每个代码段资源提供:
- 标题:代码段名称
- 描述:代码段的作用
- 内容:实际代码片段
- MIME类型:
text/plain
🌐 HTTP API终结点
流式HTTP传输(推荐)
POST /mcp-MCP请求处理程序(支持初始化和常规请求)GET /mcp-用于接收服务器发起的消息的SSE流端点DELETE /mcp-会话终止端点GET /health-服务器健康检查
苏格兰和南方能源公司运输(传统)
GET /sse-SSE连接端点POST /messages-消息发布端点GET /health-服务器健康检查
🔍 MCP检查员
使用内置检查器调试MCP服务器:
# Inspect stdio transport (starts the server automatically)
npm run inspect:stdio
# Inspect Streamable HTTP transport (requires server to be running separately)
npm run start:streamable-http # In one terminal
npm run inspect:streamable-http # In another terminal
# Inspect HTTP/SSE transport (requires server to be running separately)
npm run start:http # In one terminal
npm run inspect:http # In another terminal
检查器提供了一个web界面来测试工具、资源和其他MCP功能。
🧰 命令
npm run build # Build TypeScript
npm start # Start REST API server
npm run start:stdio # Start stdio MCP server
npm run start:streamable-http # Start Streamable HTTP MCP server (recommended)
npm run start:http # Start HTTP/SSE MCP server (legacy)
npm run inspect:stdio # Inspect stdio MCP server
npm run inspect:streamable-http # Inspect Streamable HTTP MCP server
npm run inspect:http # Inspect HTTP/SSE MCP server (requires server running)
npm test # Run tests
npm run lint # Check code with ESLint
npm run lint:fix # Auto-fix ESLint issues
npm run format # Format code with Prettier
⚙️ 环境变量
这些环境变量适用于所有服务器:
PORT-服务器端口(默认:3003)HOST-服务器主机(默认值:127.0.0.1)NODE_ENV-环境(默认:开发)SNIPPETS_DIR-包含代码段的目录(默认值:./sippets)AUTH_TOKEN-HTTP身份验证的可选承载令牌。设置后,所有HTTP MCP和API端点都需要Authorization: Bearer <token>头球这/health端点仍然可以公开访问。保持未设置以禁用身份验证。
备注:REST API服务器(npm start),HTTP/SSE MCP服务器(npm run start:http),以及流式HTTP MCP服务器(npm run start:streamable-http)共享相同的配置。如果需要同时运行它们,请使用不同的端口:
# Terminal 1: REST API on port 3003
npm start
# Terminal 2: Streamable HTTP MCP on port 3004
PORT=3004 npm run start:streamable-http
# Terminal 3: HTTP/SSE MCP on port 3005
PORT=3005 npm run start:http
📝 代码段格式
服务器使用遵循开闭原则的模块化加载器架构,可以轻松添加新的代码段格式,而无需修改现有代码。
支持格式
标准格式(.json)
每个文件一个包含元数据和内容的代码段:
{
"prefix": "log",
"title": "Console Log",
"keywords": ["console", "log", "debug"],
"scope": "javascript,typescript",
"description": "Log to console",
"content": "console.log('${1}');"
}
VS代码格式(.Code片段)
每个文件有多个代码段,与VS Code的代码段格式兼容:
{
"Print to console": {
"scope": "javascript,typescript",
"prefix": "log",
"body": [
"console.log('$1');",
"$2"
],
"description": "Log output to console"
},
"Arrow Function": {
"scope": "javascript,typescript",
"prefix": "arrowfn",
"body": [
"const ${1:name} = (${2:params}) => {",
"\t$0",
"}"
],
"description": "Arrow function"
}
}
这两种格式可以在同一个代码段目录中同时使用。通过实现以下功能可以添加新格式 SnippetLoader 界面。
📄 许可证
麻省理工学院
🚢 出版与发行
维护人员可以在以下网址找到发布到npm和Docker的分步指南 发布.md.
