跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

networkRoute()

Deprecated

Agent networks are deprecated and will be removed in a future release. Use supervisor agents with agent.stream() or agent.generate() instead. See the migration guide to upgrade.

Creates a network route handler for streaming network execution in AI SDK format. The function registers an HTTP POST endpoint that accepts messages and executes an agent network before streaming the response to the client in AI SDK-compatible format. Agent networks let a routing agent delegate tasks to other agents. Use this function inside a custom API route.

Use handleNetworkStream() if you need a framework-agnostic handler.

networkRoute() keeps the existing AI SDK v5/default behavior. If your app is typed against AI SDK v6, pass version: 'v6'.

Usage example
Usage example的直接链接

This example shows how to set up a network route at the /network endpoint that uses an agent with the ID weatherAgent.

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { networkRoute } from '@mastra/ai-sdk'

export const mastra = new Mastra({
server: {
apiRoutes: [
networkRoute({
path: '/network',
agent: 'weatherAgent',
}),
],
},
})

You can also use runtime-defined agent routing based on an agentId. The URL /network/weatherAgent will resolve to the agent with the ID weatherAgent.

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { networkRoute } from '@mastra/ai-sdk'

export const mastra = new Mastra({
server: {
apiRoutes: [
networkRoute({
path: '/network/:agentId',
}),
],
},
})

Parameters
Parameters的直接链接

version?:

'v5' | 'v6' | 'v7'
= 'v5'
Selects the AI SDK stream contract to emit. Omit it or pass 'v5' for the existing default behavior. Pass 'v6' when your app is typed against AI SDK v6 response helpers. Pass 'v7' when your app is typed against AI SDK v7.

path:

string
= '/network/:agentId'
The route path (e.g., /network or /network/:agentId). Include :agentId for dynamic agent routing.

agent?:

string
The ID of the routing agent to use for this network route. Required if the path doesn't include :agentId.

agentVersion?:

{ versionId: string } | { status?: 'draft' | 'published' }
Selects a specific agent version. Pass { versionId: '<id>' } to target an exact version, or { status: 'draft' } / { status: 'published' } to resolve by status. When the route is called, query parameters ?versionId=<id> or ?status=draft|published take precedence over this static value. Requires the Editor to be configured.

defaultOptions?:

AgentExecutionOptions
Default options passed to agent execution. These can include instructions, memory configuration, maxSteps, and other execution settings.

Additional configuration
Additional configuration的直接链接

You can use prepareSendMessagesRequest to customize the request sent to the network route, for example to pass additional configuration to the agent:

const { error, status, sendMessage, messages, regenerate, stop } = useChat({
transport: new DefaultChatTransport({
api: 'http://localhost:4111/network',
prepareSendMessagesRequest({ messages }) {
return {
body: {
messages,
// Pass memory config
memory: {
thread: 'user-1',
resource: 'user-1',
},
},
}
},
}),
})
On this page