Mastra.getMCPServerById()
.getMCPServerById() 方法通过其固有的 id 属性获取 MCP 服务器实例。若要通过注册表键(配置中使用的键)获取,请使用 .getMCPServer()。
🌐 The .getMCPServerById() method retrieves an MCP server instance by its intrinsic id property. For retrieving by registry key (the key used in configuration), use .getMCPServer() instead.
使用示例Direct link to 使用示例
🌐 Usage example
// Register an MCP server with a registry key
const myServer = new MCPServer({
id: 'my-mcp-server',
name: 'My Server',
version: '1.0.0',
tools: { /* ... */ },
});
export const mastra = new Mastra({
mcpServers: {
customKey: myServer, // 'customKey' is the registry key
},
});
// Retrieve by intrinsic ID
const server = mastra.getMCPServerById('my-mcp-server');
// Alternatively, retrieve by registry key
const serverByKey = mastra.getMCPServer('customKey');
参数Direct link to 参数
🌐 Parameters
serverId:
string
The intrinsic id property of the MCP server (set when the server was instantiated).
version?:
string
Optional. If provided, returns the server only if its version matches.
返回Direct link to 返回
🌐 Returns
server:
MCPServerBase | undefined
The MCP server instance with the specified id (and version, if provided), or undefined if not found.
注意Direct link to 注意
🌐 Notes
- 服务器的
id在构建期间会被转换为短横形式,因此'myMcpServer'会变成'my-mcp-server' - 如果多个服务器具有相同的
id但版本不同,你必须提供version参数以消除歧义 - 如果未提供
version且存在多个版本,该方法将返回第一个匹配项
相关方法Direct link to 相关方法
🌐 Related Methods
- Mastra.getMCPServer() - 通过其注册表键检索 MCP 服务器
- Mastra.listMCPServers() - 列出所有已注册的 MCP 服务器
另请参阅Direct link to 另请参阅
🌐 See Also