1- ![ Icon] ( assets/icon.png ) xAI .NET SDK
1+ ![ Icon] ( assets/icon.png ) .NET SDK
22============
33
44[ ![ Version] ( https://img.shields.io/nuget/vpre/xAI.svg?color=royalblue )] ( https://www.nuget.org/packages/xAI )
55[ ![ Downloads] ( https://img.shields.io/nuget/dt/xAI.svg?color=darkmagenta )] ( https://www.nuget.org/packages/xAI )
66[ ![ EULA] ( https://img.shields.io/badge/EULA-OSMF-blue?labelColor=black&color=C9FF30 )] ( osmfeula.txt )
77[ ![ OSS] ( https://img.shields.io/github/license/devlooped/oss.svg?color=blue )] ( license.txt )
88
9- xAI .NET SDK based on the official gRPC API reference from xAI
9+ xAI .NET SDK based on the official gRPC API reference from xAI with integration for
10+ Microsoft.Extensions.AI and Microsoft.Agents.AI.
1011
1112<!-- include https://github.com/devlooped/.github/raw/main/osmf.md -->
1213## Open Source Maintenance Fee
@@ -21,10 +22,188 @@ OSMF tier. A single fee covers all of [Devlooped packages](https://www.nuget.org
2122
2223<!-- https://github.com/devlooped/.github/raw/main/osmf.md -->
2324
24- <!-- #content -->
25+ <!-- #xai -->
26+ xAI/Grok integration for Microsoft.Extensions.AI ` IChatClient ` with full support for all
27+ [ agentic tools] ( https://docs.x.ai/docs/guides/tools/overview ) :
28+
29+ ``` csharp
30+ var grok = new GrokClient (Environment .GetEnvironmentVariable (" XAI_API_KEY" )! )
31+ .AsIChatClient (" grok-4.1-fast" );
32+ ```
33+ ## Web Search
34+
35+ ``` csharp
36+ var messages = new Chat ()
37+ {
38+ { " system" , " You are an AI assistant that knows how to search the web." },
39+ { " user" , " What's Tesla stock worth today? Search X and the news for latest info." },
40+ };
41+
42+ var grok = new GrokClient (Environment .GetEnvironmentVariable (" XAI_API_KEY" )! ).AsIChatClient (" grok-4.1-fast" );
43+
44+ var options = new ChatOptions
45+ {
46+ Tools = [new HostedWebSearchTool ()] // 👈 compatible with OpenAI
47+ };
48+
49+ var response = await grok .GetResponseAsync (messages , options );
50+ ```
51+
52+ In addition to basic web search as shown above, Grok supports more
53+ [ advanced search] ( https://docs.x.ai/docs/guides/tools/search-tools ) scenarios,
54+ which can be opted-in by using Grok-specific types:
55+
56+ ``` csharp
57+ var grok = new GrokChatClient (Environment .GetEnvironmentVariable (" XAI_API_KEY" )! )
58+ .AsIChatClient (" grok-4.1-fast" );
59+ var response = await grok .GetResponseAsync (
60+ " What are the latest product news by Tesla?" ,
61+ new ChatOptions
62+ {
63+ Tools = [new GrokSearchTool ()
64+ {
65+ AllowedDomains = [ " ir.tesla.com" ]
66+ }]
67+ });
68+ ```
69+
70+ You can alternatively set ` ExcludedDomains ` instead, and enable image
71+ understanding with ` EnableImageUndestanding ` . Learn more about these filters
72+ at [ web search parameters] ( https://docs.x.ai/docs/guides/tools/search-tools#web-search-parameters ) .
73+
74+ ## X Search
75+
76+ In addition to web search, Grok also supports searching on X (formerly Twitter):
77+
78+ ``` csharp
79+ var response = await grok .GetResponseAsync (
80+ " What's the latest on Optimus?" ,
81+ new ChatOptions
82+ {
83+ Tools = [new GrokXSearchTool
84+ {
85+ // AllowedHandles = [...],
86+ // ExcludedHandles = [...],
87+ // EnableImageUnderstanding = true,
88+ // EnableVideoUnderstanding = true,
89+ // FromDate = ...,
90+ // ToDate = ...,
91+ }]
92+ });
93+ ```
94+
95+ Learn more about available filters at [ X search parameters] ( https://docs.x.ai/docs/guides/tools/search-tools#x-search-parameters ) .
96+
97+ You can combine both web and X search in the same request by adding both tools.
98+
99+ ## Code Execution
100+
101+ The code execution tool enables Grok to write and execute Python code in real-time,
102+ dramatically expanding its capabilities beyond text generation. This powerful feature
103+ allows Grok to perform precise calculations, complex data analysis, statistical
104+ computations, and solve mathematical problems that would be impossible through text alone.
105+
106+ This is Grok's equivalent of the OpenAI code interpreter, and is configured the same way:
107+
108+ ``` csharp
109+ var grok = new GrokClient (Configuration [" XAI_API_KEY" ]! ).AsIChatClient (" grok-4-fast" );
110+ var response = await grok .GetResponseAsync (
111+ " Calculate the compound interest for $10,000 at 5% annually for 10 years" ,
112+ new ChatOptions
113+ {
114+ Tools = [new HostedCodeInterpreterTool ()]
115+ });
116+
117+ var text = response .Text ;
118+ Assert .Contains (" $6,288.95" , text );
119+ ```
120+
121+ If you want to access the output from the code execution, you can add that as an
122+ include in the options:
123+
124+ ``` csharp
125+ var grok = new GrokClient (Configuration [" XAI_API_KEY" ]! ).AsIChatClient (" grok-4-fast" );
126+ var options = new GrokChatOptions
127+ {
128+ Include = { IncludeOption .CodeExecutionCallOutput },
129+ Tools = [new HostedCodeInterpreterTool ()]
130+ };
131+
132+ var response = await grok .GetResponseAsync (
133+ " Calculate the compound interest for $10,000 at 5% annually for 10 years" ,
134+ options );
135+
136+ var content = response .Messages
137+ .SelectMany (x => x .Contents )
138+ .OfType <CodeInterpreterToolResultContent >()
139+ .First ();
140+
141+ foreach (AIContent output in content .Outputs )
142+ // process outputs from code interpreter
143+ ```
144+
145+ Learn more about the [ code execution tool] ( https://docs.x.ai/docs/guides/tools/code-execution-tool ) .
146+
147+ ## Collection Search
148+
149+ If you maintain a [ collection] ( https://docs.x.ai/docs/key-information/collections ) ,
150+ Grok can perform semantic search on it:
151+
152+ ``` csharp
153+ var options = new ChatOptions
154+ {
155+ Tools = [new HostedFileSearchTool {
156+ Inputs = [new HostedVectorStoreContent (" [collection_id]" )]
157+ }]
158+ };
159+ ```
160+
161+ Learn more about [ collection search] ( https://docs.x.ai/docs/guides/tools/collections-search-tool ) .
162+
163+ ## Remote MCP
164+
165+ Remote MCP Tools allow Grok to connect to external MCP (Model Context Protocol) servers.
166+ This example sets up the GitHub MCP server so queries about releases (limited specifically
167+ in this case):
168+
169+ ``` csharp
170+ var options = new ChatOptions
171+ {
172+ Tools = [new HostedMcpServerTool (" GitHub" , " https://api.githubcopilot.com/mcp/" ) {
173+ AuthorizationToken = Configuration [" GITHUB_TOKEN" ]! ,
174+ AllowedTools = [" list_releases" ],
175+ }]
176+ };
177+ ```
178+
179+ Just like with code execution, you can opt-in to surfacing the MCP outputs in
180+ the response:
181+
182+ ``` csharp
183+ var options = new GrokChatOptions
184+ {
185+ // Exposes McpServerToolResultContent in responses
186+ Include = { IncludeOption .McpCallOutput },
187+ Tools = [new HostedMcpServerTool (" GitHub" , " https://api.githubcopilot.com/mcp/" ) {
188+ AuthorizationToken = Configuration [" GITHUB_TOKEN" ]! ,
189+ AllowedTools = [" list_releases" ],
190+ }]
191+ };
192+
193+ ```
194+
195+ Learn more about [ Remote MCP tools] ( https://docs.x.ai/docs/guides/tools/remote-mcp-tools ) .
196+ <!-- #xai -->
197+
198+ # xAI.Protocol
199+
200+ [ ![ Version] ( https://img.shields.io/nuget/vpre/xAI.Protocol.svg?color=royalblue )] ( https://www.nuget.org/packages/xAI.Protocol )
201+ [ ![ Downloads] ( https://img.shields.io/nuget/dt/xAI.Protocol.svg?color=green )] ( https://www.nuget.org/packages/xAI.Protocol )
202+
203+ <!-- #protocol -->
25204## Usage
26205
27- This project provides a .NET client for the gRPC API of xAI with full support for all services
206+ The xAI.Protocol package provides a .NET client for the gRPC API of xAI with full support for all services
28207documented in the [ official API reference] ( https://docs.x.ai/docs/grpc-reference ) and
29208corresponding [ proto files] ( https://github.com/xai-org/xai-proto/tree/main/proto/xai/api/v1 ) .
30209
@@ -54,7 +233,7 @@ ensuring it remains up-to-date with any changes or additions made to the API as
54233
55234See for example the [ introduction of tool output and citations] ( https://github.com/devlooped/GrokClient/pull/3 ) .
56235
57- <!-- #content -->
236+ <!-- #protocol -->
58237
59238<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->
60239# Sponsors
0 commit comments