工具

Remote MCP Tool

Remote MCP Tool 允许 Grok 连接外部 MCP(Model Context Protocol)server,并通过第三方或自行实现的 custom tool 扩展能力。只需指定 server URL 和可选配置,xAI 会代为管理 MCP server 连接与交互。

SDK 支持

xAI native SDK、兼容 OpenAI 的 Responses API 以及 Speech to Speech API 均支持 Remote MCP tool。

配置

要使用 remote MCP tool,需要在请求的 tools array 中配置与 MCP server 的连接。

参数必需说明
server_url要连接的 MCP server URL。仅支持 Streaming HTTP 和 SSE transport。
server_label用于标识 server 的 label(用于 tool call prefix)
server_descriptionServer 所提供内容的说明
allowed_tools允许使用的特定 tool 名称列表(为空时允许全部)。xAI native SDK 使用参数名 allowed_tool_names 均支持 Remote MCP tool。
authorization将在发往 MCP server 的请求 Authorization header 中设置的 token
headers请求中要包含的额外 header。xAI native SDK 使用参数名 extra_headers 均支持 Remote MCP tool。

MCP Tool 基础用法

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.5",
    tools=[
        mcp(server_url="https://mcp.deepwiki.com/mcp"),
    ],
    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)

Tool 启用与访问控制

配置 Remote MCP Tool 时,如果未指定 allowed_tools,MCP server 公开的所有 tool definition 都会自动注入 model context。这意味着 model 可以访问 MCP server 提供的每个 tool,并在对话中使用其中任意一个。

例如,如果 MCP server 公开了 10 个不同 tool,而未指定 allowed_tools,则全部 10 个 tool definition 都可供 model 使用。Model 随后可根据用户请求和 tool description 选择调用其中任意 tool。

使用 allowed_tools 参数,仅选择性启用 MCP server 中的特定 tool。这可以带来以下主要优势:

  • 更高性能:通过限制 model 需要考虑的 tool definition,减少 context overhead

  • 降低风险:例如,只允许访问执行 read-only operation 的 tool,防止 model 修改数据

Python

# 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"]
)

该方式不会让 model 访问 server 提供的每个 tool,而是在确保 Grok 具备所需能力的同时,使其保持专注和高效。

Multi-Server 支持

同时启用多个 MCP server,构建丰富的专业 tool 生态:

Python

chat = client.chat.create(
    model="grok-4.5",
    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"),
    ],
)

每个 server 可以提供不同能力,例如文档 tool、API 集成、custom business logic 或专业数据处理,并且都可在单个 conversation 中访问。

最佳实践

  • 提供清晰的 server metadata:配置多个 MCP server 时,使用描述明确的 server_labelserver_description,帮助 model 理解每个 server 的用途并选择正确 tool

  • 适当过滤 tool:使用 allowed_tools,将访问范围限制为必要 tool。Server 有大量 tool 时尤其重要,因为 model 必须在 context 中保留所有可用 tool definition

  • 使用安全连接:始终使用 HTTPS URL,并在 MCP server 上实现适当的身份验证机制

  • 提供示例:虽然 model 通常能根据 tool description 和用户请求判断使用哪些 tool,但在 prompt 中提供示例可能会有所帮助