SJ 0711ec346f Fix/fix: resolve MCP tools race condition causing 'completion 无法解析' error (#5534)
* fix: resolve MCP tools race condition causing 'completion 无法解析' error

- Wait for MCP client initialization to complete before accepting requests
- Add Future-based synchronization in init_mcp_clients()
- Prevent tool_calls from being rejected due to empty func_list
- Improve error logging for MCP initialization failures

Fixes race condition where AI attempts to call MCP tools before they are
registered, resulting in 'API 返回的 completion 无法解析' exceptions.

The issue occurred because:
1. MCP clients were initialized asynchronously without waiting
2. System accepted user requests immediately after startup
3. AI received empty tool list and attempted to call non-existent tools
4. Tool matching failed, causing parsing errors

This fix ensures all MCP tools are loaded before the system processes
any requests that might use them.

* perf: add timeout and better error handling for MCP initialization

- Add 20-second total timeout to prevent slow MCP servers from blocking startup
- Show detailed configuration info when MCP initialization fails
- List all failed services in a summary warning
- Gracefully handle timeout by using already-completed services

This ensures that even if some MCP servers are slow or unreachable,
the system will start within a reasonable time and provide clear
feedback about which services failed and why.

* refactor: simplify MCP init orchestration and improve log security

- Replace Future-based sync with asyncio.wait + name→task mapping
- Explicitly cancel timed-out tasks after 20s timeout
- Downgrade sensitive config details (command/args/URL) to debug level
- Move urllib.parse import to top-level

* fix: prevent initialized MCP clients from being cleaned up on timeout

- Do not cancel pending tasks on timeout; let them continue running
  in the background waiting for the termination signal (event.set()),
  so successfully initialized services remain available
- Track initialization state with a flag to distinguish init failures
  from post-init cancellations in _init_mcp_client_task_wrapper

* fix: restore task cancellation on timeout per review feedback

Pending tasks in asyncio.wait are tasks that have NOT completed
initialization within 20s, so cancelling them is safe and correct.

* fix: separate init signal from client lifetime in MCP task wrapper

The previous design awaited task completion, but tasks only finish
on shutdown (after event.wait()), causing asyncio.wait to always
hit the 20s timeout and cancel all clients.

Fix: introduce a dedicated ready_event that is set immediately after
_init_mcp_client completes. init_mcp_clients now waits only for
ready_event (with 20s timeout), while the long-lived client task
continues running in the background until shutdown_event is set.

This ensures startup returns promptly once clients are ready.

* security: redact sensitive MCP config from debug logs

Only log executable name and argument count instead of full
command/args to avoid leaking tokens or credentials even at
debug level.

* refactor: use McpClientInfo dataclass and MCP_INIT_TIMEOUT constant

- Extract MCP_INIT_TIMEOUT = 20.0 as a named module-level constant
- Replace tuple-based client_info with _McpClientInfo dataclass to
  eliminate index-based access and improve readability
- Remove _wait_ready helper; use asyncio.create_task(event.wait()) directly
- Await cancelled tasks after timeout to prevent lingering background
  tasks and unobserved exceptions

* fix: handle CancelledError and clean up wait_tasks on timeout

- Catch asyncio.CancelledError separately in _init_mcp_client_task_wrapper
  so ready_event.set() is always called (Python 3.8+ CancelledError
  inherits BaseException, not Exception)
- Cancel and await lingering wait_tasks after timeout to prevent
  them from hanging indefinitely when ready_event is never set

* fix: align enable_mcp_server with new wrapper API and fix security/config issues

- Fix enable_mcp_server to pass shutdown_event + ready_event instead of
  ready_future, matching _init_mcp_client_task_wrapper's current signature
- Cancel and await init_task on timeout; clean up mcp_client_event on failure
- Read MCP_INIT_TIMEOUT from env var ASTRBOT_MCP_INIT_TIMEOUT (default 20s)
  so operators can tune it without code changes
- Strip userinfo from URL in debug log (use hostname+port only, not netloc)
  to avoid leaking credentials embedded in URLs

* refactor: register mcp_client_event only after successful init in enable_mcp_server

Move self.mcp_client_event[name] assignment to after initialization
succeeds, so callers never observe a stale event for a failed client.

* fix: harden MCP init state handling and timeout parsing

* fix: improve MCP timeout and post-init error observability

* refactor: simplify MCP init lifecycle orchestration

* refactor: simplify MCP init flow and cap timeout values

* fix: refine mcp timeout handling and lifecycle task tracking

* fix: harden mcp shutdown and timeout source logging

* refactor: simplify mcp runtime registry and timeout flow

* fix: keep mcp init summary return contract

* refactor: streamline mcp lifecycle and init errors

* refactor: unify mcp lifecycle wait handling

* refactor: simplify mcp runtime ownership and timeout resolution

* fix: harden mcp shutdown waiting and startup signaling

* refactor: streamline mcp lifecycle and shutdown errors

* refactor: harden mcp runtime access and shutdown

* fix: ensure mcp client cleanup and clarify views

* refactor: cache mcp client view and guard startup

* refactor: simplify mcp init cleanup and runtime lock

* refactor: reduce mcp runtime duplication

* refactor: reuse mcp cleanup and client view

---------

Co-authored-by: idiotsj <idiotsj@users.noreply.github.com>
Co-authored-by: 邹永赫 <1259085392@qq.com>
2026-03-03 01:09:45 +09:00
2026-02-26 19:12:09 +08:00
2023-04-15 15:33:00 +08:00
2026-01-13 12:19:05 +08:00
2025-02-24 00:31:57 +08:00
2026-02-21 17:20:26 +08:00
2026-02-28 21:23:44 +08:00
2026-02-28 21:23:44 +08:00
2026-02-28 21:23:44 +08:00
2026-02-28 21:23:44 +08:00
2026-02-28 21:23:44 +08:00

AstrBot-Logo-Simplified

AstrBot is an open-source all-in-one Agent chatbot platform that integrates with mainstream instant messaging apps. It provides reliable and scalable conversational AI infrastructure for individuals, developers, and teams. Whether you're building a personal AI companion, intelligent customer service, automation assistant, or enterprise knowledge base, AstrBot enables you to quickly build production-ready AI applications within your IM platform workflows.

screenshot_1 5x_postspark_2026-02-27_22-37-45

Key Features

  1. 💯 Free & Open Source.
  2. AI LLM Conversations, Multimodal, Agent, MCP, Skills, Knowledge Base, Persona Settings, Auto Context Compression.
  3. 🤖 Supports integration with Dify, Alibaba Cloud Bailian, Coze, and other agent platforms.
  4. 🌐 Multi-Platform: QQ, WeChat Work, Feishu, DingTalk, WeChat Official Accounts, Telegram, Slack, and more.
  5. 📦 Plugin Extensions with 1000+ plugins available for one-click installation.
  6. 🛡️ Agent Sandbox for isolated, safe execution of code, shell calls, and session-level resource reuse.
  7. 💻 WebUI Support.
  8. 🌈 Web ChatUI Support with built-in agent sandbox and web search.
  9. 🌐 Internationalization (i18n) Support.

💙 Role-playing & Emotional Companionship Proactive Agent 🚀 General Agentic Capabilities 🧩 1000+ Community Plugins

99b587c5d35eea09d84f33e6cf6cfd4f

c449acd838c41d0915cc08a3824025b1

image

image

Quick Start

One-Click Deployment

For users who want to quickly experience AstrBot, we recommend using the one-click deployment method with uv :

uv tool install astrbot
astrbot init # Only execute this command for the first time to initialize the environment
astrbot

Requires uv to be installed.

Docker Deployment

For users who want a more stable and production-ready deployment, we recommend using Docker / Docker Compose to deploy AstrBot.

Please refer to the official documentation: Deploy AstrBot with Docker.

Deploy on RainYun

For users who want to deploy AstrBot with one-click and don't want to manage the server, we recommend using RainYun's one-click cloud deployment service ☁️:

Deploy on RainYun

Desktop Application (Tauri)

For users who want to deploy AstrBot on their desktop, primarily using AstrBot ChatUI, rarely use AstrBot plugins, we recommend using the AstrBot App:

Desktop repository: AstrBot-desktop.

Supports multiple system architectures, direct package installation, and out-of-the-box usage. A convenient one-click desktop deployment option for beginners.

One-Click Launcher Deployment (AstrBot Launcher)

For users who want a quick deployment and multi-instance solution with environment isolation, we recommend using the AstrBot Launcher:

Visit the AstrBot Launcher repository and install the package for your OS from the latest release.

A quick deployment and multi-instance solution with environment isolation.

Deploy on Replit

Community-contributed deployment method.

Run on Repl.it

AUR

yay -S astrbot-git

More deployment methods: BT-Panel Deployment | 1Panel Deployment | CasaOS Deployment | Manual Deployment

Supported Messaging Platforms

Connect AstrBot to your favorite chat platform.

Platform Maintainer
QQ Official
OneBot v11 protocol implementation Official
Telegram Official
Wecom & Wecom AI Bot Official
WeChat Official Accounts Official
Feishu (Lark) Official
DingTalk Official
Slack Official
Discord Official
LINE Official
Satori Official
Misskey Official
WhatsApp (Coming Soon) Official
Matrix Community
KOOK Community
VoceChat Community

Supported Model Services

Service Type
OpenAI and Compatible Services LLM Services
Anthropic LLM Services
Google Gemini LLM Services
Moonshot AI LLM Services
Zhipu AI LLM Services
DeepSeek LLM Services
Ollama (Self-hosted) LLM Services
LM Studio (Self-hosted) LLM Services
AIHubMix LLM Services (API Gateway, supports all models)
CompShare LLM Services
302.AI LLM Services
TokenPony LLM Services
SiliconFlow LLM Services
PPIO Cloud LLM Services
ModelScope LLM Services
OneAPI LLM Services
Dify LLMOps Platforms
Alibaba Cloud Bailian Applications LLMOps Platforms
Coze LLMOps Platforms
OpenAI Whisper Speech-to-Text Services
SenseVoice Speech-to-Text Services
OpenAI TTS Text-to-Speech Services
Gemini TTS Text-to-Speech Services
GPT-Sovits-Inference Text-to-Speech Services
GPT-Sovits Text-to-Speech Services
FishAudio Text-to-Speech Services
Edge TTS Text-to-Speech Services
Alibaba Cloud Bailian TTS Text-to-Speech Services
Azure TTS Text-to-Speech Services
Minimax TTS Text-to-Speech Services
Volcano Engine TTS Text-to-Speech Services

❤️ Contributing

Issues and Pull Requests are always welcome! Feel free to submit your changes to this project :)

