Skip to content

Commit a96d0b5

Browse files
committed
fix(mcp): resolve server query parameter error and enable stateless HTTP
- Fix FastAPI dependency injection in maibot action endpoint (missing Depends) - Fix FastMCP run method: use run_async with stateless_http=True instead of run_http_async - Fix port from invalid 80214 to valid 8021 across config and code - Add proper asyncio.wait_for with timeout in cleanup for graceful shutdown
1 parent 86ce54e commit a96d0b5

4 files changed

Lines changed: 12 additions & 9 deletions

File tree

config-template.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,4 @@ enable_validation = false
385385
[mcp]
386386
enabled = false
387387
host = "127.0.0.1"
388-
port = 80214
388+
port = 8021

src/modules/dashboard/api/maibot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
import uuid
99
from typing import TYPE_CHECKING
1010

11-
from fastapi import APIRouter, HTTPException, status
11+
from fastapi import APIRouter, Depends, HTTPException, status
1212

13+
from src.modules.dashboard.dependencies import get_dashboard_server
1314
from src.modules.dashboard.schemas.maibot import MaibotActionRequest, MaibotActionResponse
1415
from src.modules.events.names import CoreEvents
1516
from src.modules.events.payloads.decision import IntentPayload
1617
from src.modules.logging import get_logger
1718
from src.modules.types.intent import Intent, IntentMetadata
1819

19-
2020
if TYPE_CHECKING:
2121
from src.modules.dashboard.server import DashboardServer
2222

@@ -27,7 +27,7 @@
2727
@router.post("/action", response_model=MaibotActionResponse)
2828
async def handle_maibot_action(
2929
request: MaibotActionRequest,
30-
server: "DashboardServer",
30+
server: "DashboardServer" = Depends(get_dashboard_server), # noqa: B008
3131
) -> MaibotActionResponse:
3232
event_bus = server.event_bus
3333
if not event_bus:

src/modules/mcp/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ class MCPServerConfig(BaseModel):
1212

1313
enabled: bool = Field(default=False, description="是否启用 MCP 服务")
1414
host: str = Field(default="127.0.0.1", description="MCP 服务监听地址")
15-
port: int = Field(default=80214, description="MCP 服务监听端口")
15+
port: int = Field(default=8021, description="MCP 服务监听端口(有效范围:1024-65535)")

src/modules/mcp/service.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ async def send_intent_tool(
8686
return await self._send_intent_internal(action, emotion, speech, context, priority)
8787

8888
host = config.get("host", "127.0.0.1")
89-
port = config.get("port", 80214)
89+
port = config.get("port", 8021)
90+
91+
self.logger.info(f"启动 MCP 服务器: {host}:{port} (stateless_http=True)")
9092

91-
self.logger.info(f"启动 MCP 服务器: {host}:{port}")
9293
self._server_task = asyncio.create_task(
93-
self._mcp.run_http_async(transport="streamable-http", host=host, port=port)
94+
self._mcp.run_async(transport="http", host=host, port=port, stateless_http=True)
9495
)
9596

9697
self._initialized = True
@@ -106,7 +107,9 @@ async def cleanup(self) -> None:
106107
if self._server_task:
107108
self._server_task.cancel()
108109
try:
109-
await self._server_task
110+
await asyncio.wait_for(self._server_task, timeout=5.0)
111+
except asyncio.TimeoutError:
112+
self.logger.warning("MCP 服务器关闭超时,强制取消")
110113
except asyncio.CancelledError:
111114
pass
112115

0 commit comments

Comments
 (0)