> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Mastra.getMCPServerById() `.getMCPServerById()` 方法按 MCP server 的固有 `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'` 會變成 `'my-mcp-server'` - 如多個 server 具有相同 `id` 但版本不同,必須提供 `version` 參數以作區分 - 如未提供 `version` 且存在多個版本,此方法會傳回第一個相符項目 ## 相關方法 - [Mastra.getMCPServer()](https://mastra.zisheng.pro/zh-HK/reference/core/getMCPServer):按註冊鍵擷取 MCP server - [Mastra.listMCPServers()](https://mastra.zisheng.pro/zh-HK/reference/core/listMCPServers):列出所有已註冊的 MCP server ## 另請參閱 - [MCP 概覽](https://mastra.zisheng.pro/zh-HK/docs/mcp/overview) - [MCP server 參考](https://mastra.zisheng.pro/zh-HK/reference/tools/mcp-server)