Pipeline 持久化 — 結構化搜尋計畫管理
描述
將複雜的搜尋流程保存為可重複使用的 Pipeline YAML 配置。支援:
- 從模板快速建立(pico、comprehensive、exploration、gene_drug、snowball)
- 自訂 DAG(有向無環圖)多步驟管道
- 雙層儲存(workspace + global)
- Schema-exact 驗證 + 有限度的安全 normalization
- 系統性回顧:
retrieval: exhaustive完整 PubMed 集合、PRISMA-S run 紀錄、完整集合匯出、MeSH 區塊分析、篩選與 PRISMA 2020 流程
觸發條件
- 「把這個搜尋存起來」、「建立搜尋計畫」
- 「每週跑一次這個搜尋」、「保存這個 pipeline」
- 「列出我的管道」、「上次的搜尋可以再跑嗎」
- 「把剛才的搜尋轉成 search plan」
- 「做系統性回顧/統合分析的檢索」、「PRISMA 流程圖」、「匯出給 Rayyan/Covidence」、「篩選紀錄」
🌟 快速開始
方法 1: 用模板(最簡單)
# 保存一個 PICO 模板 pipeline
save_pipeline(
name="icu_remimazolam_vs_propofol",
config="""
template: pico
template_params:
P: ICU patients requiring sedation
I: remimazolam
C: propofol
O: delirium incidence, sedation quality
""",
tags=["anesthesia", "sedation", "ICU"],
description="Weekly monitoring: remimazolam vs propofol ICU sedation"
)
# 執行已保存的 pipeline
unified_search(pipeline="saved:icu_remimazolam_vs_propofol")
方法 2: 自訂 DAG(完整控制)
save_pipeline(
name="brca1_comprehensive",
config="""
steps:
- id: expand
action: expand
params:
topic: BRCA1 breast cancer
- id: pubmed
action: search
params:
query: BRCA1 breast cancer
sources: [pubmed]
limit: 50
- id: expanded
action: search
inputs: [expand]
params:
strategy: mesh
sources: [pubmed, openalex]
limit: 50
- id: merged
action: merge
inputs: [pubmed, expanded]
params:
method: rrf
- id: enriched
action: metrics
inputs: [merged]
output:
format: markdown
limit: 30
ranking: quality
""",
tags=["genetics", "oncology"],
description="BRCA1 breast cancer comprehensive search with MeSH expansion"
)
8 個 MCP 工具
save_pipeline — 保存管道
save_pipeline(
name="weekly_remimazolam", # 唯一名稱 (英數 + _ -, max 64)
config="<YAML or JSON>", # Pipeline 配置
tags=["tag1", "tag2"], # 最多 20 個 canonical 字串標籤
description="...", # 人類可讀描述
scope="auto" # "workspace" | "global" | "auto"
)
Fail-closed contract:
- action/template 只接受下列 canonical 值;alias 與拼字錯誤直接拒絕
- template 參數只使用頂層
template_params;已退役的頂層params直接拒絕 - step ID 必須明確且唯一;dependency ID 必須精確引用前面的 step
- 未知
on_error、output format 或 ranking 不會被猜測或改寫
list_pipelines — 列出管道
list_pipelines() # 列出所有
list_pipelines(tag="ICU") # 按標籤過濾
list_pipelines(scope="workspace") # 只看工作區
load_pipeline — 載入管道
load_pipeline(source="weekly_remimazolam") # 從已保存
load_pipeline(source="file:path/to/pipeline.yaml") # 從檔案
delete_pipeline — 刪除管道
delete_pipeline(name="old_search") # 刪除配置 + 歷史
# 篩選紀錄已有決策時會拒絕(不刪任何東西)並回報決策/PMID 數;
# 先 pipeline_screening(request={"action":"log",...}) 匯出,再明確同意:
delete_pipeline(name="old_search", delete_screening_log=True)
檢查與刪除是原子操作(期間新記錄的決策仍會阻止刪除);檔案被鎖住時刪除會停止、pipeline 保持完整,可重試。
get_pipeline_history — 查看執行歷史
get_pipeline_history(name="weekly_remimazolam", limit=5)
# 顯示:run ID、日期、紀錄數、取回模式、PubMed 總數、新增/移除、策略 hash、狀態
get_pipeline_history(name="weekly_remimazolam", view="run") # 單一 run 來源資訊(JSON)
get_pipeline_history(name="weekly_remimazolam", view="prisma_s") # PRISMA-S 表(Markdown + CSV)
get_pipeline_history(name="weekly_remimazolam", view="records", offset=0, limit=100) # 完整紀錄集分頁(可超過 10,000 筆)
schedule_pipeline — 建立或更新排程
schedule_pipeline(name="weekly_remimazolam", cron="0 9 * * 1")
diff_mode=True 會在排程報告附上與前次成功執行的差異:新增文章與移除 PMID 各最多 20 筆,首次執行標示 baseline。Partial/error 不據以推論移除。
notify=True 保存最後 run ID 與新增/移除數量;list_pipelines 顯示新增文章數並引導至 get_pipeline_history,不是外部推播。停用通知會清除舊摘要。
unschedule_pipeline — 移除排程
unschedule_pipeline(name="weekly_remimazolam")
pipeline_screening — 篩選紀錄與 PRISMA 2020 流程
pipeline_screening(request={"action": "queue", "pipeline": "my_review", "include_abstract": True})
pipeline_screening(request={"action": "record", "pipeline": "my_review", "decisions": [
{"pmid": "12345678", "stage": "title_abstract", "decision": "include", "reviewer": "ak"}]})
pipeline_screening(request={"action": "flow", "pipeline": "my_review"}) # PRISMA 2020 計數
pipeline_screening(request={"action": "log", "pipeline": "my_review"}) # 決策稽核紀錄
pipeline_screening(request={"action": "register_run", "pipeline": "my_review"}) # 手動登記某次 run
完整識別(exhaustive)的 run 會自動登記;更新檢索第一次出現的 PMID 自動進入標題/摘要待篩佇列。
決策只追加、以最新決策計算;未給 decision_id 時,只有與同一 reviewer 對該 PMID/階段的最新決策
完全相同才算重送,回到先前的決策(include → exclude → include)一定會記錄並計入;要讓重送冪等請給穩定的
decision_id。全文排除必須附理由(取不到全文用 not retrieved)。
flow 的識別筆數是 filter step 之前的檢索結果,被 filter 移除的筆數列為「自動化工具判為不符」。
prepare_export(pipeline_run=...) — 匯出完整紀錄集
prepare_export(pipeline_run="my_review", format="ris") # 最新 run
prepare_export(pipeline_run="my_review@<run_id>", format="csv") # 指定 run;另有 csl、bibtex、medline、json
匯出摘要標明 record_basis、retrieval_mode、complete_record_set;排序預覽、mixed 或不完整的 run 會附警告。
5 個內建模板
pico — PICO 臨床問題
template: pico
template_params:
P: ICU patients requiring sedation
I: remimazolam
C: propofol
O: delirium incidence
sources: [pubmed] # 可選,預設 pubmed
limit: 20 # 可選
自動產生的 DAG:
pico → search_p ──┐
→ search_i ──┤
→ search_c ──┼→ merged → enriched
comprehensive — 多資料庫 + MeSH 擴展
template: comprehensive
template_params:
query: CRISPR gene therapy safety
sources: [pubmed, openalex, europe_pmc] # 可選
limit: 30 # 可選
min_year: 2020 # 可選
自動產生的 DAG:
expand → search_expanded ──┐
search_original ──┼→ merged → enriched
exploration — 種子論文探索
template: exploration
template_params:
pmid: "33475315"
limit: 20 # 每個方向的限制
自動產生的 DAG:
related ──┐
citing ──┼→ merged → enriched
refs ──┘
gene_drug — 基因/藥物搜尋
template: gene_drug
template_params:
term: BRCA1
sources: [pubmed, openalex] # 可選
limit: 20 # 可選
min_year: 2020 # 可選
snowball — 多種子引用擴展
template: snowball
template_params:
query: remimazolam ICU sedation
seed_limit: 5 # 可選,1–10,預設 5
limit: 20 # 可選,預設 20
流程:search → citing + references → union merge(含原始搜尋結果)。
related/citing/references 也可透過 inputs 搭配 seed_limit,但不可同時給 pmid。
依輸入順序取前幾個不重複 PMID,每個 seed 消耗一次外部呼叫預算;合併去重後排除 seeds,並以 limit 截斷該 action 的結果。
11 個可用 Action
| Action | 說明 | 主要參數 |
|--------|------|----------|
| search | 文獻搜尋 | query, sources, limit, min_year, max_year;系統性檢索用 retrieval: exhaustive, max_records, fetch_metadata |
| pico | Agent-provided PICO handoff search | P, I, optional C, recommended O |
| expand | MeSH/同義詞擴展 | topic |
| details | 取得文章詳情 | pmids |
| related | 相關文章 | pmid 或 inputs + seed_limit,limit |
| citing | 引用文章 | pmid 或 inputs + seed_limit,limit |
| references | 參考文獻 | pmid 或 inputs + seed_limit,limit |
| metrics | iCite 引用指標 | (從 inputs 取得) |
| merge | 合併結果 | method: union / intersection / rrf |
| filter | 過濾結果(無 metadata 的紀錄一律保留並回報 metadata_missing) | min_year, max_year, article_types, min_citations, has_abstract |
| block_analysis | MeSH/概念區塊與詞彙獨特貢獻(僅計數) | query 或一個 input search step,max_count_queries |
雙層儲存模型
Workspace scope (.pubmed-search/pipelines/)
├── 每個專案獨立
├── 新建立的 .pubmed-search/ 會寫入忽略全部的 .gitignore(紀錄集含摘要、篩選決策)
└── 要團隊共享 YAML:改寫該 .gitignore(檔內有只保留 pipelines/*.yaml 的範例)
Global scope (~/.pubmed-search-mcp/pipelines/)
├── 跨專案共用
├── 個人偏好
└── 通用模板
解析順序: workspace 優先 → global fallback(同名時 workspace 遮蔽 global,list_pipelines 會標示;每次 run 留在載入它的 scope)
生產級範例
範例 1: 週報搜尋 — 麻醉藥物監控
name: weekly_anesthesia_monitoring
steps:
- id: search_remimazolam
action: search
params:
query: remimazolam
sources: [pubmed, europe_pmc]
limit: 50
min_year: 2024
- id: search_dex
action: search
params:
query: dexmedetomidine ICU sedation
sources: [pubmed]
limit: 50
min_year: 2024
- id: merged
action: merge
inputs: [search_remimazolam, search_dex]
params:
method: union
- id: filtered
action: filter
inputs: [merged]
params:
has_abstract: true
article_types: [journal-article, clinical-trial, randomized-controlled-trial]
- id: enriched
action: metrics
inputs: [filtered]
output:
format: markdown
limit: 30
ranking: recency
範例 2: 種子論文深度探索
name: explore_landmark_paper
steps:
- id: seed_details
action: details
params:
pmids: ["33475315"]
- id: related
action: related
params:
pmid: "33475315"
limit: 30
- id: citing
action: citing
params:
pmid: "33475315"
limit: 30
- id: refs
action: references
params:
pmid: "33475315"
limit: 30
- id: merged
action: merge
inputs: [related, citing, refs]
params:
method: rrf
- id: enriched
action: metrics
inputs: [merged]
output:
format: markdown
limit: 25
ranking: impact
範例 3: 系統性回顧/統合分析的完整檢索(PRISMA)
steps:
- id: pubmed
action: search
params:
query: >-
("Sodium-Glucose Transporter 2 Inhibitors"[Mesh] OR SGLT2[tiab] OR empagliflozin[tiab])
AND ("Heart Failure"[Mesh] OR "heart failure"[tiab])
sources: [pubmed]
retrieval: exhaustive # 完整 PMID 集合;不可同時給 limit;max_records ≤ 10000,超過拒絕不截斷
- id: blocks
action: block_analysis # 各區塊、累積 AND、每個詞彙的獨特貢獻
inputs: [pubmed]
output:
format: json
limit: 20 # 只是預覽筆數
執行 unified_search(pipeline="saved:<name>") 後,回應只是有界摘要;用 get_pipeline_history(view="prisma_s")
產生 PRISMA-S 表、prepare_export(pipeline_run=...) 匯出完整集合、pipeline_screening 篩選並計算 PRISMA 2020 流程。
策略必須先保存;修改 YAML 後的 run 會以不同 config hash 顯示。
只有餵入最終紀錄集的每個檢索都是完整 exhaustive 時,run 才標為 exhaustive: true;
與有界搜尋合併會是 mode: "mixed"(不完整、不自動登記篩選)。metadata 被 deadline 中斷時 PMID 集合仍完整並標示 metadata_error。
保存的策略絕不會被自動改寫。若 PubMed 回報某個 MeSH 子句找不到(例如 "Post-Anesthesia Care Units"[Mesh]),
該步驟只會記錄 mesh_correction 建議(mode: "suggestion"、applied: false、unresolved_clauses、corrected_query、
前後命中數),也會出現在回應的 warnings、Markdown 報告與 get_pipeline_history(view="prisma_s") 的 notes。
要採用時,用 save_pipeline 保存 corrected_query 成為新版本再執行;PRISMA-S 會以新的 config hash 記錄。
建議最多用 18 個請求,在所有 step 完成後才執行(不占用必要取回的配額),且只在剩餘配額與期限足夠時執行,從不讓 run 失敗。
block_analysis 會把 PubMed 找不到的詞標成 pubmed_not_found(contribution_meaningful: false)、列在
unresolved_terms,不再當成「零獨特貢獻」的冗餘詞,並附上同樣的建議。
範例 4: 多來源探索 — SGLT2 + 心衰竭(有界、非系統性)
name: sglt2_heart_failure_review
steps:
- id: pico
action: pico
params:
P: Type 2 diabetes with heart failure
I: SGLT2 inhibitors
C: standard care
O: hospitalization, mortality
- id: mesh_expand
action: expand
params:
topic: SGLT2 inhibitors heart failure outcomes
- id: search_pico_p
action: search
inputs: [pico]
params:
element: P
sources: [pubmed, europe_pmc]
limit: 100
- id: search_pico_i
action: search
inputs: [pico]
params:
element: I
sources: [pubmed, europe_pmc]
limit: 100
- id: search_expanded
action: search
inputs: [mesh_expand]
params:
strategy: mesh
sources: [pubmed, openalex]
limit: 100
- id: merged
action: merge
inputs: [search_pico_p, search_pico_i, search_expanded]
params:
method: rrf
- id: filtered
action: filter
inputs: [merged]
params:
min_year: 2018
has_abstract: true
- id: enriched
action: metrics
inputs: [filtered]
output:
format: markdown
limit: 50
ranking: quality
Agent 產生 Pipeline 的最佳實踐
從中斷的搜尋回復
先讀 server-side durable state,不要依賴對話記憶或 .github/hooks/_state:
read_session(request={"action":"search_runs"})
read_session(request={"action":"search_run","run_id":"<selected-run-id>"})
read_session(request={"action":"replay_search","run_id":"<selected-run-id>"})
replay_search 只回傳 credential-free unified_search arguments,不會自動執行或消耗
provider quota。Agent 檢查 source status、artifact audit 與使用者意圖後,再明確呼叫
unified_search。如果原 run 已有 artifact,優先讀 artifact,只有需要最新結果時才 replay。
將對話搜尋轉為 Pipeline
Agent 在完成一次成功搜尋後,可以:
# 1. 回顧剛才的搜尋策略
# 2. 轉化為 Pipeline YAML
save_pipeline(
name="auto_from_session",
config="""
steps:
- id: main_search
action: search
params:
query: "<剛才的查詢>"
sources: [pubmed, openalex]
limit: 50
- id: enriched
action: metrics
inputs: [main_search]
output:
limit: 20
ranking: balanced
""",
description="Auto-generated from search session"
)
# 3. 下次直接重複
# unified_search(pipeline="saved:auto_from_session")
常見模式
| 場景 | 推薦模板 | 說明 |
|------|----------|------|
| 臨床問題 A vs B | pico | Agent 提供 P/I/C/O;後端執行含 O 的 precision/recall 搜尋 |
| 主題綜合搜尋 | comprehensive | 多源 + MeSH 擴展 |
| 已知重要論文 | exploration | 三方向探索(related/citing/refs) |
| 基因/藥物研究 | gene_drug | 詞彙擴展 + 多源 |
| 複雜工作流 | 自訂 DAG | 完全控制每個步驟 |
| 系統性回顧/統合分析 | 自訂 DAG + retrieval: exhaustive | 完整集合、PRISMA-S、匯出、篩選 |