-
Notifications
You must be signed in to change notification settings - Fork 487
Description
I'm building a feature where users can dynamically add MCP (Model Context Protocol)
servers to their agent after it's been created. The workflow is:
- Create an agent and run it → serialize the state and save to database
- User later decides to add an MCP server to the agent configuration
- Load the saved state and create a new agent with the MCP servers included
- Call RunState.fromString(agent, savedState) to resume the conversation
However, deserialization fails with this error:
Error: No existing trace found
at withNewSpanContext (tracing/context.js)
at _withSpanFactory (tracing/createSpans.js)
at withMCPListToolsSpan (tracing/createSpans.js)
at getFunctionToolsFromServer (mcp.js)
at getAllMcpTools (mcp.js)
at getMcpTools (agent.js)
at getAllTools (agent.js)
at deserializeProcessedResponse (runState.js:622)
at RunState.fromString (runState.js:523)
Root Cause:
During RunState.fromString() deserialization, the code calls
deserializeProcessedResponse() at line 523 of runState.js. This function needs to call
getAllTools() to rebuild the tool maps from the saved state.
When the agent has MCP servers, getAllTools() → getMcpTools() → withMCPListToolsSpan()
tries to create a new span context by calling withNewSpanContext(). However,
withNewSpanContext() expects an existing trace context in the async local storage, which
hasn't been set up during deserialization. This causes the "No existing trace found"
error.
The trace context setup code exists in fromString() (lines 493-505), but it only applies
if currentAgentSpan was serialized. The deserializeProcessedResponse() call happens
outside of any trace context wrapping, so MCP tool list span creation fails.
Expected Behavior:
RunState.fromString() should successfully deserialize state even when the agent now has
MCP servers, since the saved state doesn't actually use those new servers (they weren't
in the agent when the state was created). The MCP servers should just be available for
future tool calls, not required for deserialization.
Alternatively, if this is not supported, there should be a clear error message
explaining that you cannot deserialize old states with agents that have new MCP servers
added.