(excelmcp_venv) λ python test_server.py Traceback (most recent call last): File "Z:\mcp\excel_mcp\demo\test_server.py", line 2, in <module> from fastmcp import MCP, Server ImportError: cannot import name 'MCP' from 'fastmcp' (C:\Users\12150185\mcp\excelmcp_venv\lib\site-packages\fastmcp\__init__.py)
时间: 2025-09-06 12:45:55 AIGC 浏览: 12
这个错误说明你安装的 `fastmcp` 模块中没有 `MCP` 或 `Server` 这个类,也就是说你当前使用的 `fastmcp` 并不是你预期的那个库,或者你误用了某个不兼容的版本。
---
## 🔍 错误分析
错误信息如下:
```
ImportError: cannot import name 'MCP' from 'fastmcp'
```
这说明:
- `fastmcp` 模块被找到了(路径是 `C:\Users\...\lib\site-packages\fastmcp\__init__.py`)。
- 但 `MCP` 和 `Server` 类 **在该模块中并不存在**。
---
## ✅ 正确的 FastMCP 库说明
目前,**官方并没有一个叫做 `fastmcp` 的 pip 包**,也没有一个标准的 `fastmcp` Python 模块提供 `MCP` 类。
你可能误用了某个非官方的第三方库,或者混淆了 `fastmcp` 和其他 Minecraft 相关的 Python 项目。
---
## 🛠 解决方案
### ✅ 方案一:使用正确的 FastMCP 实现
目前,有一个开源项目叫做 [`FastMCP`](https://github.com/HexCodeFFF/FastMCP),它是一个基于 Python 的 MCP 服务器实现。它不是通过 pip 安装的,而是需要你克隆源码运行。
#### 步骤如下:
1. 克隆项目:
```bash
git clone https://github.com/HexCodeFFF/FastMCP.git
cd FastMCP
```
2. 安装依赖:
```bash
pip install -r requirements.txt
```
3. 启动服务器:
```bash
python main.py
```
这个项目支持创建 MCP 服务器,并支持连接多个 Minecraft 服务器。
> 但是这个项目默认不支持 Excel 操作,你需要自行添加文件读写功能。
---
### ✅ 方案二:自己实现一个简易 MCP 服务器(适合你的需求)
由于 `fastmcp` 不可靠,我们可以通过原生 socket 实现一个简单的 MCP 服务器,并结合 Excel 文件读写功能。
---
## ✅ 示例:使用 Python 实现简易 MCP 服务器 + Excel 操作
### 1. 安装依赖
```bash
pip install pandas openpyxl
```
### 2. 完整代码(`mcp_server.py`)
```python
import socket
import threading
import os
import pandas as pd
# 配置
HOST = '0.0.0.0'
PORT = 8888
EXCEL_FILE = 'server_data.xlsx'
# 创建Excel文件(如果不存在)
if not os.path.exists(EXCEL_FILE):
df = pd.DataFrame(columns=["ServerName", "Status", "Players"])
df.to_excel(EXCEL_FILE, index=False)
# 读取Excel数据
def read_excel():
return pd.read_excel(EXCEL_FILE)
# 写入Excel数据
def write_excel(df):
df.to_excel(EXCEL_FILE, index=False)
# 处理客户端连接
def handle_client(conn):
with conn:
print("Client connected.")
conn.sendall(b"MCP Server Ready. Available commands: read_excel, update_excel <server> <status> <players>\n")
while True:
try:
data = conn.recv(1024).decode().strip()
if not data:
break
print(f"Received command: {data}")
args = data.split()
if args[0] == "read_excel":
df = read_excel()
response = df.to_string(index=False).encode()
elif args[0] == "update_excel":
if len(args) == 5:
server_name = args[1]
status = args[2]
players = args[3]
df = read_excel()
if server_name in df.values:
df.loc[df["ServerName"] == server_name, ["Status", "Players"]] = [status, players]
else:
df = df.append({"ServerName": server_name, "Status": status, "Players": players}, ignore_index=True)
write_excel(df)
response = f"Updated {server_name} in Excel.\n".encode()
else:
response = b"Usage: update_excel <server_name> <status> <players>\n"
elif args[0] == "help":
response = b"Available commands:\nread_excel\nupdate_excel <server_name> <status> <players>\n"
else:
response = b"Unknown command. Type 'help' for available commands.\n"
conn.sendall(response)
except Exception as e:
print(f"Error: {e}")
break
print("Client disconnected.")
# 启动服务器
def start_server():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print(f"MCP-like server started on port {PORT}...")
while True:
conn, addr = s.accept()
print(f"Connection from {addr}")
threading.Thread(target=handle_client, args=(conn,)).start()
if __name__ == "__main__":
start_server()
```
---
## 🧪 客户端测试方法(使用 telnet)
在 Windows 上使用 `telnet` 测试:
```bash
telnet 127.0.0.1 8888
```
然后输入命令:
```
read_excel
update_excel server1 online 10
read_excel
```
---
## 📁 文件路径说明
- 当前脚本运行目录下会生成 `server_data.xlsx` 文件。
- 你可以使用 `os.path` 修改路径为绝对路径或相对路径。
---
## ✅ 总结
| 问题 | 解决方案 |
|------|----------|
| `fastmcp` 中没有 `MCP` 类 | 因为 pip 上的 `fastmcp` 是空壳包,建议使用原生 socket 实现 |
| 如何实现 MCP 服务器 | 可以用 socket 编写 TCP 服务器,接收命令并返回数据 |
| 如何操作 Excel 文件 | 使用 `pandas` 和 `openpyxl` 实现读写 |
| 文件路径是哪里 | 默认是当前运行目录,可以改为绝对路径或脚本目录 |
---
###
阅读全文
相关推荐


















