Skip to content

Commit d45480b

Browse files
author
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.
1 parent bebef8a commit d45480b

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

docs/concepts/tools/tools.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,50 @@ Rules and constraints:
340340
- Values containing non-ASCII characters, control characters, or leading/trailing whitespace are Base64-encoded using the `=?base64?{value}?=` wrapper.
341341
- Header names must be case-insensitively unique within the tool's input schema.
342342
- 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+
var tool = new Tool
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
373+
var result = await client.CallToolAsync("execute_sql",
374+
new Dictionary<string, object?> { ["region"] = "us-west-2", ["query"] = "SELECT 1" });
375+
```
376+
377+
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

Comments
 (0)