5分钟了解MCP如何使用(Model Context Protocol)

5分钟了解MCP如何使用(Model Context Protocol)

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. from offical site

MCP 是一种开放协议,由Anthropic在24年11月提出. 它标准化了应用程序向大型语言模型提供上下文的方式。将 MCP 想象成 AI 应用程序的 USB-C 端口。就像 USB-C 为您的设备提供了一种标准化的方式来连接各种外围设备和配件一样,MCP 为 AI 模型提供了一种标准化的方式来连接不同的数据源和工具。

MCP实际上就是为LLM的function call 套用了一层接口(协议), 使得用户可以在与模型的交互中按照这套协议调用自己的本地工具(服务).这里要强调本地, 是因为实际上MCP机制全部都是在本地发生的.这样的设计可以有效地保护用户的隐私, 用户在调用function时, 不需要把apikey提供给LLM供应商.

MCP架构

在大模型应用里,建立多个MCP Client. 每个实例负责访问对应的MCP Server.MCP Server负责真实的function call功能交互, 如(访问数据库, API等)

  • MCP 主机:像 Claude Desktop、IDE 或 AI 工具这样的程序,它们希望通过 MCP 访问数据
  • MCP 客户端:与服务器保持 1:1 连接的协议客户端
  • MCP 服务器:轻量级程序,每个程序通过标准化的模型上下文协议暴露特定的功能
  • 本地数据源:MCP 服务器可以安全访问的计算机文件、数据库和服务
  • 远程服务:通过互联网(例如,通过 API)可用的外部系统,MCP 服务器可以连接到

MCP交互过程

用MCP的机制查询天气

现在我们搭建一个请求天气预报的MCP Server的例子, 看看具体流程是什么样子. 详细的内容参考官网例子.

1. 安装环境

我这里使用的是python sdk, 需要安装uv来启动server

2. 开发weather服务

    1. 绑定服务名
python 复制代码
from typing import Any
import httpx
from mcp.server.fastmcp import FastMCP

# Initialize FastMCP server
mcp = FastMCP("weather")
    1. 实际api请求与输出格式化
python 复制代码
async def make_nws_request(url: str) -> dict[str, Any] | None:
    """Make a request to the NWS API with proper error handling."""
    headers = {
        "User-Agent": USER_AGENT,
        "Accept": "application/geo+json"
    }
    async with httpx.AsyncClient() as client:
        try:
            response = await client.get(url, headers=headers, timeout=30.0)
            response.raise_for_status()
            return response.json()
        except Exception:
            return None

def format_alert(feature: dict) -> str:
    """Format an alert feature into a readable string."""
    props = feature["properties"]
    return f"""
Event: {props.get('event', 'Unknown')}
Area: {props.get('areaDesc', 'Unknown')}
Severity: {props.get('severity', 'Unknown')}
Description: {props.get('description', 'No description available')}
Instructions: {props.get('instruction', 'No specific instructions provided')}
"""
    1. 接口开发
python 复制代码
@mcp.tool()
async def get_alerts(state: str) -> str:
    """Get weather alerts for a US state.

    Args:
        state: Two-letter US state code (e.g. CA, NY)
    """
    url = f"{NWS_API_BASE}/alerts/active/area/{state}"
    data = await make_nws_request(url)

    if not data or "features" not in data:
        return "Unable to fetch alerts or no alerts found."

    if not data["features"]:
        return "No active alerts for this state."

    alerts = [format_alert(feature) for feature in data["features"]]
    return "\n---\n".join(alerts)

@mcp.tool()
async def get_forecast(latitude: float, longitude: float) -> str:
    """Get weather forecast for a location.

    Args:
        latitude: Latitude of the location
        longitude: Longitude of the location
    """
    # First get the forecast grid endpoint
    points_url = f"{NWS_API_BASE}/points/{latitude},{longitude}"
    points_data = await make_nws_request(points_url)

    if not points_data:
        return "Unable to fetch forecast data for this location."

    # Get the forecast URL from the points response
    forecast_url = points_data["properties"]["forecast"]
    forecast_data = await make_nws_request(forecast_url)

    if not forecast_data:
        return "Unable to fetch detailed forecast."

    # Format the periods into a readable forecast
    periods = forecast_data["properties"]["periods"]
    forecasts = []
    for period in periods[:5]:  # Only show next 5 periods
        forecast = f"""
{period['name']}:
Temperature: {period['temperature']}°{period['temperatureUnit']}
Wind: {period['windSpeed']} {period['windDirection']}
Forecast: {period['detailedForecast']}
"""
        forecasts.append(forecast)

    return "\n---\n".join(forecasts)
    1. main
ini 复制代码
if __name__ == "__main__":
    # Initialize and run the server
    mcp.run(transport='stdio')

到这里MCP Server就开发好了.

3. 修改配置

这里以Claude Desktop为例. 实际上用MCP的app有很多(如, ClaudeDesktop, Cursor, ...)

  1. 打开Claude设置Settings
  2. 选择Developer的设置
  3. 这是一个json文件, 加入MCP Server的启动配置

4. 重启Claude Desktop

重启Claude Desktop, 然后就可以小锤子选项了.

5. MCP 工具调用

e2de0b04b82e2d3dc33a5fb259e0d9e4.png

对比其他机制

这里对比一下模型版Chatglm的工具调用.

MCP Chatglm
初始化 app init MCP client tool register注册
调用方式 MCP client-server调用 toolCall调用
架构 Client-server 有状态, 可以保持连接 Function call 无状态, 单次调用

总结

总体来说, 就是MCP进行一层tool function call的完整封装. 相对于其他智能对话产品, MCP提供了用户一种自定义function call的解决方案. 由于是在本地调用api, 保障了用户的隐私(apikey). 但MCP主要还是面向于开发者用户. 需要用户自行搭建MCP Server服务以及后续链路. 对于一般用户的成本还是略高.

引用

  1. modelcontextprotocol.io/
  2. github.com/modelcontex...
  3. onevcat.com/2025/02/mcp...
  4. github.com/THUDM/ChatG...
  5. open.bigmodel.cn/dev/howuse/...

本文使用 markdown.com.cn 排版

相关推荐
掘我的金11 小时前
MCP生产部署实战:从开发到上线的完整指南
llm·mcp
bytebeats13 小时前
在本地 LLM 上运行MCP
mcp
bytebeats13 小时前
MCP + A2A 可能是自互联网以来软件领域最大的变革
mcp
bytebeats15 小时前
强大的代理通信其实是 A2A + MCP + LangChain
langchain·mcp
win4r19 小时前
🚀颠覆传统编程!Cursor 1.0+Claude Task Master+Gemini 2.5 Pro 0605开发效率提升10倍!从产品需求文档生成到子任
claude·cursor·gemini
掘我的金19 小时前
MCP 学习系列②:理解 MCP 的核心结构与思维模型
llm·mcp
福宝plus19 小时前
只需1美元薅ChatGPT Team手把手的教程,教你如何解决“你不符合参与此促销活动折扣资格”或“您的银行卡被拒绝了”附上ChatGPT Team怎么取消订阅
chatgpt·openai·claude
前端搬砖小助手20 小时前
从 Stdio 到 HTTP SSE,在 APIPark 托管 MCP Server
mcp·apipark·ai网关·ai gateway
小奏技术1 天前
基于 Spring AI 和 MCP:用自然语言查询 RocketMQ 消息
后端·aigc·mcp
柒崽2 天前
如何搭建一个MCP服务,然后在Cursor中调用,半小时,彻底掌握MCP
mcp