MCP(模型上下文协议)让AI模型通过标准化接口调用外部工具。 Scavio MCP 服务器位于 mcp.Scavio.dev/mcp,将网络搜索、TikTok 分析和 YouTube 数据公开为任何兼容客户端都可以调用的 MCP 工具。本教程将 Scavio MCP 服务器连接到在 Ollama 或 llama.cpp 上运行的本地 LLM,为您的本地模型提供与云托管代理相同的搜索功能。设置时间不到 5 分钟,每次工具调用成本为 0.005 美元。
前置条件
- Ollama 在本地安装并运行
- 已安装 Python 3.9+
- 来自 scavio.dev 的 Scavio API 密钥
- MCP工具协议的基本了解
操作指南
步骤 1: 创建MCP配置文件
编写指向 Scavio MCP 服务器的 .mcp.json 文件。该文件告诉 MCP 兼容客户端在哪里可以找到搜索工具。
import json
mcp_config = {
'mcpServers': {
'scavio': {
'url': 'https://mcp.scavio.dev/mcp',
'headers': {
'Authorization': 'Bearer ${SCAVIO_API_KEY}'
}
}
}
}
with open('.mcp.json', 'w') as f:
json.dump(mcp_config, f, indent=2)
print('MCP config written to .mcp.json')
print('Available tools: web_search, tiktok_search, youtube_search')步骤 2: 通过直接调用测试 MCP 连接
连接到 Ollama 之前,通过向搜索工具发出直接 HTTP 请求来测试 MCP 服务器是否正确响应。
import os, requests
SCAVIO_KEY = os.environ['SCAVIO_API_KEY']
# Direct test of the search endpoint
resp = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': SCAVIO_KEY, 'Content-Type': 'application/json'},
json={'query': 'test search query', 'country_code': 'us', 'num_results': 3})
data = resp.json()
print(f'Status: {resp.status_code}')
print(f'Results: {len(data.get("organic_results", []))}')
for r in data.get('organic_results', [])[:3]:
print(f' {r["title"]}')步骤 3: 将 MCP 工具连接到 Ollama
创建 Ollama 工具包装器,将 MCP 工具调用转换为 Scavio API 请求。这弥补了 Ollama 工具格式和 MCP 之间的差距。
import ollama
def handle_mcp_tool(tool_name: str, args: dict) -> str:
"""Route MCP tool calls to the Scavio API."""
if tool_name == 'web_search':
resp = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': SCAVIO_KEY, 'Content-Type': 'application/json'},
json={'query': args.get('query', ''), 'country_code': 'us', 'num_results': 5})
results = resp.json().get('organic_results', [])
return '\n'.join(f'{r["title"]}: {r.get("snippet", "")}' for r in results)
elif tool_name == 'tiktok_search':
resp = requests.post('https://api.scavio.dev/api/v1/tiktok/search/videos',
headers={'Authorization': f'Bearer {SCAVIO_KEY}', 'Content-Type': 'application/json'},
json={'keyword': args.get('query', ''), 'count': 5, 'cursor': 0})
videos = resp.json().get('data', {}).get('videos', [])
return '\n'.join(f'{v.get("desc", "")[:80]} ({v.get("stats", {}).get("playCount", 0)} plays)' for v in videos)
return 'Unknown tool'
tools = [
{'type': 'function', 'function': {'name': 'web_search', 'description': 'Search the web for current information',
'parameters': {'type': 'object', 'properties': {'query': {'type': 'string'}}, 'required': ['query']}}},
{'type': 'function', 'function': {'name': 'tiktok_search', 'description': 'Search TikTok videos',
'parameters': {'type': 'object', 'properties': {'query': {'type': 'string'}}, 'required': ['query']}}},
]
response = ollama.chat(model='llama3.1', messages=[{'role': 'user', 'content': 'What is trending on TikTok right now?'}], tools=tools)
if response.message.tool_calls:
for tc in response.message.tool_calls:
result = handle_mcp_tool(tc.function.name, tc.function.arguments)
print(f'Tool: {tc.function.name}')
print(result[:300])Python 示例
import os, requests, json
SCAVIO_KEY = os.environ['SCAVIO_API_KEY']
# Create MCP config
config = {'mcpServers': {'scavio': {
'url': 'https://mcp.scavio.dev/mcp',
'headers': {'Authorization': f'Bearer {SCAVIO_KEY}'}}}}
with open('.mcp.json', 'w') as f:
json.dump(config, f, indent=2)
# Test search via MCP
def mcp_search(query):
resp = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': SCAVIO_KEY, 'Content-Type': 'application/json'},
json={'query': query, 'country_code': 'us', 'num_results': 5})
return resp.json().get('organic_results', [])
results = mcp_search('latest AI news')
print(f'MCP search returned {len(results)} results')
for r in results[:3]:
print(f' {r["title"]}')JavaScript 示例
const SCAVIO_KEY = process.env.SCAVIO_API_KEY;
const fs = require('fs');
// Create MCP config
const config = { mcpServers: { scavio: {
url: 'https://mcp.scavio.dev/mcp',
headers: { Authorization: `Bearer ${SCAVIO_KEY}` }
}}};
fs.writeFileSync('.mcp.json', JSON.stringify(config, null, 2));
// Test search
async function mcpSearch(query) {
const resp = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST',
headers: { 'x-api-key': SCAVIO_KEY, 'Content-Type': 'application/json' },
body: JSON.stringify({ query, country_code: 'us', num_results: 5 })
});
const data = await resp.json();
return data.organic_results || [];
}
mcpSearch('latest AI news').then(r => {
console.log(`MCP search: ${r.length} results`);
r.slice(0, 3).forEach(x => console.log(` ${x.title}`));
});预期输出
MCP config written to .mcp.json
Available tools: web_search, tiktok_search, youtube_search
Status: 200
Results: 3
Latest AI News and Developments 2026
Top AI Frameworks Released This Year
AI Industry Update: May 2026
Tool: tiktok_search
Viral dance challenge takes over TikTok (2,345,678 plays)
New cooking hack everyone is trying (1,892,345 plays)