Skip to main content
Glama

reference-search-mcp

自用工具:画师找绘画参考图。AI 编码代理请先读 AGENTS.md

一个给 AI 用的参考图搜索 MCP 服务器:接收自然语言查询 → 解析为关键词 → 并行搜索多个图源 → 缩略图去重 → 拼成带编号的拼图 → 多模态模型通过工具调用筛选(而不是裸 JSON 输出)→ 客户端驱动迭代(a、b、c… 轮,跨轮去重)→ 按 ID 下载整图返回文件路径。

调用方 AI (MCP 客户端)
   │  image_search_start("找适合播客封面的太空插画素材")
   ▼
[reference-search-mcp]                        ┌──────────────────────┐
  ├─ LLM 层 (pi)  NL → 关键词 (submit_keywords 工具)          │ 搜索适配器(并行)    │
  ├─ providers    DDG / Bing / Wikimedia / Openverse / Serper │  ddg ─┐             │
  ├─ 去重         pHash(跨轮 seen 集合)                      │  bing ─┤ 结果合并    │
  ├─ 拼图         sharp 编号拼图 round-a.png(a1..aN)         │  wikimedia ─┘       │
  ├─ 视觉筛选     pi vision 模型看拼图,调用 select_images /   └──────────────────────┘
  │               reject_images / refine_search 工具
  ▼
{ round:"a", gridPath, selectedIds:["a3","a17"], metadata:[...] }
   │  image_search_iterate("不要 a3,多找像 b7 的") → round b(重复图自动剔除)
   │  image_search_collect(session, ["b1","c12"]) → 本地文件路径 + manifest.json

为什么结果用"工具调用"交付,而不是结构化 JSON?

筛选模型对拼图的选择,通过 select_images / reject_images / refine_search函数调用表达:

  • 参数 schema 由模型服务商强制校验——天然是合法 JSON,没有 markdown 围栏、散文夹杂、键名漂移问题;

  • 多意图一次表达(选 + 拒 + 建议下一轮关键词);

  • 传了无效 ID(如 a99)时执行器回执错误,模型下一轮自行修正

  • 与 MCP 外层同构:外层是调用方 AI 通过工具用我们,内层是我们通过工具用模型。

绝无文本 JSON fallback——工具调用是唯一交付通道。tool_choice 策略(实测 DeepSeek v4-flash):

  • thinking 开启(默认,本任务建议开启):DeepSeek 的 thinking 模式拒绝强制 tool_choice(实测 400 "Thinking mode does not support this tool_choice"),故用 auto + 模型未调用工具时对话逼问(追加"你必须调用工具",≤LLM_MAX_TURNS 轮);

  • thinking 关闭PI_THINKING=off):tool_choice 强制指定函数(openai-completions 传 {type:"function",function:{name}} + reasoning_effort:"none"),100% 保证调用。

LLM 层基于 pi@earendil-works/pi-ai,MIT):统一多提供商 API(Anthropic / OpenAI / DeepSeek / Gemini / 通义 / Kimi / MiniMax…)、自动认证解析、内置模型目录、重试工具。不引入重型 agent 框架——服务器端 LLM 只是三个有界函数(解析关键词 / 解读反馈 / 筛选拼图),真正的迭代循环由调用方 AI 驱动。

Related MCP server: mcp-universal-crawler

快速开始

要求:Node ≥ 22.19。

npm install --ignore-scripts
npm run build

1. 配置 LLM(pi 认证,二选一)

# 方式 A:环境变量(任意 pi 支持的提供商)
export DEEPSEEK_API_KEY=sk-...          # 文本解析(便宜)
export ANTHROPIC_API_KEY=sk-ant-...     # 视觉筛选
# 或 OPENAI_API_KEY / GEMINI_API_KEY / OPENROUTER_API_KEY ...

# 方式 B:pi 的登录体系(支持订阅制)
npx @earendil-works/pi-coding-agent /login   # 或直接 pi /login

模型选择(可选):

