v0.1.4版本 #2
@ -98,7 +98,6 @@
|
|||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="client\" />
|
|
||||||
<Folder Include="resource\" />
|
<Folder Include="resource\" />
|
||||||
<Folder Include="server\" />
|
<Folder Include="server\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
13
client/McpClient.cs
Normal file
13
client/McpClient.cs
Normal 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);
|
||||||
|
}
|
||||||
@ -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;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -3,54 +3,35 @@ using System.Collections.Generic;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using ModelContextProtocol.Client;
|
using ModelContextProtocol.Client;
|
||||||
using ModelContextProtocol.Protocol.Transport;
|
using ModelContextProtocol.Protocol.Transport;
|
||||||
|
using ModelContextProtocol.Protocol.Types;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace LinkToolAddin.client;
|
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...");
|
options = new SseClientTransportOptions
|
||||||
|
|
||||||
// 创建 MCP Server 配置
|
|
||||||
SseClientTransportOptions options = new SseClientTransportOptions
|
|
||||||
{
|
{
|
||||||
Endpoint = new Uri("https://mcp.amap.com/sse?key=ed418512c94ade8f83d42c37b77d2bb2"),
|
Endpoint = new Uri(url),
|
||||||
};
|
};
|
||||||
|
transport = new SseClientTransport(options);
|
||||||
IClientTransport transport = new SseClientTransport(options);;
|
}
|
||||||
|
public async Task<IList<McpClientTool>> GetToolListAsync()
|
||||||
|
{
|
||||||
// 创建 MCP Client
|
// 创建 MCP Client
|
||||||
var client = await McpClientFactory.CreateAsync(transport);
|
IMcpClient client = await McpClientFactory.CreateAsync(transport);
|
||||||
Console.WriteLine("Connected to 高德 MCP Server");
|
var tools = await client.ListToolsAsync();
|
||||||
|
return tools;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
public async Task<CallToolResponse> CallToolAsync(string toolName,Dictionary<string, object> parameters = null)
|
||||||
{
|
{
|
||||||
// 获取可用工具列表
|
IMcpClient client = await McpClientFactory.CreateAsync(transport);
|
||||||
var tools = await client.ListToolsAsync();
|
CallToolResponse result = await client.CallToolAsync(toolName,parameters);
|
||||||
Console.WriteLine("\nAvailable Tools:");
|
return result;
|
||||||
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";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -11,6 +11,7 @@ using Microsoft.Extensions.Hosting;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Windows.Documents;
|
||||||
using ArcGIS.Desktop.Core.Geoprocessing;
|
using ArcGIS.Desktop.Core.Geoprocessing;
|
||||||
using LinkToolAddin.client;
|
using LinkToolAddin.client;
|
||||||
using LinkToolAddin.host.llm;
|
using LinkToolAddin.host.llm;
|
||||||
@ -20,6 +21,8 @@ using log4net;
|
|||||||
using log4net.Appender;
|
using log4net.Appender;
|
||||||
using log4net.Config;
|
using log4net.Config;
|
||||||
using log4net.Layout;
|
using log4net.Layout;
|
||||||
|
using ModelContextProtocol.Client;
|
||||||
|
using ModelContextProtocol.Protocol.Types;
|
||||||
using ModelContextProtocol.Server;
|
using ModelContextProtocol.Server;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
@ -46,8 +49,28 @@ namespace LinkToolAddin.ui.dockpane
|
|||||||
private async void TestServer_OnClick(object sender, RoutedEventArgs e)
|
private async void TestServer_OnClick(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
log.Info("TestServer Clicked");
|
log.Info("TestServer Clicked");
|
||||||
string res = await SseMcpClient.testGaodeMcp();
|
List<string> args = new List<string>();
|
||||||
log.Info(res);
|
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()
|
private async void Retrieve_Test()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user