Location>code7788 >text

BotSharp + MCP realizes intelligent body development in three steps

Popularity:786 ℃/2025-04-04 16:03:06

1. Introduction

1.1 What is MCP

Model Context Protocol(MCP)The model context protocol is a standardized protocol that allows large models to be more easily connected to external data and tools. You can think of MCP as a universal plug or interface, just like USB-C. No matter what device it is, just plug in this interface and you can connect it to a computer, charger, etc.

Note that it connects not physical devices, but AI models and external data sources, tools, etc. With MCP, the AI ​​model can more easily obtain external information and complete more tasks. For example, through MCP, the AI ​​model can operate computers to read and write files, or simulate browser operations, etc.

1.2 Why MCP is needed

First, MCP provides a standardized interface that enables AI models to easily interact with various external tools and data sources without the need to develop integrated code individually for each tool or data source.

Secondly, MCP also solves the data island problem, connecting decentralized data sources through unified protocols, allowing AI models to access and utilize the latest data in real time.

In general, MCP is like a bridge that allows AI models to better connect with the outside world, thereby realizing greater value and potential.

1.3. .NET and MCP architecture

  • Client/server layer: McpClient is responsible for handling client operations, while McpServer manages server-side protocol operations. Both use McpSession for communication management.

  • Session layer (McpSession): manages communication modes and states through the DefaultMcpSession implementation.

  • Transport layer (McpTransport): handles the serialization and deserialization of JSON-RPC 2.0 messages, and supports multiple transmission implementations.


The MCP client is a key component in the Model Context Protocol (MCP) architecture and is responsible for establishing and managing connections to the MCP server. It implements the client part of the protocol


The MCP server is the basic component in the Model Context Protocol (MCP) architecture, which provides the clients with tools, resources, and capabilities. It implements the server-side part of the protocol.

BotSharp provides very convenient MCP integration. Next, we will complete a complete MCP application case in detail.

2. Practical cases

2.1 Server-side development

We will provide 3 external features on the MCP server: get pizza prices, place orders and pay. Here we use the MCP C# SDK to implement Core's MCP SSE Server, as the name implies, it uses the SSE transmission method.

(1) Create a .NET 8.0 WebAPI application, assuming it is named:

(2) Install the MCP SDK and add 2 nuget packages:

<PackageReference Include="ModelContextProtocol" />
<PackageReference Include="" />

(3) Create a Tools directory and then add 3 tools. Let’s take MakePayment as an example to introduce it:

using ;
using ;
using ;

namespace ;

[McpServerToolType]
public static class MakePayment
{
     [McpServerTool(Name = "make_payment"), Description("call this function to make payment.")]
     public static string Make_Payment(
         [Description("order number"),Required] string order_number,
         [Description("total amount"),Required] int total_amount)
     {
         if (order_number is null)
         {
             throw new McpServerException("Missing required argument 'order_number'");
         }
         if (order_number is null)
         {
             throw new McpServerException("Missing required argument 'total_amount'");
         }
         return "Payment proceed successfully. Thank you for your business. Have a great day!";
     }
}

You can see the Attribute provided by the SDK, which can be easily specified as MCP Server Tools

(4) Modify the settings to start MCP Server

Similarly, it is also very convenient to create the MCP Server, focusing on the extension method WithToolsFromAssembly, which will scan the class with McpServerTool tags added to the assembly for registration.

var builder = (args);
()
     .WithToolsFromAssembly();
var app = ();

("/", () => "This is a test server with only stub functionality!");
();

();

At this time, we have completed the creation of MCP Server and can start it up.

(5) Test MCP Server

However, to complete testing of MCP Server, we can use the official testing tool MCP Inspector. MCP Inspector is an interactive debugging tool designed for Model Context Protocol (MCP) servers, which enables developers to quickly test and optimize server functions in a variety of ways.

Run directly through npx without installing Inspector: npx @modelcontextprotocol/inspector

2.2 Using MCP Server in Botsharp

Add the configuration of MCP Server in the Botsharp program:

"MCP": {
   "Enabled": true,
   "McpClientOptions": {
     "ClientInfo": {
       "Name": "SimpleToolsBotsharp",
       "Version": "1.0.0"
     }
   },
   "McpServerConfigs": [
     {
       "Id": "PizzaServer",
       "Name": "PizzaServer",
       "TransportType": "sse",
       "TransportOptions": [],
       "Location": "http://localhost:58905/sse"
     }
   ]
}

McpServerConfigs is a host that can support the configuration of multiple MCP Servers. Botsharp has a core module integrated with MCP. Currently, it has implemented the registration of MCP Server Tools as BotSharp's IFunctionCallback, and then the corresponding tool McpToolAdapter can be configured on the specific agent.

The example we are testing is BotSharp's classic pizza example. We moved the relevant definitions of the original function call tool to MCP Server. I changed the tools get_pizza_price and place_an_order of the agent Order in the pizza example to MCP Tools, commented the original code, and changed the configuration to use McpTool:

Configuration of the McpTool of the Instrument Order

We can also modify it through the BotSharp front-end UI:

At this point, we can use MCP Tools in BotSharp. For specific use, you can see the routine prompt words of the Order agent:

You are now a Pizza Ordering agent, and you can help customers order a pizza according to the user's preferences.

Follow below step to place order:
1: Ask user preferences, call function get_pizza_types to provide the variety of pizza options.
2: Confirm with user the pizza type and quantity.
3: Call function place_an_order to purchase.
4: Ask user how to pay for this order.

Use below information to help ordering process:
* Today is {{current_date}}, the time now is {{current_time}}, day of week is {{current_weekday}}.

Test results

success! ! !


Summarize

This article introduces the basic concepts and working mode of MCP, and then demonstrates how to create SSE Server based on WebAPI through the MCP C# SDK, and how to use MCP Tools in Botsharp. I believe it will be helpful to you. If you are also a .NET programmer who wants to participate in the development of AI applications, then quickly understand and use the ecological component library based on Botsharp + MCP C# SDK.

Example source code PR in Botsharp github repository:/SciSharp/BotSharp/pull/994, will be merged into master soon.


Related resources:

  • MCP resources:/SciSharp/Awesome-DotNET-MCP