export PI_TEXT_MODEL=deepseek/deepseek-v4-flash
export PI_VISION_MODEL=anthropic/claude-sonnet-4-5
# 本任务建议开启思考,不要设 off;设 off 会切换为强制 tool_choice
# export PI_THINKING=off|minimal|low|medium|high

自定义 OpenAI 兼容端点(Qwen-VL / GLM-4V / Ollama 等):

export PI_CUSTOM_PROVIDER_API=openai-completions
export PI_CUSTOM_PROVIDER_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1
export PI_CUSTOM_PROVIDER_MODELS=qwen-vl-max,qwen-turbo
export PI_CUSTOM_PROVIDER_API_KEY=sk-...

DeepSeek 视觉模型deepseek-v4-flash-vision-exp,不在 pi 内置目录里,走自定义端点):

export DEEPSEEK_API_KEY=sk-...
export PI_TEXT_MODEL=deepseek/deepseek-v4-flash
export PI_VISION_MODEL=deepseek-vision/deepseek-v4-flash-vision-exp
export PI_CUSTOM_PROVIDER_ID=deepseek-vision
export PI_CUSTOM_PROVIDER_API=openai-completions
export PI_CUSTOM_PROVIDER_BASE_URL=https://api.deepseek.com
export PI_CUSTOM_PROVIDER_MODELS=deepseek-v4-flash-vision-exp
export PI_CUSTOM_PROVIDER_API_KEY_ENV=DEEPSEEK_API_KEY

没有 LLM 凭据也能用(降级模式)start/iterate 时显式传 keywords,跳过自动解析与筛选,返回全部候选。

2. 配置图源

export PROVIDERS=ddg,bing,wikimedia          # 默认;并行查询
export OPENVERSE_TOKEN=...                   # 启用 openverse(CC 图库)
export SERPER_API_KEY=...                    # 启用 serper(Google 图搜)
export SAFE_SEARCH=true
export HTTPS_PROXY=http://127.0.0.1:7890      # 可选:部分图源被墙时走代理

3. 接入 MCP 客户端

Claude Code:

{
  "mcpServers": {
    "reference-search": {
      "command": "node",
      "args": ["D:/path/to/reference-search-mcp/dist/index.js"],
      "env": { "DEEPSEEK_API_KEY": "...", "ANTHROPIC_API_KEY": "..." }
    }
  }
}

自研 stdio 客户端:node dist/index.js,标准 MCP 协议,工具返回 JSON 文本块。

双模式:这个 MCP 是"视觉能力外包"

这个 MCP 的本质是给纯文本模型一双眼睛:搜索、拼图、编号是机械部分;视觉筛选(看拼图选编号)是"外包的视觉能力"。调用方是否多模态,决定服务器要不要替它看:

模式

适用调用方

服务器行为

交互

serverFILTER_MODE=server

纯文本模型

文本解析关键词 + 视觉筛选

返回 selectedIds + reasons(视觉模型的"看图报告")

clientFILTER_MODE=clientfilter:false

多模态模型

只做机械部分,不调用视觉模型(省一次视觉 API)

返回拼图路径 + 全部候选编号,调用方自己看拼图自己选 ID

auto(默认)

任意

配了视觉模型就筛,没配就降级

同 server / client

collect 本来就接受任意有效 ID——多模态调用方可以无视 selectedIds 自己挑。每次调用也可用 filter: false 覆盖全局配置。

工具契约

工具

入参

返回要点

image_search_start

query, keywords?, criteria?, count?, safe_search?, filter?

session_id, round:"a", grid_path, filtered, selected_ids, metadata(编号→title/域名/license/尺寸/URL), keywords_used, warnings

image_search_iterate

session_id, feedback(可引用 a3/b12), keywords?, filter?

下一轮 round:"b"…;跨轮 pHash 去重(dedupe_skipped);LLM 经 refine_search 调整关键词

image_search_collect

session_id, ids:["b1","c12"]

files(本地路径/URL/license/宽高), manifest_path, failures(逐 ID)

image_search_status

