Introduction
AI assistants are becoming more capable, but their real power emerges when they can tap into their own systems, logic, and data. The Model Context Protocol (MCP) makes this possible by providing a standardized way for tools and services to interact directly with assistants like GitHub Copilot Chat. By exposing your backend capabilities through an MCP server, you can extend Copilot far beyond code suggestions and turn it into a practical interface for your applications.
Key Topics Covered:
- A breakdown of how the Model Context Protocol works and the components that make up its architecture.
- Steps to create an MCP server in C# and implement your own custom tools.
- How to link your .NET-based MCP server with GitHub Copilot Chat in VS Code so they can communicate seamlessly.
What is MCP?
MCP defines a standard protocol for AI clients to connect to external servers.
- MCP Server → your app or API that provides tools.
- MCP Client → AI assistant (like GitHub Copilot Chat) that calls those tools.
Think of it like plugins for Copilot, but built with simple attributes and a lightweight protocol.
Project Setup:
- Create a new .NET Core application to serve as the base for your MCP server.
- Add the required dependencies, including:
- ModelContextProtocol.AspNetCore
- Microsoft.Azure.Functions.Worker.Extensions.Mcp
- System.Data.SqlClient (for database communication)
Defining a tool:
-
A tool is a simple class decorated with
McpServerToolType. Each method markedMcpServerToolis automatically exposed to the MCP client. - Define tools with clear, detailed descriptions so the LLM can interpret them effectively and deliver more accurate responses.
- Below is an example of the
EmployeeTool.csthat has the tool defined:
[McpServerTool, Description("Get Employee details")]
public string GetEmployeeDetails(
[McpToolTrigger("employee_tool", "MCP Tool that fetches employee records based on hiring dates.")]
ToolInvocationContext trigger,
[McpToolProperty("startDate", "string",
"Start date of the provided date range."
)]
string startDate,
[McpToolProperty("endDate", "string",
"End date of the provided date range."
)]
string endDate
)
{
// Write business logic to retrieve data
return $"Fetching employees hired between {startDate} and {endDate}";
}
- Register your tool in the Program.cs file as shown below.
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly();
builder.Services.AddSingleton<EmployeeTool>();
var app = builder.Build();
app.MapMcp();
app.Run();
- Once you’ve defined your tools, run the project:
Connecting with GitHub Copilot Chat:
Now that the tools are defined, let’s connect them to Copilot Chat.
- A GitHub account
- The GitHub Copilot and GitHub Copilot Chat extensions are installed in VS Code
Next, we’ll add the server using the steps below:
- Open the Command Palette.
- Search for “MCP: Add Server” and select it.
- Choose HTTP as the transport mode.
- Enter the server URL (for example:
http://localhost:5000). - Provide a name for your server and choose whether to save it as Global (user) or just for the current workspace.
- When asked, confirm that you trust this MCP server.
- Your MCP server is now registered and ready to be used through Copilot Chat.
Verify the Server:
- Access the Command Palette and select “MCP: List Servers” to verify the server’s presence in the list.
- Alternatively, navigate to the Extensions view and examine the section labeled MCP Servers => Installed.
Using MCP Tools Inside Copilot Chat:
Once the MCP server is added, you can start using the tools directly inside Copilot Chat:
- Open the Copilot Chat interface in VS Code.
- Switch to Agent mode from the drop-down beneath the chat box.
- Click the Tools icon to explore available MCP tools.
- Provide a prompt like: “Provide me with the employees hired in the last month.”
- To explicitly invoke a tool, type
#and select it by name. - When Copilot suggests a tool invocation, review it and click Continue to execute.
That’s it - Copilot will now call your MCP tools and return live data straight into chat.
Conclusion
In this guide, we explored how to build a custom MCP server using C# .NET, define powerful tools, and integrate them with GitHub Copilot Chat to extend its capabilities. With MCP, you can enable Copilot to access real-time data, execute business logic, and provide accurate, context-aware responses.
For more details and official documentation, check out the C# MCP SDK on GitHub.

No comments:
Post a Comment