MCP极简入门:超快速上手运行简单的MCP服务和MCP客户端

MCP是什么?

首先我们快速过一下MCP的基本概念,接着我们会通过一个简单的天气服务的教程,来上手学会使用MCP服务和在主机运行服务。本文根据官方教程改编。

1. MCP的基本概念

MCP(Model Context Protocol,模型上下文协议)是一个开放协议,旨在标准化应用程序如何向大型语言模型(LLM)提供上下文。它允许LLM与外部数据源和工具无缝集成,从而使AI模型能够访问实时数据并执行更复杂的任务。

官方MCP Github主页
官方文档Introduction
支持MCP特性的客户端列表

2. MCP的架构

MCP的核心组件包括:

  • 主机(Host):运行LLM的应用程序(如Claude Desktop),负责发起与MCP服务器的连接。
  • 客户端(Client):在主机应用程序内部运行,与MCP服务器建立1:1连接。
  • 服务器(Server):提供对外部数据源和工具的访问,响应客户端的请求。
  • LLM:大型语言模型,通过MCP获取上下文并生成输出。
  • 工作流程
    1. 主机启动客户端。
    2. 客户端连接到MCP服务器。
    3. 服务器提供资源、提示或工具。
    4. LLM使用这些信息生成响应。
      在这里插入图片描述

3. MCP的原语

MCP通过三种主要原语(Primitives)增强LLM的功能,理解这些原语是编写MCP的关键:

  1. 提示(Prompts):预定义的指令或模板,指导LLM如何处理输入或生成输出。
  2. 资源(Resources):提供额外上下文的结构化数据,例如文件或数据库内容。
  3. 工具(Tools):可执行的函数,允许LLM执行操作(如查询API)或检索信息。
  • 关键点:这些原语是MCP的核心,决定了服务器能为LLM提供什么能力。

MCP Server 构建一个简单的MCP服务器

在我们的示例中,使用 Claude for Desktop 作为客户端,自己编写python文件作为服务端,在 Claude Desktop 里调用server.py。

先决条件

  • 已安装 python 3.10 或更高
  • 已安装 Claude for Desktop

1. 安装uv,设置环境变量

打开 Powershell,输入如下命令:

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

打开系统高级环境变量,在 Path 将uv路径添加进去:

C:\Users\windows\.local\bin

在这里插入图片描述

重启 Powershell 。
在命令行输入 uv --version , 能返回版本信息就算安装成功了:

在这里插入图片描述

2. 创建和设置项目

打开 Powershellcd 到你想要创建项目的目录位置,如:

在这里插入图片描述

接着依次输入以下命令:

# Create a new directory for our project
uv init weather
cd weather

# Create virtual environment and activate it
uv venv
.venv\Scripts\activate

# Install dependencies
uv add mcp[cli] httpx

# Create our server file。new-item 是powershell 命令,用于创建文件
new-item weather.py

3. 添加代码

将以下代码整个复制到 weather.py

from typing import Any  
import httpx  
from mcp.server.fastmcp import FastMCP  
  
# Initialize FastMCP server  
mcp = FastMCP("weather")  
  
