Toolset setup failures (e.g. an unreachable MCP server) are only logged, never emitted as an error event
Bug Description
When a toolset fails in setup(), for example an MCP server that is unreachable or that never answers initialize, AgentActivity._setup_toolsets only logs the exception:
@utils.log_exceptions(logger=logger)
async def _do_setup(toolset: llm.Toolset) -> None:
await toolset.setup()
await asyncio.gather(*(_do_setup(ts) for ts in all_toolsets), return_exceptions=True)
The session then starts without that toolset's tools, and nothing reaches the application. AgentSession emits no "error" event, so a handler that reacts to LLM/STT/TTS failures (transfer the call, alert, end the session) never learns that the agent's tools are missing.
This is costly on phone calls. The setup runs before on_enter, so the greeting waits for it. With the SSE transport, a POST that fails (for example a 5xx on initialize) is only logged by the MCP SDK's post_writer, and the pending request waits out the full client_session_timeout_seconds (modelcontextprotocol/python-sdk#2110). The caller hears silence for that long, then the agent answers without its tools, and the app has no event to act on.
Expected Behavior
A toolset whose setup() raises is reported on the session's "error" event as an ErrorEvent, like the other component errors (LLMError, STTError, TTSError, RealtimeModelError). It is recoverable: the session keeps running without that toolset's tools, and the app decides what to do.
Reproduction Steps
1. Point an MCPServerHTTP at a URL nothing listens on (or an SSE server whose POST endpoint returns 500).
2. AgentSession(..., tools=[MCPToolset(id="mcp", mcp_server=server)]) and session.on("error", print)
3. await session.start(agent)
4. "failed to set up toolset"-style traceback in the log, no "error" event, and the agent has no MCP tools.
Minimal, without MCP:
class Unreachable(llm.Toolset):
def __init__(self):
super().__init__(id="unreachable")
async def setup(self):
raise ConnectionError("server unreachable")
session.on("error", print) # never called
await session.start(Agent(instructions="...", tools=[Unreachable()]))
Operating System
macOS 26 and Linux (production)
Models Used
Any (independent of the model; seen with OpenAI realtime and with STT/LLM/TTS pipelines)
Package Versions
livekit-agents 1.8.3 and current main
mcp 1.30.0 (livekit-agents 1.8.3) and 2.3.0 (main)
Session/Room/Call IDs
N/A
Proposed Solution
Catch the exception in `_do_setup`, log it as today, and emit
`ErrorEvent(error=ToolsetError(label=toolset.id, error=e, recoverable=True, ...), source=toolset)`,
with `ToolsetError` shaped like `LLMError`. It does not go through `AgentSession._on_error`,
so it never closes the session.
PR: (linked below)
Additional Context
The SSE wait itself comes from the MCP SDK (modelcontextprotocol/python-sdk#2110). This issue is about LiveKit not surfacing the failure once setup() does raise, which also covers a refused connection, a 404, a bad URL or a server that times out.
Screenshots and Recordings
No response
@longcw could you take a look at this issue and review the fix in #8724 when you have a moment?
The change is small (+68/−4): a toolset that fails in setup() is now reported as a recoverable ErrorEvent carrying a new ToolsetError, so an app can act on an unreachable MCP server, for example by transferring the call, instead of the agent running silently without its tools. It does not change session start, speech scheduling or handoffs, and it includes a regression test. Devin's review flags have been addressed.
Thank you, your feedback would be very helpful.