困惑API平台MCP服务器
困惑API平台的官方MCP服务器实现,通过Sonar模型和搜索API为人工智能助手提供实时网络搜索、推理和研究能力。该服务现在公开了一个HTTP接口,因此它可以远程运行(例如在Heroku上),而不依赖于stdio传输。
请咨询官方 DeepWiki页面 以帮助理解模型上下文协议概念。
可用工具
困惑研究
使用困惑搜索API直接进行网络搜索。返回带有元数据的排名搜索结果,非常适合查找当前信息。
困惑_问题
通用对话式人工智能,使用实时网络搜索 sonar-pro 模型。非常适合快速提问和日常搜索。
困惑研究
使用 sonar-deep-research 模型。非常适合进行深入分析和详细报告。
困惑原因
使用高级推理和解决问题 sonar-reasoning-pro 模型。非常适合复杂的分析任务。
配置
所需的环境变量
PERPLEXITY_API_KEY:您的困惑API密钥(从 API门户网站).PORT:(可选)HTTP服务器的端口。默认为3000当地;Heroku会自动注入这个。PERPLEXITY_TIMEOUT_MS:(可选)上游请求的超时时间(毫秒)。默认为300000(5分钟)。
本地运行
npm install
npm run build
npm start一旦启动,服务器就会监听 http://localhost:3000 (或价值 PORT).您可以使用标准HTTP请求与它进行交互:
# Retrieve tool metadata
curl http://localhost:3000/tools
# Invoke a tool
curl \
-H "Content-Type: application/json" \
-d '{
"name": "perplexity_search",
"arguments": {"query": "latest space news", "max_results": 3}
}' \
http://localhost:3000/tools/callAPI终点
GET /–基本服务信息和可用工具名称。GET /health–正常运行时间的健康报告。GET /tools–完整的工具定义和JSON模式。POST /tools/call-调用一个包含JSON正文的工具name和arguments.POST|GET|DELETE /mcp–MCP客户端的流式HTTP传输。POST请求必须公告Accept: application/json, text/event-stream并在打开SSE流之前初始化会话GET /mcp.
MCP流式HTTP客户端
- 使用MCP Streamable HTTP传输连接
http(s):///mcp.
- 一个最小的Python示例与官方
mcp包裹:
import asyncio
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
async def main():
async with streamablehttp_client("https://your-app.herokuapp.com/mcp") as (read_stream, write_stream, _):
async with ClientSession(read_stream, write_stream) as session:
await session.initialize()
tools = await session.list_tools()
print(tools)
asyncio.run(main())部署到Heroku
- 创建Heroku应用程序:
heroku create your-perplexity-server- 设置所需配置:
heroku config:set PERPLEXITY_API_KEY=your_key_here PERPLEXITY_TIMEOUT_MS=600000- 部署:
git push heroku main- 测试部署(替换
your-app使用您的Heroku应用程序名称):
curl https://your-app.herokuapp.com/health故障排除
- API关键问题:确保
PERPLEXITY_API_KEY为运行时环境配置。 - 连接错误:检查出站连接并确认困惑API是可访问的。
- 无效请求:验证工具调用是否使用记录的JSON模式。
- 超时错误:对于长时间运行的查询,增加
PERPLEXITY_TIMEOUT_MS.
