MCP Client — Connect to External MCP Servers
Granit.Mcp.Client provides a named factory for connecting to external MCP
servers — ERP systems, data APIs, CLI tools, or any MCP-compatible service.
Installation
Section titled “Installation”dotnet add package Granit.Mcp.Clientbuilder.AddGranitMcpClient();Configuration
Section titled “Configuration”Named connections are configured in appsettings.json:
{ "Mcp": { "Client": { "Connections": { "erp": { "Url": "https://erp.internal/mcp", "Transport": "http" }, "data-tools": { "Transport": "stdio", "Command": "dotnet", "Arguments": ["run", "--project", "../DataTools/DataTools.csproj"] }, "python-analyzer": { "Transport": "stdio", "Command": "python", "Arguments": ["-m", "analyzer_mcp"] } } } }}Connection options
Section titled “Connection options”| Property | Type | Default | Required | Description |
|---|---|---|---|---|
Url | string? | — | HTTP | Server URL (must use HTTPS in production) |
Transport | string | "http" | — | "http" or "stdio" |
AllowInsecureTransport | bool | false | — | Allow http:// (non-TLS) transport. Enable only for local development |
Command | string? | — | stdio | Executable command |
Arguments | string[] | [] | — | Command-line arguments |
Basic client creation
Section titled “Basic client creation”public sealed class ExportService(IMcpClientFactory mcpFactory){ public async Task<IReadOnlyList<McpClientTool>> GetErpToolsAsync(CancellationToken ct) { await using McpClient client = await mcpFactory.CreateAsync("erp", ct); return await client.ListToolsAsync(cancellationToken: ct); }
public async Task<CallToolResult> InvokeErpToolAsync( string toolName, Dictionary<string, object?> args, CancellationToken ct) { await using McpClient client = await mcpFactory.CreateAsync("erp", ct); return await client.CallToolAsync(toolName, args, cancellationToken: ct); }}HTTP transport
Section titled “HTTP transport”For remote MCP servers accessible via HTTPS. The SDK uses Streamable HTTP (with automatic fallback to SSE for older servers):
{ "erp": { "Url": "https://erp.internal/mcp" }}Stdio transport
Section titled “Stdio transport”For local tools running as child processes. The factory spawns the process and communicates via stdin/stdout:
{ "local-tools": { "Transport": "stdio", "Command": "dotnet", "Arguments": ["run", "--project", "../InvoiceTools/"] }}Error handling
Section titled “Error handling”The factory throws InvalidOperationException for configuration errors:
| Scenario | Exception message |
|---|---|
| Connection not found | "MCP connection 'xyz' is not configured" |
| Unsupported transport | "Unsupported MCP transport type 'websocket'" |
| Missing URL (HTTP) | ArgumentException from ThrowIfNullOrWhiteSpace |
| Missing command (stdio) | ArgumentException from ThrowIfNullOrWhiteSpace |
See also
Section titled “See also”- AI workspace integration — inject MCP tools into
IChatClient - Setup guide — server-side MCP configuration
- MCP overview — architecture and design principles