Connections 概览
Connections 让 Mastra 能够与远程 Agent、coding agent、Provider 软件开发工具包(SDK)运行时以及外部 Tool 和资源协同工作。请根据由哪个系统负责 Agent 运行时,以及需要交换哪些内容来选择连接类型。
- Agent-to-Agent(A2A):跨服务、框架、供应商和编程语言边界公开或使用远程 Agent。
- Agent Client Protocol(ACP):将兼容的 coding agent 进程作为 Mastra Tool 或 subagent 运行。
- SDK agents:注册由 Claude、Cursor 或 OpenAI SDK 支持的 Agent,同时仍由 Provider SDK 控制运行时、Tool、权限和 Agent 循环。
- Model Context Protocol(MCP):将 Agent 连接到外部 Tool 和资源,或向兼容 MCP 的系统公开 Mastra Agent、Tool、Workflow、prompt 和资源。
何时使用 Connections何时使用 Connections的直接链接
在需要执行以下操作时使用 Connections:
- 将工作委派给在其他服务或运行时中运行的 Agent。
- 让 coding agent 针对项目 Workspace 运行。
- 添加 Provider 原生 Agent,而不替换其 SDK 运行时或 Agent 循环。
- 将 Agent 连接到外部 Tool 和资源,或向其他系统发布 Mastra 能力。
开始使用开始使用的直接链接
先确定需要跨越的边界。远程 Agent endpoint 使用 A2A,coding agent 进程使用 ACP,Provider 管理的运行时使用 SDK agents,Tool 和资源则使用 MCP。
- A2A
- ACP
- SDK agents
- MCP
import { A2AAgent } from '@mastra/core/a2a'
const agent = new A2AAgent({
url: 'https://agent.example.com/.well-known/agent-card.json',
})
const result = await agent.generate('Summarize the latest report')
console.log(result.text)
import { AcpAgent } from '@mastra/acp'
const agent = new AcpAgent({
id: 'coding-agent',
description: 'Inspects and edits code',
command: 'claude',
args: ['--acp'],
persistSession: false,
})
const result = await agent.generate('Review this project')
console.log(result.text)
import { OpenAISDKAgent } from '@mastra/openai'
const agent = new OpenAISDKAgent({
id: 'openai-agent',
description: 'Answers project questions',
sdkOptions: {
name: 'Project assistant',
model: 'gpt-5',
},
})
const result = await agent.generate('Explain agent loops in one sentence')
console.log(result.text)
import { MCPClient } from '@mastra/mcp'
const client = new MCPClient({
id: 'wikipedia-client',
servers: {
wikipedia: {
command: 'npx',
args: ['-y', 'wikipedia-mcp'],
},
},
})
try {
const tools = await client.listTools()
console.log(Object.keys(tools))
} finally {
await client.disconnect()
}