MCP协议的简介和简单实现

​模型上下文协议(Model Context Protocol,简称 MCP)一种开放的、通用的标准化协议, 旨在标准化大型语言模型(LLM)与外部数据源或工具之间的交互方式。

MCP 的本质是为 AI 模型提供统一的交互标准;

MCP 由三个核心组件构成:Host、Client 和 Server。

Host 主机: 大模型的应用程序, 负责接收你的提问并与 大模型交互,比如聊天机器人。

MCP Client 客户端:运行在主机(host)里的客户端,当有需要的时候被激活,与MCP Server 建立连接(客户端和服务器是一对一连接)。

MCP Server 服务器:负责向客户端(MCP Client )提供 资源、提示 和 工具 的服务器。

工作流程:

举个栗子: 问聊天机器人 明天天气怎么样?

① 用户提问:用户通过聊天机器人询问 "明天天气怎么样?"。

②意图解析: 聊天机器人(Host主机)将问题发给大模型,分析可用的工具,并决定用哪一个工具,如查询天气的工具;

③ 工具调用: 聊天机器人(Host主机)使用MCP Client 客户端向MCP Server 服务器发出请求,调用工具

④结果整合: 工具调用的结果送回给大模型, 结合执行结果生成自然语言的回应。

⑤自然语言输出: 最终展示给用户

MCP 协议内置了两种标准传输方式:标准输入/输出(stdio) 和 Server-Sent Events(SSE) 。

stdio 传输通过 标准输入输出流 实现客户端与服务器之间的通信,适用于本地集成与命令行工具。

SSE 传输 通过 HTTP POST 请求实现 客户端到服务器通信,同时支持 服务器到客户端流式传输 。

MCP协议建立在 JSON-RPC 2.0 之上,定义了AI模型与外部工具之间的标准通信规范。

底层传输协议可以是 http/https、WebSocket ,tcp等

使用langchain实现一个简单的demo
stdio类型

mcp_server_stdio.py

(程序不需要启动)

复制代码
from fastmcp import FastMCP

mcp = FastMCP("Math")

@mcp.tool()
def add(a: int, b: int) -> str:
    """Add two numbers"""
    return str(a + b)

@mcp.tool()
def multiply(a: int, b: int) -> str:
    """Multiply two numbers"""
    return str(a * b)

if __name__ == "__main__":
    mcp.run(transport="stdio")

sse类型

mcp_server_sse.py , 这个需要启动

复制代码
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Weather")

@mcp.tool()
async def get_weather(location: str) -> str:
    """Get weather for location."""
    print(f"Getting weather for {location}")
    return "It's always sunny in New York"

if __name__ == "__main__":
    mcp.run(transport="streamable-http")

客户端

mcp_client.py

偶尔可能因为网络不好, 启动时注册阿里mcp会导致失败, 重新启动一下

复制代码
import asyncio
from typing import List
from langchain_mcp_adapters.client import MultiServerMCPClient  
from langchain.agents import create_agent,AgentState
from langchain_core.messages import AIMessage,ToolMessage
from langchain_openai import ChatOpenAI

QWEN_OPENAI_API_BASE = "https://dashscope.aliyuncs.com/compatible-mode/v1" #OPENAI API  
QWEN_API_KEY = "sk-*************************************"  
QWEN_MODEL = "qwen-plus"

# 设置DeepSeek API Key和基础地址
llm = ChatOpenAI(
    model=QWEN_MODEL,
    openai_api_key=QWEN_API_KEY,
    openai_api_base=QWEN_OPENAI_API_BASE,  # 关键配置
    temperature=0.7,
)

from langchain.agents.middleware import after_model,before_model,before_agent,after_agent,wrap_tool_call
from langchain.tools import tool, ToolRuntime
from langgraph.runtime import Runtime


