You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Browse filesBrowse the repository at this point in the historyBrowse files
Tarek Mahmoud Sayed
committed
Add conceptual docs for AddKnownTools/RemoveKnownTools/ClearKnownTools
Add 'Pre-loading tool definitions on the client' section to
docs/concepts/tools/tools.md covering usage, cache behavior,
removal APIs, and validation semantics.
Copy file name to clipboardExpand all lines: docs/concepts/tools/tools.md
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -340,3 +340,50 @@ Rules and constraints:
340
340
- Values containing non-ASCII characters, control characters, or leading/trailing whitespace are Base64-encoded using the `=?base64?{value}?=` wrapper.
341
341
- Header names must be case-insensitively unique within the tool's input schema.
342
342
- Header validation is enforced only for protocol versions that support the HTTP Standardization feature (currently `DRAFT-2026-v1` and later).
343
+
344
+
### Pre-loading tool definitions on the client
345
+
346
+
By default, `Mcp-Param-*` headers are sent only for tools discovered via <xref:ModelContextProtocol.Client.McpClient.ListToolsAsync*>. If a client already has tool schema information (for example, from a previous session, hardcoded configuration, or an out-of-band source), it can pre-load those definitions so that headers are sent immediately—without a round trip to the server.
347
+
348
+
```csharp
349
+
// Build the tool definition with x-mcp-header annotations
350
+
vartool=newTool
351
+
{
352
+
Name="execute_sql",
353
+
InputSchema=JsonDocument.Parse("""
354
+
{
355
+
"type": "object",
356
+
"properties": {
357
+
"region": {
358
+
"type": "string",
359
+
"x-mcp-header": "Region"
360
+
},
361
+
"query": {
362
+
"type": "string"
363
+
}
364
+
}
365
+
}
366
+
""").RootElement.Clone(),
367
+
};
368
+
369
+
// Pre-load the tool definition — no ListToolsAsync needed
370
+
client.AddKnownTools([tool]);
371
+
372
+
// This call now sends an Mcp-Param-Region header automatically
Known tools survive <xref:ModelContextProtocol.Client.McpClient.ListToolsAsync*> cache clears—they remain in the cache even when the server's tool list is refreshed. If the server returns a tool with the same name, the server's definition overwrites the cached one, but the tool keeps its known status.
378
+
379
+
To remove known tools, use <xref:ModelContextProtocol.Client.McpClient.RemoveKnownTools*> for specific tools or <xref:ModelContextProtocol.Client.McpClient.ClearKnownTools*> to remove all:
380
+
381
+
```csharp
382
+
// Remove specific known tools by name
383
+
client.RemoveKnownTools(["execute_sql"]);
384
+
385
+
// Or remove all known tools at once
386
+
client.ClearKnownTools();
387
+
```
388
+
389
+
All tools passed to <xref:ModelContextProtocol.Client.McpClient.AddKnownTools*> are validated for correct `x-mcp-header` annotations. If any tool in the batch fails validation, an <xref:System.ArgumentException> is thrown and no tools are added (all-or-nothing).
0 commit comments