API / connect-remote-mcp-in-grok-api

API

Connect a remote MCP server in the Grok API

Connect a remote MCP server in the Grok API

Put an mcp tool on POST https://api.x.ai/v1/responses. xAI opens the MCP server, injects its tools, and runs the calls. Transports: Streaming HTTP or SSE. Get a key at console.x.ai.

Minimal call

server_url and server_label are required.

curl https://api.x.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
  "model": "grok-4.6",
  "input": [
    {
      "role": "user",
      "content": "What can you do with https://github.com/xai-org/xai-sdk-python?"
    }
  ],
  "tools": [
    {
      "type": "mcp",
      "server_url": "https://mcp.deepwiki.com/mcp",
      "server_label": "deepwiki"
    }
  ]
}'

Python with the official SDK:

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.6",
    tools=[mcp(server_url="https://mcp.deepwiki.com/mcp", server_label="deepwiki")],
)
chat.append(user("What can you do with https://github.com/xai-org/xai-sdk-python?"))
for response, chunk in chat.stream():
    if chunk.content:
        print(chunk.content, end="", flush=True)
print("\nUsage:", response.server_side_tool_usage)

Optional fields: server_description, allowed_tools (SDK: allowed_tool_names), authorization (Bearer token on the MCP request), headers (SDK: extra_headers). You can list several mcp entries in one tools array. require_approval and connector_id from OpenAI's Responses API are not supported here.

Limit which tools load

With no allowed_tools, every tool on that server lands in the model context. Pin the names you want:

mcp(
    server_url="https://comprehensive-tools.example.com/mcp",
    allowed_tool_names=["search_database", "format_data"],
)

Usage reports SERVER_SIDE_TOOL_MCP. Call names look like {server_label}.{tool_name}. Responses API output items are mcp_call.

Pitfalls

  • Only Streaming HTTP and SSE. stdio MCP servers used by local CLIs will not connect.
  • HTTPS plus authorization or headers for anything that is not a public demo server.
  • A fat tool list burns context. Use allowed_tools when the server exposes more than you need.
  • Console API credits are a separate bill from SuperGrok's weekly pool on grok.com.