session_id

各轮选中/拒绝、当前关键词、已收集

ID 规则:轮次字母 + 格序号。a3 = 第 1 轮第 3 格,b12 = 第 2 轮第 12 格。所有引用与 collect 均以此为准。

配置参考

变量

默认

说明

PROVIDERS

ddg,bing,wikimedia

启用图源,逗号分隔

OPENVERSE_TOKEN / SERPER_API_KEY

可选图源凭据

GRID_COLUMNS / GRID_ROWS

6 / 8

每轮 48 格;GRID_CELL_SIZE 默认 256px

SESSION_TTL_MINUTES

120

会话与临时拼图自动清理

DATA_DIR / OUT_DIR

系统 temp / ./out

数据与收集产物目录

HTTP_TIMEOUT_MS

15000

抓取超时

LLM_MAX_TURNS

3

内层工具循环最大轮数(含逼问轮)

FILTER_MODE

auto

auto | server | client(见"双模式")

PI_TEXT_MODEL / PI_VISION_MODEL / PI_THINKING

自动挑选

LLM 模型选择;建议开启思考,不要设 off(设 off 切换为强制 tool_choice)

HTTPS_PROXY / HTTP_PROXY / NO_PROXY

图源抓取代理(部分图源被墙时配置)

架构

src/
  mcp/        # MCP server(stdio)与 4 个工具注册
  llm/        # pi-ai 之上的工具调用循环:parseKeywords / interpretFeedback / filterGrid
  providers/  # SearchProvider 接口 + ddg/bing/wikimedia/openverse/serper 适配器,并行容错
  grid/       # sharp 拼图构建(编号徽章/占位格)、pHash 去重
  session/    # 会话状态机(轮次 a/b/c、seen 哈希、TTL 清理)
  collect/    # 整图下载(UA/Referer/重试/校验)、manifest 生成
  service.ts  # 编排:search → dedupe → grid → filter → round state

测试与脚本

npm test                              # 41 个测试:单测 + 真实 MCP stdio 集成测试
npm run smoke -- --query "space nebula" --keywords "nebula,art" --collect "a1,a2" [--iterate "更多星球"]
npm run handshake -- --query "cat" --keywords "cat"     # MCP stdio 握手冒烟(先 build)
npx tsx scripts/debug-pi.ts           # 诊断:真实 parseKeywords(工具调用 + 多组关键词)
npx tsx scripts/debug-vision.ts       # 诊断:视觉模型对最近一轮拼图的原始响应

注意事项

  • 多角度查询:自然语言需求含多个方面时(如"M1911 各角度"),关键词解析产出分组关键词(正面/侧面/正侧面…),各组并行搜索、去重后拼成一张图,metadata 每行带 group 标签。

  • 每图描述:视觉筛选时 select_images 的 note 填每张选中图的简短视觉描述(中文,面向绘画参考:角度/构图/光照/风格),随 reasons 字段交付——纯文本调用方也能"看到"图。

  • 版权metadata/manifest 透传 license(Wikimedia/Openverse 自带),商用素材请自行核验来源授权。

  • 热链保护:部分站点(如 people.com.cn/Etsy)拒绝第三方下载,collect 会逐 ID 报告失败;403 时可用浏览器直接打开 URL。

  • 图源网络问题:ddg/wikimedia 在部分网络环境(如中国大陆)不可达或间歇超时——warnings 会提示"网络不可达:配置 HTTPS_PROXY 后重试";bing 通常稳定。

  • 降级模式:无 LLM 凭据时需显式传 keywords,且不自动筛选(返回全部候选)。

Available Tools

4 tools
image_search_collectDownload the full images for chosen cell idsA

Download the full-resolution images for a list of round-qualified ids (e.g. ['a3', 'b12']) to the output directory. Returns local file paths plus a manifest with source URLs and licenses.

ParametersJSON Schema
NameRequiredDescriptionDefault
idsYes
session_idYes

TDQS

