56 lines
1.7 KiB
C#
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/sse?key=ed418512c94ade8f83d42c37b77d2bb2"),
|
|
};
|
|
|
|
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";
|
|
}
|
|
} |