温度转换工具

FastMCP服务器提供温度转换功能,可通过HTTP传输与AI工具包集成。

  • by cpateldev
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 cpateldev/FastMCP.SimpleDemo

使用说明

太好了——让我们来参观一下建筑 FastMCP服务器 使用简单的温度转换工具,将其暴露在 HTTP传输,然后将其连接进去 VS代码的AI工具包.


设置pip

确保你有 pip 已安装并更新:

python -m ensurepip --upgrade
python -m pip install --upgrade pip

🛠 步骤1:项目设置

  1. 创建一个新文件夹,例如。 FastMCP.SimpleDemo.
  2. 初始化Python环境:
python -m venv .venv
.venv\Scripts\activate # on Windows

source .venv/bin/activate # on macOS/Linux

pip install fastmcp
  1. 检查fastmcp版本
fastmcp --version

output:

FastMCP version:                                                                     2.14.0
MCP version:                                                                         1.24.0
Python version:                                                                      3.11.9
Platform:                                                         Windows-10-10.0.26100-SP0
FastMCP root path: C:\Users\ch15p\AppData\Local\Programs\Python\Python311\Lib\site-packages

🌡 步骤2:定义温度转换工具

FastMCP允许您定义可以暴露给AI代理的工具。
这是一个简单的摄氏度工具↔ 华氏转换:

# my_server.py

from fastmcp import FastMCP

mcp = FastMCP("Temp Conversion MCP Server")

@mcp.tool
def celsius_to_fahrenheit(celsius: float) -> float:
    """Convert Celsius to Fahrenheit."""
    return (celsius * 9/5) + 32

@mcp.tool
def fahrenheit_to_celsius(fahrenheit: float) -> float:
    """Convert Fahrenheit to Celsius."""
    return (fahrenheit - 32) * 5/9

if __name__ == "__main__":
    # Expose via HTTP transport
    #mcp.run(host="0.0.0.0", port=8001)
    mcp.run(transport="http", port=8001)

🌐 步骤3:运行服务器

启动服务器:

fastmcp run tempconversion-mcp-http.py:mcp --transport http --port 8001

output:

╭──────────────────────────────────────────────────────────────────────────────╮
│                                                                              │
│                         ▄▀▀ ▄▀█ █▀▀ ▀█▀ █▀▄▀█ █▀▀ █▀█                        │
│                         █▀  █▀█ ▄▄█  █  █ ▀ █ █▄▄ █▀▀                        │
│                                                                              │
│                                FastMCP 2.14.0                                │
│                                                                              │
│                                                                              │
│                  🖥  Server name: Temp Conversion MCP Server                 │
│                                                                              │
│                  📦 Transport:   HTTP                                        │
│                  🔗 Server URL:  http://127.0.0.1:8001/mcp                   │
│                                                                              │
│                  📚 Docs:        https://gofastmcp.com                       │
│                  🚀 Hosting:     https://fastmcp.cloud                       │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯
[12/14/25 11:19:02] INFO     Starting MCP server 'Temp Conversion MCP Server' with transport 'http' on                server.py:2582
                             http://127.0.0.1:8001/mcp                                                                              
INFO:     Started server process [26956]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit)

它会继续听 http://localhost:8001/mcp.


🔗 步骤4:通过VS代码中的AI工具包连接

  1. 打开 VS Code 随着 AI工具包扩展 安装。
  2. 首选 AI工具包→ MCP服务器→ 添加服务器.
  3. 选择 HTTP传输.
  4. 输入:
    • 名称: Temp Conversion MCP
    • 统一资源定位符: http://localhost:8001/mcp
  5. 保存并连接。

现在,您的AI工具包可以调用 celsius_to_fahrenheit 和 fahrenheit_to_celsius 直接作为MCP工具。


✅ 步骤5:使用VS代码>mcp.json进行测试

  • 添加 .vscode\mcp.json 在项目的根目录下创建文件,并在其中添加以下内容。
{
  "servers": {
    "tempconversion-mcp-http": {
        "type": "http",
        "url": "http://127.0.0.1:8001/mcp"            
    },
    "tempconversion-mcp-stdio": {
      "type": "stdio",
      "command": "uv",
      "cwd": "${workspaceFolder}",
      "args": [
        "run",
        "tempconversion-mcp-stdio.py"
      ]
    },
    "tempconversion-mcp-stdio-debug": {
      "type": "stdio",
      "command": "uv",
      "cwd": "${workspaceFolder}",
      "args": [
        "run",
        "--",
        "python",
        "-m",
        "debugpy",
        "--listen",
        "0.0.0.0:5678",
        "tempconversion-mcp-stdio.py"
      ]
    }
  }
}
  • 使用MCP.json中的MCP工具命令启动/停止/重新启动fastmcp服务器

✅ 步骤6:在AI工具包中测试

  • 打开AI Toolkit聊天。
  • 类型:
    Use the Temp Conversion MCP server to convert 100 Celsius to Fahrenheit.
    
  • 工具应作出响应 212.0.

让我们添加一个 清单文件(mcp.json) 因此,VS Code中的AI Toolkit可以自动发现您的FastMCP服务器。


📄 步骤6:创建 mcp.json

在项目根目录中(fastmcp-temp-server),添加一个名为的文件 mcp.json:

{
  "name": "Temp Conversion MCP",
  "version": "1.0.0",
  "description": "A simple FastMCP server for temperature conversion tools.",
  "transport": {
    "type": "http",
    "url": "http://localhost:8001"
  },
  "tools": [
    {
      "name": "celsius_to_fahrenheit",
      "description": "Convert Celsius to Fahrenheit",
      "input_schema": {
        "type": "object",
        "properties": {
          "celsius": { "type": "number" }
        },
        "required": ["celsius"]
      },
      "output_schema": {
        "type": "number"
      }
    },
    {
      "name": "fahrenheit_to_celsius",
      "description": "Convert Fahrenheit to Celsius",
      "input_schema": {
        "type": "object",
        "properties": {
          "fahrenheit": { "type": "number" }
        },
        "required": ["fahrenheit"]
      },
      "output_schema": {
        "type": "number"
      }
    }
  ]
}

📂 步骤8:放置清单

  • 保存 mcp.json 在 同一文件夹 如你的 server.py.
  • 当您添加服务器时,AI Toolkit将查找此清单。

🔗 步骤9:在AI工具包中连接

  1. 打开VS代码→ AI工具包。
  2. 首选 MCP服务器→ 添加服务器→ 从舱单导入.
  3. 选择您的 mcp.json.
  4. AI Toolkit将使用定义的工具自动注册服务器。

✅ 步骤10:测试

  • 在AI Toolkit聊天中,尝试:
    Convert 0 Celsius to Fahrenheit using Temp Conversion MCP.
    
  • 它应该会回来 32.0.

🔗 参考文献

快速多通道处理

FastMCP快速入门

如何在Python中创建MCP服务器

FastMCP云