工具
远程 MCP 工具
远程 MCP 工具让 Grok 能够连接外部 MCP(Model Context Protocol)服务器,并通过第三方或自行实现的自定义工具扩展能力。只需指定服务器 URL 和可选配置,xAI 会代为管理 MCP 服务器的连接与交互。
SDK 支持
xAI 原生 SDK、兼容 OpenAI 的 Responses API 以及 语音转语音 API 均支持远程 MCP 工具。
配置
要使用远程 MCP 工具,需要在请求的 `tools` 数组中配置与 MCP 服务器的连接。
| 参数 | 必需 | 说明 |
|---|---|---|
server_url | 是 | 要连接的 MCP 服务器 URL。仅支持 Streaming HTTP 和 SSE 传输。 |
server_label | 是 | 用于标识服务器的标签(用于工具调用前缀) |
server_description | 否 | 服务器所提供功能的说明 |
allowed_tools | 否 | 允许使用的特定工具名称列表(为空时允许全部)。xAI 原生 SDK 使用参数名 allowed_tool_names 均支持远程 MCP 工具。 |
authorization | 否 | 会设置在发往 MCP 服务器请求的 `Authorization` 请求头中的 token |
headers | 否 | 请求中要包含的额外请求头。xAI 原生 SDK 使用参数名 extra_headers 均支持远程 MCP 工具。 |
MCP 工具基础用法
import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import mcp
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
model="grok-4.7",
tools=[
mcp(server_url="https://mcp.deepwiki.com/mcp", server_label="deepwiki"),
],
include=["verbose_streaming"],
)
chat.append(user("What can you do with https://github.com/xai-org/xai-sdk-python?"))
is_thinking = True
for response, chunk in chat.stream():
# View the server-side tool calls as they are being made in real-time
for tool_call in chunk.tool_calls:
print(f"\\nCalling tool: {tool_call.function.name} with arguments: {tool_call.function.arguments}")
if response.usage.reasoning_tokens and is_thinking:
print(f"\\rThinking... ({response.usage.reasoning_tokens} tokens)", end="", flush=True)
if chunk.content and is_thinking:
print("\\n\\nFinal Response:")
is_thinking = False
if chunk.content and not is_thinking:
print(chunk.content, end="", flush=True)
print("\\n\\nUsage:")
print(response.usage)
print(response.server_side_tool_usage)
print("\\n\\nServer Side Tool Calls:")
print(response.tool_calls)工具启用与访问控制
配置远程 MCP 工具时,如果未指定 allowed_tools,MCP 服务器公开的所有工具定义都会自动注入模型上下文。这意味着模型可以访问 MCP 服务器提供的每个工具,并在对话中使用其中任意一个。
例如,如果 MCP 服务器公开了 10 个不同工具,而未指定 allowed_tools,则全部 10 个工具定义都可供模型使用。模型随后可根据用户请求和工具说明选择调用其中任意工具。
使用 allowed_tools 参数可以选择性地只启用 MCP 服务器中的特定工具。这有以下主要优势:
更高性能:通过限制模型需要考虑的工具定义,减少上下文开销
降低风险:例如,只允许访问执行只读操作的工具,防止模型修改数据
# Enable only specific tools from a server with many available tools
mcp(
server_url="https://comprehensive-tools.example.com/mcp",
allowed_tool_names=["search_database", "format_data"]
)这种方式不会让模型访问服务器提供的每个工具,而是在确保 Grok 具备所需能力的同时,使其保持专注和高效。
多服务器支持
同时启用多个 MCP 服务器,构建丰富的专业工具生态:
chat = client.chat.create(
model="grok-4.7",
tools=[
mcp(server_url="https://mcp.deepwiki.com/mcp", server_label="deepwiki"),
mcp(server_url="https://your-custom-tools.com/mcp", server_label="custom"),
mcp(server_url="https://api.example.com/tools", server_label="api-tools"),
],
)每个服务器可以提供不同能力,例如文档工具、API 集成、自定义业务逻辑或专业数据处理,并且都可在单个对话中访问。
最佳实践
提供清晰的服务器元数据:配置多个 MCP 服务器时,使用描述明确的
server_label和server_description,帮助模型理解每个服务器的用途并选择正确工具适当过滤工具:使用
allowed_tools,将访问范围限制为必要工具。服务器有大量工具时尤其重要,因为模型必须在上下文中保留所有可用工具定义使用安全连接:始终使用 HTTPS URL,并在 MCP 服务器上实现适当的身份验证机制
提供示例:虽然模型通常能根据工具说明和用户请求判断要使用哪些工具,但在 prompt 中提供示例可能会有所帮助
最后更新:2026 年 9 月 1 日