背景
那些LLM不知道的事
开篇先尝试直接问LLM一个问题,“今天天气如何”。
然而,并未能从LLM获得期望回答。原因也很简单,今天是哪天?天气是哪里的天气?这些问题对于LLM来说统统不得而知。
因此,我们很自然地想到,是不是能让LLM自己学会用工具,哪里不会点哪里呢?
当LLM学会用工具
- “让LLM自己学会用工具,来解答用户问题。”
上面这句话中,出现了三个角色,“用户”、“工具”、“LLM”,以及隐藏的第四个角色——将这一切粘合起来的“主控程序”。
关于四者的交互流程,我从百炼找了张图,供以参考:
MCP干嘛来了
- 没有MCP,我要怎么做
按着Agent+FunctionCall的模式,我设计了工具schema,走通了LLM的服务调用,终于让LLM学会了用工具。但随着工具越来越多、工具调用与LLM耦合得越来越深,不管是维护还是迭代,都会消耗大量的精力。
那么,问题来了:
- 能不能实现Agent与Tools的解耦?
- 能不能能统一不同Tools的调用协议,让模型快速接入?
- 能不能实现Tools的共享?
- 有了MCP,我会怎么做
现在有了MCP,一切都好起来了:
- Agent和Tools,我可以分开维护了。
- 再多的Tools,我用"list_tools"+"call_tool"就解决了。
- 我可以分享自己的Tools,也可以快速接入别人的Tools了。
近距离看看MCP
MCP is an open protocol that standardizes how applications provide context to LLMs. Think of MCP like a USB-C port for AI applications. Just as USB-C provides a standardized way to connect your devices to various peripherals and accessories, MCP provides a standardized way to connect AI models to different data sources and tools.
MCP架构中的角色主要有以下几种:
- MCP Hosts: 相当于上文提到的“主控程序”,比如Claude Desktop、IDE等。
- MCP Clients: 服务调用的客户端,通常会被集成到Host中执行list_tools、call_tool等操作。
- MCP Servers: 服务调用的服务端,通常在此定义tools、prompts、resources等。
- Local Data Sources: 本地数据。
- Remote Services: 远端服务。
ps:写了个mcp demo,就想让LLM告诉我,今天天气到底如何?
mcp = FastMCP("Demo") @mcp.tool( name="get_current_time", description="获取当前时间", ) def get_current_time(): """ 获取当前时间并进行格式化展示 :return: """ now = datetime.datetime.now() formatted_time = now.strftime("%Y-%m-%d %H:%M:%S") return formatted_time @mcp.tool( name="get_location", description="获取当前地点", ) def get_location(): """ 获取当前地点 :return: """ try: response = requests.get("http://ip-api.com/json/") data = response.json() if data["status"] == "success": location_info = { "country": data.get("country", ""), "region": data.get("regionName", ""), "city": data.get("city", "") } return json.dumps(location_info, ensure_ascii=False) else: return json.dumps({"error": "无法获取地理位置"}, ensure_ascii=False) except Exception as e: return json.dumps({"error": str(e)}, ensure_ascii=False)
AI搜索怎么玩MCP
场景一:文件解析与总结
1. 前置准备
1.1 注册 AI搜索平台[1],获取 api-key[2]
1.2 vscode + cline
1.3 cline配置llm接口
API Provider选择 OpenAI Compatible
Base URL设为:
https://dashscope.aliyuncs.com/compatible-mode/v1
1.4 安装uv,管理python环境
curl -LsSf https://astral.sh/uv/install.sh | sh 或者 pip install uv
2. cline配置mcp server
2.1 下载 alibabacloud-opensearch-mcp-server[3]
2.2 配置mcp server
{ "mcpServers": { "aisearch-mcp-server": { "command": "uv", "args": [ "--directory", "/path/to/aisearch-mcp-server", "run", "aisearch-mcp-server" ], "env": { "AISEARCH_API_KEY": "<AISEARCH_API_KEY>", "AISEARCH_ENDPOINT": "<AISEARCH_ENDPOINT>" } } } }
3. 任务演示
此处为语雀视频卡片,点击链接查看:aisearch_mcp_demo.mp4
4. 业务价值
- 降低接入成本,提升用户体验:支持用户通过标准化方式快速集成AI搜索平台的服务,降低了开发门槛和接入成本,同时提升了用户体验。
- 提高系统灵活性:用户可根据业务需求灵活配置AI服务,适应多样化的业务需求。
- 支持自然语言交互与智能化操作:用户可通过简单的自然语言指令进行智能化任务编排,完成复杂的任务执行。
- 促进创新和业务增长:可通过MCP的标准化集成,进行快速试错与迭代,加速产品上线周期。
场景二:向量检索及排序
1. 前置准备
(新增)开通 opensearch向量检索版[4],构建一张向量表
(其他)同场景一
2. cline配置mcp server
2.1 下载 alibabacloud-opensearch-mcp-server
2.2 配置mcp server
{ "mcpServers": { "aisearch-mcp-server": { "command": "uv", "args": [ "--directory", "/path/to/aisearch-mcp-server", "run", "aisearch-mcp-server" ], "env": { "AISEARCH_API_KEY": "<AISEARCH_API_KEY>", "AISEARCH_ENDPOINT": "<AISEARCH_ENDPOINT>" } }, "opensearch-vector-mcp-server": { "command": "uv", "args": [ "--directory", "/path/to/opensearch-vector-mcp-server", "run", "opensearch-vector-mcp-server" ], "env": { "OPENSEARCH_VECTOR_ENDPOINT": "http://ha-cn-***.public.ha.aliyuncs.com", "OPENSEARCH_VECTOR_USERNAME": "<username>", "OPENSEARCH_VECTOR_PASSWORD": "<password>", "OPENSEARCH_VECTOR_INSTANCE_ID": "ha-cn-***", "OPENSEARCH_VECTOR_INDEX_NAME": "<Optional: index in vector table>", "AISEARCH_API_KEY": "<Optional: AISEARCH_API_KEY for embedding>", "AISEARCH_ENDPOINT": "<Optional: AISEARCH_ENDPOINT for embedding>" } } } }
3. 任务演示
4. 业务价值
- 扩展AI应用场景:增强Agent向量检索能力,支持动态扩展和无缝集成。
- 优化用户体验:提供更精准的向量搜索服务,降低使用成本。
场景三:Elasticsearch智能检索
1. 前置准备
(新增)开通 Elasticsearch[5],创建一份索引并写入测试数据
(其他)同场景一
2. cline配置mcp server
2.1 参考 elasticsearch-mcp-server[6]
2.2 配置mcp server
{ "mcpServers": { "elasticsearch-mcp-server": { "command": "npx", "args": [ "-y", "@elastic/mcp-server-elasticsearch" ], "env": { "ES_URL": "http://es-cn-***.public.elasticsearch.aliyuncs.com:9200", "ES_USERNAME": "<USERNAME>", "ES_PASSWORD": "<PASSWORD>" } } } }
3. 任务演示
4. 业务价值
- 提升数据搜索和分析能力:支持高效的全文搜索、实时分析和复杂查询。
- 优化用户体验:提供更精准的搜索结果和个性化服务。
参考链接:
[1]https://help.aliyun.com/zh/open-search/activate-services-and-create-api-key?utm_content=g_1000405161
[2]https://help.aliyun.com/zh/open-search/api-keys-management?utm_content=g_1000405162
[3]https://github.com/aliyun/alibabacloud-opensearch-mcp-server?tab=readme-ov-file
[5]https://help.aliyun.com/zh/es/user-guide/getting-started?utm_content=g_1000405164
[6]https://github.com/elastic/mcp-server-elasticsearch
来源 | 阿里云开发者公众号
作者 | 是溪