Skip to content

Commit eba38a1

Browse files
committed
feat: 支持SSE流式传输
1 parent 6748d6b commit eba38a1

File tree

4 files changed

+332
-10
lines changed

4 files changed

+332
-10
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
.env
2-
cache/endnote/index.json
3-
cache/fulltext/index.json
4-
cache/index.json
5-
/node_modules
2+
cache/
3+
node_modules/

README.md

Lines changed: 175 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969

7070
## 🚀 快速部署
7171

72+
> **💡 新功能:支持SSE模式云端部署!** 现在可以通过 `npm run start:sse` 启动SSE服务器,支持远程访问和多客户端并发连接。详见[云端部署指南](#☁️-云端部署指南)
73+
7274
### 前置要求
7375
安装Node.js (v18.0.0+):[nodejs.org](https://nodejs.org/)
7476

@@ -122,12 +124,89 @@ PUBMED_EMAIL=你的邮箱地址
122124
1. 访问 [NCBI API Key Management](https://www.ncbi.nlm.nih.gov/account/settings/)
123125
2. 登录NCBI账户,生成API密钥
124126

125-
### 步骤四:测试服务器
127+
### 步骤四:运行模式选择
128+
129+
服务器支持两种运行模式:
130+
131+
#### **stdio 模式(默认)** - 本地开发推荐
132+
适用于本地开发、MCP客户端集成(如Claude Desktop、Cline等)
133+
126134
```bash
135+
# 方式一:使用npm脚本
136+
npm run start:stdio
137+
138+
# 方式二:直接运行
139+
node src/index.js --mode=stdio
140+
141+
# 方式三:默认模式(不指定参数时自动使用stdio)
127142
node src/index.js
143+
```
144+
145+
**特点:**
146+
- ✅ 通过标准输入输出与MCP客户端通信
147+
- ✅ 适合本地开发和MCP客户端集成
148+
- ✅ 无需配置端口,开箱即用
149+
150+
#### **SSE 模式** - 云端部署推荐
151+
适用于云端部署、远程访问、多客户端并发访问
152+
153+
```bash
154+
# 使用npm脚本启动
155+
npm run start:sse
156+
157+
# 或直接运行
158+
node src/index.js --mode=sse
159+
160+
# 指定端口(默认3000)
161+
PORT=8080 node src/index.js --mode=sse
162+
```
163+
164+
**特点:**
165+
- ✅ 基于HTTP Server-Sent Events (SSE) 协议
166+
- ✅ 支持远程访问和云端部署
167+
- ✅ 支持多客户端并发连接
168+
- ✅ 提供健康检查端点
169+
170+
**SSE模式端点:**
171+
- `GET /sse` - 建立SSE连接
172+
- `POST /message` - 接收客户端消息(需要sessionId参数)
173+
- `GET /health` - 健康检查端点
174+
175+
**环境变量配置:**
176+
```env
177+
# SSE模式端口配置(可选,默认3000)
178+
PORT=3000
179+
180+
# 或通过命令行参数指定模式
181+
# --mode=stdio 或 --mode=sse
182+
# 或通过环境变量 MCP_TRANSPORT=stdio 或 MCP_TRANSPORT=sse
183+
```
184+
185+
### 步骤五:测试服务器
186+
187+
**测试stdio模式:**
188+
```bash
189+
npm run start:stdio
128190
# 看到 "PubMed Data Server v2.0 running on stdio" 表示成功
129191
```
130192

193+
**测试SSE模式:**
194+
```bash
195+
npm run start:sse
196+
# 看到以下信息表示成功:
197+
# "PubMed Data Server v2.0 running on SSE"
198+
# "[SSE] Server listening on http://0.0.0.0:3000"
199+
# "[SSE] SSE endpoint: http://0.0.0.0:3000/sse"
200+
# "[SSE] Message endpoint: http://0.0.0.0:3000/message"
201+
# "[SSE] Health check: http://0.0.0.0:3000/health"
202+
```
203+
204+
**验证SSE模式健康检查:**
205+
```bash
206+
curl http://localhost:3000/health
207+
# 应返回:{"status":"ok","mode":"sse","sessions":0}
208+
```
209+
131210
> 环境变量说明:项目已内置 `.env` 自动加载(使用 dotenv)。在项目根目录创建 `.env`,例如:
132211
133212
```env
@@ -146,9 +225,14 @@ FULLTEXT_MODE=disabled
146225
# enabled:自动导出RIS和BibTeX格式(默认)
147226
# disabled:禁用EndNote导出
148227
ENDNOTE_EXPORT=enabled
228+
# SSE模式端口配置(仅SSE模式需要,默认3000)
229+
PORT=3000
230+
# 传输模式选择(可选,默认stdio)
231+
# 可通过命令行参数 --mode=stdio 或 --mode=sse 覆盖
232+
MCP_TRANSPORT=stdio
149233
```
150234

151-
### 步骤五:MCP客户端配置
235+
### 步骤六:MCP客户端配置
152236

153237
#### 1. Cline (VS Code Extension) 配置
154238

@@ -402,11 +486,87 @@ cp config/claude_desktop_config.json.example config/claude_desktop_config.json
402486
}
403487
```
404488

405-
### 步骤六:验证集成
489+
### 步骤七:验证集成
406490
在客户端中测试:`使用pubmed_search工具搜索"acupuncture"`
407491

408492
---
409493

494+
## ☁️ 云端部署指南
495+
496+
### SSE模式云端部署
497+
498+
SSE模式专为云端部署设计,支持远程访问和多客户端并发连接。
499+
500+
#### **部署步骤:**
501+
502+
1. **准备服务器环境**
503+
```bash
504+
# 安装Node.js (v18.0.0+)
505+
# 克隆或上传项目到服务器
506+
git clone [项目地址]
507+
cd mcp-pubmed-server-pancrpal
508+
npm install
509+
```
510+
511+
2. **配置环境变量**
512+
```bash
513+
# 创建.env文件
514+
cp .env.example .env
515+
# 编辑.env,填入API密钥和邮箱
516+
```
517+
518+
3. **启动SSE服务器**
519+
```bash
520+
# 使用PM2等进程管理器(推荐)
521+
npm install -g pm2
522+
pm2 start npm --name "pubmed-server" -- run start:sse
523+
524+
# 或直接运行
525+
PORT=3000 npm run start:sse
526+
```
527+
528+
4. **配置防火墙和反向代理(可选)**
529+
```nginx
530+
# Nginx配置示例
531+
server {
532+
listen 80;
533+
server_name your-domain.com;
534+
535+
location / {
536+
proxy_pass http://localhost:3000;
537+
proxy_http_version 1.1;
538+
proxy_set_header Upgrade $http_upgrade;
539+
proxy_set_header Connection 'keep-alive';
540+
proxy_set_header Host $host;
541+
proxy_cache_bypass $http_upgrade;
542+
}
543+
}
544+
```
545+
546+
5. **验证部署**
547+
```bash
548+
# 健康检查
549+
curl http://your-server:3000/health
550+
# 应返回:{"status":"ok","mode":"sse","sessions":0}
551+
```
552+
553+
#### **使用场景对比:**
554+
555+
| 模式 | 适用场景 | 优势 |
556+
|------|----------|------|
557+
| **stdio** | 本地开发、MCP客户端集成 | 简单、无需配置端口 |
558+
| **SSE** | 云端部署、远程访问 | 支持多客户端、可扩展 |
559+
560+
#### **生产环境建议:**
561+
562+
- ✅ 使用进程管理器(PM2、systemd等)确保服务持续运行
563+
- ✅ 配置反向代理(Nginx、Caddy等)提供HTTPS支持
564+
- ✅ 设置防火墙规则,仅开放必要端口
565+
- ✅ 配置日志轮转和监控告警
566+
- ✅ 定期更新依赖和检查安全漏洞
567+
568+
---
569+
410570
## 🛠️ 11个高效工具
411571

412572
### 1. `pubmed_search` - 智能文献搜索
@@ -597,6 +757,18 @@ mcp-pubmed-server/
597757
- 不要使用 `cwd` 参数
598758
- 使用正斜杠 `/` 或双反斜杠 `\\`
599759

760+
5. **SSE模式启动失败**
761+
- 检查端口是否被占用:`lsof -i :3000` (macOS/Linux) 或 `netstat -ano | findstr :3000` (Windows)
762+
- 确认防火墙允许指定端口访问
763+
- 检查环境变量 `PORT` 是否正确设置
764+
- 验证SSE端点可访问:`curl http://localhost:3000/health`
765+
766+
6. **SSE模式连接问题**
767+
- 确保客户端支持SSE协议
768+
- 检查网络连接和防火墙设置
769+
- 验证sessionId是否正确传递
770+
- 查看服务器日志获取详细错误信息
771+
600772
### 部署清单
601773
- [ ] Node.js已安装 (v18.0.0+)
602774
- [ ] npm 包已安装 (`npm install -g mcp-pubmed-llm-server`) 或从源码安装 (`npm install`)

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
},
1010
"scripts": {
1111
"start": "node src/index.js",
12+
"start:stdio": "node src/index.js --mode=stdio",
13+
"start:sse": "node src/index.js --mode=sse",
1214
"dev": "node --watch src/index.js",
1315
"build": "echo 'No build step required'",
1416
"test": "node test/test.js",

0 commit comments

Comments
 (0)