A3.9/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries the full behavioral burden. It discloses the action (downloads to output directory) and the return format (local paths and manifest with URLs/licenses). It does not mention file overwriting, network requirements, or session validity, but these are minor gaps for a download tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two sentences that are tightly written: the first states the core action, the second states the return value. No filler, and the most important information is front-loaded.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

The description covers the essential function but omits the purpose of session_id and its relation to the broader search workflow. Without annotations or an output schema, an agent may not know if session_id must come from a prior start/iterate call or whether the output directory is session-scoped. This is incomplete for a multi-step tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description explains 'ids' with an example and meaning (round-qualified ids), but 'session_id' is not explained at all. Since schema description coverage is 0%, the description should have delineated both parameters; it partially compensates but leaves a critical gap on how session_id is used.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description states a specific verb (download), resource (full-resolution images), and target (list of ids) with an example. It clearly distinguishes from siblings (start/iterate/status) which are about session management, not downloading.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage by mentioning 'round-qualified ids' and downloading, but it never explicitly says when to use this tool versus siblings or that it should follow a prior step. The workflow relationship to image_search_start/iterate/status is left to inference, with no exclusions or explicit conditions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

image_search_iterateIterate on an existing search sessionA

Give feedback referencing round-qualified ids (e.g. 'keep a3, more like b7, no photos') plus optional explicit keywords. Produces the next round (b, c, ...) with dedup against all previously shown images. The LLM interprets the feedback into keyword additions/removals via refine_search.

ParametersJSON Schema
NameRequiredDescriptionDefault
countNo
filterNofalse = skip the server-side vision filter for this round
criteriaNo
feedbackYesNatural-language feedback; may reference cell ids like a3 / b12
keywordsNoExplicit keyword replacement; skips LLM feedback interpretation
session_idYes
safe_searchNo

TDQS

A3.8/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries the full burden. It discloses dedup against previously shown images and that the LLM interprets feedback into keyword changes via refine_search. However, it doesn't mention side effects like session mutation, reversibility, or any potential rate limits. It covers some key behaviors but not all.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two sentences with zero waste. The primary usage is front-loaded, and the key behaviors (dedup, LLM interpretation) are clearly stated. Very efficient.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with 7 parameters, no output schema, and no annotations, the description is relatively brief. It covers core mechanics but omits details on several parameters and doesn't describe the return value or any side effects. It's adequate but not fully complete for complex usage.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is only 43%, so the description must compensate for undocumented parameters like count, criteria, safe_search, and session_id. It adds meaning for feedback (referencing cell ids) and keywords (explicit replacement) but does not explain the remaining parameters. The description fails to bridge the coverage gap for those.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: it iterates on an existing search session by taking feedback referencing round-qualified ids, producing the next round with dedup. It distinguishes from siblings by implying it's not for starting a new session (image_search_start) but for refinement, and the title reinforces 'existing'.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

It gives clear usage instructions: provide feedback with round-qualified ids and optionally explicit keywords. It implies when to use (with an existing session) but does not explicitly mention when not to use or name alternatives. Context from siblings suggests this is for continuation, but the description alone doesn't state exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

image_search_startStart an image search sessionA

Parse a natural-language query into keywords (unless keywords are given), search all configured image providers in parallel, dedupe, render a numbered composite grid (round 'a', cell ids a1..aN), and run a multimodal filter that returns the selected cell ids. Returns the grid file path, the selected ids, the metadata table, and the keywords actually used.

ParametersJSON Schema
NameRequiredDescriptionDefault
countNoMax candidates in this round (default: grid capacity)
queryYesNatural-language image request, e.g. 'space nebula illustrations for a podcast cover'
filterNofalse = skip the server-side vision filter and return all candidates (use when the calling model is multimodal and will look at the grid itself); default follows FILTER_MODE
criteriaNoStyle / quality criteria for filtering, e.g. 'flat vector, no text, dark background'
keywordsNoExplicit search keywords; skips LLM parsing when given
safe_searchNo

TDQS

