-
Notifications
You must be signed in to change notification settings - Fork 6
Library Implementation Guide
Gunpal Jain edited this page Apr 7, 2025
·
3 revisions
This guide covers the various approaches to implementing QuickMCP in your .NET projects.
QuickMCP provides two main approaches to create and configure MCP servers:
- Object-based configuration using
BuilderConfig - Programmatic configuration using individual method calls
The BuilderConfig approach allows you to define your entire server configuration in a single object:
// Create a builder configuration
var config = new BuilderConfig
{
Type = "openapi",
ServerName = "PetStore",
ApiSpecUrl = "https://petstore.swagger.io/v2/swagger.json",
ApiBaseUrl = "http://localhost:5000",
ServerHeaders = new Dictionary<string, string>
{
{ "User-Agent", "QuickMCP Client" }
},
ExcludedPaths = new List<string> { "/admin/*" },
Authentication = new AuthConfig
{
Type = "apiKey",
Settings = new Dictionary<string, string>
{
{ "apiKey", "your-api-key" },
{ "paramName", "X-API-Key" },
{ "location", "header" }
}
}
};
// Create server info builder with configuration
var serverInfoBuilder = McpServerInfoBuilder.FromConfig(config);
// Build the server info
var serverInfo = await serverInfoBuilder.BuildAsync();This approach is ideal for:
- Configurations loaded from JSON files
- Dynamic configurations constructed at runtime
- Scenarios requiring serialization/deserialization of configurations
For a more fluent API experience, you can configure your server using individual method calls:
// Create server info builder
var serverInfoBuilder = McpServerInfoBuilder.ForOpenApi();
// Configure from OpenAPI specification
serverInfoBuilder.FromUrl("https://petstore.swagger.io/v2/swagger.json");
// Configure authentication with a pre-built authenticator
var apiKeyAuthenticator = new ApiKeyAuthenticator("your-api-key", "X-API-Key", "header");
serverInfoBuilder.AddAuthentication(apiKeyAuthenticator);
// Set base URL
serverInfoBuilder.WithBaseUrl("http://localhost:5000");
// Add default headers
serverInfoBuilder.AddDefaultHeader("User-Agent", "QuickMCP Client");
// Configure request timeout
serverInfoBuilder.WithTimeout(TimeSpan.FromSeconds(30));
// Filter which endpoints to include
serverInfoBuilder.OnlyForPaths(path => path.StartsWith("/pets"));
// Build the server info
var serverInfo = await serverInfoBuilder.BuildAsync();This approach provides:
- Better IDE IntelliSense support
- More readable code for complex configurations
- Easier step-by-step debugging
Once you have your McpServerInfo object, you can integrate it with the official MCP SDK:
//Integrate with official MCP C# SDK
var hostBuilder = Host.CreateApplicationBuilder();
var mcpBuilder = hostBuilder.Services
.AddMcpServer()
.WithQuickMCP(mcpServerInfo)
.WithStdioServerTransport();
//Run Server
await hostBuilder.Build().RunAsync(); To implement custom authentication with the IAuthenticator interface, create a class that implements this interface. Below is a sample implementation:
public class ApiKeyAuthenticator : IAuthenticator
{
private readonly string _apiKey;
private readonly string _paramName;
private readonly string _location;
public string Type => "apiKey";
public AuthenticatorMetadata Metadata => new AuthenticatorMetadata(
"API Key Authentication",
"Authenticates requests using an API key",
new Dictionary<string, string>
{
{ "paramName", _paramName },
{ "location", _location }
});
public ApiKeyAuthenticator(string apiKey, string paramName, string location)
{
_apiKey = apiKey;
_paramName = paramName;
_location = location;
}
public Task AuthenticateRequestAsync(HttpRequestMessage request)
{
if (_location.Equals("header", StringComparison.OrdinalIgnoreCase))
{
request.Headers.Add(_paramName, _apiKey);
}
else if (_location.Equals("query", StringComparison.OrdinalIgnoreCase))
{
var uriBuilder = new UriBuilder(request.RequestUri);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query[_paramName] = _apiKey;
uriBuilder.Query = query.ToString();
request.RequestUri = uriBuilder.Uri;
}
return Task.CompletedTask;
}
public Task<Dictionary<string, string>> GetAuthHeadersAsync()
{
var headers = new Dictionary<string, string>();
if (_location.Equals("header", StringComparison.OrdinalIgnoreCase))
{
headers.Add(_paramName, _apiKey);
}
return Task.FromResult(headers);
}
public Task<bool> IsAuthenticatedAsync()
{
return Task.FromResult(!string.IsNullOrEmpty(_apiKey));
}
}AuthenticatorFactory.Register(ApiKeyAuthenticator.GetMetadata(), ApiKeyAuthenticator.Create);