Grok API 交互工具

提供基于Grok API的Web和X(Twitter)搜索交互工具,支持状态化对话和多工具集成。

  • by rrajasek95
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 rrajasek95/grok-api-mcp

使用说明

Grok API MCP公司

两个独立的工具用于Grok与web和X(Twitter)搜索基础的有状态交互:

  1. CLI客户端(Rust) -独立 grok-ask 直接调用Grok API的二进制文件
  2. MCP服务器(Python) -FastMCP后端用于基于工具的集成(Claude Desktop等)

特性

  • 网络搜索限制:自动网络搜索当前/事实信息
  • X(推特)搜索暂停:使用句柄、日期和媒体过滤器搜索X个帖子
  • 有状态的对话:通过以下方式维护多个查询的上下文 response_id
  • 多种工具:搜索、询问、思考、聊天、x-Search和x-ask模式

建筑

更喜欢直接使用Rust CLI。当您特别需要在像Claude Desktop这样的主机中集成MCP时,请联系Python MCP服务器。

CLI和MCP服务器是相同Grok Responses API的独立客户端:

┌─────────────────────────────────────────────────────────┐
│  CLI Client (Rust) - grok-ask                           │
│  - Standalone binary, no dependencies                   │
│  - Direct Grok API calls via HTTP                       │
│  - Use as Claude Code skill or command line             │
└─────────────────────┬───────────────────────────────────┘
                      │
                      ▼
┌─────────────────────────────────────────────────────────┐
│  Grok API                                               │
│  - Models: grok-4-1-fast-non-reasoning / grok-4-1-fast  │
│  - Automatic grounding (web_search, x_search)           │
│  - Server-side conversation state                       │
└─────────────────────┬───────────────────────────────────┘
                      ▲
                      │
┌─────────────────────┴───────────────────────────────────┐
│  MCP Server (Python/FastMCP) - optional                 │
│  - For Claude Desktop, Cursor, etc.                     │
│  - Same API features via MCP tools                      │
└─────────────────────────────────────────────────────────┘

快速开始

命令行用法

# Install the binary first
cd cli
cargo install --path .

# Make sure Cargo binaries are on your PATH
export PATH="$HOME/.cargo/bin:$PATH"

# Set API key
export XAI_API_KEY=your_key_here

# Quick web search
grok-ask --search "latest AI news"

# Get grounded answer
grok-ask --ask "What is xAI?"

# Deep reasoning
grok-ask --think "Compare Grok to GPT-4"

# Chat without web search
grok-ask --chat "Tell me a joke"

# Search X (Twitter) posts
grok-ask --x-search "AI news"

# Get answers grounded in X posts
grok-ask --x-ask "What are people saying about Grok?"

# X search with filters
grok-ask --x-search "AI safety" --allowed-handles elonmusk,sama
grok-ask --x-search "SpaceX" --from-date 2025-01-01 --to-date 2025-01-15
grok-ask --x-ask "Latest xAI updates" --enable-images --enable-video

# Follow-up conversation
grok-ask --ask "What products does xAI offer?" -r <response_id>

# JSON output
grok-ask --ask "Query" -o json

MCP服务器使用情况

cd server
uv sync
cp .env.example .env
# Edit .env with your XAI_API_KEY
uv run python server.py

然后使用工具通过MCP客户端连接: search, ask, think, chat, x_search, x_ask

安装

CLI(Rust)

# Install globally
cd cli
cargo install --path .

# If grok-ask is not found afterwards, add Cargo's bin directory to your shell PATH
echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> ~/.zshrc
# Reload your shell if you want the new PATH to apply immediately in the current terminal.
# This is only needed after editing ~/.zshrc; opening a new terminal works too.
source ~/.zshrc

# Or build locally
cargo build --release
# Binary at: ./target/release/grok-ask

MCP服务器(Python)

cd server
uv sync
cp .env.example .env
# Edit .env with your XAI_API_KEY

API密钥

从以下位置获取Grok API密钥:https://console.x.ai/

工具

工具 模型 搜索 最大令牌
search grok-4-1快速推理 网络 4096
ask grok-4-1快速推理 网络 8192
think grok-4-1-fast 网络 16384
chat grok-4-1不讲道理 无 8192
x_search grok-4-1快速推理 X(推特) 4096
x_ask grok-4-1快速推理 X(推特) 8192

X搜索筛选器选项

选项 描述
allowed_handles 仅包括来自这些X手柄的帖子(最多10个)
excluded_handles 从这些X手柄中排除帖子(最多10个)
from_date YYYY-MM-DD格式的开始日期
to_date YYYY-MM-DD格式的结束日期
enable_images 允许模型分析帖子中的图像
enable_video 允许模型分析帖子中的视频

用法示例

搜索当前信息

search("latest AI news 2025")

问一个事实问题

ask("What is xAI and what products do they offer?")

深度分析

think("Compare Grok's capabilities to other AI models")

普通聊天(无网络搜索)

chat("Tell me a joke about programming")

搜索X个帖子

x_search("AI safety discussions")

使用筛选器搜索X

# Only from specific handles
x_search("AI news", allowed_handles=["elonmusk", "sama"])

# Date range
x_search("SpaceX launch", from_date="2025-01-01", to_date="2025-01-15")

# With media understanding
x_search("xAI demos", enable_images=True, enable_video=True)

从X个帖子中获取答案

x_ask("What are people saying about Grok?")

# From specific accounts
x_ask("Latest xAI announcements", allowed_handles=["xai"])

多回合对话

# First question
response = ask("What is quantum computing?")
# Returns: ... response_id: resp_abc123

# Follow-up (Grok remembers context)
response = ask("How is it different from classical computing?", response_id="resp_abc123")

有状态的对话

API通过 response_id:

  • 无需重新发送对话历史记录
  • 隐式缓存(更快、更便宜)
  • 通过 response_id 从之前的回复继续对话

Claude桌面集成

添加到您的Claude Desktop配置(~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "grok": {
      "command": "uv",
      "args": [
        "run",
        "--directory",
        "/path/to/grok-api-mcp/server",
        "python",
        "server.py"
      ],
      "env": {
        "XAI_API_KEY": "your_api_key_here"
      }
    }
  }
}

项目结构

grok-api-mcp/
├── cli/
│   ├── src/
│   │   └── main.rs        # Rust CLI
│   └── Cargo.toml
├── server/
│   ├── server.py          # MCP server (Python/FastMCP)
│   ├── pyproject.toml
│   └── .env.example
├── skill.md               # Claude Code skill
└── README.md

API 参考

CLI和MCP服务器都使用Grok API端点: https://api.x.ai/v1/responses

请求格式(Web搜索)

{
  "model": "grok-4-1-fast-non-reasoning",
  "input": [
    {"role": "user", "content": "Your question"}
  ],
  "tools": [
    {"type": "web_search"}
  ]
}

请求格式(X搜索)

{
  "model": "grok-4-1-fast-non-reasoning",
  "input": [
    {"role": "user", "content": "Your question"}
  ],
  "tools": [
    {
      "type": "x_search",
      "allowed_x_handles": ["elonmusk"],
      "from_date": "2025-01-01",
      "to_date": "2025-01-15",
      "enable_image_understanding": true,
      "enable_video_understanding": true
    }
  ]
}

对于 think 命令,使用 grok-4-1-fast (推理能力模型)。

认证

通过以下方式进行承载令牌身份验证 XAI_API_KEY 环境变量。

学分

基于 双子相互作用mcp 通过 @DigiBugCat.

许可证

麻省理工学院