ScavioScavio
产品定价文档
登录开始使用
  1. 首页
  2. 教程
  3. 如何构建自定义 MCP 搜索服务器
教程

如何构建自定义 MCP 搜索服务器

使用搜索和提取工具构建由 Scavio API 支持的自定义 MCP 服务器。 Node.js 实现与任何 MCP 客户端兼容。

获取免费API密钥API文档

模型上下文协议允许人工智能代理通过标准化接口调用外部工具。构建由 Scavio API 支持的自定义 MCP 服务器使您可以完全控制工具定义、响应格式和速率限制,同时保持与任何 MCP 客户端(Cursor、Claude Desktop、自定义代理)的兼容性。本教程使用搜索和提取工具构建 Node.js MCP 服务器。

前置条件

  • Node.js 18+
  • npm or pnpm
  • 来自 scavio.dev 的 Scavio API 密钥
  • 基本熟悉MCP协议

操作指南

步骤 1: 设置 MCP 服务器项目

使用 MCP SDK 和 HTTP 依赖项初始化项目。

Bash
# Create project
mkdir scavio-mcp-server && cd scavio-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk

# Project structure:
# scavio-mcp-server/
#   index.js
#   package.json

# package.json - add type module:
# { "type": "module", "main": "index.js" }

步骤 2: 使用搜索工具实现 MCP 服务器

使用调用 Scavio API 的搜索工具创建 MCP 服务器。

JavaScript
// index.js
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';

const API_KEY = process.env.SCAVIO_API_KEY;
const SH = { 'x-api-key': API_KEY, 'Content-Type': 'application/json' };

const server = new McpServer({ name: 'scavio-search', version: '1.0.0' });

server.tool('search',
  { query: { type: 'string', description: 'Search query' },
    platform: { type: 'string', description: 'Platform: google, reddit, youtube, amazon, walmart', default: 'google' },
    country: { type: 'string', description: 'Country code', default: 'us' } },
  async ({ query, platform, country }) => {
    const body = { query, country_code: country || 'us' };
    if (platform && platform !== 'google') body.platform = platform;
    const r = await fetch('https://api.scavio.dev/api/v1/search', {
      method: 'POST', headers: SH, body: JSON.stringify(body)
    });
    const data = await r.json();
    const results = (data.organic_results || []).slice(0, 5)
      .map(r => `${r.position}. ${r.title}\n   ${r.link}\n   ${(r.snippet || '').slice(0, 120)}`).join('\n\n');
    return { content: [{ type: 'text', text: results || 'No results found.' }] };
  }
);

console.error('Scavio MCP server starting...');

步骤 3: 添加提取工具

在MCP服务器上添加URL内容提取工具。

JavaScript
// Add after the search tool definition:

server.tool('extract',
  { url: { type: 'string', description: 'URL to extract content from' } },
  async ({ url }) => {
    const r = await fetch('https://api.scavio.dev/api/v1/extract', {
      method: 'POST', headers: SH, body: JSON.stringify({ url })
    });
    const data = await r.json();
    const content = data.content || data.text || JSON.stringify(data);
    return { content: [{ type: 'text', text: content.slice(0, 2000) }] };
  }
);

// List available tools for debugging:
server.tool('list_platforms',
  {},
  async () => {
    const platforms = ['google', 'youtube', 'amazon', 'walmart', 'reddit'];
    const text = platforms.map(p => `- ${p}: Search ${p} results via Scavio API`).join('\n');
    return { content: [{ type: 'text', text: `Available search platforms:\n${text}\n\nCost: $0.005 per search` }] };
  }
);

步骤 4: 启动服务器并配置客户端

启动 MCP 服务器并将其连接到 Cursor 或 Claude Desktop。

Bash
// Add at the end of index.js:
const transport = new StdioServerTransport();
await server.connect(transport);

// Run the server:
// SCAVIO_API_KEY=your_key node index.js

// Configure in Cursor (~/.cursor/mcp.json):
// {
//   "mcpServers": {
//     "scavio-custom": {
//       "command": "node",
//       "args": ["/path/to/scavio-mcp-server/index.js"],
//       "env": { "SCAVIO_API_KEY": "your_key" }
//     }
//   }
// }

