Skip to content

Grok (xAI)

xAI’s API connects to remote MCP servers for you: add jitprompt to the request’s tools, and Grok calls get_toc, search_instructions and get_section on xAI’s side while it answers. Your code makes one request.

You need an API key for the namespace (namespace → API keys) and an xAI API key. Keep both in environment variables, not in code. The examples use grok-4.7; any current Grok model works.

Terminal window
pip install xai-sdk
import os
from xai_sdk import Client
from xai_sdk.chat import system, user
from xai_sdk.tools import mcp
client = Client(api_key=os.environ["XAI_API_KEY"])
chat = client.chat.create(
model="grok-4.7",
tools=[mcp(
server_url="https://app.jitprompt.com/mcp",
server_label="jitprompt",
server_description="The company's operating instructions for this agent.",
authorization=os.environ["JITPROMPT_KEY"],
allowed_tool_names=["get_toc", "search_instructions", "get_section"],
)],
)
chat.append(system(
"You are a support agent. Your operating instructions are in the jitprompt tools: "
"call get_toc first, and search_instructions before each new task. Follow what they return."
))
chat.append(user("Can I get a refund on my annual plan?"))
print(chat.sample().content)
  • authorization is sent as the bearer token on each call xAI makes to jitprompt. Send it with every request.
  • allowed_tool_names limits the import to jitprompt’s three tools. Without it, every tool the server offers is loaded; for jitprompt that’s the same three.

xAI also accepts the OpenAI Responses API format, so the OpenAI SDK works with xAI’s base URL:

import os
from openai import OpenAI
client = OpenAI(base_url="https://api.x.ai/v1", api_key=os.environ["XAI_API_KEY"])
resp = client.responses.create(
model="grok-4.7",
instructions=(
"Your operating instructions are in the jitprompt tools: call get_toc first, and "
"search_instructions before each new task. Follow what they return."
),
tools=[{
"type": "mcp",
"server_label": "jitprompt",
"server_url": "https://app.jitprompt.com/mcp",
"authorization": os.environ["JITPROMPT_KEY"],
"allowed_tools": ["get_toc", "search_instructions", "get_section"],
}],
input="Can I get a refund on my annual plan?",
)
print(resp.output_text)

Unlike OpenAI’s own API, xAI doesn’t take require_approval: leave it out. The tools run without a round trip to you (all three are read-only).

The same request with curl:

Terminal window
curl https://api.x.ai/v1/responses \
-H "Content-Type: application/json" -H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-4.7",
"tools": [{
"type": "mcp",
"server_label": "jitprompt",
"server_url": "https://app.jitprompt.com/mcp",
"authorization": "'"$JITPROMPT_KEY"'"
}],
"input": "Can I get a refund on my annual plan?"
}'

The Authorization header there is your xAI key; the jitprompt key goes in the tool’s authorization field.

To run the agent loop in your own process instead, use the LangChain guide with MODEL = "xai:grok-4.7" (pip install langchain-xai).

See MCP tools for what each tool returns, and Writing prompts for agents for the system-prompt line that makes agents search at the right moments.