Skip to content

AIToolsFactory

Rasmus Wulff Jensen edited this page Dec 26, 2025 · 6 revisions

Note

AIToolsFactory is shared among all the providers (provider-independent) and can be used directly with AIAgent's in Microsoft Agent Framework, should you not wish to the the AgentFactories.

Define your tools with the [AITool] attribute

In order to use the AIToolsFactory you need to add the [AITool] attribute to the methods you consider as Tools

public class MyTools
{
    [AITool("get_weather", "Get the current weather for a city")]
    public string GetWeather(string city)
    {
        //Real implementation goes here...
        return $"Weather for {city}";
    }

    [AITool] //Name/description are optional
    private static string Ping()
    {
        return "pong";
    }
}

AIToolsFactory scans for methods with the [AITool] attribute, including public/private and static/instance methods. Tool names default to the method name if not set.

Create tools from instances or types

Once the [AITool] attributes are in place, you can get your tools in the following way.

AIToolsFactory toolsFactory = new();

//From instance
IList<AITool> tools1 = toolsFactory.GetTools(new MyTools());

//From type (requires a parameterless constructor unless the type is abstract)
IList<AITool> tools2 = toolsFactory.GetTools(typeof(MyTools));

//From multiple types or instances
IList<AITool> tools3 = toolsFactory.GetTools(typeof(MyTools), typeof(OtherTools));
IList<AITool> tools4 = toolsFactory.GetTools(new MyTools(), new OtherTools());

Dependency Injection

You can Dependency Inject the AIToolsFactory if you wish instead of creating own instance.

builder.Services.AddAIToolFactory();

Tools from Model Context Protocol (MCP)

To pull tools from MCP servers, reference the AgentFrameworkToolkit.Tools.ModelContextProtocol package and use the extension methods in AgentFrameworkToolkit.Tools.ModelContextProtocol.

AIToolsFactory toolsFactory = new();

// Remote MCP server
await using McpClientTools remoteTools = await toolsFactory.GetToolsFromRemoteMcpAsync(
    "https://mcp.example.com");
IList<AITool> mcpTools = remoteTools.Tools;

// Local MCP server
await using McpClientTools localTools = await toolsFactory.GetToolsFromLocalMcpAsync(
    "npx",
    ["@playwright/mcp@latest"]);
IList<AITool> localMcpTools = localTools.Tools;

Warning

McpClientTools should be kept while the tools are in use and disposed (await using) to shut down the underlying MCP client when not needed anymore.

Clone this wiki locally