高德MCP服务接入

创建一个agent,集成高德MCP工具

文章目录

一、安装依赖

复制代码
pip install openai
pip install langchain (1.0版本以上)
pip install langchain_mcp_adapters

二、获取高德key

https://lbs.amap.com/api/mcp-server/create-project-and-key

三、代码实现

python 复制代码
import asyncio

from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent

llm = ChatOpenAI(
    model="qwen3-max",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    api_key="{阿里云百炼 api-key}",
    streaming=True,
)

# 高德mcp客户端
async def create_amap_client():
    mcp_config = {
        "amap": {
            "url": "https://mcp.amap.com/sse?key={你的高德key}",
            "transport":"sse"
        }
    }

    client = MultiServerMCPClient(mcp_config)
    tools = await client.get_tools()
    return client,tools

# 创建智能体,集成MCP工具
async def create_and_run_agent():
    client,tools = await create_amap_client()

    agent = create_agent(
        model=llm,
        tools=tools,
        system_prompt="你是一个智能助手,可以调用高德MCP工具。",
    )

    question = "请提供青岛北站的坐标?"
    async for step in agent.astream(
            {'messages': question},
            stream_mode="values"
    ):
        step["messages"][-1].pretty_print()

asyncio.run(create_and_run_agent())

输出:

相关推荐
质造者16 分钟前
LangChain + Ollama + Tavily 实现旅游问答系统
linux·人工智能·python·langchain·rag
伊布拉西莫29 分钟前
【流畅的Python】第20章:并发执行器 — 学习笔记
笔记·python·学习
IT策士34 分钟前
Redis 从入门到精通:Python 操作 Redis
redis·python·bootstrap
编码者卢布40 分钟前
【Azure AI Search】 searchMode=any 和 searchMode=all 有什么区别?
人工智能·python·flask
Samooyou1 小时前
大模型微调(Fine Tuning)
人工智能·python·ai·语言模型
qq_8573058191 小时前
python语法
开发语言·python·算法
AI行业学习1 小时前
CC-Switch v3.16.1 官方下载 | 安装配置详细教程【2026.6.10】
java·开发语言·vue.js·python·mysql·eclipse·html
早起CaiCai1 小时前
【Pytorch 实践1】手写数字
人工智能·pytorch·python
吴梓穆2 小时前
Python 语法基础 函数
开发语言·python
Kobebryant-Manba2 小时前
学习文本处理
开发语言·python