Skip to content

添加自然语言生成SUMO路网和车流功能#74

Merged
donghaiwang merged 9 commits into
OpenHUTB:mainfrom
Dame-time-0034:main
Jul 4, 2026
Merged

添加自然语言生成SUMO路网和车流功能#74
donghaiwang merged 9 commits into
OpenHUTB:mainfrom
Dame-time-0034:main

Conversation

@Dame-time-0034

Copy link
Copy Markdown
Contributor

No description provided.

@donghaiwang

Copy link
Copy Markdown
Member

增加的功能需要在 常用对话中添加

原有的功能需要保持没有问题

Comment thread llm/main_ai.py

# 车辆相关关键词
vehicle_keywords = ['车', '车辆', '汽车', '生成车', '创建车', '来车', '加车', '添加车辆']
# 行人相关关键词(更精确,避免误判)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前的注释一般不删除,删除的话需要说明下,后面类似的都是

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修改的内容 为页面显示的改变,而不是所提到的注释的删除

Comment thread llm/main_ai.py
@donghaiwang

Copy link
Copy Markdown
Member

前面提到的3个问题都没有解决或者回复

@Dame-time-0034

This comment was marked as outdated.

@donghaiwang

Copy link
Copy Markdown
Member

增加的功能需要在 常用对话中添加

原有的功能需要保持没有问题

之前提到的这个还没结解决

@donghaiwang

Copy link
Copy Markdown
Member

直接运行mcp.bat后,在浏览器点击第一个指令
提示没有成功:
image

@donghaiwang

This comment was marked as resolved.

@Dame-time-0034

Copy link
Copy Markdown
Contributor Author
image 56112597-8a58-4f25-ac72-5def2dd7ba8d 在本设备中是使用cmd打开的FastMCP,可以正常运行,如上图所示。按钮会按要求修改

@donghaiwang

donghaiwang commented Jul 2, 2026

Copy link
Copy Markdown
Member

点击第二个按钮后,后台输出为:

2026-07-02 20:38:19 | INFO     | main_ai:chat:2551 - 💬 用户消息: 生成一个4x4网格路网,跑300秒,每3秒发一辆车
2026-07-02 20:38:21 | INFO     | main_ai:chat:2561 - 🔧 检测到 1 个FastMCP工具调用
2026-07-02 20:38:21 | INFO     | main_ai:chat:2564 - 🔨 执行FastMCP工具: generate_sumo_network
2026-07-02 20:38:21 | INFO     | main_ai:execute_fastmcp_tool_call:1866 - 🔧 执行FastMCP工具: generate_sumo_network
2026-07-02 20:38:21 | INFO     | main_ai:execute_fastmcp_tool_call:1867 - 📝 参数: {'grid_x': 4, 'grid_y': 4, 'duration': 300, 'rate': 3.0}
2026-07-02 20:38:21 | ERROR    | main_ai:execute_fastmcp_tool_call:2030 - ❌ FastMCP工具执行失败: 'FunctionTool' object is not callable
2026-07-02 20:38:21 | INFO     | main_ai:chat:2566 - ✅ FastMCP工具执行完成,结果长度: 68
2026-07-02 20:38:21 | INFO     | main_ai:chat:2576 - 🤖 正在生成最终回答...
2026-07-02 20:38:23 | INFO     | main_ai:chat:2580 - ✅ 最终回答生成成功,长度: 304
INFO:     192.168.56.1:50637 - "POST /chat HTTP/1.1" 200 OK

错误信息为:

main_ai:execute_fastmcp_tool_call:2030 - ❌ FastMCP工具执行失败: 'FunctionTool' object is not callable

还需要解决这个问题,保证在其他电脑上能够运行。或者提供解决的思路,我来测试

@donghaiwang

Copy link
Copy Markdown
Member

确认下,看是不是mcp版本的问题,现在 mcp.bat 启动所使用的 python 版本为 3.10 (dependencies\prerequisites\miniconda3\envs\hutb_3.10),mcp的版本为2.14.5
看你开发使用的是不是这个版本

@Dame-time-0034

Copy link
Copy Markdown
Contributor Author

