LinkToolAddin/client/SseMcpClient.cs
2025-05-13 23:01:17 +08:00

56 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
using Newtonsoft.Json;
namespace LinkToolAddin.client;
public class SseMcpClient
{
public static async Task<string> testGaodeMcp()
{
Console.WriteLine("Connecting to 高德 MCP Server via SSE...");
// 创建 MCP Server 配置
SseClientTransportOptions options = new SseClientTransportOptions
{
Endpoint = new Uri("https://mcp.amap.com/v1/sse"),
};
IClientTransport transport = new SseClientTransport(options);;
// 创建 MCP Client
var client = await McpClientFactory.CreateAsync(transport);
Console.WriteLine("Connected to 高德 MCP Server");
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";
}
}