Examples of adding tools dynamically based on methods from another class #669
Replies: 2 comments 2 replies
|
You can add tool at runtime without any attribute decorated class, using this:
tools list can be constructed by code, creating an array of McpServerTool CreateInputSchema need to return a JsonElement representing your Input Schema |
|
There are now two supported patterns on current For a catalog known while the host is being built, create the tools from the reflected methods and pass the metadata through var tools = configuredMethods.Select(entry =>
McpServerTool.Create(
entry.Method,
request => request.Services.GetRequiredService<MyToolClass>(),
new McpServerToolCreateOptions
{
Name = entry.Name,
Description = entry.Description,
SerializerOptions = serializerOptions
}));
builder.WithTools(tools);That For a catalog that changes after the server starts, configure a shared One boundary matters for the |
Uh oh!
There was an error while loading. Please reload this page.
Pre-submission Checklist
Question Category
Your Question
I have been following the SDK and examples and could create an MCP server based on a class (say MyMcpTools) having methods decorated with MCPServerTool attribute having description and names and then using .WithTools.
However, now I want to conditionally add tools from methods from a generic class which as injected Dependencies. And add tool description, name etc. dynamically (I will fetch them from config for example).
I tried using overloaded methods of McpServerTool.Create() but either it doesn't add McpServerToolCreateOptions to the tool or client not able to run the tool. Are there any clear examples that I can refer to achieve this.
All reactions