v0.1.4版本 #2

Merged
PeterZhong merged 70 commits from host into master 2025-06-15 14:42:58 +00:00
5 changed files with 118 additions and 43 deletions
Showing only changes of commit 792c458f6c - Show all commits

View File

@ -98,7 +98,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="client\" />
<Folder Include="resource\" />
<Folder Include="server\" />
</ItemGroup>

13
client/McpClient.cs Normal file
View File

@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Types;
namespace LinkToolAddin.client;
public interface McpClient
{
public Task<IList<McpClientTool>> GetToolListAsync();
public Task<CallToolResponse> CallToolAsync(string toolName, Dictionary<string, object> parameters = null);
}

View File

@ -1,6 +1,65 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Documents;
using ModelContextProtocol.Protocol.Types;
using Newtonsoft.Json;
namespace LinkToolAddin.client;
public class PythonMcpClient
{
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
public class StdioMcpClient : McpClient
{
private List<string> arguments;
private StdioClientTransportOptions transportOptions;
public StdioMcpClient(string command,List<string> arguments)
{
this.arguments = arguments;
transportOptions = new StdioClientTransportOptions()
{
Command = command,
Arguments = this.arguments
};
}
public static async Task<string> StdioMcpTest()
{
List<string> arguments = new List<string>();
arguments.Add("mcp-server-time");
arguments.Add("--local-timezone=America/New_York");
StdioClientTransportOptions transportOptions = new StdioClientTransportOptions()
{
Command = "uvx", // 运行服务器的命令
Arguments = arguments
};
var client = await McpClientFactory.CreateAsync(new StdioClientTransport(transportOptions));
var tools = await client.ListToolsAsync();
Console.WriteLine("Available Tools:");
foreach (var tool in tools)
{
Console.WriteLine($"- {tool.Name}");
}
var result = await client.CallToolAsync("get_current_time",
new Dictionary<string, object> { { "timezone", "America/New_York" } });
Console.WriteLine(JsonConvert.SerializeObject(result));
return tools[0].Name;
}
public async Task<IList<McpClientTool>> GetToolListAsync()
{
IMcpClient client = await McpClientFactory.CreateAsync(new StdioClientTransport(transportOptions));
IList<McpClientTool> tools = await client.ListToolsAsync();
return tools;
}
public async Task<CallToolResponse> CallToolAsync(string toolName, Dictionary<string, object> parameters = null)
{
IMcpClient client = await McpClientFactory.CreateAsync(new StdioClientTransport(transportOptions));
CallToolResponse result = await client.CallToolAsync(toolName,parameters);
return result;
}
}

View File

@ -3,54 +3,35 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
using ModelContextProtocol.Protocol.Types;
using Newtonsoft.Json;
namespace LinkToolAddin.client;
public class SseMcpClient
public class SseMcpClient : McpClient
{
public static async Task<string> testGaodeMcp()
private SseClientTransportOptions options;
private IClientTransport transport;
public SseMcpClient(string url)
{
Console.WriteLine("Connecting to 高德 MCP Server via SSE...");
// 创建 MCP Server 配置
SseClientTransportOptions options = new SseClientTransportOptions
options = new SseClientTransportOptions
{
Endpoint = new Uri("https://mcp.amap.com/sse?key=ed418512c94ade8f83d42c37b77d2bb2"),
Endpoint = new Uri(url),
};
IClientTransport transport = new SseClientTransport(options);;
transport = new SseClientTransport(options);
}
public async Task<IList<McpClientTool>> GetToolListAsync()
{
// 创建 MCP Client
var client = await McpClientFactory.CreateAsync(transport);
Console.WriteLine("Connected to 高德 MCP Server");
IMcpClient client = await McpClientFactory.CreateAsync(transport);
var tools = await client.ListToolsAsync();
return tools;
}
try
{
// 获取可用工具列表
var tools = await client.ListToolsAsync();
Console.WriteLine("\nAvailable Tools:");
foreach (var tool in tools)
{
Console.WriteLine($"- {tool.Name}: {tool.Description}");
}
// 示例调用:获取当前定位
var result = await client.CallToolAsync("amap.maps_weather", new Dictionary<string, object>{{"city","北京"}});
Console.WriteLine("\n[amap.get_location] Result:");
Console.WriteLine(result);
return JsonConvert.SerializeObject(result);
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred: {ex.Message}");
}
finally
{
await client.DisposeAsync();
}
Console.WriteLine("Client closed.");
return "failed";
public async Task<CallToolResponse> CallToolAsync(string toolName,Dictionary<string, object> parameters = null)
{
IMcpClient client = await McpClientFactory.CreateAsync(transport);
CallToolResponse result = await client.CallToolAsync(toolName,parameters);
return result;
}
}

View File

@ -11,6 +11,7 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Threading;
using System.Windows.Documents;
using ArcGIS.Desktop.Core.Geoprocessing;
using LinkToolAddin.client;
using LinkToolAddin.host.llm;
@ -20,6 +21,8 @@ using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Layout;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Types;
using ModelContextProtocol.Server;
using Newtonsoft.Json;
@ -46,8 +49,28 @@ namespace LinkToolAddin.ui.dockpane
private async void TestServer_OnClick(object sender, RoutedEventArgs e)
{
log.Info("TestServer Clicked");
string res = await SseMcpClient.testGaodeMcp();
log.Info(res);
List<string> args = new List<string>();
args.Add("mcp-server-time");
args.Add("--local-timezone=America/New_York");
McpClient stdioMcpClient = new StdioMcpClient("uvx",args);
IList<McpClientTool> tools = await stdioMcpClient.GetToolListAsync();
foreach (McpClientTool tool in tools)
{
log.Info(tool.JsonSchema.ToString());
}
CallToolResponse response = await stdioMcpClient.CallToolAsync("get_current_time",
new Dictionary<string, object> { { "timezone", "America/New_York" } });
log.Info(JsonConvert.SerializeObject(response));
}
private async void SseMcp_test()
{
SseMcpClient client = new SseMcpClient("https://mcp.amap.com/sse?key=ed418512c94ade8f83d42c37b77d2bb2");
IList<McpClientTool> tools = await client.GetToolListAsync();
foreach (McpClientTool tool in tools)
{
log.Info(tool.JsonSchema.ToString());
}
}
private async void Retrieve_Test()