Skip to main content

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 且存在多个版本,该方法将返回第一个匹配项

🌐 Related Methods

另请参阅
Direct link to 另请参阅

🌐 See Also