其 Tool 名称如下:
maps_direction_bicycling maps_direction_driving maps_direction_transit_integrated maps_direction_walking maps_distance maps_geo maps_regeocode maps_ip_location maps_around_search maps_search_detail maps_text_search maps_weather高德地图每天都给开发者提供了免费额度所以做该实验时不需要担心需要付费。打开 我的应用 | 高德控制台 创建一个新的应用然后复制应用 key。高德 mcp 服务器地址https://mcp.amap.com/sse?key{在高德官网上申请的key}在 amap 项目的appsettings.json添加以下 json替换里面的部分参数。笔者注除了 gpt-4o 模型其它注册 Function call 的模型也可以使用。McpServers: { amap-amap-sse: { url: https://mcp.amap.com/sse?key{在高德官网上申请的key} } }, AIModel: { ModelId: gpt-4o, DeploymentName: gpt-4o, Endpoint: https://openai.com/, Key: aaaaaaaa }导入配置并创建日志var configuration new ConfigurationBuilder() .AddJsonFile(appsettings.json) .AddJsonFile(appsettings.Development.json) .Build(); using ILoggerFactory factory LoggerFactory.Create(builder builder.AddConsole());第一步创建 mcp 客户端连接高德 MCP Server并获取 Tool 列表。var defaultOptions new McpClientOptions { ClientInfo new() { Name 地图规划, Version 1.0.0 } }; var defaultConfig new SseClientTransportOptions { Endpoint new Uri(configuration[McpServers:amap-amap-sse:url]!), Name amap-amap-sse, }; await using var client await McpClientFactory.CreateAsync( new SseClientTransport(defaultConfig), defaultOptions, loggerFactory: factory); var tools await client.ListToolsAsync(); foreach (var tool in tools) { Console.WriteLine($Connected to server with tools: {tool.Name}); }第二步连接 AI 模型和配置 MCP使用 SemanticKernel 框架对接 LLM将 MCP Tool 转换为 Function 添加到对话上下文中。var aiModel configuration.GetSection(AIModel); var builder Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( deploymentName: aiModel[ModelId], endpoint: aiModel[Endpoint], apiKey: aiModel[Key]); builder.Services.AddLogging(s { s.AddConsole(); }); Kernel kernel builder.Build(); // 这里将 mcp 转换为 functaion call kernel.Plugins.AddFromFunctions(amap, tools.Select(aiFunction aiFunction.AsKernelFunction())); var chatCompletionService kernel.GetRequiredServiceIChatCompletionService(); OpenAIPromptExecutionSettings openAIPromptExecutionSettings new() { Temperature 0, FunctionChoiceBehavior FunctionChoiceBehavior.Auto(options: new() { RetainArgumentTypes true }) };第三步对话交互编写控制台与用户对话交互。var history new ChatHistory(); string? userInput; do { Console.Write(用户提问 ); userInput Console.ReadLine(); history.AddUserMessage(userInput!); var result await chatCompletionService.GetChatMessageContentAsync( history, executionSettings: openAIPromptExecutionSettings, kernel: kernel); Console.WriteLine(AI 回答 result); history.AddMessage(result.Role, result.Content ?? string.Empty); } while (userInput is not null);演示地图规划注意由于高德地图免费额度限流而 AI 对话时可能有多次对 MCP Server 请求因此有时候效果并不是那么好。1. 智能旅游路线规划最多支持16个途经点的旅游路线规划自动计算最优顺序并提供可视化地图链接。使用示例请帮我规划一条上海三日游路线包括外滩、东方明珠、迪士尼、豫园、南京路并提供可视化地图2. 景点搜索与详情查询查询景点的详细信息包括评分、开放时间、门票价格等。使用示例请查询黄山风景区的开放时间、门票价格和旅游季节推荐AI 是怎么识别调用 MCP在编写高德地图规划时有一段代码是将 MCP 服务器的接口转换为 Function 的代码如下kernel.Plugins .AddFromFunctions(amap, tools.Select(aiFunction aiFunction.AsKernelFunction()))其实在这里就可以下结论并不是 AI 模型直接调用 MCP Server 的依然 Client 进行是 Function call 。通过拦截 http 请求可以发现当用户输入请帮我规划一条上海三日游路线包括外滩、东方明珠、迪士尼、豫园、南京路并提供可视化地图时客户端首先将用户提问和 mcp 服务所提供的 function call 一起发送到 AI 模型服务器。对话时Client 提供给 LLM 的 Function MCP Tool列表。然后 AI 回答要调用的 Function call 步骤和参数接着由客户端实现将 Function 定位 MCP Server并顺序调用每个 Tool。LLM 返回要顺序调用的 Function 列表以及参数