Memstate + Grok SDK
Give Grok agents persistent, structured memory via remote MCP.
The xAI SDK supports remote MCP over HTTP/SSE — Grok connects directly to the hosted Memstate MCP endpoint. No local server process is required; just pass your API key as a Bearer token.
Grok uses remote MCP (HTTP/SSE)
Unlike stdio-based MCP (which spawns a local subprocess), Grok connects to a remote MCP server over HTTP. Memstate provides a hosted SSE endpoint at https://mcp.memstate.ai/sse — no local Node.js process needed.
Requirements
- Python 3.9+
- xAI API key — console.x.ai
- Memstate API key — get one free
1
Install the xAI SDK
Terminal
pip install xai-sdk2
Set environment variables
Shell
export XAI_API_KEY="xai-..."
export MEMSTATE_API_KEY="mst_..."3
Connect Memstate via remote MCP
Use the mcp() tool helper from xai_sdk.tools. Grok will automatically discover and call all Memstate tools.
agent.py
"""
Memstate AI + Grok (xAI SDK)
Requires: pip install xai-sdk
Env: XAI_API_KEY, MEMSTATE_API_KEY
Grok uses REMOTE MCP over HTTP/SSE. The Memstate hosted MCP endpoint
is available at https://mcp.memstate.ai/sse — no local server needed.
"""
import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import mcp
client = Client(api_key=os.environ["XAI_API_KEY"])
# Grok connects to Memstate via the hosted remote MCP endpoint
chat = client.chat.create(
model="grok-3-latest",
tools=[
mcp(
server_url="https://mcp.memstate.ai/sse",
server_label="memstate",
server_description="Persistent versioned memory for AI agents",
authorization=f"Bearer {os.environ['MEMSTATE_API_KEY']}",
),
],
)
chat.append(
user(
'Save a memory: memstate_remember(project_id="demo", '
'content="## Grok Integration\\nMemstate connected via xAI remote MCP.", '
'source="agent"). Then call memstate_get(project_id="demo").'
)
)
for response, chunk in chat.stream():
if chunk.content:
print(chunk.content, end="", flush=True)
print()✓
Test it — run the onboarding prompt
Onboarding prompt
I'm onboarding Memstate AI memory for this project. Please:
1. Analyze this codebase and write a concise high-level architecture overview in markdown.
2. Save it with: memstate_remember(project_id="<your_project>", content="<the markdown>", source="agent")
3. Then call memstate_get(project_id="<your_project>") and show me the memory tree.