高德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())

输出:

相关推荐
Weigang13 小时前
用 LlamaIndex 做 RAG 前,先把 Reader、Index、Retriever 的边界写清楚
人工智能·python·开源
小九九的爸爸13 小时前
前端入门Agent开发,掌握这些Python数据基础就够啦
python·agent
风之所往_13 小时前
Python 3.9 新特性全面总结
python
W是笔名13 小时前
python___容器类型的数据___序列
开发语言·python
aqi0013 小时前
15天学会AI应用开发(十一)从TXT文件构建RAG知识库
人工智能·python·大模型·ai编程·ai应用
火星校尉13 小时前
一场数据基建与消费场景的跨界实验
java·前端·数据库·python·php
Sam092713 小时前
【AI 算法精讲 14】TF-IDF:词频与逆文档频率
人工智能·python·算法·ai
W是笔名14 小时前
python_let`s try it 6___BMI计算器
java·前端·python
内蒙深海大鲨鱼14 小时前
数据操作+数据预处理
python