> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # Mastra.getMCPServerById() `.getMCPServerById()` 方法按固有 `id` 属性获取 MCP server 实例。要按注册键(配置中使用的键)获取,请改用 `.getMCPServer()`。 ## 使用示例 ```typescript // 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') ``` ## 参数 **serverId** (`string`): MCP server 的固有 id 属性(实例化 server 时设置)。 **version** (`string`): 可选。提供后,仅当版本匹配时才返回 server。 ## 返回值 **server** (`MCPServerBase | undefined`): 具有指定 id(以及提供时的版本)的 MCP server 实例;如果未找到,则为 undefined。 ## 注意事项 - server 的 `id` 会在构造期间进行 slug 化,因此 `'myMcpServer'` becomes `'my-mcp-server'` - 如果多个 server 具有相同 `id` 但版本不同,则必须提供 `version` 参数以消除歧义 - 如果未提供 `version` 且存在多个版本,此方法返回第一个匹配项 ## 相关方法 - [Mastra.getMCPServer()](https://mastra.zisheng.pro/reference/core/getMCPServer): 按注册键获取 MCP server - [Mastra.listMCPServers()](https://mastra.zisheng.pro/reference/core/listMCPServers): 列出所有已注册的 MCP server ## 另请参阅 - [MCP 概览](https://mastra.zisheng.pro/docs/mcp/overview) - [MCP server 参考](https://mastra.zisheng.pro/reference/tools/mcp-server)