// Or in Claude Desktop (claude_desktop_config.json):
// {
//   "mcpServers": {
//     "scavio-custom": {
//       "command": "node",
//       "args": ["/path/to/scavio-mcp-server/index.js"],
//       "env": { "SCAVIO_API_KEY": "your_key" }
//     }
//   }
// }

Python 示例

Python
# Python MCP server alternative:
import os, requests
SH = {'x-api-key': os.environ['SCAVIO_API_KEY'], 'Content-Type': 'application/json'}

def search(query, platform=None):
    body = {'query': query, 'country_code': 'us'}
    if platform: body['platform'] = platform
    data = requests.post('https://api.scavio.dev/api/v1/search',
        headers=SH, json=body).json()
    for r in data.get('organic_results', [])[:3]:
        print(f'{r["position"]}. {r["title"][:50]}')

search('mcp server tutorial')

JavaScript 示例

JavaScript
// Test the MCP server tools directly:
const API_KEY = process.env.SCAVIO_API_KEY;
const SH = { 'x-api-key': API_KEY, 'Content-Type': 'application/json' };

async function testSearch(query, platform) {
  const body = { query, country_code: 'us' };
  if (platform) body.platform = platform;
  const data = await fetch('https://api.scavio.dev/api/v1/search', {
    method: 'POST', headers: SH, body: JSON.stringify(body)
  }).then(r => r.json());
  (data.organic_results || []).slice(0, 3).forEach(r =>
    console.log(`${r.position}. ${r.title.slice(0, 50)}`)
  );
}
await testSearch('mcp server tutorial');

预期输出

JSON
Scavio MCP server starting...

# In Cursor after configuration:
Cursor Settings > MCP:
  scavio-custom: Connected (3 tools)

# Tools available:
  - search: Multi-platform web search
  - extract: URL content extraction
  - list_platforms: Show available platforms

# Test query in Cursor Composer:
User: Search Reddit for MCP server best practices
Agent: [Calling scavio-custom.search with platform='reddit']

1. Building MCP Servers - Best Practices for 2026
   https://reddit.com/r/...
   Here are the key patterns I've found...

Cost per search: $0.005

相关教程

  • 如何通过 MCP 将实时搜索添加到光标
  • 如何通过 MCP 将 Web 搜索添加到 Hermes 代理
  • 如何为代理即服务构建搜索层

常见问题

大多数开发者在15到30分钟内完成本教程。您需要一个Scavio API密钥(免费套餐即可)和可用的Python或JavaScript环境。

Node.js 18+. npm or pnpm. 来自 scavio.dev 的 Scavio API 密钥. 基本熟悉MCP协议. Scavio API密钥注册即送50个免费积分。

可以。免费套餐注册即送50个积分,完全足够完成本教程并构建一个可运行的原型解决方案。

Scavio提供原生LangChain包(langchain-scavio)、MCP服务器以及适用于任何HTTP客户端的REST API。本教程使用 the raw REST API, 但您可以根据需要适配您选择的框架。

相关资源

Use Case

MCP 自定义搜索服务器

Read more
Comparison

MCP Search Integration vs Direct API Integration

Read more
Best Of

Google I/O 2026 AI模式变化后最佳搜索API

Read more
Use Case

IDE MCP 搜索

Read more
Glossary

搜索 API 供应商格局(2026)

Read more
Comparison

Scavio MCP vs Perplexity Advanced MCP

Read more

开始构建

使用搜索和提取工具构建由 Scavio API 支持的自定义 MCP 服务器。 Node.js 实现与任何 MCP 客户端兼容。

获取免费API密钥阅读文档
ScavioScavio

面向AI智能体的实时搜索API。搜索所有平台,不仅仅是Google。

产品

  • 功能
  • 定价
  • 控制台
  • 联盟计划

开发者

  • 文档
  • API参考
  • 快速开始
  • MCP集成
  • Python SDK

替代方案

  • Tavily替代方案
  • SerpAPI替代方案
  • Firecrawl替代方案
  • Exa替代方案

工具

  • JSON格式化
  • cURL转代码
  • Token计数器
  • 全部工具

© 2026 Scavio. 保留所有权利。

Featured on TAAFT
服务条款隐私政策