Skip to content

Configuration Methods

Gunpal Jain edited this page Apr 7, 2025 · 2 revisions

The McpServerInfoBuilder implements IMcpServerInfoBuilder and provides numerous methods to configure your MCP server. This document details all available configuration methods with examples.

Basic Configuration Methods

FromUrl

Loads and parses an OpenAPI/Swagger specification from a URL.

serverInfoBuilder.FromUrl("https://petstore.swagger.io/v2/swagger.json");

FromFile

Loads and parses an OpenAPI/Swagger specification from a local file.

serverInfoBuilder.FromFile("./swagger/petstore.json");

FromConfiguration

Loads server configuration from a JSON configuration file.

serverInfoBuilder.FromConfiguration("./config/mcp_server_config.json");

WithConfig

Configures the server using a BuilderConfig object.

var config = new BuilderConfig
{
    Type = "openapi",
    ServerName = "PetStore",
    ApiSpecUrl = "https://petstore.swagger.io/v2/swagger.json",
    // Other configuration properties...
};
serverInfoBuilder.WithConfig(config);

URL Configuration

WithBaseUrl

Sets the base URL for API requests.

serverInfoBuilder.WithBaseUrl("https://api.example.com/v1");

HTTP Configuration

WithHttpClient

Sets a custom HttpClient instance for making API requests.

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", "Custom Client");
serverInfoBuilder.WithHttpClient(httpClient);

AddDefaultHeader

Adds a default header to all HTTP requests.

serverInfoBuilder.AddDefaultHeader("User-Agent", "QuickMCP Client");
serverInfoBuilder.AddDefaultHeader("Accept-Language", "en-US");

WithTimeout

Sets the timeout duration for HTTP requests.

serverInfoBuilder.WithTimeout(TimeSpan.FromSeconds(30));

Path Filtering

ExcludePaths (with Predicate)

Excludes paths based on a predicate function.

serverInfoBuilder.ExcludePaths(path => path.StartsWith("/admin") || path.Contains("internal"));

ExcludePaths (with Collection)

Excludes specific paths from the API.

serverInfoBuilder.ExcludePaths(new[] { "/admin/users", "/internal/logs" });

OnlyForPaths (with Predicate)

Includes only paths that match a predicate function.

serverInfoBuilder.OnlyForPaths(path => path.StartsWith("/public") || path.StartsWith("/api/v1"));

OnlyForPaths (with Collection)

Includes only specific paths from the API.

serverInfoBuilder.OnlyForPaths(new[] { "/products", "/categories", "/orders" });

Authentication

AddAuthentication

Adds an authenticator for API requests.

var apiKeyAuthenticator = new ApiKeyAuthenticator("your-api-key", "X-API-Key", "header");
serverInfoBuilder.AddAuthentication(apiKeyAuthenticator);

Parameter Configuration

WithDefaultPathParams

Sets default values for path parameters.

serverInfoBuilder.WithDefaultPathParams(new Dictionary<string, string>
{
    { "version", "v1" },
    { "format", "json" }
});

Logging

AddLogging

Configures logging for operations and events.

var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConsole();
    builder.SetMinimumLevel(LogLevel.Information);
});
serverInfoBuilder.AddLogging(loggerFactory);

Building the Configuration

BuildAsync

Builds and registers all configured tools with the MCP server.

var serverInfo = await serverInfoBuilder.BuildAsync();

Method Chaining

All configuration methods return the builder instance, allowing for method chaining:

var serverInfoBuilder = McpServerInfoBuilder.ForOpenApi()
    .FromUrl("https://petstore.swagger.io/v2/swagger.json")
    .WithBaseUrl("https://api.example.com")
    .AddDefaultHeader("User-Agent", "QuickMCP Client")
    .WithTimeout(TimeSpan.FromSeconds(30))
    .OnlyForPaths(path => path.StartsWith("/pets"))
    .AddAuthentication(new ApiKeyAuthenticator("your-api-key", "X-API-Key", "header"))
    .AddLogging(loggerFactory);

var serverInfo = await serverInfoBuilder.BuildAsync();