Many applications need more than a chatbot. A customer support platform may need an AI agent that searches orders, checks delivery status, and creates support tickets. A developer portal may need an agent that reads repository information, checks build results, and helps developers investigate issues.
Building these capabilities from scratch means implementing conversation management, model communication, tool execution, session state, and the agent loop. These responsibilities can become difficult to maintain as the application grows.
The Microsoft Copilot SDK provides another approach. It allows a C# application to use the Copilot runtime as its agent execution engine while keeping business logic, authentication, data access, and application security under the developer's control.
This architecture is useful when you want to embed an agent into an existing .NET application without building a complete orchestration runtime yourself.
In this article, we will build a multi-user application architecture using the Copilot SDK. We will connect a C# application to the runtime, create isolated sessions, expose application-specific tools, and control which capabilities each session can access.
Understanding Copilot as an Agent Runtime
A language model generates responses based on the context it receives. An agent runtime manages the larger process required to complete a task.
For example, when a user asks, "Check my order and create a support ticket if it has not been delivered," the application needs to perform several operations.
Understand the user's request.
Identify the required tools.
Call the order lookup operation.
Process the returned information.
Decide whether a support ticket is necessary.
Call the ticket creation operation.
Return the final result.
The agent runtime coordinates this process. It manages the conversation, sends requests to the model, handles tool calls, and incorporates tool results into the next step of the conversation.
The Copilot SDK provides this runtime through a C# client and session API.
The application remains responsible for the operations that belong to its business domain. For example, it still decides how an order is retrieved, which database is used, whether a customer is authorized to access an order, and what happens when a ticket is created.
The separation looks like this:
User
|
v
.NET Application
|
+---- Authentication
|
+---- Authorization
|
+---- Session Management
|
v
Copilot SDK
|
v
Copilot Agent Runtime
|
+---- Model Interaction
|
+---- Tool Selection
|
+---- Agent Loop
|
v
Application Tools
|
+---- Order Service
|
+---- Customer Service
|
+---- Ticket Service
|
v
Business DataThe runtime coordinates the agent workflow, while the .NET application owns the business operations and security boundaries.
This distinction is especially important for applications that serve multiple users.
Installing the Copilot SDK
Create a new ASP.NET Core Web API project:
dotnet new webapi -n CopilotAgentApi
cd CopilotAgentApiInstall the current .NET SDK package:
dotnet add package Microsoft.Copilot.SDKThe package provides the client, session management, runtime communication, and custom tool integration.
The SDK can manage a compatible runtime process or connect to a separately hosted runtime. For a multi-user backend, a shared runtime can be useful because multiple application instances or SDK clients can connect to the same runtime service.
For this example, we will use a standalone runtime server and connect to it over TCP.
Start the runtime using a compatible Copilot runtime installation:
copilot-runtime --server --port 4321The runtime server should be deployed in a controlled environment. Configure its storage directory, authentication, network access, and idle-session settings according to the deployment requirements.
The SDK and runtime versions should also be kept compatible.
Choosing the Right Client Mode
The Copilot SDK provides two important client modes:
Mode | Purpose |
|---|---|
| Provides a CLI-like experience with the runtime's coding-agent defaults and tools |
| Starts with a restricted configuration so the application can explicitly enable capabilities |
For a local developer tool, CodingAgent may be convenient because it provides a broader set of built-in capabilities.
For a multi-user backend, Empty is the safer starting point.
The coding-agent mode can expose ambient host capabilities, including filesystem access associated with the runtime process. If multiple users share that process, those capabilities can create security problems.
The empty mode disables optional CLI-style defaults. The application must explicitly decide which tools, MCP servers, and other capabilities each session can use.
This is an important architectural decision. A backend should not automatically inherit every capability available to the machine hosting it.
Connecting a C# Application to the Runtime
Create a service that manages the Copilot client.
Add the following code to CopilotRuntimeService.cs:
using Microsoft.Copilot;
public sealed class CopilotRuntimeService : IAsyncDisposable
{
private readonly CopilotClient _client;
public CopilotRuntimeService(
IConfiguration configuration)
{
var host = configuration["Copilot:Host"]
?? "127.0.0.1";
var port = int.Parse(
configuration["Copilot:Port"] ?? "4321");
_client = new CopilotClient(
new CopilotClientOptions
{
Mode = CopilotClientMode.Empty,
Runtime = RuntimeConnection.ForTcp(
host,
port)
});
}
public Task StartAsync()
{
return _client.StartAsync();
}
public CopilotClient Client => _client;
public async ValueTask DisposeAsync()
{
await _client.DisposeAsync();
}
}The service creates a client in empty mode and connects it to the standalone runtime.
The host and port come from application configuration:
{
"Copilot": {
"Host": "127.0.0.1",
"Port": "4321"
}
}In production, use the actual private runtime endpoint and protect the connection with an appropriate network security configuration. Do not expose an unauthenticated runtime endpoint to the public internet.
The service also owns the client lifecycle. The application starts the client during initialization and disposes it during shutdown.
Because the runtime is external, its process lifecycle and persistent storage are managed separately from this C# client.
Exposing Application Services as Agent Tools
An agent becomes useful when it can access application functionality.
Consider a customer support application with two operations:
Retrieve an order using its identifier.
Create a support ticket for an order.
These operations should remain inside the .NET application rather than being implemented by the model.
Create an order service:
public sealed class OrderService
{
public Task<OrderInfo?> GetOrderAsync(
string orderId,
CancellationToken cancellationToken)
{
// Replace with database or API access.
var order = new OrderInfo(
orderId,
"ORD-1001",
"Shipped");
return Task.FromResult<OrderInfo?>(order);
}
}
public sealed record OrderInfo(
string OrderId,
string Reference,
string Status);Now create a ticket service:
public sealed class TicketService
{
public Task<string> CreateTicketAsync(
string orderId,
string description,
CancellationToken cancellationToken)
{
// Replace with the actual ticket system.
return Task.FromResult(
$"TICKET-{Guid.NewGuid():N}"[..15]);
}
}These services represent ordinary application functionality. They could use Entity Framework Core, Dapper, an external REST API, or another internal service.
The Copilot SDK can expose their methods as tools.
Create a tool factory:
using Microsoft.Copilot;
using Microsoft.Extensions.AI;
using System.ComponentModel;
public sealed class SupportTools
{
private readonly OrderService _orders;
private readonly TicketService _tickets;
public SupportTools(
OrderService orders,
TicketService tickets)
{
_orders = orders;
_tickets = tickets;
}
public AIFunctionDeclaration[] Create()
{
var lookupOrder = CopilotTool.DefineTool(
async (
[Description("Order identifier")] string orderId,
CancellationToken cancellationToken) =>
{
return await _orders.GetOrderAsync(
orderId,
cancellationToken);
},
factoryOptions: new AIFunctionFactoryOptions
{
Name = "lookupOrder",
Description = "Retrieve the status of an order"
});
var createTicket = CopilotTool.DefineTool(
async (
[Description("Order identifier")] string orderId,
[Description("Reason for the support ticket")]
string description,
CancellationToken cancellationToken) =>
{
return await _tickets.CreateTicketAsync(
orderId,
description,
cancellationToken);
},
factoryOptions: new AIFunctionFactoryOptions
{
Name = "createTicket",
Description = "Create a support ticket for an order"
});
return
[
lookupOrder,
createTicket
];
}
}
Join the conversation! Your thoughts help the community grow.