mcp服务器grok聊天
用于xAI Grok API的MCP(模型上下文协议)服务器。内置Rust,将聊天完成、视觉、web/X搜索、嵌入和模型列表作为MCP工具公开。
与所有MCP服务器一样,使用JSON-RPC 2.0通过stdio进行通信。
工具
| 工具 | 说明 |
|---|---|
chat | 向Grok发送聊天完成请求,其中包含可选的多回合历史记录、系统提示、结构化输出(JSON模式)、模型选择和多代理研究 |
chat_with_vision | 在给定图像URL和文本提示的情况下,使用Grok的视觉能力分析图像 |
chat_with_search | 使用实时网络搜索和/或X(推特)搜索到地面响应与Grok聊天 |
embedding | 使用Grok的嵌入模型生成文本嵌入 |
list_models | 列出所有可用的Grok型号及其ID(缓存5分钟) |
聊天
发送聊天完成请求。支持通过JSON消息历史数组进行多回合对话、系统提示、通过JSON模式进行结构化输出、温度控制、模型选择和多代理研究。
使用多代理模型时(任何包含以下内容的模型ID multi-agent),该请求通过Responses API自动路由。多代理模型将您的查询分派给并行研究的多个代理,然后综合他们的发现。使用 reasoning_effort 以控制代理数量。叫 list_models 工具查看当前可用的多代理模型。
参数:
| 名称 | 类型 | 必填 | 描述 |
|---|---|---|---|
prompt | string | yes | 要发送的用户消息 |
model | string | no | 型号ID(默认值: grok-4.3).呼叫 list_models 对于当前设置。 |
system_prompt | string | no | 设置上下文的系统提示 |
messages | string | no | 完整的对话历史记录为JSON数组 {role, content} 物体 |
temperature | 浮子 | 否 | 取样温度(0.0-2.0) |
max_tokens | integer | 否 | 要生成的最大令牌数 |
response_schema | string | no | 用于强制结构化输出的JSON模式字符串 |
reasoning_effort | string | 否 | 打开 grok-4.3: low/medium/high 控制本地推理深度。关于多代理模型: low/medium =4个代理, high/xhigh =16名代理人(xhigh 仅限多代理)。 |
聊天与视觉
使用Grok的视觉能力分析图像。
参数:
| 名称 | 类型 | 必填 | 描述 |
|---|---|---|---|
prompt | string | yes | 描述要分析内容的文本提示 |
image_url | string | yes | 图像的URL(必须是http://或https://) |
model | string | no | 型号ID(默认值: grok-4.3).必须是一个有远见的模特。呼叫 list_models 对于当前设置。 |
detail | string | no | 图像细节级别: low 或 high (默认值: high) |
temperature | 浮子 | 否 | 取样温度(0.0-2.0) |
max_tokens | integer | 否 | 要生成的最大令牌数 |
聊天与搜索
使用实时网络搜索和/或X(推特)搜索与Grok聊天。该模型会自动搜索互联网以确定其响应。
参数:
| 名称 | 类型 | 必填 | 描述 |
|---|---|---|---|
prompt | string | yes | 要发送的用户消息 |
search_type | string | 否 | 搜索类型: web, x,或 both (默认值: both) |
model | string | no | 型号ID(默认值: grok-4.3).呼叫 list_models 对于当前设置。 |
system_prompt | string | no | 设置上下文的系统提示 |
temperature | 浮子 | 否 | 取样温度(0.0-2.0) |
max_tokens | integer | 否 | 要生成的最大令牌数 |
reasoning_effort | string | 否 | 打开 grok-4.3: low/medium/high 控制本地推理深度。关于多代理模型: low/medium =4个代理, high/xhigh =16名代理人(xhigh 仅限多代理)。 |
嵌入
生成文本嵌入。
参数:
| 名称 | 类型 | 必填 | 描述 |
|---|---|---|---|
input | string | yes | 要嵌入为JSON的文本:单个字符串或字符串数组 |
model | string | no | 要使用的嵌入模型(默认值: grok-2-text-embedding) |
list_models
列出所有可用的Grok型号。没有参数。结果将缓存5分钟。
先决条件
- 铁锈(2024年版)
- 来自的xAI API密钥 console.x.ai
设置
创建配置文件:
mkdir -p ~/.config/mcp-server-grok-chat创建 ~/.config/mcp-server-grok-chat/config.toml:
api_key = "xai-..."构建
cargo build --release这产生 target/release/grok-chat.
发展:
cargo build # debug build
cargo run # run in dev mode
RUST_LOG=debug cargo run # run with debug loggingMCP配置
添加到您的Claude桌面配置(~/.config/Claude/claude_desktop_config.json):
{
"mcpServers": {
"grok-chat": {
"command": "/path/to/grok-chat"
}
}
}项目结构
src/
main.rs - entry point, config loading, stdio transport setup
server.rs - MCP tool definitions (chat, chat_with_vision, chat_with_search, embedding, list_models)
api.rs - xAI HTTP client, request/response types, response formatters
params.rs - tool parameter types with serde and JSON Schema derives
config.rs - TOML config loading许可证
麻省理工学院
