Class McpTool
- Namespace
- GenerativeAI.Tools.Mcp
- Assembly
- GenerativeAI.Tools.dll
A tool implementation that integrates MCP (Model Context Protocol) servers with Google Generative AI. This allows you to expose tools from any MCP server to Gemini models for function calling. Supports all MCP transport protocols: stdio, HTTP/SSE, and custom transports.
public class McpTool : GoogleFunctionTool, IFunctionTool, IDisposable, IAsyncDisposable
- Inheritance
-
McpTool
- Implements
- Inherited Members
- Extension Methods
Remarks
McpTool connects to an MCP server via the official C# SDK and automatically discovers and exposes all available tools from that server. When the model requests to call a function, McpTool forwards the request to the MCP server and returns the response.
Example usage with stdio transport:
var transport = McpTransportFactory.CreateStdioTransport(
"my-server",
"npx",
new[] { "-y", "@modelcontextprotocol/server-everything" }
);
using var mcpTool = await McpTool.CreateAsync(transport);
var model = new GenerativeModel(apiKey, GoogleAIModels.Gemini38Flash);
model.AddFunctionTool(mcpTool);
var result = await model.GenerateContentAsync("Your query here");
Example usage with HTTP transport:
var transport = McpTransportFactory.CreateHttpTransport("http://localhost:8080");
using var mcpTool = await McpTool.CreateAsync(transport);
// Use as above
Properties
Client
Gets the MCP client instance. May be null if not connected.
public McpClient? Client { get; }
Property Value
- McpClient
IsConnected
Gets whether the tool is currently connected to the MCP server.
public bool IsConnected { get; }
Property Value
Methods
AsTool()
Converts the current instance to a Tool object.
public override Tool AsTool()
Returns
CallAsync(FunctionCall, CancellationToken)
Executes the specified FunctionCall asynchronously and retrieves the resulting FunctionResponse containing any output from the function execution.
public override Task<FunctionResponse?> CallAsync(FunctionCall functionCall, CancellationToken cancellationToken = default)
Parameters
functionCallFunctionCallThe FunctionCall instance containing the name and arguments required for the function execution.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
- Task<FunctionResponse>
A task representing the asynchronous operation, which, upon completion, provides a FunctionResponse with the execution results.
CreateAsync(IClientTransport, McpToolOptions?, CancellationToken)
Creates a new McpTool instance and connects using the provided transport. Supports all MCP transport types: stdio, HTTP/SSE, etc.
public static Task<McpTool> CreateAsync(IClientTransport transport, McpToolOptions? options = null, CancellationToken cancellationToken = default)
Parameters
transportIClientTransportThe MCP client transport (stdio, HTTP, etc.).
optionsMcpToolOptionsOptional configuration options for the tool behavior.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
Exceptions
- ArgumentNullException
Thrown when transport is null.
- InvalidOperationException
Thrown when unable to connect to the MCP server.
CreateAsync(Func<IClientTransport>, McpToolOptions?, CancellationToken)
Creates a new McpTool instance with a transport factory for auto-reconnection support. The factory will be called to create a new transport when reconnection is needed.
public static Task<McpTool> CreateAsync(Func<IClientTransport> transportFactory, McpToolOptions? options = null, CancellationToken cancellationToken = default)
Parameters
transportFactoryFunc<IClientTransport>Factory function that creates a new transport instance.
optionsMcpToolOptionsOptional configuration options for the tool behavior.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
CreateMultipleAsync(IEnumerable<IClientTransport>, McpToolOptions?, CancellationToken)
Creates multiple McpTool instances from a list of transports.
public static Task<List<McpTool>> CreateMultipleAsync(IEnumerable<IClientTransport> transports, McpToolOptions? options = null, CancellationToken cancellationToken = default)
Parameters
transportsIEnumerable<IClientTransport>List of MCP client transports.
optionsMcpToolOptionsOptional configuration options for the tool behavior.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
Dispose()
Disposes the McpTool and closes the connection to the MCP server.
public void Dispose()
Dispose(bool)
Releases resources used by the tool.
protected virtual void Dispose(bool disposing)
Parameters
disposingbooltrue when called from Dispose(); false when called from DisposeAsync().
DisposeAsync()
Asynchronously disposes the McpTool and closes the connection to the MCP server.
public ValueTask DisposeAsync()
Returns
DisposeAsyncCore()
Asynchronously releases the MCP client and transport.
protected virtual ValueTask DisposeAsyncCore()
Returns
- ValueTask
A task that completes when cleanup has finished.
GetAvailableFunctions()
Gets a list of all available function names from the MCP server.
public IReadOnlyList<string> GetAvailableFunctions()
Returns
- IReadOnlyList<string>
List of function names.
GetFunctionInfo(string)
Gets detailed information about a specific function.
public FunctionDeclaration? GetFunctionInfo(string functionName)
Parameters
functionNamestringThe name of the function.
Returns
- FunctionDeclaration
The FunctionDeclaration for the specified function, or null if not found.
IsContainFunction(string)
Determines whether the specified function exists within the current tool's context.
public override bool IsContainFunction(string name)
Parameters
namestringThe name of the function to check for existence.
Returns
- bool
A boolean value indicating whether the function is present.
RefreshToolsAsync(CancellationToken)
Refreshes the list of available tools from the MCP server.
public Task RefreshToolsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenToken to monitor for cancellation requests.