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