设置 MCP 预编码搜索例程意味着配置您的 Claude Code 环境,以便代理在生成代码之前搜索文档和 API 参考,从而防止幻觉导入和过时的 API 调用。通过添加 Scavio MCP 服务器和 CLAUDE.md 指令,您可以创建一个工作流程,其中 Claude Code 在写入一行之前验证软件包版本、读取更改日志并根据实时搜索数据检查 API 签名。
前置条件
- 克劳德代码安装和配置
- 来自 scavio.dev 的 Scavio API 密钥
- 带有 CLAUDE.md 的项目目录
操作指南
步骤 1: 配置 Scavio MCP 服务器
将 Scadio MCP 服务器添加到您的项目中,以便 Claude Code 可以调用搜索工具。
cat > .mcp.json << 'MCPEOF'
{
"mcpServers": {
"scavio": {
"url": "https://mcp.scavio.dev/mcp",
"headers": {
"Authorization": "Bearer YOUR_SCAVIO_API_KEY"
}
}
}
}
MCPEOF步骤 2: 将预编码搜索指令添加到 CLAUDE.md
在 CLAUDE.md 中编写指令,告诉 Claude Code 在编码之前进行搜索。这将创建自动预编码搜索例程。
cat >> CLAUDE.md << 'EOF'
## Pre-Coding Search Routine
Before writing code that uses an external library or API:
1. Search for the current version: "[library name] latest version 2026"
2. Search for the API signature: "[library name] [function] API reference"
3. If the library was updated in the last 6 months, search for migration guides
4. Verify import paths match the current version
Do NOT rely on training data for:
- Package versions or install commands
- API endpoint URLs or authentication methods
- Configuration file formats
- CLI flag syntax
EOF步骤 3: 添加特定于域的搜索规则
使用针对您团队使用的框架和 API 的特定于项目的规则来扩展例程。
cat >> CLAUDE.md << 'EOF'
## Project-Specific Search Rules
Always verify before using:
- Next.js: search "next.js [feature] app router" (not pages router)
- Stripe: search "stripe api [endpoint] 2026" (API changes frequently)
- Prisma: search "prisma [method] latest" (ORM syntax varies by version)
- AWS SDK: search "aws sdk v3 [service]" (v2 is deprecated)
EOF步骤 4: 测试常规
打开 Claude Code 并给它一个需要外部 API 知识的任务。在编码之前验证其搜索。
# In Claude Code, try:
# 'Add Stripe checkout to this Next.js app'
#
# Expected behavior:
# 1. Claude searches 'stripe checkout api next.js 2026'
# 2. Claude searches 'next.js server actions stripe'
# 3. Claude writes code using verified API signatures
#
# Without the routine, Claude might use:
# - Deprecated Stripe API methods
# - Pages router patterns instead of App Router
# - Wrong import paths from outdated training dataPython 示例
# Generate the MCP config and CLAUDE.md programmatically
import json, os
def setup_pre_coding_routine(project_dir, api_key, frameworks=None):
# Write MCP config
mcp_config = {
'mcpServers': {
'scavio': {
'url': 'https://mcp.scavio.dev/mcp',
'headers': {'Authorization': f'Bearer {api_key}'}
}
}
}
mcp_path = os.path.join(project_dir, '.mcp.json')
with open(mcp_path, 'w') as f:
json.dump(mcp_config, f, indent=2)
# Append search routine to CLAUDE.md
routine = '''
## Pre-Coding Search Routine
Before writing code that uses an external library or API:
1. Search for the current version
2. Search for the API signature or reference docs
3. Check for recent breaking changes or migration guides
4. Verify import paths match the current version
'''
if frameworks:
routine += '\n## Framework-Specific Rules\n'
for fw in frameworks:
routine += f'- {fw}: always search "{fw} latest API" before using\n'
claude_md_path = os.path.join(project_dir, 'CLAUDE.md')
with open(claude_md_path, 'a') as f:
f.write(routine)
print(f'MCP config written to {mcp_path}')
print(f'Search routine appended to {claude_md_path}')
setup_pre_coding_routine('.', 'YOUR_API_KEY', ['Next.js', 'Stripe', 'Prisma'])JavaScript 示例
const fs = require('fs');
function setupPreCodingRoutine(projectDir, apiKey, frameworks = []) {
const mcpConfig = {
mcpServers: {
scavio: {
url: 'https://mcp.scavio.dev/mcp',
headers: {Authorization: \`Bearer \${apiKey}\`}
}
}
};
fs.writeFileSync(\`\${projectDir}/.mcp.json\`, JSON.stringify(mcpConfig, null, 2));
let routine = \`
## Pre-Coding Search Routine
Before writing code that uses an external library or API:
1. Search for the current version
2. Search for the API signature or reference docs
3. Check for recent breaking changes
4. Verify import paths match the current version
\`;
if (frameworks.length) {
routine += '\n## Framework-Specific Rules\n';
frameworks.forEach(fw => {
routine += \`- \${fw}: always search "\${fw} latest API" before using\n\`;
});
}
fs.appendFileSync(\`\${projectDir}/CLAUDE.md\`, routine);
console.log('MCP config and search routine configured');
}
setupPreCodingRoutine('.', 'YOUR_API_KEY', ['Next.js', 'Stripe', 'Prisma']);预期输出
MCP config written to .mcp.json
Search routine appended to CLAUDE.md
Claude Code now searches docs and API references before writing code.