启动FastMCP Inspector时遭遇JSONRPC Validation Error问题求助
问题描述
跟随视频教程搭建示例MCP服务器,功能为通过MCP发起API调用。启动MCP Inspector时程序未自动启动,抛出JSONRPC验证错误。
环境信息
- Python 3(使用虚拟环境
.venv) - 用uv安装了
mcp[cli]、requests、os依赖 - 终端环境:zsh / bash
错误输出
[05/08/26 01:49:08] ERROR Received exception from stream: 1 validation error for server.py:707 JSONRPCMessage Invalid JSON: EOF while parsing a value at line 2 column 0 [type=json_invalid, input_value='\n', input_type=str] For further information visit https://errors.pydantic.dev/2.13/v/json_invalid
启动命令
(deepdive) ➜ deepdive git:(main) ✗ mcp dev <my_file_name.py>
相关代码
from mcp.server.fastmcp import FastMCP import requests import os # 使用MCP发起API调用的示例 mcp = FastMCP("Crypto") @mcp.tool() def get_crytocurrency_price(crypto: str) -> str: """ 获取加密货币价格 参数: crypto: 加密货币标识(例如 'bitcoin', 'ethereum') """ try: url = f"https://api.coingecko.com/api/v3/simple/price" params = {"ids": crypto.lower(), "vs_currencies": "eur" } response = requests.get(url, params = params, timeout = 10 ) response.raise_for_status() data = response.json() price = data.get(crypto.lower(), {}).get("eur") if price is not None: return f"当前货币价格为 {price} 欧元。" else: return f"无法查询到 {crypto} 的价格。" except Exception as e: return f"查询 {crypto} 价格时出错:{e}" mcp.run()
已尝试的排查步骤
- 确认虚拟环境中所有依赖已正确安装
- 尝试调试导入内容,但错误提示无法定位具体代码行
- 手动测试API调用,返回数据正常
解决方案
修复版本兼容性问题
这个JSONRPC解析错误大多是MCP客户端与服务端版本不匹配导致的。卸载当前包后重新安装稳定版本:uv uninstall mcp[cli] uv install mcp[cli]==0.12.0版本号可根据教程或官方文档调整,优先选择教程对应的版本。
修正代码拼写错误
工具函数名get_crytocurrency_price存在拼写错误(应为cryptocurrency),虽不直接引发JSONRPC错误,但可能导致MCP Inspector识别工具异常,修正后可避免潜在问题。重置终端环境后重启服务
关闭当前终端窗口,重新激活虚拟环境再启动服务:source .venv/bin/activate mcp dev <my_file_name.py>检查Inspector连接配置
如果是手动启动Inspector而非自动打开,确认连接地址和端口与服务端一致,默认MCP服务运行在http://localhost:8000。
内容的提问来源于stack exchange,提问作者Eric Tao
相关产品推荐
相关产品推荐

