Skip to content

Commit 62491b0

Browse files
committed
Document Grok agentic tools usage
1 parent c407f90 commit 62491b0

File tree

2 files changed

+131
-68
lines changed

2 files changed

+131
-68
lines changed

readme.md

Lines changed: 122 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -325,104 +325,170 @@ configuration without restarting the application.
325325

326326
## Grok
327327

328-
Full support for Grok [Live Search](https://docs.x.ai/docs/guides/live-search)
329-
and [Reasoning](https://docs.x.ai/docs/guides/reasoning) model options.
328+
Full support for Grok new [agentic tools](https://docs.x.ai/docs/guides/tools/overview):
329+
330+
### Web Search
330331

331332
```csharp
332-
// Sample X.AI client usage with .NET
333333
var messages = new Chat()
334334
{
335-
{ "system", "You are a highly intelligent AI assistant." },
336-
{ "user", "What is 101*3?" },
335+
{ "system", "You are an AI assistant that knows how to search the web." },
336+
{ "user", "What's Tesla stock worth today? Search X and the news for latest info." },
337337
};
338338

339-
var grok = new GrokChatClient(Environment.GetEnvironmentVariable("XAI_API_KEY")!, "grok-3-mini");
339+
var grok = new GrokClient(Environment.GetEnvironmentVariable("XAI_API_KEY")!).AsIChatClient("grok-4.1-fast");
340340

341-
var options = new GrokChatOptions
341+
var options = new ChatOptions
342342
{
343-
ModelId = "grok-4-fast-reasoning", // 👈 can override the model on the client
344-
Temperature = 0.7f,
345-
ReasoningEffort = ReasoningEffort.High, // 👈 or Low
346-
Search = GrokSearch.Auto, // 👈 or On/Off
343+
Tools = [new HostedWebSearchTool()] // 👈 compatible with OpenAI
347344
};
348345

349346
var response = await grok.GetResponseAsync(messages, options);
350347
```
351348

352-
Search can alternatively be configured using a regular `ChatOptions`
353-
and adding the `HostedWebSearchTool` to the tools collection, which
354-
sets the live search mode to `auto` like above:
349+
In addition to basic web search as shown above, Grok supports more
350+
[advanced search](https://docs.x.ai/docs/guides/tools/search-tools) scenarios,
351+
which can be opted-in by using Grok-specific types:
355352

356353
```csharp
357-
var messages = new Chat()
358-
{
359-
{ "system", "You are an AI assistant that knows how to search the web." },
360-
{ "user", "What's Tesla stock worth today? Search X and the news for latest info." },
361-
};
354+
var grok = new GrokChatClient(Environment.GetEnvironmentVariable("XAI_API_KEY")!).AsIChatClient("grok-4.1-fast");
355+
var response = await grok.GetResponseAsync(
356+
"What are the latest product news by Tesla?",
357+
new ChatOptions
358+
{
359+
Tools = [new GrokSearchTool()
360+
{
361+
AllowedDomains = [ "ir.tesla.com" ]
362+
}]
363+
});
364+
```
362365

363-
var grok = new GrokChatClient(Environment.GetEnvironmentVariable("XAI_API_KEY")!, "grok-3");
366+
You can alternatively set `ExcludedDomains` instead, and enable image
367+
understanding with `EnableImageUndestanding`. Learn more about these filters
368+
at [web search parameters](https://docs.x.ai/docs/guides/tools/search-tools#web-search-parameters).
364369

365-
var options = new ChatOptions
370+
### X Search
371+
372+
In addition to web search, Grok also supports searching on X (formerly Twitter):
373+
374+
```csharp
375+
var response = await grok.GetResponseAsync(
376+
"What's the latest on Optimus?",
377+
new ChatOptions
378+
{
379+
Tools = [new GrokXSearchTool
380+
{
381+
// AllowedHandles = [...],
382+
// ExcludedHandles = [...],
383+
// EnableImageUnderstanding = true,
384+
// EnableVideoUnderstanding = true,
385+
// FromDate = ...,
386+
// ToDate = ...,
387+
}]
388+
});
389+
```
390+
391+
Learn more about available filters at [X search parameters](https://docs.x.ai/docs/guides/tools/search-tools#x-search-parameters).
392+
393+
You can combine both web and X search in the same request by adding both tools.
394+
395+
### Code Execution
396+
397+
The code execution tool enables Grok to write and execute Python code in real-time,
398+
dramatically expanding its capabilities beyond text generation. This powerful feature
399+
allows Grok to perform precise calculations, complex data analysis, statistical
400+
computations, and solve mathematical problems that would be impossible through text alone.
401+
402+
This is Grok's equivalent of the OpenAI code interpreter, and is configured the same way:
403+
404+
```csharp
405+
var grok = new GrokClient(Configuration["XAI_API_KEY"]!).AsIChatClient("grok-4-fast");
406+
var response = await grok.GetResponseAsync(
407+
"Calculate the compound interest for $10,000 at 5% annually for 10 years",
408+
new ChatOptions
409+
{
410+
Tools = [new HostedCodeInterpreterTool()]
411+
});
412+
413+
var text = response.Text;
414+
Assert.Contains("$6,288.95", text);
415+
```
416+
417+
If you want to access the output from the code execution, you can add that as an
418+
include in the options:
419+
420+
```csharp
421+
var grok = new GrokClient(Configuration["XAI_API_KEY"]!).AsIChatClient("grok-4-fast");
422+
var options = new GrokChatOptions
366423
{
367-
Tools = [new HostedWebSearchTool()] // 👈 equals setting GrokSearch.Auto
424+
Include = { IncludeOption.CodeExecutionCallOutput },
425+
Tools = [new HostedCodeInterpreterTool()]
368426
};
369427

370-
var response = await grok.GetResponseAsync(messages, options);
428+
var response = await grok.GetResponseAsync(
429+
"Calculate the compound interest for $10,000 at 5% annually for 10 years",
430+
options);
431+
432+
var content = response.Messages
433+
.SelectMany(x => x.Contents)
434+
.OfType<CodeInterpreterToolResultContent>()
435+
.First();
436+
437+
foreach (AIContent output in content.Outputs)
438+
// process outputs from code interpreter
371439
```
372440

373-
We also provide an OpenAI-compatible `WebSearchTool` that can be used to restrict
374-
the search to a specific country in a way that works with both Grok and OpenAI:
441+
Learn more about the [code execution tool](https://docs.x.ai/docs/guides/tools/code-execution-tool).
442+
443+
### Collection Search
444+
445+
If you maintain a [collection](https://docs.x.ai/docs/key-information/collections),
446+
Grok can perform semantic search on it:
375447

376448
```csharp
377449
var options = new ChatOptions
378450
{
379-
Tools = [new WebSearchTool("AR")] // 👈 search in Argentina
451+
Tools = [new HostedFileSearchTool {
452+
Inputs = [new HostedVectorStoreContent("[collection_id]")]
453+
}]
380454
};
381455
```
382456

383-
This is equivalent to the following when used with a Grok client:
457+
Learn more about [collection search](https://docs.x.ai/docs/guides/tools/collections-search-tool).
458+
459+
### Remote MCP
460+
461+
Remote MCP Tools allow Grok to connect to external MCP (Model Context Protocol) servers.
462+
This example sets up the GitHub MCP server so queries about releases (limited specifically
463+
in this case):
464+
384465
```csharp
385466
var options = new ChatOptions
386467
{
387-
// 👇 search in Argentina
388-
Tools = [new GrokSearchTool(GrokSearch.On) { Country = "AR" }]
468+
Tools = [new HostedMcpServerTool("GitHub", "https://api.githubcopilot.com/mcp/") {
469+
AuthorizationToken = Configuration["GITHUB_TOKEN"]!,
470+
AllowedTools = ["list_releases"],
471+
}]
389472
};
390473
```
391474

392-
### Advanced Live Search
393-
394-
To configure advanced live search options, beyond the `On|Auto|Off` settings
395-
in `GrokChatOptions`, you can use the `GrokSearchTool` instead, which exposes
396-
the full breath of [live search options](https://docs.x.ai/docs/guides/live-search)
397-
available in the Grok API.
475+
Just like with code execution, you can opt-in to surfacing the MCP outputs in
476+
the response:
398477

399478
```csharp
400-
var options = new ChatOptions
479+
var options = new GrokChatOptions
401480
{
402-
Tools = [new GrokSearchTool(GrokSearch.On)
403-
{
404-
FromDate = new DateOnly(2025, 1, 1),
405-
ToDate = DateOnly.FromDateTime(DateTime.Now),
406-
MaxSearchResults = 10,
407-
Sources =
408-
[
409-
new GrokWebSource
410-
{
411-
AllowedWebsites =
412-
[
413-
"https://catedralaltapatagonia.com",
414-
"https://catedralaltapatagonia.com/parte-de-nieve/",
415-
"https://catedralaltapatagonia.com/tarifas/"
416-
]
417-
},
418-
]
481+
// Exposes McpServerToolResultContent in responses
482+
Include = { IncludeOption.McpCallOutput },
483+
Tools = [new HostedMcpServerTool("GitHub", "https://api.githubcopilot.com/mcp/") {
484+
AuthorizationToken = Configuration["GITHUB_TOKEN"]!,
485+
AllowedTools = ["list_releases"],
419486
}]
420487
};
488+
421489
```
422490

423-
> [!TIP]
424-
> You can configure multiple sources including `GrokWebSource`, `GrokNewsSource`,
425-
> `GrokRssSource` and `GrokXSource`, each containing granular options.
491+
Learn more about [Remote MCP tools](https://docs.x.ai/docs/guides/tools/remote-mcp-tools).
426492

427493
## OpenAI
428494

src/Tests/GrokTests.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,26 +251,23 @@ public async Task GrokInvokesGrokSearchToolExcludesDomain()
251251
[SecretsFact("XAI_API_KEY")]
252252
public async Task GrokInvokesHostedCodeExecution()
253253
{
254-
var messages = new Chat()
255-
{
256-
{ "user", "Calculate the compound interest for $10,000 at 5% annually for 10 years" },
257-
};
258-
259254
var grok = new GrokClient(Configuration["XAI_API_KEY"]!).AsIChatClient("grok-4-fast");
260255

261-
var options = new ChatOptions
262-
{
263-
Tools = [new HostedCodeInterpreterTool()]
264-
};
256+
var response = await grok.GetResponseAsync(
257+
"Calculate the compound interest for $10,000 at 5% annually for 10 years",
258+
new ChatOptions
259+
{
260+
Tools = [new HostedCodeInterpreterTool()]
261+
});
265262

266-
var response = await grok.GetResponseAsync(messages, options);
267263
var text = response.Text;
268264

269265
Assert.Contains("$6,288.95", text);
270266
Assert.NotEmpty(response.Messages
271267
.SelectMany(x => x.Contents)
272268
.OfType<CodeInterpreterToolCallContent>());
273269

270+
// result content is not available by default
274271
Assert.Empty(response.Messages
275272
.SelectMany(x => x.Contents)
276273
.OfType<CodeInterpreterToolResultContent>());
@@ -293,13 +290,13 @@ public async Task GrokInvokesHostedCodeExecutionWithOutput()
293290
};
294291

295292
var response = await grok.GetResponseAsync(messages, options);
296-
var text = response.Text;
297293

298-
Assert.Contains("$6,288.95", text);
294+
Assert.Contains("$6,288.95", response.Text);
299295
Assert.NotEmpty(response.Messages
300296
.SelectMany(x => x.Contents)
301297
.OfType<CodeInterpreterToolCallContent>());
302298

299+
// result content opted-in is found
303300
Assert.NotEmpty(response.Messages
304301
.SelectMany(x => x.Contents)
305302
.OfType<CodeInterpreterToolResultContent>());

0 commit comments

Comments
 (0)