高级用法
本指南涵盖了 GPT Researcher MCP 服务器的高级使用场景和配置。
自定义配置
您可以通过修改各种配置参数来自定义 MCP 服务器的行为。
环境变量
创建一个包含附加配置选项的 .env 文件。
# Required API keys
OPENAI_API_KEY=your_openai_api_key
TAVILY_API_KEY=your_tavily_api_key
# Optional configurations assuming using OpenAI
STRATEGIC_LLM=openai:gpt-4o-mini # Change default to faster reasoning model
MAX_ITERATIONS=2 # Make the research faster by reducing iterations
SCRAPER=tavily_extract # For production use, using hosted scraping methods (assuming you use tavily)
服务器配置文件
您可以创建一个 config.json 文件来自定义服务器行为。
{
"host": "0.0.0.0",
"port": 8000,
"debug": false,
"timeout": 300,
"max_concurrent_requests": 10
}
与 Claude 集成
要有效地与 Claude 集成:
- 确保您的 Claude 模型已启用 MCP 功能。
- 将 Claude 指向 MCP 服务器端点。
- 使用适当的提示来指导 Claude 使用研究工具。
Claude 的配置示例
{
"tools": [
{
"name": "gptr-researcher",
"endpoint": "https://:8000/mcp"
}
]
}
高级工具用法
进行深度研究
为实现更深度的研究能力:
Use the conduct_research tool with these advanced parameters:
{
"query": "quantum computing advancements 2024",
"depth": "deep",
"focus_areas": ["hardware", "algorithms", "applications"],
"timeline": "last 1 year"
}
自定义报告生成
write_report 工具接受多个自定义选项。
Use the write_report tool with:
{
"style": "academic",
"format": "markdown",
"include_images": true,
"citation_style": "APA",
"executive_summary": true
}
保护您的 MCP 服务器
要保护您的 MCP 服务器部署:
-
添加 API 密钥认证
# Add to server.py
@app.middleware("http")
async def verify_api_key(request, call_next):
api_key = request.headers.get("X-API-Key")
if api_key != os.getenv("MCP_API_KEY"):
return JSONResponse(status_code=401, content={"error": "Invalid API key"})
return await call_next(request) -
启用 HTTPS
# Run with HTTPS
uvicorn server:app --host 0.0.0.0 --port 8000 --ssl-keyfile=./key.pem --ssl-certfile=./cert.pem -
设置速率限制
# Add rate limiting
from fastapi import Depends, HTTPException
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
@app.post("/mcp")
@limiter.limit("10/minute")
async def mcp_endpoint(request: Request, payload: dict):
# Endpoint code
使用 Docker 部署
为了方便使用 Docker 部署:
- 创建一个 Dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "server.py"]
- 构建并运行 Docker 容器
docker build -t gpt-researcher-mcp .
docker run -p 8000:8000 -e OPENAI_API_KEY=your_key -e TAVILY_API_KEY=your_key gpt-researcher-mcp
监控与日志记录
启用详细日志记录以监控服务器活动。
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("mcp_server.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger("mcp_server")
扩展功能
您可以通过附加功能来扩展 MCP 服务器。
- 添加新的研究工具
- 实现自定义报告格式
- 与额外的数据源集成
- 添加专门的研究代理
例如,要添加一个新工具:
@app.tool("analyze_sentiment")
async def analyze_sentiment(query: str):
"""Analyze the sentiment of research results."""
# Implementation
return {"sentiment": "positive", "confidence": 0.87}
高级问题故障排除
处理速率限制
如果您遇到外部 API 的速率限制:
import time
from tenacity import retry, wait_exponential, stop_after_attempt
@retry(wait=wait_exponential(multiplier=1, min=4, max=10), stop=stop_after_attempt(5))
def search_with_retry(query):
try:
return search_engine.search(query)
except RateLimitError:
time.sleep(5)
raise
内存管理
用于处理大型研究任务:
import gc
def clean_memory():
"""Force garbage collection to free memory"""
gc.collect()
后续步骤
- 探索与您自己的应用程序集成
- 了解创建自定义代理以增强研究能力
- 为 GPT Researcher 项目做贡献
:-)