> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # Mastra.addGateway() 动态向 Mastra 实例添加 gateway。可在初始化期间或之后使用。 ## 使用示例 ```typescript import { Mastra } from '@mastra/core' const mastra = new Mastra() // Add with explicit key mastra.addGateway(new MyCustomGateway(), 'myGateway') // Add using gateway's ID as the key mastra.addGateway(new MyCustomGateway()) // Stored with key 'my-custom-gateway' (the gateway's id) ``` ## 参数 **gateway** (`MastraModelGateway`): 要添加的 gateway 实例 **key** (`string`): 可选的注册键。如果未提供,则使用 gateway.getId() ## 返回值 无返回值。gateway 会添加到内部注册表。 ## 行为 - 如果已存在具有相同键的 gateway,则跳过添加 - 在开发模式(`MASTRA_DEV=true`)下,会自动触发自定义 gateway 的类型生成 - gateway 会立即可用于模型解析 ## 相关内容 - [Mastra.getGateway()](https://mastra.zisheng.pro/reference/core/getGateway): 按注册键获取 gateway - [Mastra.getGatewayById()](https://mastra.zisheng.pro/reference/core/getGatewayById): 按 ID 获取 gateway - [Mastra.listGateways()](https://mastra.zisheng.pro/reference/core/listGateways): 列出所有 gateway - [MastraModelGateway](https://mastra.zisheng.pro/reference/core/mastra-model-gateway): Gateway 基类 - [自定义 Gateway 指南](https://mastra.zisheng.pro/models/gateways/custom-gateways): 创建自定义 gateway