A multi-type search tool based on SearchAPI (Google), suitable for Agents to obtain real-time web information.
Features
- Multiple search types: Web, news, video, finance, maps, hotels, flights
- Automatic retry: Automatically retries on network errors, improving stability
- Structured return: JSON format directly usable by Agents
- Zero external dependencies: Pure Node.js built-in modules
Installation
# 复制 tools/websearchapi 目录到你的项目
# 配置 API Key
cd tools/websearchapi
node websearchapi.js config set-key YOUR_API_KEY
API Key acquisition: https://searchapi.io (free registration)
Usage
Use the exec tool call:
node <path>/websearchapi.js <命令> [关键词] [选项]
Search types
| Type | Command | Description |
|---|---|---|
| Web | s or search |
General web search |
| News | news |
Latest news |
| Video | video |
Video search |
| Finance | finance |
Stock/financial information |
| Maps | maps |
Places/businesses |
| Hotels | hotels |
Hotel search |
| Flights | flights |
Flight search |
Options
| Option | Description | Default value |
|---|---|---|
--num |
Number of results | 5 |
--lang |
Language | zh-CN |
--gl |
Region | cn |
--json |
JSON format output | Text |
Agent call example
# 网页搜索
node websearchapi.js s "MCP 协议"
# 新闻搜索
node websearchapi.js news "人工智能"
# 财经搜索 (用股票代码)
node websearchapi.js finance "AAPL"
# JSON 格式 (推荐,便于解析)
node websearchapi.js s "关键词" --json
Return format
JSON mode return:
{
"success": true,
"query": "关键词",
"type": "google",
"count": 5,
"results": [
{
"title": "结果标题",
"link": "链接地址",
"snippet": "摘要内容",
"source": "来源"
}
],
"metadata": {
"totalResults": 数量,
"timeTaken": 耗时,
"engine": "google"
}
}
Configuration
View configuration
node websearchapi.js config
Set API Key
node websearchapi.js config set-key YOUR_API_KEY
Modify default options
node websearchapi.js config set-num 10 # 默认结果数
node websearchapi.js config set-lang en # 默认语言
node websearchapi.js config set-gl us # 默认地区
node websearchapi.js config set-retry 5 # 重试次数
Migration deployment
Copy the entire tools directory to the new machine:
# 复制到新机器
scp -r ./tools/websearchapi user@new-server:/path/to/tools/
# 配置 API Key
cd /path/to/tools/websearchapi
node websearchapi.js config set-key YOUR_API_KEY
# 测试
node websearchapi.js test
Troubleshooting
Search failed
- Check whether the API Key is correct:
node websearchapi.js config - Check the network connection
Finance search returns no results
- Try using the stock ticker:
finance "AAPL" - Try English:
finance "Tesla stock"
Request timeout
- When the network is slow, you can increase the number of retries:
config set-retry 5