@@ -322,174 +322,6 @@ var grok = app.Services.GetRequiredKeyedService<IChatClient>("Grok");
322322Changing the ` appsettings.json ` file will automatically update the client
323323configuration without restarting the application.
324324
325-
326- ## Grok
327-
328- Full support for Grok new [ agentic tools] ( https://docs.x.ai/docs/guides/tools/overview ) :
329-
330- ### Web Search
331-
332- ``` csharp
333- var messages = new Chat ()
334- {
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." },
337- };
338-
339- var grok = new GrokClient (Environment .GetEnvironmentVariable (" XAI_API_KEY" )! ).AsIChatClient (" grok-4.1-fast" );
340-
341- var options = new ChatOptions
342- {
343- Tools = [new HostedWebSearchTool ()] // 👈 compatible with OpenAI
344- };
345-
346- var response = await grok .GetResponseAsync (messages , options );
347- ```
348-
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:
352-
353- ``` csharp
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- ```
365-
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 ) .
369-
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
423- {
424- Include = { IncludeOption .CodeExecutionCallOutput },
425- Tools = [new HostedCodeInterpreterTool ()]
426- };
427-
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
439- ```
440-
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:
447-
448- ``` csharp
449- var options = new ChatOptions
450- {
451- Tools = [new HostedFileSearchTool {
452- Inputs = [new HostedVectorStoreContent (" [collection_id]" )]
453- }]
454- };
455- ```
456-
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-
465- ``` csharp
466- var options = new ChatOptions
467- {
468- Tools = [new HostedMcpServerTool (" GitHub" , " https://api.githubcopilot.com/mcp/" ) {
469- AuthorizationToken = Configuration [" GITHUB_TOKEN" ]! ,
470- AllowedTools = [" list_releases" ],
471- }]
472- };
473- ```
474-
475- Just like with code execution, you can opt-in to surfacing the MCP outputs in
476- the response:
477-
478- ``` csharp
479- var options = new GrokChatOptions
480- {
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" ],
486- }]
487- };
488-
489- ```
490-
491- Learn more about [ Remote MCP tools] ( https://docs.x.ai/docs/guides/tools/remote-mcp-tools ) .
492-
493325## OpenAI
494326
495327The support for OpenAI chat clients provided in [ Microsoft.Extensions.AI.OpenAI] ( https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI ) fall short in some scenarios:
@@ -695,6 +527,181 @@ IChatClient client = new GrokChatClient(Environment.GetEnvironmentVariable("XAI_
695527```
696528<!-- #extensions -->
697529
530+ ## Grok
531+ <!-- #grok-title -->
532+ Microsoft.Extensions.AI ` IChatClient ` for Grok with full support for all
533+ [ agentic tools] ( https://docs.x.ai/docs/guides/tools/overview ) :
534+
535+ ``` csharp
536+ var grok = new GrokClient (Environment .GetEnvironmentVariable (" XAI_API_KEY" )! )
537+ .AsIChatClient (" grok-4.1-fast" );
538+ ```
539+ <!-- #grok-title -->
540+ <!-- #grok -->
541+ ### Web Search
542+
543+ ``` csharp
544+ var messages = new Chat ()
545+ {
546+ { " system" , " You are an AI assistant that knows how to search the web." },
547+ { " user" , " What's Tesla stock worth today? Search X and the news for latest info." },
548+ };
549+
550+ var grok = new GrokClient (Environment .GetEnvironmentVariable (" XAI_API_KEY" )! ).AsIChatClient (" grok-4.1-fast" );
551+
552+ var options = new ChatOptions
553+ {
554+ Tools = [new HostedWebSearchTool ()] // 👈 compatible with OpenAI
555+ };
556+
557+ var response = await grok .GetResponseAsync (messages , options );
558+ ```
559+
560+ In addition to basic web search as shown above, Grok supports more
561+ [ advanced search] ( https://docs.x.ai/docs/guides/tools/search-tools ) scenarios,
562+ which can be opted-in by using Grok-specific types:
563+
564+ ``` csharp
565+ var grok = new GrokChatClient (Environment .GetEnvironmentVariable (" XAI_API_KEY" )! ).AsIChatClient (" grok-4.1-fast" );
566+ var response = await grok .GetResponseAsync (
567+ " What are the latest product news by Tesla?" ,
568+ new ChatOptions
569+ {
570+ Tools = [new GrokSearchTool ()
571+ {
572+ AllowedDomains = [ " ir.tesla.com" ]
573+ }]
574+ });
575+ ```
576+
577+ You can alternatively set ` ExcludedDomains ` instead, and enable image
578+ understanding with ` EnableImageUndestanding ` . Learn more about these filters
579+ at [ web search parameters] ( https://docs.x.ai/docs/guides/tools/search-tools#web-search-parameters ) .
580+
581+ ### X Search
582+
583+ In addition to web search, Grok also supports searching on X (formerly Twitter):
584+
585+ ``` csharp
586+ var response = await grok .GetResponseAsync (
587+ " What's the latest on Optimus?" ,
588+ new ChatOptions
589+ {
590+ Tools = [new GrokXSearchTool
591+ {
592+ // AllowedHandles = [...],
593+ // ExcludedHandles = [...],
594+ // EnableImageUnderstanding = true,
595+ // EnableVideoUnderstanding = true,
596+ // FromDate = ...,
597+ // ToDate = ...,
598+ }]
599+ });
600+ ```
601+
602+ Learn more about available filters at [ X search parameters] ( https://docs.x.ai/docs/guides/tools/search-tools#x-search-parameters ) .
603+
604+ You can combine both web and X search in the same request by adding both tools.
605+
606+ ### Code Execution
607+
608+ The code execution tool enables Grok to write and execute Python code in real-time,
609+ dramatically expanding its capabilities beyond text generation. This powerful feature
610+ allows Grok to perform precise calculations, complex data analysis, statistical
611+ computations, and solve mathematical problems that would be impossible through text alone.
612+
613+ This is Grok's equivalent of the OpenAI code interpreter, and is configured the same way:
614+
615+ ``` csharp
616+ var grok = new GrokClient (Configuration [" XAI_API_KEY" ]! ).AsIChatClient (" grok-4-fast" );
617+ var response = await grok .GetResponseAsync (
618+ " Calculate the compound interest for $10,000 at 5% annually for 10 years" ,
619+ new ChatOptions
620+ {
621+ Tools = [new HostedCodeInterpreterTool ()]
622+ });
623+
624+ var text = response .Text ;
625+ Assert .Contains (" $6,288.95" , text );
626+ ```
627+
628+ If you want to access the output from the code execution, you can add that as an
629+ include in the options:
630+
631+ ``` csharp
632+ var grok = new GrokClient (Configuration [" XAI_API_KEY" ]! ).AsIChatClient (" grok-4-fast" );
633+ var options = new GrokChatOptions
634+ {
635+ Include = { IncludeOption .CodeExecutionCallOutput },
636+ Tools = [new HostedCodeInterpreterTool ()]
637+ };
638+
639+ var response = await grok .GetResponseAsync (
640+ " Calculate the compound interest for $10,000 at 5% annually for 10 years" ,
641+ options );
642+
643+ var content = response .Messages
644+ .SelectMany (x => x .Contents )
645+ .OfType <CodeInterpreterToolResultContent >()
646+ .First ();
647+
648+ foreach (AIContent output in content .Outputs )
649+ // process outputs from code interpreter
650+ ```
651+
652+ Learn more about the [ code execution tool] ( https://docs.x.ai/docs/guides/tools/code-execution-tool ) .
653+
654+ ### Collection Search
655+
656+ If you maintain a [ collection] ( https://docs.x.ai/docs/key-information/collections ) ,
657+ Grok can perform semantic search on it:
658+
659+ ``` csharp
660+ var options = new ChatOptions
661+ {
662+ Tools = [new HostedFileSearchTool {
663+ Inputs = [new HostedVectorStoreContent (" [collection_id]" )]
664+ }]
665+ };
666+ ```
667+
668+ Learn more about [ collection search] ( https://docs.x.ai/docs/guides/tools/collections-search-tool ) .
669+
670+ ### Remote MCP
671+
672+ Remote MCP Tools allow Grok to connect to external MCP (Model Context Protocol) servers.
673+ This example sets up the GitHub MCP server so queries about releases (limited specifically
674+ in this case):
675+
676+ ``` csharp
677+ var options = new ChatOptions
678+ {
679+ Tools = [new HostedMcpServerTool (" GitHub" , " https://api.githubcopilot.com/mcp/" ) {
680+ AuthorizationToken = Configuration [" GITHUB_TOKEN" ]! ,
681+ AllowedTools = [" list_releases" ],
682+ }]
683+ };
684+ ```
685+
686+ Just like with code execution, you can opt-in to surfacing the MCP outputs in
687+ the response:
688+
689+ ``` csharp
690+ var options = new GrokChatOptions
691+ {
692+ // Exposes McpServerToolResultContent in responses
693+ Include = { IncludeOption .McpCallOutput },
694+ Tools = [new HostedMcpServerTool (" GitHub" , " https://api.githubcopilot.com/mcp/" ) {
695+ AuthorizationToken = Configuration [" GITHUB_TOKEN" ]! ,
696+ AllowedTools = [" list_releases" ],
697+ }]
698+ };
699+
700+ ```
701+
702+ Learn more about [ Remote MCP tools] ( https://docs.x.ai/docs/guides/tools/remote-mcp-tools ) .
703+ <!-- #grok -->
704+
698705<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->
699706# Sponsors
700707
0 commit comments