[Microsoft.Extensions.AI] A way to dynamically provide Tools (AIFunctions) #6526
-
As far as I looked the docs, one needs a C# method per AIFunction. I'm looking for a way to dynamically provide them - theoretically it should be possible as at the end of the day that metadata gets transformed to JSON, I guess. I would create metadata required (descriptions, names, argument types ...) and have a single C# method as an entry point. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You can derive from AIFunction and implement a derived type however you want. AIFunctionFactory.Create is just a convenient way to create an instance that represents a very common case, where you supply a delegate and it creates such a derived instance for you, but that's not the only way they're created. In the ModelContextProtocol library, for example, it has its own AIFunction-derived type that wraps the MCP server's tool information and overrides InvokeAsync to call back to the server. |
Beta Was this translation helpful? Give feedback.
You can derive from AIFunction and implement a derived type however you want. AIFunctionFactory.Create is just a convenient way to create an instance that represents a very common case, where you supply a delegate and it creates such a derived instance for you, but that's not the only way they're created. In the ModelContextProtocol library, for example, it has its own AIFunction-derived type that wraps the MCP server's tool information and overrides InvokeAsync to call back to the server.
https://github.com/modelcontextprotocol/csharp-sdk/blob/befa31d5c948f72396a280a675535a653ee19aee/src/ModelContextProtocol.Core/Client/McpClientTool.cs