模型上下文协议服务器

该项目是一个演示如何创建和使用基于模型上下文协议(MCP)的代理的验证性概念,适用于与AI应用程序如Claude Desktop和Gemini CLI的集成。

  • by michelcaradec
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 michelcaradec/mcp-poc

使用说明

模型上下文协议概念证明

Table of contents

摘要

这个概念验证项目旨在演示如何使用 模型上下文协议.

这是受到这篇文章的启发 构建MCP服务器.

独自创立

项目设置:

uv init --python 3.10
uv add "fastmcp aiofiles"

环境初始化:

uv sync --extra dev

演示工作流程

  1. 启动 MCP服务器.

    uv run ./src/weather/main.py --transport streamable-http
    
  2. 开始 克劳德桌面版.

[!提示] 转到“开发人员设置”,检查MCP服务器是否正确 配置 跑步。

  1. 创建新聊天。

[!提示] 点击“搜索和工具”按钮,检查MCP服务器“天气”是否已激活。
展开项目以查看可用和已激活的工具。 在中禁用“Web搜索”以强制使用代理。

  1. 运行演示提示符 get_demonstration_prompt.

[!提示] 点击按钮➕, 展开“从天气添加”项,然后选择提示“get_demonstration_front”。
点击文本文件“get_demonstration_front.txt”查看提示内容。
点击按钮⬆️ 或在提示编辑器中按[Enter]运行提示。

[!注意] 如果需要执行工具,AI应用程序将请求授权。
通过单击“允许一次”或“始终允许”按钮来允许执行。

  1. 通过提示获取另一个地方的天气 get_weather_prompt.
  2. 通过提示获取一些建议 get_weather_advice_prompt.

[!注意] 我们看到代理的兴趣,这是由LLM触发的,以满足用户请求,需要天气信息,但不是直接关于天气的信息。

  1. 通过提示获取另一条建议 get_where_should_i_go_prompt.

[!注意] 两名代理人将被处决: get_favorite_cities_prompt 要获取最喜欢的城市列表,那么 get_weather_prompt 每个城市。

  1. 退出克劳德桌面。
  2. 停止MCP服务器。

MCP服务器

要手动启动MCP服务器,请执行以下操作:

uv run ./src/weather/main.py [--transport {stdio|sse|streamable-http}]

它将监听来自MCP主机的消息。

[!重要] 不要忘记停止MCP服务器。

MCP检查员

这 MCP检查员 允许对MCP服务器公开的工具、提示和资源进行可视化测试。

  1. 运行MCP检查器:

    uv run fastmcp dev ./src/weather/main.py
    

[!提示] 看 fastmcp CLI选项位于 https://fastmcp.wiki/en/patterns/cli#fastmcp-dev.

  1. 点击“连接”按钮。
  2. 转到“工具”选项卡,然后单击“列出工具”。

人工智能应用

克劳德桌面版

克劳德桌面-设置

克劳德桌面-配置MCP服务器

  1. 打开Claude配置文件:

    nano ~/.config/Claude/claude_desktop_config.json
    
  2. 声明MCP服务器:

    随着运输 stdio:

    {
        "mcpServers": {
            "weather": {
                "command": "${HOME}/.local/bin/uv",
                "args": [
                    "--directory",
                    "${HOME}/Projects/mcp-poc",
                    "run",
                    "./src/weather/main.py"
                ]
            }
        }
    }
    

    随着运输 streamable-http:

    {
        "mcpServers": {
            "weather": {
                "command": "npx",
                "args": [
                    "-y",
                    "mcp-remote",
                    "http://127.0.0.1:8000/mcp/"
                ],
                "env": {
                    "MCP_TRANSPORT_STRATEGY": "http-only"
                }
            }
        }
    }
    

[!提示] 您可能需要在命令字段中输入uv可执行文件的完整路径。

[!提示] 这 JSON配置 可以使用FastMCP CLI工具生成: uv run fastmcp install mcp-json ./src/weather/main.py.

[!提示] 看 使用FastMCP(STDIO、HTTP和SSE)构建和公开MCP服务器 和 克劳德代码MCP指南.

[!提示] 运输 streamable-http 更便于调试MCP服务器。

[!重要] 需要付费订阅Claude才能添加远程服务器。

用法

  1. 启动MCP服务器(此处为传输 streamable-http):

    uv run ./src/weather/main.py --transport streamable-http
    
  2. 启动Claude桌面。

  3. 写一个提示:

    雷德蒙德的天气怎么样?

[!提示] 在“搜索和工具”菜单中禁用“Web搜索”以强制使用代理。

[!提示] 要检查侦听端口,请执行以下操作: netstat -tunlp.

Gemini CLI

Gemini CLI-设置

  1. 安装:

    # Global installation
    npm install -g @google/gemini-cli
    
    # Instant run (no installation)
    npx https://github.com/google-gemini/gemini-cli
    
  2. 启动CLI:

    gemini
    

Gemini CLI-配置MCP服务器

  1. 打开Gemini CLI配置文件:

    nano ~/.gemini/settings.json
    
  2. 声明MCP服务器:

    随着运输 stdio:

    {
        "mcpServers": {
            "weather": {
                "command": "${HOME}.local/bin/uv",
                "args": [
                    "--directory",
                    "${HOME}Projects/mcp-poc",
                    "run",
                    "src/weather/main.py"
                ],
                "trust": false
            }
        }
    }
    

[!提示] 您可能需要在命令字段中输入uv可执行文件的完整路径。

[!提示] 看 带Gemini CLI的MCP服务器.

资源