How to Contribute

You can contribute by reviewing issues or helping with pull request reviews. Any issues or PRs are welcome to encourage community participation. Of course, these are just suggestions—you can contribute in any way you like. For adding new features, please discuss through an Issue first.

Development Environment

AstrBot uses ruff for code formatting and linting.

git clone https://github.com/AstrBotDevs/AstrBot
pip install pre-commit
pre-commit install

🌍 Community

QQ Groups

  • Group 1: 322154837
  • Group 3: 630166526
  • Group 5: 822130018
  • Group 6: 753075035
  • Group 7: 743746109
  • Group 8: 1030353265
  • Developer Group: 975206796

Discord Server

Discord_community

❤️ Special Thanks

Special thanks to all Contributors and plugin developers for their contributions to AstrBot ❤️

Additionally, the birth of this project would not have been possible without the help of the following open-source projects:

Star History

Tip

If this project has helped you in your life or work, or if you're interested in its future development, please give the project a Star. It's the driving force behind maintaining this open-source project <3

Star History Chart

Companionship and capability should never be at odds. What we aim to create is a robot that can understand emotions, provide genuine companionship, and reliably accomplish tasks.

私は、高性能ですから!

S
Description
No description provided
Readme AGPL-3.0 65 MiB
Languages
Python 69.1%
Vue 25.4%
TypeScript 3.4%
JavaScript 1.4%
Shell 0.3%
Other 0.4%