MCP客户端/服务器

一个基于Model Context Protocol的客户端和服务器实现,用于处理天气数据和工具调用。

  • by tikoehle
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 tikoehle/mcp_server

使用说明

简单的MCP客户端/服务器

https://modelcontextprotocol.io/quickstart/server

安装 紫外线,在macOS和Linux上。

$ curl -LsSf https://astral.sh/uv/install.sh | sh

然后git克隆仓库。

MCP服务器(stdio)

uv init mcp_server
cd mcp_server/
uv run main.py                        # just to create the .venv, then rm the main.py

uv add "mcp[cli]" httpx               # MCP server Python lib

touch weather_stdio.py                # STDIO server implementation

启动服务器

mcp_server$ uv run weather_stdio.py

使用VSCode+Cline进行测试

将MCP服务器添加到Cline。

Cline -> MCP Servers (icon close to '+') -> Installed -> Add

您得到一个json模板“mcpServers”:{},然后添加新服务器。 以下是cline_mcp_settings.json:

{
  "mcpServers": {
    "weather": {
            "command": "uv",
            "args": [
                "--directory",
                "/home/users/tikoehle/mcp_server",
                "run",
                "weather_stdio.py"
            ]
        }
  }
}

=>Cline显示了2个get_*Tools函数。单击“完成”以返回尝试新服务器的临床任务输入。

测试

键入一个调用这两个工具的任务: “你能告诉我加利福尼亚州旧金山的天气预报吗?” “你能告诉我加利福尼亚州旧金山的天气警报吗?”

=>使用我的两个新的MCP“天气”服务器工具。

注: 本地MCP服务器不应将消息记录到stdout,因为这会干扰协议操作。

MCP客户端

https://modelcontextprotocol.io/quickstart/client

uv init mcp_client
cd mcp_client/
uv run main.py                     # create venv

uv add mcp

touch client_stdio.py              # MCP client impl.

运行客户端

uv运行client.py路径/to/server.py

mcp_client$ uv run client_stdio.py ../mcp_server/weather_stdio.py

MCP检查员

安装Node.js

在MCP服务器计算上,获取 Node.js (当前)用于 Linux 使用 nvm 随着 npm. https://nodejs.org/en/download

cd $HOME

# Download and install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash

# in lieu of restarting the shell
\. "$HOME/.nvm/nvm.sh"

# Download and install Node.js:
nvm install 23

# Verify the Node.js version:
node -v # Should print "v23.10.0".
nvm current # Should print "v23.10.0".

# Verify npm version:
npm -v # Should print "10.9.2".

检查本地开发的服务器

mcp_server$ uv run mcp dev ./weather_stdio.py
Starting MCP inspector...
Proxy server listening on port 3000

🔍 MCP Inspector is up and running at http://localhost:5173 🚀

注:

安装最新版本并启动检查器的另一种方法。

mcp_server$ npx @modelcontextprotocol/inspector@latest

在浏览器中打开MCP检查器客户端UI

http://comp9:5173   --> Connect

注:

如果MCP开发服务器在远程计算上运行,例如comp9,那么MCP检查器客户端UI需要连接到此计算机。

在浏览器中启动UI时,传输类型、命令和参数显示为具有正确的参数。

Transport Type: STDIO
Command: uv
Arguments: run --with mcp mcp run ./weather_stdio.py

或

Transport Type: SSE
URL: http://comp9:8001/sse

注:

MCP开发工具

mcp_server$ uv run mcp

SSE客户端/服务器

服务器

mcp_server$ uv run weather_sse.py      # port 8001

客户端

mcp_client$ uv run client_sse.py http://comp9:8001/sse

Python SDK

https://github.com/modelcontextprotocol/python-sdk/

SSE问题:

https://github.com/modelcontextprotocol/python-sdk/issues/398