-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_grok_mcp.py
More file actions
68 lines (52 loc) · 1.71 KB
/
test_grok_mcp.py
File metadata and controls
68 lines (52 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from pathlib import Path
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import mcp
def load_env() -> None:
env_path = Path(__file__).resolve().parent / ".env"
if not env_path.exists():
return
try:
from dotenv import load_dotenv
except ImportError:
return
load_dotenv(env_path)
def get_xai_config() -> tuple[str, str]:
api_key = os.getenv("XAI_API_KEY", "").strip()
if not api_key:
raise RuntimeError("Set XAI_API_KEY to call Grok.")
model = os.getenv("XAI_MODEL", "grok-4-1-fast-reasoning")
return api_key, model
def main() -> None:
load_env()
server_url = os.getenv("MCP_SERVER_URL", "http://127.0.0.1:8000/mcp")
api_key, model = get_xai_config()
client = Client(api_key=api_key)
chat = client.chat.create(
model=model,
tools=[
mcp(
server_url=server_url,
)
],
include=["verbose_streaming"],
)
chat.append(
user('create a post saying xmcp test')
)
print("Starting chat stream...\n")
is_thinking = True
for response, chunk in chat.stream():
if response.usage.reasoning_tokens and is_thinking:
print("\rGrok is thinking...", end="", flush=True)
is_thinking = False
for tool_call in chunk.tool_calls:
print(f"\nGrok calling tool: {tool_call.function.name}")
print(f"With args: {tool_call.function.arguments}")
if chunk.content:
print(chunk.content, end="", flush=True)
print("\n\nFinal usage:", response.usage)
print("Server-side tool usage:", response.server_side_tool_usage)
if __name__ == "__main__":
main()