# Constants  
NWS_API_BASE = "https://api.weather.gov"  
USER_AGENT = "weather-app/1.0"  
  
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')}  
"""  
  
@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)  
  
if __name__ == "__main__":  
    # Initialize and run the server  
    mcp.run(transport='stdio')

如果代码里提示依赖错误,安装对应的包就好。

4. 运行服务

打开 Claude for Desktop , 点击左上角菜单 —— File —— Settings —— Developer

点击 Edit Config ,就会在 C:\Users\windows\AppData\Roaming\Claude 目录下自动创建 claude_desktop_config.json 文件。

打开 claude_desktop_config.json , 添加如下代码:

{
    "mcpServers": {
        "weather": {
            "command": "uv",
            "args": [
                "--directory",
                "T:\\PythonProject\\weather",
                "run",
                "weather.py"
            ]
        }
    }
}

其中路径为在上一步创建的weather目录, 使用绝对路径

这会告诉 Claude for Desktop ,

  • 我们的服务名叫 weather ,
  • 通过 uv --directory T:\\PythonProject\\weather run weather 来启动服务。

保存文件。

5. 在Claude中使用服务

打开任务管理器,将 Claude 结束任务,彻底关掉。
重新打开 Claude for Desktop

如果在Claude的对话框下看到了一把锤子,说明我们的MCP服务配置成功了。
在这里插入图片描述

点击锤子能看到:
在这里插入图片描述

在设置页显示如下:
在这里插入图片描述

下面测试服务:

在对话框输入:what’s the weather in NY

在这里插入图片描述

服务配置成功啦!

MCP Client

要使用Claude API, 需要充值购买credits
否则请求会报Error: Error code: 403 - {‘error’: {‘type’: ‘forbidden’, ‘message’: ‘Request not allowed’}}

1. 创建和设置项目

前期的步骤与上文介绍的一致,先决条件和uv的安装看 MCP Server 部分。
打开Powershell , cd 到python项目的目录下,依次输入如下命令:

# Create project directory
uv init mcp-client
cd mcp-client

# Create virtual environment
uv venv

# Activate virtual environment
# On Windows:
.venv\Scripts\activate
# On Unix or MacOS:
source .venv/bin/activate

# Install required packages
uv add mcp anthropic python-dotenv

# Create our main file
new-item client.py

2. 配置API_KEY

new-item .env

打开.env文件,复制以下代码:

ANTHROPIC_API_KEY=<your key here>

Claude控制台创建KEY(需充值才能用),将API Key复制到.env (确保key的安全,不要分享出去!)

.env文件添加到.gitignore , 在 powershell 输入以下命令:

echo ".env" >> .gitignore

3. 添加代码

import asyncio  
from typing import Optional  
from contextlib import AsyncExitStack  
  
from mcp import ClientSession, StdioServerParameters  
from mcp.client.stdio import stdio_client  
  
from anthropic import Anthropic  
from dotenv import load_dotenv  
  
load_dotenv()  # load environment variables from .env  
  
class MCPClient:  
    def __init__(self):  
        # Initialize session and client objects  
        self.session: Optional[ClientSession] = None  
        self.exit_stack = AsyncExitStack()  
        self.anthropic = Anthropic()  
    # methods will go here  
  
    async def connect_to_server(self, server_script_path: str):  
        """Connect to an MCP server  
  
        Args:            server_script_path: Path to the server script (.py or .js)        """        is_python = server_script_path.endswith('.py')  
        is_js = server_script_path.endswith('.js')  
        if not (is_python or is_js):  
            raise ValueError("Server script must be a .py or .js file")  
  
        command = "python" if is_python else "node"  
        server_params = StdioServerParameters(  
            command=command,  
            args=[server_script_path],  
            env=None  
        )  
  
        stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))  
        self.stdio, self.write = stdio_transport  
        self.session = await self.exit_stack.enter_async_context(ClientSession(self.stdio, self.write))  
  
        await self.session.initialize()  
  
        # List available tools  
        response = await self.session.list_tools()  
        tools = response.tools  
        print("\nConnected to server with tools:", [tool.name for tool in tools])  
  
    async def process_query(self, query: str) -> str:  
        """Process a query using Claude and available tools"""  
        messages = [  
            {  
                "role": "user",  
                "content": query  
            }  
        ]  
  
        response = await self.session.list_tools()  
        available_tools = [{  
            "name": tool.name,  
            "description": tool.description,  
            "input_schema": tool.inputSchema  
        } for tool in response.tools]  
  
        # Initial Claude API call  
        response = self.anthropic.messages.create(  
            model="claude-3-5-sonnet-20241022",  
            max_tokens=1000,  
            messages=messages,  
            tools=available_tools  
        )  
  
        # Process response and handle tool calls  
        tool_results = []  
        final_text = []  
  
        assistant_message_content = []  
        for content in response.content:  
            if content.type == 'text':  
                final_text.append(content.text)  
                assistant_message_content.append(content)  
            elif content.type == 'tool_use':  
                tool_name = content.name  
                tool_args = content.input  
  
                # Execute tool call  
                result = await self.session.call_tool(tool_name, tool_args)  
                tool_results.append({"call": tool_name, "result": result})  
                final_text.append(f"[Calling tool {tool_name} with args {tool_args}]")  
  
                assistant_message_content.append(content)  
                messages.append({  
                    "role": "assistant",  
                    "content": assistant_message_content  
                })  
                messages.append({  
                    "role": "user",  
                    "content": [  
                        {  
                            "type": "tool_result",  
                            "tool_use_id": content.id,  
                            "content": result.content  
                        }  
                    ]  
                })  
  
                # Get next response from Claude  
                response = self.anthropic.messages.create(  
                    model="claude-3-5-sonnet-20241022",  
                    max_tokens=1000,  
                    messages=messages,  
                    tools=available_tools  
                )  
  
                final_text.append(response.content[0].text)  
  
        return "\n".join(final_text)  
  
    async def chat_loop(self):  
        """Run an interactive chat loop"""  
        print("\nMCP Client Started!")  
        print("Type your queries or 'quit' to exit.")  
  
        while True:  
            try:  
                query = input("\nQuery: ").strip()  
  
                if query.lower() == 'quit':  
                    break  
  
                response = await self.process_query(query)  
                print("\n" + response)  
  
            except Exception as e:  
                print(f"\nError: {str(e)}")  
  
    async def cleanup(self):  
        """Clean up resources"""  
        await self.exit_stack.aclose()  
  
async def main():  
    if len(sys.argv) < 2:  
        print("Usage: python client.py <path_to_server_script>")  
        sys.exit(1)  
  
    client = MCPClient()  
    try:  
        await client.connect_to_server(sys.argv[1])  
        await client.chat_loop()  
    finally:  
        await client.cleanup()  
  
if __name__ == "__main__":  
    import sys  
    asyncio.run(main())

如果开头anthropic报错,安装anthropic就好。

4. 运行Client

这里我们使用上文创建的mcp服务weather
在powershell输入:

uv run client.py T:/PythonProject/weather/weather.py

接着,我们就可以在 Query 输入问题了。

至此,我们的第一个MCP服务端和客户端编写完成。


<think>好的,我现在需要帮助用户查找关于Traefik插件的使用说明、配置示例。首先,用户提到他们想了解与Traefik插件相关的信息,特别是使用说明、配置示例。根据用户提供的引用内容,我需要整理这些信息,并按照用户的要求进行结构化回答。 首先,我要回顾用户提供的引用内容。引用[1]提到了在静态配置文件中添加插件的配置示例,引用[3][5]介绍了GitHub上的示例插件demo,说明插件是通过Yaegi动态执行的,并提到了开发指南。引用[4]提到了使用Helm chart部署Traefik,这可能与插件的安装有关。引用[2]可能是一个Terraform的示例,但具体内容不明确,暂时可能用不上。 接下来,我需要确定用户的需求:他们需要知道如何配置使用Traefik插件,包括具体的步骤示例。根据引用内容,可以分步骤说明配置方法,比如静态配置文件中的设置,以及插件的开发或使用现有插件的步骤。可能还需要提到动态加载注意事项,如实验性功能、版本兼容性等。 需要确保回答符合用户的要求,结构清晰,分步骤说明。同时,要引用提供的引用内容,正确标注引用标识。例如,引用[1]中的配置示例,引用[3]关于GitHub仓库的信息,引用[4]提到的Helm安装方法,以及引用[5]的项目介绍部分。 另外,用户可能对插件的实际应用场景如何开发自己的插件感兴趣,所以可能需要包含这些信息。还要生成相关问题,如如何开发自定义插件、动态加载机制、性能影响等。 需要注意的是,用户提供的引用中有部分内容可能不直接相关,比如引用[2]的Terraform示例,但如果没有更相关的信息,可以暂时忽略。确保回答中的引用准确对应到相关的部分。 总结回答结构: 1. 插件配置步骤,包括静态配置文件示例Helm安装。 2. 使用现有插件的步骤,比如demo插件的安装。 3. 开发自定义插件的基本流程。 4. 注意事项,如实验性功能、版本要求等。 5. 相关问题部分,生成相关问题。 现在需要将这些信息整合成自然的中文回答,正确引用来源,并确保LaTeX格式符合要求,虽然用户的问题可能不涉及数学公式,但如果有变量或代码,需要使用正确的格式。例如,配置示例中的代码块需要用反引号包裹,并注明语言类型。 检查是否有遗漏的信息,比如引用[3]提到的Yaegi解释器,可能需要解释动态加载机制。同时,引用[5]提到插件是扩展Traefik功能的强大工具,可以用于流量管理,这部分可以在应用场景中提及。 最后,生成的相关问题需要与Traefik插件的使用、开发、配置相关,确保问题有针对性,能够引导用户进一步探索。</think>### Traefik 插件使用说明与配置示例 #### 1. 插件配置基础步骤 Traefik 插件通过静态配置文件或动态加载实现。以下为静态配置示例(需在 Traefik 的 YAML 配置文件中添加): ```yaml experimental: localPlugins: example: moduleName: github.com/traefik/plugindemo # 插件模块路径[^1] ``` 此配置声明了一个名为 `example` 的本地插件,指向 GitHub 上的示例仓库[^3]。 若使用 Helm 安装 Traefik,需在 `values.yaml` 中启用插件支持: ```yaml experimental: plugins: enabled: true # 启用插件功能[^4] ``` #### 2. 使用现有插件(以 demo 插件为例) 1. **安装插件** 通过修改 Traefik 部署配置,将插件代码编译到 Traefik 二进制文件中,或通过动态加载(需 Yaegi 支持)。 2. **配置路由规则** 在 `IngressRoute` 中引用插件: ```yaml apiVersion: traefik.io/v1alpha1 kind: Middleware metadata: name: demo-plugin spec: plugin: example: # 插件名称 headerName: "X-Custom-Header" headerValue: "Hello from Plugin" ``` #### 3. 开发自定义插件 1. **代码结构** 遵循 Traefik 插件接口规范,实现 `New` 初始化方法 `Handle` 请求处理逻辑[^3]。 2. **动态加载** 利用 Yaegi 解释器实时加载插件(无需重启 Traefik): ```go // 示例插件逻辑 func New(ctx context.Context, config *Configuration) (http.Handler, error) { return &demoPlugin{config}, nil } ``` #### 4. 注意事项 - **实验性功能**:插件功能标记为实验性,需在配置中显式启用[^4]。 - **版本兼容性**:确认 Traefik 版本支持插件(建议 v2.3+)[^4]。 - **安全限制**:动态加载插件需注意代码安全性,建议审核第三方插件[^5]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值