-
Notifications
You must be signed in to change notification settings - Fork 0
Lab 2.3 Model Context Protocol integration
Model Context Protocol (MCP) servers can easily be integrated into Semantic Kernel so that they can also become an available tool for your LLM. This lab focuses on integrating an the GitHub MCP server with Semantic Kernel.
GloboTicket wants to use GitHub issues for customer requests. We will integrate the GitHub MCP server, which requires your authentication token. We will add this to the user secrets.
-
Open a Terminal and sign in to GitHub with the GitHub CLI:
gh auth login
Go through the sign in process, using the
Login with a web browserflow. -
After authenticating, you can fetch the token using
gh auth token:gh auth token
-
Copy the output, which is a string that looks something like
gho_....... -
Next, add this token to your .NET User Secrets:
dotnet user-secrets set "GitHubToken" "<token>" -p ./HolSemanticKernel.csproj
The ModelContextProtocol package is a library for developing and integrating MCP Servers in .NET applications. We will use it to integrate the GitHub MCP server with Semantic Kernel.
-
Install the
ModelContextProtocolNuGet package into your application:dotnet add package ModelContextProtocol --prerelease -
Bring in the necessary
usingstatement in the top of yourProgram.cs:using ModelContextProtocol.Client;
-
From the
ModelContextProtocollibrary, we can use anMcpClientwhich we can add as a tool in the kernel. Add the following to yourProgram.csafter the call tokernelBuilder.Build();:var mcpClient = await McpClient.CreateAsync(new HttpClientTransport( new HttpClientTransportOptions { Name = "GitHub", Endpoint = new Uri("https://api.githubcopilot.com/mcp/"), AdditionalHeaders = new Dictionary<string, string> { ["Authorization"] = $"Bearer {config["GitHubToken"]}" } }));
We now have an instance of
McpClientwhich can call services using the MCP protocol. The sample uses the public GitHub MCP endpoint, and leverages theGitHubTokenvariable you added to the .NET User Secrets in the previous steps.In order to make the
McpClientusable in Semantic Kernel, we have to transform it into a plugin. Every tool that the MCP Server exposes can be made available as callable functions with the following code:var tools = await mcpClient.ListToolsAsync(); kernel.ImportPluginFromFunctions( "GitHub", tools.Select(x => x.AsKernelFunction()));
- To let the LLM know it can use the MCP, add this to the system prompt:
You also have access to GitHub using the GitHub MCP.
The LLM is now aware of the GitHub MCP server and can invoke it to solve a user question.
- Start your application and ask the LLM to list the issues in your repository, named
XpiritCommunityEvents/attendee-workshopSK-<your GitHub handle>repo. It should list the issues from GitHub. - Ask it to create a new issue in the
XpiritCommunityEvents/attendee-workshopSK-<your GitHub handle>repo, give it a title and a description and tell it to add no labels and no assignees. It should respond with the URL to the newly created issue.
- To let the LLM know it can use the MCP, add this to the system prompt:
This shows how easy it is to integrate any MCP with LLMs. As long as you have your authentication set up, and the MCP Server is reachable from where your application runs, the LLM can issue a tool_call response, which it routed to the MCP through Semantic Kernel.
This concludes lab 2.3.
Now you can navigate back to the Home and continue with the next Lab