A4/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries the full burden of behavioral disclosure. It details the process steps, the return values (grid file path, selected ids, metadata table, keywords used), and explains special behaviors like the 'filter' parameter (false skips filter) and 'keywords' parameter (skips LLM parsing). It also notes the default for filter follows FILTER_MODE. This is transparent, though it does not mention session state persistence or file system side effects beyond returning a path.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is two concise sentences. The first enumerates the pipeline steps without redundancy, and the second lists the return values. It is front-loaded with the core action and contains no filler or repetitive language.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

The description covers the tool's behavior, parameters, and return values. It does not explain the overall session workflow (e.g., that image_search_iterate follows), but that is arguably outside the scope of a single tool description. The return list compensates for the absent output schema. Completeness is high for the tool's complexity.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 83%, so most parameters are described in the schema. The description adds meaningful context beyond the schema by explaining that 'count' defaults to grid capacity, 'filter=false' skips the vision filter, and 'keywords' skips LLM parsing. It also clarifies the filter default via FILTER_MODE. This enriches the parameter understanding.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description opens with a clear verb and resource: 'start an image search session.' It then describes the full pipeline (parse query, search parallel, dedupe, render grid, filter, return results). The action is specific and distinct from siblings like iterate, collect, and status, making it obvious this tool initiates the session.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description does not explicitly state when to use this tool versus its siblings. It does not mention 'use this to begin a session' or direct the agent to image_search_iterate for refinement. While the name implies it is the starting point, there is no explicit guidance on choosing it over alternatives or any exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

image_search_statusShow session stateB

Rounds so far, per-round selections/rejections, current keywords, and collected ids.

ParametersJSON Schema
NameRequiredDescriptionDefault
session_idYes

TDQS

B3.2/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description bears full responsibility for disclosing side effects. It lists what data is returned but does not explicitly state that the operation is read-only or free of side effects. The verb 'show' implies a non-mutating action, but this is not made explicit, leaving some ambiguity.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single sentence that front-loads the core purpose ('show session state') and then lists all contents in a compact list. There is no superfluous information, and every item contributes to understanding the returned data.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a simple status tool with one parameter and no output schema, the description lists the key fields returned, which is helpful. However, it lacks an explicit read-only statement and does not mention error conditions (e.g., invalid session_id), which are important given the absence of annotations. It is adequate but not fully complete.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description completely ignores the only parameter, session_id, and the schema provides no description for it either. The agent gets no additional context about the format, origin, or purpose of session_id beyond its name, which is insufficient given the 0% schema description coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb 'show' and the resource 'session state', and then enumerates the specific contents ('rounds so far, per-round selections/rejections, current keywords, and collected ids'). This distinguishes it from sibling tools that start, iterate, or collect, as it is the only one that reports on state.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is given on when to use this tool versus its siblings. It does not mention that it should be used between iterations, nor does it reference alternatives or conditions that would select it. The only hint is the name 'status', which implies a read operation, but there is no explicit context.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

  1. 4 tool updatesv0.1.0
    • First observedimage_search_collect
    • First observedimage_search_iterate
    • First observedimage_search_start
    • First observedimage_search_status

TDQS

A4/5.0

Scored across 4 tools

Disambiguation5/5

Each tool has a unique and clearly defined role: start initializes a search, iterate refines it with feedback, collect downloads selected results, and status reports the current state. No two tools overlap in purpose.

Naming Consistency5/5

All tools follow the exact same `image_search_<verb>` pattern, with verbs that accurately describe the action (start, iterate, collect, status). The naming is uniformly styled and predictable.

Tool Count5/5

Four tools is perfectly scoped for an iterative image search workflow. Each tool covers a necessary step without redundancy, making the set concise and well-balanced.

Completeness5/5

The tools cover the full lifecycle: initiating a search, refining it through feedback, collecting final results, and monitoring progress. No obvious gaps exist for the intended use case, as the start tool integrates search and multimodal filtering.

Maintenance

ActivityMaintained
ResponsivenessNo issues

Related MCP Connectors

Related MCP Servers