D:\mcp\llm>pip show mcppip show mcp
WARNING: Package(s) not found: mcppip, show
Name: mcp
Version: 1.27.2
Summary: Model Context Protocol SDK
Home-page: https://modelcontextprotocol.io
Author: Anthropic, PBC.
Author-email:
License: MIT
Location: C:\Users\xiaoyi\AppData\Local\Programs\Python\Python311\Lib\site-packages
Requires: anyio, httpx, httpx-sse, jsonschema, pydantic, pydantic-settings, pyjwt, python-multipart, pywin32, sse-starlette, starlette, typing-extensions, typing-inspection, uvicorn
Required-by:

对的,版本不一致。这是我开发分别使用的版本号,分别是mcp:1.27.2 Python: 3.11.9
请问下一步需要如何操作呢

@Dame-time-0034

Copy link
Copy Markdown
Contributor Author
8c4ab8a0-2f6f-44ea-afaa-b2c391001059 这是更新后一个按钮的页面

@donghaiwang

donghaiwang commented Jul 3, 2026

Copy link
Copy Markdown
Member

使用运行mcp.bat下载的dependencies\prerequisites\miniconda3\envs\hutb_3.10进行开发和测试。
或者切换到本地安装的python 3.10 进行测试,并指定安装mcp的版本为2.14.5,看是否能够正常运行

Comment thread README.md
天气设置(默认雨天)
生成行人
生成车辆
开启自动驾驶

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不能把之前的功能对话给删掉了

@Dame-time-0034

Copy link
Copy Markdown
Contributor Author
c42d4024-fbe7-4bf8-aef5-a29566368bdb 571f67be8e5851b7c0718c7c46c31636 我已把python版本切换到3.10,但是双击mcp.bat无法打开模拟器,电脑提示如图所示

@donghaiwang

Copy link
Copy Markdown
Member

在某些机器上不能运行都算bug,可以后面再修复,先保证在自己建立的python3.10上测试通过

@Dame-time-0034

Copy link
Copy Markdown
Contributor Author

在我的计算机上python3.10无论如何也打不开mcp,python3.11可以,以下是终端
python3.11:
D:\mcp\llm>C:\Users\xiaoyi\AppData\Local\Programs\Python\Python311\python.exe main_ai.py
[OK] 环境配置验证通过
[WEB] 启动 FastMCP AI助手 Web 对话界面...
[INFO] 访问地址: http://192.168.1.107:3000
INFO: Will watch for changes in these directories: ['D:\mcp\llm']
INFO: Uvicorn running on http://192.168.1.107:3000 (Press CTRL+C to quit)
INFO: Started reloader process [10652] using WatchFiles
INFO: Started server process [8140]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [8140]
INFO: Stopping reloader process [10652]

python3.10:
D:\mcp\llm>python.exe main_ai.py
Traceback (most recent call last):
File "D:\mcp\llm\main_ai.py", line 13, in
from fastapi import FastAPI, Form
ModuleNotFoundError: No module named 'fastapi'

@donghaiwang

Copy link
Copy Markdown
Member

先看报错信息,已经提示了,ModuleNotFoundError: No module named 'fastapi',需要安装fastapi的包

@Dame-time-0034

Copy link
Copy Markdown
Contributor Author

D:\mcp>python -m pip install mcp==2.14.5 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
ERROR: Could not find a version that satisfies the requirement mcp==2.14.5 (from versions: 0.9.1, 1.0.0, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.2.0rc1, 1.2.0, 1.2.1, 1.3.0rc1, 1.3.0, 1.4.0, 1.4.1, 1.5.0, 1.6.0, 1.7.0, 1.7.1, 1.8.0, 1.8.1, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.9.4, 1.10.0, 1.10.1, 1.11.0, 1.12.0, 1.12.1, 1.12.2, 1.12.3, 1.12.4, 1.13.0, 1.13.1, 1.14.0, 1.14.1, 1.15.0, 1.16.0, 1.17.0, 1.18.0, 1.19.0, 1.20.0, 1.21.0, 1.21.1, 1.21.2, 1.22.0, 1.23.0, 1.23.1, 1.23.2, 1.23.3, 1.24.0, 1.25.0, 1.26.0, 1.27.0, 1.27.1, 1.27.2, 1.28.0, 1.28.1, 2.0.0a1, 2.0.0a2, 2.0.0a3, 2.0.0b1)
ERROR: No matching distribution found for mcp==2.14.5