#  这里是因为返回的ToolMessage中content类型是List, 而langchain中需要的是str类型
@before_model
async def before_model_messages(state: AgentState, runtime: Runtime) -> dict | None:
    """Remove old messages to keep conversation manageable."""
    message_list = state["messages"]
    for message in message_list:
         if isinstance(message, ToolMessage):
             if type( message.content) is not str:
                message.content = str(message.content)
    return {"messages": message_list}

async def main():
    client = MultiServerMCPClient(  
        {
            "mcp_sever_stdio": {
                "transport": "stdio",  
                "command": "python",
                # Absolute path to your math_server.py file
                "args": ["D:/MyWork/demo/mcp_server_stdio.py"],
            },
            "mcp_sever_http": {
                "transport": "streamable_http",  # 就是启动后的 mcp_server_sse.py
                # Ensure you start your weather server on port 8000
                "url": "http://localhost:8000/mcp",
            },
             "ali_news_mcp": {  #阿里云百炼_联网搜索
                 "transport":"sse",   # 根据实际填写   stdio/sse/streamable_http/ 也可能有http
                "url": "https://dashscope.aliyuncs.com/api/v1/mcps/WebSearch/sse",
                "headers": {"Authorization": "Bearer sk-*************************************""}
             },
            "ali_logistics_mcp": {  #阿里MCP服务 阿里云百炼_全国快递物流查询
                "transport":"streamable_http",   # 根据实际填写   stdio/sse/streamable_http/ 也可能有http
                "url": "https://dashscope.aliyuncs.com/api/v1/mcps/market-cmapi021863/mcp",
                "headers": {"Authorization": "Bearer sk-*************************************""}
             }
  }
    )


    mcp_tools = await client.get_tools()
   # print(mcp_tools)  
    agent = create_agent(
        model=llm,
        tools = mcp_tools,
        middleware=[before_model_messages],
        debug= True,
    )
    math_response = await agent.ainvoke({"messages": [{"role": "user", "content": "what's (3 + 5) x 12?"}]})
    print("\n math response:", math_response)
    print("\n math response:", math_response["messages"][-1].content)

    weather_response = await agent.ainvoke({"messages": "what is the weather in nyc?"})
    print("\n Weather response:", weather_response)
    print("\n Weather response:", weather_response["messages"][-1].content)


    hz_response = await agent.ainvoke({"messages": "查询一下杭州今日天气"})
    print("\n hz_response response:", hz_response)
    print("\n hz_response response:", hz_response["messages"][-1].content)

    
    logistics_response = await agent.ainvoke({"messages": "查询一快递 434*********"})
    print("\n logistics_response response:", logistics_response)
    print("\n logistics_response response:", logistics_response["messages"][-1].content)

if __name__ == "__main__":
    asyncio.run(main())

相关包版本

复制代码
python 3.12
mcp>=1.24.0
fastmcp>=2.14.1
langchain>=1.0.7
langchain-mcp-adapters>=0.2.1
相关推荐
Codebee8 小时前
能力中心 (Agent SkillCenter):开启AI技能管理新时代
人工智能
聆风吟º9 小时前
CANN runtime 全链路拆解:AI 异构计算运行时的任务管理与功能适配技术路径
人工智能·深度学习·神经网络·cann
uesowys9 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
AI_56789 小时前
AWS EC2新手入门:6步带你从零启动实例
大数据·数据库·人工智能·机器学习·aws
User_芊芊君子9 小时前
CANN大模型推理加速引擎ascend-transformer-boost深度解析:毫秒级响应的Transformer优化方案
人工智能·深度学习·transformer
智驱力人工智能10 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
qq_1601448710 小时前
亲测!2026年零基础学AI的入门干货,新手照做就能上手
人工智能
Howie Zphile10 小时前
全面预算管理难以落地的核心真相:“完美模型幻觉”的认知误区
人工智能·全面预算
人工不智能57710 小时前
拆解 BERT:Output 中的 Hidden States 到底藏了什么秘密?
人工智能·深度学习·bert
盟接之桥10 小时前
盟接之桥说制造:引流品 × 利润品,全球电商平台高效产品组合策略(供讨论)
大数据·linux·服务器·网络·人工智能·制造