
本节不讲解 Streamable 。MCP Tool 说明目前社区有两大主流 LLM 开发框架分别是 Microsoft.SemanticKernel、LangChain它们都支持 Plugin 能够将本地函数、Swagger 等转换为函数将 Function 提交给 LLMAI 返回要调用的 Function 后由框架引擎实现动态调用这样功能叫 Function call。注意MCP 有很多功能其中一个叫 MCP Tool可以视为跟 Plugin 实现类似功能的东西。MCP Tool 对标 Plugin MCP 不止包含 Tool 这一功能。但是每个 LLM 框架的 Plugin 实现方式不一样其使用和实现机制跟语言特性深度绑定不能实现跨服务跨平台使用所以出现了 MCP Tool MCP Tool 是对标 Plugin 的一类功能主要目的跟 Plugin 一样提供 Function但是 MCP 有统一协议标准跟语言无关、跟平台无关但是 MCP 也不是完全替换 Plugin Plugin 依然具有很大的用武之地。MCP Tool、Plugin 最后都是转换为 Function call 的有很多人会把 MCP 、MCP Tool 和 Function call 搞混认为 MCP 是替换 Function call 的所以要注意对标 Plugin 的是 MCP Tool而两者都是转换为 Function 给 AI 使用的。MCP Tool以 TransportSseClient 为例如果要在 Client 调用 TransportSseServer 的 Tool需要指定 Tool 名字和参数。后续将会讲解如何通过 SK 将 mcp tool 提供给 AI 模型。var echoTool tools.First(x x.Name Echo); var result await client.CallToolAsync(Echo, new Dictionarystring, object? { { message,痴者工良} }); foreach (var item in result.Content) { Console.WriteLine($type: {item.Type},text: {item.Text}); }让我们再回顾 MCP Server 是怎么提供 Tool 的。首先服务端需要定义 Tool 类和函数。[McpServerToolType] public sealed class EchoTool { [McpServerTool, Description(Echoes the input back to the client.)] public static string Echo(string message) { return hello message; } }Mcp server 可以通过以下两种方式暴露 tool。// 直接指定 Tool 类 builder.Services .AddMcpServer() .WithHttpTransport() .WithToolsEchoTool() .WithToolsSampleLlmTool(); // 扫描程序集 builder.Services .AddMcpServer() .WithHttpTransport() .WithStdioServerTransport() .WithToolsFromAssembly();Client 识别服务端的 Tool 列表时可以使用McpClientTool.ProtocolTool.InputSchema获取 tool 的输入参数格式其内容格式示例如下Annotations: null Description: Echoes the input back to the client. Name: Echo InputSchema: {title:Echo,description:Echoes the input back to the client.,type:object,properties:{message:{type:string}},required:[message]}[McpServerToolType]用于将包含应该作为ModelContextProtocol.Server.McpServerTools公开的方法的类型属性化。[McpServerTool]用于指示应该将方法视为 ModelContextProtocol.Server.McpServerTool。[Description]则用于添加注释。依赖注入在实现 Tool 函数时服务端是可以通过函数实现依赖注入的。参考示例项目 InjectServer、InjectClient。添加一个服务类并注册到容器中。public class MyService { public string Echo(string message) { return hello message; } }builder.Services.AddScopedMyService();在 Tool 函数中注入该服务[McpServerToolType] public sealed class MyTool { [McpServerTool, Description(Echoes the input back to the client.)] public static string Echo(MyService myService, string message) { return myService.Echo(message); } }将 MCP Tool 提交到 AI 对话中前面提到MCP Tool 和 Plugin 都是实现 Function call 的一种方式当在 AI 对话中使用 Tool 时其主要过程如下当你提出问题时client 将你的问题发送给 LLM LLM 分析可用的 tools 并决定使用哪些 toolclient 通过 MCP server 执行选择的 tool结果被发回给 LLMLLM 制定自然语言响应响应显示给你这个过程并不是只有一两次可能发生多次具体细节将会在高德地图 MCP 实战中讲解这里只是简单提及。将 Tool 提交到对话上下文的伪代码// Get available functions. IListMcpClientTool tools await client.ListToolsAsync(); // Call the chat client using the tools. IChatClient chatClient ...; var response await chatClient.GetResponseAsync( your prompt here, new() { Tools [.. tools] },高德地图 MCP 实战聊了这么久终于到了实战对接环节本节将会通过高德地图案例讲解 MCP Tool 的逻辑细节和对接使用方式。代码参考示例项目 amap。高德地图 MCP Server 目前主要提供的功能地理编码逆地理编码IP 定位天气查询骑行路径规划步行路径规划驾车路径规划公交路径规划距离测量关键词搜索周边搜索详情搜索