请问 mcp==2.14.5版本在哪里下载?

@donghaiwang

Copy link
Copy Markdown
Member

直接指定版本安装就行:pip install mcp==2.14.5

@Dame-time-0034

Copy link
Copy Markdown
Contributor Author

D:\mcp>pip install mcp==2.14.5
ERROR: Could not find a version that satisfies the requirement mcp==2.14.5 (from versions: 0.9.1, 1.0.0, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.2.0rc1, 1.2.0, 1.2.1, 1.3.0rc1, 1.3.0, 1.4.0, 1.4.1, 1.5.0, 1.6.0, 1.7.0, 1.7.1, 1.8.0, 1.8.1, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.9.4, 1.10.0, 1.10.1, 1.11.0, 1.12.0, 1.12.1, 1.12.2, 1.12.3, 1.12.4, 1.13.0, 1.13.1, 1.14.0, 1.14.1, 1.15.0, 1.16.0, 1.17.0, 1.18.0, 1.19.0, 1.20.0, 1.21.0, 1.21.1, 1.21.2, 1.22.0, 1.23.0, 1.23.1, 1.23.2, 1.23.3, 1.24.0, 1.25.0, 1.26.0, 1.27.0, 1.27.1, 1.27.2, 1.28.0, 1.28.1, 2.0.0a1, 2.0.0a2, 2.0.0a3, 2.0.0b1)
ERROR: No matching distribution found for mcp==2.14.5

@donghaiwang

Copy link
Copy Markdown
Member

是fastmcp,pip install fastmcp==2.14.5

@Dame-time-0034

Copy link
Copy Markdown
Contributor Author

好的,已经下载成功了

@Dame-time-0034

Copy link
Copy Markdown
Contributor Author

