Mastra推出实验性MCP后台任务
Mastra为MCPServer和MCPClient引入了MCP任务,使耗时较长的工具调用可在后台运行,智能体则能继续响应。服务器会返回任务ID,客户端轮询working、input_required、completed、failed或cancelled等状态,完成后将结果交回智能体。该功能需要@mastra/mcp 2.7.0和@mastra/core 1.81.0或更高版本。
据Mastra Blog称,任务ID可结束长时间保持的HTTP请求,让智能体循环不再被缓慢或高成本MCP工具调用阻塞。
You can now use tasks with Mastra MCPServer and MCPClient to run long-running tool calls in the background. The client can poll the server until the task is completed, then hand the result back to the agent.
While tool calls are in progress, agents are blocked and can't respond to user requests. Moving tool calls to tasks lets the agent keep responding while slow or expensive computations run in the background.
Your browser does not support the video tag.An agent keeps answering questions while a 30-day repo report runs as an MCP task, then shows the finished report as a dashboard.
Before tasks, MCP tool calls were synchronous. Each call was handled by a single HTTP request that remained open until complete, and blocked the agent loop. Mastra's existing background tasks primitive could be used to free up the agent loop, but if a tool call outlasted a proxy timeout or a load balancer's idle limit, it would eventually fail.
With tasks, MCP servers can quickly respond with a task ID (which completes the HTTP request and unblocks the agent). MCP clients then use the ID to poll the server, receiving a response that includes a task status of working, input_required, completed, failed, or cancelled. When the task completes, Mastra sends the agent a notification signal, which reaches an active run on its next model request, instead of waiting for the agent loop to finish.
#Mastra#Mastra MCPServer#Mastra MCPClient#Model Context Protocol