tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get weather of an location, the user shoud supply a location first", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA", } }, "required": ["location"] }, } }, ]
var openAiOptions = new OpenAIClientOptions(); openAiOptions.Endpoint = new Uri("https://api.deepseek.com/v1/");
var chatClient = new ChatClient("deepseek-chat", new ApiKeyCredential("******"), openAiOptions);
var client = new ChatClientBuilder(chatClient.AsIChatClient()).UseFunctionInvocation().Build();
var prompts = new List<Microsoft.Extensions.AI.ChatMessage> { new ChatMessage(ChatRole.System, """you are a professional enginer in BIM , so you can select the greate tool to user , and generation a standard input style And Arguments to tools"""), new ChatMessage(ChatRole.User, input) };
var tools = await mcpClient.ListToolsAsync();
var chatOptions = new ChatOptions() { Tools = [.. tools] };
获取数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var res = await client.GetResponseAsync(prompts, chatOptions);
var message = res.Messages[1].Contents[0]; varvalue = ((Microsoft.Extensions.AI.FunctionResultContent)message).Result; var convert = JsonConvert.DeserializeObject(value.ToString());
// 反序列化 ResponseData data = JsonConvert.DeserializeObject<ResponseData>(value.ToString());
// 访问数据 foreach (var item in data.Content) { //Console.WriteLine($"Type: {item.Type}, Text: {item.Text}"); var d = item.Text; Console.WriteLine(d); }
"Generation A Window In A Selection Wall , Define Window Size : 1500 x 1200 d, Need To Calculate The Window-Top Is Small Then Wall-Height , This Command Need Input Args : ElementId , LocationX , LocationY ,LocationZ")
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Tool示例: ```csharp [McpServerTool(Name = "CreateWall"), Description("Generation Paramaters That Can Create Wall in Revit")] public string RevitCreateWallTool(string command, double x, double y, double z, double x1, double y2) { return $@" {{ ""command"": ""CreateWall"", ""arguments"": {{ ""start"": [{x}, {y}, {z}], ""end"": [{x1}, {y2}, {z}] }} }}"; }
///<summary> /// This A MCP Test ///</summary> ///<param name="x"></param> ///<param name="y"></param> ///<param name="x1"></param> ///<param name="y1"></param> ///<param name="z"></param> privateclassCreateWall : IRevitCommand { publicvoidExecute(string jsonArgs) { varargs = JsonConvert.DeserializeObject<CreateWallArguments>(jsonArgs); var x = args.Start[0]; var y = args.Start[1]; var z = args.Start[2]; var x1 = args.End[0]; var y1 = args.End[1]; var z1 = args.End[2];
TransactionUtils.Execute(RevitCommandData.Document, (nx) => { var start = new XYZ(x/304.8, y/304.8, z); var end = new XYZ(x1/304.8, y1/304.8, z1); var line = Line.CreateBound(start, end); Wall.Create(RevitCommandData.Document, line ,RevitCommandData.ActiveView.GenLevel.Id, false); }, "CreateWall");
using System.ComponentModel; using System.Windows.Forms; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using ModelContextProtocol.Server;
var builder = Host.CreateApplicationBuilder(); builder.Services.AddMcpServer() .WithStdioServerTransport() .WithTools<RevitTool>();
[McpServerTool(Name = "InsertWindowInWall"), Description("Generation A Window In A Selection Wall , Define Window Size : 1500 x 1200 d, Need To Calculate The Window-Top Is Small Then Wall-Height , This Command Need Input Args : ElementId , LocationX , LocationY ,LocationZ")] publicstringInsertWindowInWallTool(string command , int eId , double x , double y , double z) { return$@" {{ ""command"": ""InsertWindowInWall"", ""arguments"": {{ ""eId"" : {eId} , ""location"": [{x},{y},{z}] }} }}"; } }
using Microsoft.Extensions.AI; using ModelContextProtocol.Client; using ModelContextProtocol.Protocol; using OpenAI.Chat; using OpenAI; using System.ClientModel; using System.Collections.ObjectModel; using System.Diagnostics; using System.Net; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using ChatMessage = Microsoft.Extensions.AI.ChatMessage; using Newtonsoft.Json; using System.Windows.Forms;
var input = string.Join("",args);
Debug.Print(input); //var input = "在一个已经存在的id为333160的坐标为(0,0,0)到(10000,0,0)高度为3000 单位是mm的墙体 , 插入一个窗户,窗户位置可以由你自行决定"; //var input = "创建一个墙体,墙体坐标为(0,0,0)->(10000,0,0),单位是mm"; //var input = // "选中的墙体高度为3000 单位是mm的墙体 , 插入一个窗户,窗户位置可以由你自行决定选中构件的数据为 :WallId:333160 , WallData: Curve Data is : Start = X = 0, Y = 0, Z = 0 , End = X = 10000, Y = 0, Z = 0"; //"选中的墙体高度为3000 单位是mm的墙体 , 插入一个窗户,窗户位置可以由你自行决定 , Curve Data is : Start = X = 0, Y = 0, Z = 0 , End = X = 10000, Y = 0, Z = 0"; awaitusingvar mcpClient = await McpClientFactory.CreateAsync(new StdioClientTransport(new StdioClientTransportOptions() { Name = "Demo Server", Command = "powershell", Arguments = ["D:\\NET.Mcp.Server.exe"] }));
var openAiOptions = new OpenAIClientOptions(); openAiOptions.Endpoint = new Uri("https://api.deepseek.com/v1/");
var chatClient = new ChatClient("deepseek-chat", new ApiKeyCredential("sk-0000000000000"), openAiOptions);
var client = new ChatClientBuilder(chatClient.AsIChatClient()).UseFunctionInvocation().Build();
var prompts = new List<Microsoft.Extensions.AI.ChatMessage> { new ChatMessage(ChatRole.System, """you are a professional enginer in BIM , so you can select the greate tool to user , and generation a standard input style And Arguments to tools"""), new ChatMessage(ChatRole.User, input) };
var tools = await mcpClient.ListToolsAsync();
var chatOptions = new ChatOptions() { Tools = [.. tools] }; var res = await client.GetResponseAsync(prompts, chatOptions);
var message = res.Messages[1].Contents[0]; varvalue = ((Microsoft.Extensions.AI.FunctionResultContent)message).Result; var convert = JsonConvert.DeserializeObject(value.ToString());
// 反序列化 ResponseData data = JsonConvert.DeserializeObject<ResponseData>(value.ToString());
// 访问数据 foreach (var item in data.Content) { //Console.WriteLine($"Type: {item.Type}, Text: {item.Text}"); var d = item.Text; Console.WriteLine(d); }