A dialogue on Mannequin Context Protocol:
- Half 1 – Why is there a necessity for MCP Hyperlink half 1
- Half 2 – What MCP is, together with its structure and core elements Hyperlink half 2
- Half 3 – A demo of MCP — together with how you can configure it so anybody can run it Hyperlink half 3
- Half 4 – An instance of how you can develop an MCP server — a possible starter challenge for connecting to your personal data sources
On this part we will focus on creating an MCP Server – particularly, the Color MCP server that was lined within the earlier part, and used within the demo.
To assist construct MCP servers with minimal effort – there are Software program Growth Kits (SDKS) for the next languages:
- C#
- Java
- Kotlin
- Python
- Ruby
- Swift
- TypeScript
Hyperlinks to the totally different SDKS are at https://modelcontextprotocol.io/
The supply code to the Colours MCP server is at : https://github.com/markharrison/ColorsMCP
It may very well be used as the idea of your personal MCP Server – for data that you simply wish to make obtainable to your AI functions. So take a duplicate:
git clone https://github.com/markharrison/ColorsMCP
There are a number of initiatives within the answer:
- ColorsMCP … this makes use of the STDIO transport protocol
- ColorsMCP-http … this makes use of the HTTP Streamable transport protocol
- ColorsCommonMCP … that is widespread code utilized by each the ColorMCP and ColorsMCP-http initiatives
- MCPClient … it is a easy consumer challenge to check calling the MCP servers
The code is easy – due to the C# SDK, which does the majority of the work.
Test program.cs to see how the server software is instantiated with MCP assist.
var builder = WebApplication.CreateBuilder(args);
builder.Companies
.AddMcpServer()
.WithHttpTransport()
.WithTools(); builder.Companies.AddCors(choices =>
{
choices.AddDefaultPolicy(coverage =>
{
coverage.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});builder.Companies.AddSingleton
(); var app = builder.Construct();
var colorsService = app.Companies.GetRequiredService
(); app.UseCors();
app.MapMcp();
app.MapGet("/well being", () => "Wholesome");
app.Run();
We will see that the startup logic .WithTools refers to a category known as ColorsTools – that is what incorporates our MCP Instruments. We will have a look at this intimately shortly.
The challenge incorporates our widespread code – that’s utilized by the initiatives to assist totally different transport protocols.
The Colours data is tough coded in a JSON file. However different such servers may entry info in databases or name APIs.
[
{
"name": "ALICEBLUE",
"hexcode": "#F0F8FF",
"rgb": "RGB(240, 248, 255)",
"families": [ "white" ]
},
{
"identify": "ANTIQUEWHITE",
"hexcode": "#FAEBD7",
"rgb": "RGB(250, 235, 215)",
"households": [ "white", "tan" ]
},
{
"identify": "AQUA",
"hexcode": "#00FFFF",
"rgb": "RGB(0, 255, 255)",
"households": [ "blue", "aqua" ]
},and so on
]
The three MCP Instruments are positioned in ColorsTools.cs
utilizing ModelContextProtocol.Server;
utilizing System.ComponentModel;
utilizing System.Textual content.Json;namespace ColorsCommonMCP;
[McpServerToolType]
public sealed class ColorsTools
{
personal readonly ColorsService colorsService;public ColorsTools(ColorsService colorsService)
{
this.colorsService = colorsService;
}[McpServerTool, Description(ColorsInfo.GetAllColorsToolDescription)]
public async ProcessGetAllColors()
{
var colours = await colorsService.GetColors();
return JsonSerializer.Serialize(colours, ColorsContext.Default.ListColors);
}[McpServerTool, Description(ColorsInfo.GetColorsByFamilyToolDescription)]
public async ProcessGetColorByFamily(
[Description(ColorsInfo.GetColorsByFamilyParamFamilyDescription)] string household)
{
var colours = await colorsService.GetColorsByFamily(household);
return JsonSerializer.Serialize(colours, ColorsContext.Default.ListColors);
}[McpServerTool, Description(ColorsInfo.GetColorToolDescription)]
public async ProcessGetColor([Description(ColorsInfo.GetColorParamNameDescription)] string identify)
{
var colours = await colorsService.GetColors(identify);
return JsonSerializer.Serialize(colours, ColorsContext.Default.Colours);
}
}
The annotations on the features are essential, as they’re utilized by this system logic to establish and hyperlink to the suitable instruments.
The outline fields inside these annotations should be clear and exact, as they’re utilized by the AI logic to find out whether or not a selected instrument needs to be known as to fulfil a given request.
The ColorsMCP-http challenge features a Docker file to construct a container. This makes it easy to deploy e.g. to Azure App Service.
The Command to construct the container is within the repo ReadMe file. Hyperlink to ReadMe
A consumer take a look at utility was included within the answer. This examines the server to see what Instruments it exposes, after which it invoked the Device to get the Purple info.
Alternatively use the MCP Inspector utility – directions to run this are within the ReadMe file. Hyperlink to ReadMe
—
Mark Harrison https://markharrison.io/