D:\mcp\llm>python main_ai.py
C:\Users\xiaoyi\AppData\Local\Programs\Python\Python310\lib\site-packages\fastmcp\server\auth\providers\jwt.py:10: AuthlibDeprecationWarning: authlib.jose module is deprecated, please use joserfc instead.
It will be compatible before version 2.0.0.
from authlib.jose import JsonWebKey, JsonWebToken
[OK] 环境配置验证通过
[WEB] 启动 FastMCP AI助手 Web 对话界面...
[INFO] 访问地址: http://192.168.1.107:3000
INFO: Will watch for changes in these directories: ['D:\mcp\llm']
INFO: Uvicorn running on http://192.168.1.107:3000 (Press CTRL+C to quit)
INFO: Started reloader process [10576] using WatchFiles
C:\Users\xiaoyi\AppData\Local\Programs\Python\Python310\lib\site-packages\fastmcp\server\auth\providers\jwt.py:10: AuthlibDeprecationWarning: authlib.jose module is deprecated, please use joserfc instead.
It will be compatible before version 2.0.0.
from authlib.jose import JsonWebKey, JsonWebToken
C:\Users\xiaoyi\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy_init_.py:6: DeprecationWarning: websockets.legacy is deprecated; see https://websockets.readthedocs.io/en/stable/howto/upgrade.html for upgrade instructions
warnings.warn( # deprecated in 14.0 - 2024-11-09
C:\Users\xiaoyi\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\protocols\websockets\websockets_impl.py:17: DeprecationWarning: websockets.server.WebSocketServerProtocol is deprecated
from websockets.server import WebSocketServerProtocol
INFO: Started server process [18864]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 192.168.1.107:53468 - "GET / HTTP/1.1" 200 OK
2026-07-04 12:09:37 | INFO | main_ai:chat:2551 - 💬 用户消息: 生成一个3x3网格路网,跑200秒,每2秒发一辆车
2026-07-04 12:09:38 | INFO | main_ai:chat:2561 - 🔧 检测到 1 个FastMCP工具调用
2026-07-04 12:09:38 | INFO | main_ai:chat:2564 - 🔨 执行FastMCP工具: generate_sumo_network
2026-07-04 12:09:38 | INFO | main_ai:execute_fastmcp_tool_call:1866 - 🔧 执行FastMCP工具: generate_sumo_network
2026-07-04 12:09:38 | INFO | main_ai:execute_fastmcp_tool_call:1867 - 📝 参数: {'grid_x': 3, 'grid_y': 3, 'duration': 200, 'rate': 2.0}
2026-07-04 12:09:39 | INFO | main_ai:chat:2566 - ✅ FastMCP工具执行完成,结果长度: 303
2026-07-04 12:09:39 | INFO | main_ai:chat:2576 - 🤖 正在生成最终回答...
2026-07-04 12:09:42 | INFO | main_ai:chat:2580 - ✅ 最终回答生成成功,长度: 433
INFO: 192.168.1.107:53468 - "POST /chat HTTP/1.1" 200 OK

FastMCP2.14.5+python3.10没有问题了,但这两个配合起来兼容性有点差,修改了一些文件,路径和修改内容如下
1.C:\Users\xiaoyi\AppData\Local\Programs\Python\Python310\lib\site-packages\fastmcp\mcp_config.py

在文件最后(update_config_file 函数之后),添加:
python
def _coerce_tool_transform_configs(configs):
"""Coerce tool transform configs to a list of dictionaries.
这是一个临时修复函数,用于兼容 fastmcp 2.14.5。
"""
if configs is None:
return []
if isinstance(configs, list):
return configs
if isinstance(configs, dict):
return [configs]
# 如果是 ToolTransformConfig 对象
if hasattr(configs, 'model_dump'):
return [configs.model_dump()]
return []

2.C:\Users\xiaoyi\AppData\Local\Programs\Python\Python310\lib\site-packages\fastmcp\exceptions.py

在文件末尾添加缺失的类
python
class FastMCPDeprecationWarning(DeprecationWarning):
"""Warning for deprecated FastMCP features."""
pass

3.C:\Users\xiaoyi\AppData\Local\Programs\Python\Python310\lib\site-packages\fastmcp\client\transports_init_.py

在文件末尾的 all 之前添加:
python
from fastmcp.client.transports.ws import WSTransport

并在 all 中添加 "WSTransport":
python
all = [
"ClientTransport",
"FastMCPStdioTransport",
"FastMCPTransport",
"NodeStdioTransport",
"NpxStdioTransport",
"PythonStdioTransport",
"SSETransport",
"StdioTransport",
"StreamableHttpTransport",
"UvStdioTransport",
"UvxStdioTransport",
"WSTransport", # 新增
"infer_transport",
]

  1. 创建 ws.py 文件(如果不存在)

C:\Users\xiaoyi\AppData\Local\Programs\Python\Python310\lib\site-packages\fastmcp\client\transports\ws.py
添加内容:

python
"""WebSocket transport for FastMCP client."""

from typing import Any

from fastmcp.client.transports.base import ClientTransport

class WSTransport(ClientTransport):
"""WebSocket transport for MCP client."""

def __init__(self, url: str, headers: dict[str, str] | None = None):
    self.url = url
    self.headers = headers or {}

async def connect(self):
    """Connect to the WebSocket server."""
    pass

async def send(self, message: Any):
    """Send a message over WebSocket."""
    pass

async def receive(self):
    """Receive a message over WebSocket."""
    pass

async def close(self):
    """Close the WebSocket connection."""
    pass

最后直接运行我修改过的main ai.py文件就可以了

@donghaiwang

Copy link
Copy Markdown
Member
image 调用成功了,但是没找到sumo。看是不是mcp.bat中设置环境变量没有生效。注意:默认需要使用的是`mcp\hutb\sumo`目录下的sumo软件

@donghaiwang donghaiwang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

将SUMO_HOME的设置改为默认的hutb\sumo下的软件的保存路径即可

set "SUMO_HOME=%PROJECT_ROOT%\hutb\sumo"

Comment thread mcp.bat Outdated
set "VIRTUAL_ENV=%VENV_PATH%"

REM ===== 设置 SUMO_HOME(相对路径) =====
set "SUMO_HOME=%PROJECT_ROOT%\sumo\sumo_install\sumo-win64-1.27.0\sumo-1.27.0"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set "SUMO_HOME=%PROJECT_ROOT%\sumo\sumo_install\sumo-win64-1.27.0\sumo-1.27.0"
set "SUMO_HOME=%PROJECT_ROOT%\hutb\sumo"

@Dame-time-0034

Copy link
Copy Markdown
Contributor Author

已提交修改

@donghaiwang donghaiwang merged commit fe89781 into OpenHUTB:main Jul 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants