太好了——让我们来参观一下建筑 FastMCP服务器 使用简单的温度转换工具,将其暴露在 HTTP传输,然后将其连接进去 VS代码的AI工具包.
______________________________________________________________________
🛠 步骤1:项目设置
- 创建一个新文件夹,例如。
FastMCP.SimpleDemo. - 初始化Python环境:
python -m venv .venv
source .venv/bin/activate # or .venv\Scripts\activate on Windows
pip install fastmcp- 检查fastmcp版本
fastmcp --versionoutput:
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 8001output:
╭──────────────────────────────────────────────────────────────────────────────╮
│ │
│ ▄▀▀ ▄▀█ █▀▀ ▀█▀ █▀▄▀█ █▀▀ █▀█ │
│ █▀ █▀█ ▄▄█ █ █ ▀ █ █▄▄ █▀▀ │
│ │
│ 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工具包连接
- 打开 VS Code 随着 AI工具包扩展 安装。
- 首选 AI工具包→ MCP服务器→ 添加服务器.
- 选择 HTTP传输.
- 输入:
- 名字: Temp Conversion MCP - 统一资源定位符: http://localhost:8001/mcp
- 保存并连接。
现在,您的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工具包中连接
- 打开VS代码→ AI工具包。
- 首选 MCP服务器→ 添加服务器→ 从舱单导入.
- 选择您的
mcp.json. - AI Toolkit将使用定义的工具自动注册服务器。
______________________________________________________________________
✅ 步骤10:测试
- 在AI Toolkit聊天中,尝试:
Convert 0 Celsius to Fahrenheit using Temp Conversion MCP.- 它应该会回来
32.0.
