Overhaul McpClientFactory/McpServerFactory (#87)
Handlers are now specified before connecting the client/server. Otherwise, race conditions exist. Handlers move into being a part of the McpClientOptions/McpServerOptions, so that they're provided to the factories. Where relevant, the handlers are specified as part of the capability descriptors, so that they go hand in hand. The factories are no longer stateful. Instead of allocating a factory and then calling a create method on it, you just call a static factory method.
This commit is contained in:
parent
63b24a4036
commit
08b0a04dab
1 changed files with 1306 additions and 2243 deletions
84
README.MD
84
README.MD
|
|
@ -71,9 +71,7 @@ McpServerConfig config = new()
|
|||
}
|
||||
};
|
||||
|
||||
var factory = new McpClientFactory([config], options, NullLoggerFactory.Instance);
|
||||
|
||||
var client = await factory.GetClientAsync("everything");
|
||||
var client = await McpClientFactory.CreateAsync(config, options);
|
||||
|
||||
// Print the list of tools available from the server.
|
||||
await foreach (var tool in client.ListToolsAsync())
|
||||
|
|
@ -136,55 +134,58 @@ using McpDotNet.Protocol.Types;
|
|||
using McpDotNet.Server;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
var loggerFactory = NullLoggerFactory.Instance;
|
||||
McpServerOptions options = new()
|
||||
{
|
||||
ServerInfo = new() { Name = "MyServer", Version = "1.0.0" },
|
||||
Capabilities = new() { Tools = new() },
|
||||
};
|
||||
McpServerFactory factory = new(new StdioServerTransport("MyServer", loggerFactory), options, loggerFactory);
|
||||
IMcpServer server = factory.CreateServer();
|
||||
|
||||
server.SetListToolsHandler(async (request, cancellationToken) =>
|
||||
{
|
||||
return new ListToolsResult()
|
||||
Capabilities = new()
|
||||
{
|
||||
Tools =
|
||||
[
|
||||
new Tool()
|
||||
Tools = new()
|
||||
{
|
||||
ListToolsHandler = async (request, cancellationToken) =>
|
||||
{
|
||||
Name = "echo",
|
||||
Description = "Echoes the input back to the client.",
|
||||
InputSchema = new JsonSchema()
|
||||
return new ListToolsResult()
|
||||
{
|
||||
Type = "object",
|
||||
Properties = new Dictionary<string, JsonSchemaProperty>()
|
||||
Tools =
|
||||
[
|
||||
new Tool()
|
||||
{
|
||||
Name = "echo",
|
||||
Description = "Echoes the input back to the client.",
|
||||
InputSchema = new JsonSchema()
|
||||
{
|
||||
Type = "object",
|
||||
Properties = new Dictionary<string, JsonSchemaProperty>()
|
||||
{
|
||||
["message"] = new JsonSchemaProperty() { Type = "string", Description = "The input to echo back." }
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
};
|
||||
},
|
||||
|
||||
CallToolHandler = async (request, cancellationToken) =>
|
||||
{
|
||||
if (request.Params?.Name == "echo")
|
||||
{
|
||||
if (request.Params.Arguments?.TryGetValue("message", out var message) is not true)
|
||||
{
|
||||
["message"] = new JsonSchemaProperty() { Type = "string", Description = "The input to echo back." }
|
||||
throw new McpServerException("Missing required argument 'message'");
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
|
||||
server.SetCallToolHandler(async (request, cancellationToken) =>
|
||||
{
|
||||
if (request.Params?.Name == "echo")
|
||||
{
|
||||
if (request.Params.Arguments?.TryGetValue("message", out var message) is not true)
|
||||
{
|
||||
throw new McpServerException("Missing required argument 'message'");
|
||||
return new CallToolResponse()
|
||||
{
|
||||
Content = [new Content() { Text = $"Echo: {message}", Type = "text" }]
|
||||
};
|
||||
}
|
||||
|
||||
throw new McpServerException($"Unknown tool: '{request.Params?.Name}'");
|
||||
},
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
return new CallToolResponse()
|
||||
{
|
||||
Content = [new Content() { Text = $"Echo: {message}", Type = "text" }]
|
||||
};
|
||||
}
|
||||
|
||||
throw new McpServerException($"Unknown tool: '{request.Params?.Name}'");
|
||||
});
|
||||
await using IMcpServer server = McpServerFactory.Create(new StdioServerTransport("MyServer"), options);
|
||||
|
||||
await server.StartAsync();
|
||||
|
||||
|
|
@ -192,7 +193,6 @@ await server.StartAsync();
|
|||
await Task.Delay(Timeout.Infinite);
|
||||
```
|
||||
|
||||
|
||||
## Roadmap
|
||||
|
||||
- Expand documentation with detailed guides for:
|
||||
|
|
|
|||
Loading…
Reference in a new issue